B4J Question Run schedule from database

hakha4

Member
Licensed User
Longtime User
I have a B4J project connected to a RF sender/receiver for controlling rf switches,read sensors etc and it works ok. My aim is now to fetch schedule times for devices and check if it's time to switch on/off. I find this code in a thread that might be a start but I'm unsure how to implement reading from database to the schedule code find from Erel's example :

B4X:
Dim t As Long = FindNextTime(Array As Double(5, 6.5, 20))
Log($"Next time is: $DateTime{t}"$)

Sub FindNextTime(Times As List) As Long
   Times.Sort(True)
   For Each st As Double In Times
     If SetHours(st) > DateTime.Now Then
       Return SetHours(st)
     End If
   Next
   Return DateTime.Add(SetHours(Times.Get(0)), 0, 0, 1)
End Sub

Sub SetHours(st As Double) As Long
  Dim hours As Int = Floor(st)
  Dim minutes As Int = Round(60 * (st - hours))
  Return DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), _
  DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), hours, minutes, 0)
End Sub

Data is saved as in pic below (just testdata but structure is real) and I need to check start and stop time (to send to a server and to the devices for action)

schedule.jpg


Can anyone give a hint how to code the Dim t As Long = FindNextTime(Array As Double(5, 6.5, 20)) part ?
Is there a better way to accomplish this ?
I don't expect anyone to code for me but an example or advice howto is appreciated

Regards Håkan
 

Daestrum

Expert
Licensed User
Longtime User
This may help - you will need to change it for your needs
B4X:
	Dim start_at As Long
	Dim end_at As Long
	Dim starting As String
	Dim ending As String
	starting = "02:36:00" ' this would be the string from database
	ending = "02:37:00"
	start_at = DateTime.TimeParse(starting) ' turn it into long value
	end_at = DateTime.TimeParse(ending)
	Dim myNow As Long =DateTime.TimeParse(DateTime.Time(DateTime.now))
	Dim delaystart As Int = start_at - myNow ' time till event in milliseconds
	Dim delayend As Int = end_at - myNow     ' time till event in milliseconds
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
One thing I did not take into account is, if the start is before midnight and the end is after midnight, you will need to adjust the end to allow for it to be the following day.
 
Upvote 0
Top