' This service module provides a background task that updates QNH value onto a file
' the service is started from the Main, not at boot
#Region Service Attributes
#StartAtBoot: false
#End Region
' this service module works in the background (phone sleeping) and cyclicly update barometric pressure and saves it into a file
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private nid As Int = 1 ' Notification ID.
Private lock As PhoneWakeState
Private tmrForClock As Timer
End Sub
Sub Globals
End Sub
Sub Service_Create
Log("--------------------------")
Log("At Service_Create")
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
lock.PartialLock
End Sub
Sub Service_Start (StartingIntent As Intent)
Log("--------------------------")
Log("At Pages service module Service_Start at: " & _
DateTime.GetHour(DateTime.Now) & ":" & DateTime.GetMinute(DateTime.Now) & ":" & DateTime.GetSecond(DateTime.Now))
' the following code sends a notification displayed as "Benachrichtigung"
Service.StartForeground(nid, CreateNotification("Tapping this notification will open the app."))
' StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True) 'this is no longer required and not allowed in modern sdk
InitializeAndStartWhatsNeeded
End Sub
Sub Service_Destroy
lock.ReleasePartialLock
End Sub
' Handlers.
'----------
Sub tmrForClock_Tick
Log("Ticking at: " & _
DateTime.GetHour(DateTime.Now) & ":" & DateTime.GetMinute(DateTime.Now) _
& ":" & DateTime.GetSecond(DateTime.Now))
' 'now read the QNH from the internet
Dim job As HttpJob
Dim Link As String
Dim httpstring As String
'get QNH once when activity is created
Link = "https://www.checkwx.com/weather/"&Main.Station&"/metar"
job.Initialize("",Me)
job.Download(Link)
' Log ("Looking for Link")
Wait For (job) JobDone(job As HttpJob)
If Not(job.Success) Then 'this test is only used to show if the internet fails
Dim message As String
message = job.ErrorMessage
Log("httpjob failed"&message)
Else
httpstring = job.GetString
Dim Result As String 'does not work if a string manipulated on itself
Dim startindex As Int
Dim localQNH As Int
Result = httpstring 'copy passed string to local variable
' Log ("httpstring length: "&Result.Length)
job.Release 'we no longer need the resources
startindex = Result.IndexOf("Pressure") 'look for keyword pressure
If startindex = -1 Then
Log("Pressure not found")
Else
' Log("Pressure found at index:"&startindex)
startindex = Result.IndexOf2(">",startindex) 'skip twice a >
' Log(startindex)
startindex = Result.IndexOf2(">",startindex+1)
' Log(startindex)
localQNH = Result.Substring2(startindex+1,startindex+8)
Log("QNH in "&Main.Station&": "&localQNH)
Main.QNH = localQNH
AddtoBuffer(localQNH)
StartServiceAt(Me, DateTime.Now + 58 * DateTime.TicksPerMinute, True) 'this is no longer required and not allowed in modern sdk ?!
End If 'parsing successful acquisition
End If
End Sub
' Misc. sub routines.
'--------------------
Sub CreateNotification (Body As String) As Notification 'shows a notification on the phone start page
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Now running in the background.", Body, Main)
Return notification
End Sub
Sub InitializeAndStartWhatsNeeded
tmrForClock.Initialize("tmrForClock", DateTime.TicksPerHour)
' tmrForClock.Initialize("tmrForClock", DateTime.TicksPerMinute) 'for tests only
tmrForClock.Enabled = True
tmrForClock_Tick 'force one QNH query before the cycles begin. So we have a QNH to start in the main.
End Sub
'add to buffer with timestamp is more complex as without since b4x is not able to
'handle read and write list with custom types. So we squeeze QNH, day and hour into
'a single string.
Sub AddtoBuffer(QNHitem As Float)
Dim QNHString As String = NumberFormat2(QNHitem,4,0,0,False)
Dim Day As String = NumberFormat2(DateTime.GetDayOfMonth(DateTime.Now),2,0,0,False) 'easier to parse that way
Dim Hour As String = NumberFormat2(DateTime.GetHour(DateTime.Now),2,0,0,False)
Main.QNHList.Add(QNHString&","&Day&","&Hour)
' Main.QNHList.InsertAt(0,QNHitem) 'inserts the new value leftmost
If Main.QNHList.Size > 100 Then
Main.QNHList.RemoveAt(0) 'deletes the leftmost
End If
'now write it to a text file, no append
File.WriteList(File.DirInternal,"QNHbuffer.txt",Main.QNHList) 'the whole buffer is written to the file
Log ("get Text: "&File.GetText(File.DirInternal,"QNHbuffer.txt"))
Main.UpdateFlag = True
End Sub