#Region Service Attributes
#StartAtBoot: True
#End Region
Sub Process_Globals
Private nid As Int = 1
Private lock As PhoneWakeState
Private Tracking As Boolean
Private fm As FirebaseMessaging
Public Token As String
Public APIKey As String
Public mustPlay As Boolean
Public regDate As String
' Dim SQL1 As SQL
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
' Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
fm.Initialize("fm")
Token = fm.token
Log("First Token: " & Token)
lock.PartialLock
End Sub
Public Sub SubscribeToTopic (Topic As String)
fm.SubscribeToTopic(Topic)
End Sub
Public Sub SubscribeToTopics
If Tracking Then Return
fm.SubscribeToTopic("is") 'you can subscribe to more topics
fm.SubscribeToTopic("avp") 'you can subscribe to more topics
Tracking = True
End Sub
Public Sub UnsubscribeToTopics(topic As String)
fm.UnsubscribeFromTopic(topic)
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
Service.StartForeground(nid, CreateNotification("iS-PROTECTOR is running"))
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
SubscribeToTopics
'commented out on 13 september
'Sleep(0)
'Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub
'
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "Logo-IS-HP"
notification.SetInfo("iS-PROTECTOR", Body, Splash)
Return notification
End Sub
Sub fm_TokenRefresh (sToken As String)
Token = sToken
End Sub
Sub GetMessageDescription(xtype As String) As String
Select Case xtype
Case "00"
Return "Closed"
Case "01"
Return "Open"
Case "03"
Return "Under Attack"
Case "04"
Return "Closed"
Case "05"
Return "Open"
Case "06"
Return "Start"
Case "07"
Return "Stop"
Case "09"
Return "SOS"
Case "10"
Return "Disarmed"
Case "11"
Return "Armed"
Case "12"
Return "Motion Detected"
Case "20"
Return "Power ON"
Case "21"
Return "Power OFF"
Case Else
Return "Unknown Status"
End Select
End Sub
Sub fm_MessageArrived (Message As RemoteMessage)
B4XPages.MainPage.MP.Stop
'LogColor("fm_MessageArrived", Colors.Magenta)
'get the device id for this user
Dim data As Map = Message.getdata
Log("data: " & data)
If data.IsInitialized Then
'get the details of the message
Dim sdeviceID As String = data.Get("deviceid")
Dim smsgtype As String = data.Get("msgtype")
Dim svoltage As String = data.Get("voltage")
Dim smsgdate As String = data.Get("msgdate")
' Dim smsgid As String = data.Get("seqnumber")
' Dim myRegDate = smsgdate
smsgdate = modAVP.NiceDate(smsgdate)
'*************************************
Dim msgdesc As String = GetMessageDescription(smsgtype)
'get the device name
Dim devicename As String = modAVP.Devices.GetDefault(sdeviceID, "")
mustPlay = False
If devicename <> "" Then
' '*********************************************************
' ' Check for regdate
' Private dbUser As B4XDBUtils
' dbUser.Initialize(modAVP.SQLiteDB, "devices", "deviceid", "id")
' dbUser.Execute("SELECT COUNT(datereg) FROM devices WHERE deviceid=" & "'" & sdeviceID & "'")
' dbUser.FromJSON
' Dim regTot As Int = dbUser.result.Size
' LogColor("COUNT: " & regTot, Colors.Magenta )
'
' Dim myPos As Int
' myPos = myRegDate.IndexOf(":")
' myRegDate = myRegDate.SubString2(0, 11)
'
' If (regTot = 0) Then
'' regDate = smsgdate
' SQL1.Initialize(File.DirDefaultExternal, "avp.db", True)
' SQL1.ExecNonQuery("INSERT INTO devices '" & myRegDate & "' WHERE deviceid = '" & sdeviceID & "'")
'
'
' End If
' '**********************************************************
'create the title
Dim stitle As String = $"${devicename}: ${msgdesc}"$
Dim inbox As Map = MapCopy(data, Array("deviceid","voltage","msgdate","msgid","msgtype"))
inbox.Put("msgtypedesc", msgdesc)
inbox.Put("status", "A")
Dim jsonData As String = modAVP.Map2Json(inbox)
'
Dim tbInbox As B4XDBUtils
tbInbox.Initialize(modAVP.SQLiteDB, "inbox", "id", "id")
tbInbox.SchemaAddInt(Array("msgid", "id"))
tbInbox.Insert1(inbox)
tbInbox.FromJSON
'update the status of the device
Dim dstatus As Map = MapCopy(data, Array("voltage", "msgdate", "msgtype", "deviceid"))
dstatus.Put("msgtypedesc", msgdesc)
'
Dim rsDevice As B4XDBUtils
rsDevice.Initialize(modAVP.SQLiteDB, "devices", "deviceid", "")
rsDevice.SchemaAddInt(Array("int"))
rsDevice.Update1(dstatus, sdeviceID)
rsDevice.FromJSON
'increment the number of messages
modAVP.BadgeNumber = modAVP.BadgeNumber + 1
Starter.UpdateAppBadge(modAVP.BadgeNumber)
'
RndSeed(DateTime.Now)
Dim rint As Int=Rnd(1,65536)
'*******************************************************
Private dbUser As B4XDBUtils
dbUser.Initialize(modAVP.SQLiteDB, "devices", "id", "id")
dbUser.Execute("SELECT * FROM devices WHERE deviceid=" & "'" & sdeviceID & "'")
dbUser.FromJSON
Dim notifTot As Int = dbUser.result.Size
modAVP.DeviceNumber = notifTot
modAVP.Devices.Initialize
For Each devicem As Map In dbUser.result
Dim alarmState As String = devicem.Get("pac")
Log("PAC: " & alarmState)
Next
If alarmState = "true" Then
mustPlay = True
End If
'*******************************************************
lock.PartialLock
Dim n1 As NB6
n1.Initialize("avp3", Application.LabelName, "HIGH")
n1.SmallIcon(LoadBitmapResize(File.DirAssets, "is-icon-small.jpg", 24dip, 24dip, False))
n1.AutoCancel(True)
n1.OnlyAlertOnce(False)
n1.Visibility("PUBLIC")
n1.SetDefaults(True, True, True)
Dim n1msg As String = stitle & ", voltage @ " & svoltage & ", on " & smsgdate
Dim n As Notification = n1.Build(stitle, n1msg, jsonData, Splash)
n.Notify(rint)
' lock.ReleasePartialLock
If B4XPages.IsInitialized Then
B4XPages.MainPage.UpdateBadge
If mustPlay Then
B4XPages.MainPage.PlaySound
End If
StartActivity(Splash)
End If
End If
End If
End Sub
Sub MapCopy(sourceM As Map, sourceKeys As List) As Map
Dim nm As Map = CreateMap()
If sourceKeys.Get(0) = "*" Then
For Each k As String In sourceM.Keys
Dim v As Object = sourceM.Get(k)
nm.Put(k, v)
Next
Else
For Each k As String In sourceKeys
Dim v As Object = sourceM.Get(k)
nm.Put(k, v)
Next
End If
Return nm
End Sub
Sub Service_Destroy
'13 september
If Tracking Then
'GPS.Stop
End If
Tracking = False
lock.ReleasePartialLock
''13 september
End Sub