#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
Private working As Boolean = True
Private client As MqttClient
Public connected As Boolean
Public timeCount As Int = 0
Dim mo As MqttConnectOptions
Private listener As NotificationListener
Dim wakelock As PhoneWakeState
Dim tm As Timer
Dim reconnect As Timer
Dim beepTimer As Timer
Private user As String = "*******"
Private password As String = "*********"
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
listener.Initialize("listener")
working = True
ConnectAndReconnect
End Sub
Sub client_Connected (Success As Boolean)
connected = Success
If Success Then
ToastMessageShow("Connected!", True)
Log("Connected to broker")
client.Subscribe("Termoelement", 0)
Log("Subscribe to Termoelement.")
Else
Log(LastException)
End If
CallSub(Main, "SetState")
End Sub
Public Sub ToggleButtonChanged(state As Boolean)
Dim b As Byte
If state Then b = 1 Else b = 0
client.Publish("Termoelement", Array As Byte(b))
End Sub
Sub client_Disconnected
connected = False
CallSub(Main, "SetState")
End Sub
Sub client_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=")
Log( Topic)
Log(" payload: ")
Log(BytesToString(Payload, 0, Payload.Length, "UTF-8"))
timeCount = 0
beepTimer.Enabled=False
CallSub2(Main, "setLabelText", Payload)
End Sub
Sub Service_Start (StartingIntent As Intent)
wakelock.PartialLock
listener.HandleIntent(StartingIntent)
tm.Initialize("tm",60000)
tm.Enabled = True
[B] [/B] reconnect.Initialize("reconnect", 15000)
reconnect.Enabled=True
beepTimer.Initialize("beepTM", 2000)
Dim m As String = DateTime.Now + 30 * 1000
Dim mText As StringBuilder
mText.Initialize
mText.Append("StartServiceAt: ")
mText.Append (m)
If listener.HandleIntent(StartingIntent) Then Return
Sleep(0)
Service.StopAutomaticForeground
End Sub
Sub ConnectAndReconnect
Do While working
If client.IsInitialized Then client.Close
client.Initialize("client", "ssl://m24.cloudmqtt.com:!!!!!!!!!", "Termoelement")
Dim mo As MqttConnectOptions
mo.Initialize(user, password)
Log("Trying to connect")
client.Connect2(mo)
Wait For Mqtt_Connected (Success As Boolean)
If Success Then
Log("Mqtt connected")
reconnect.Enabled = True
Do While working And client.Connected
client.Publish2("Termoelement", Array As Byte(0), 1, False)
Sleep(5000)
Loop
Log("Disconnected")
Else
Log("Error connecting.")
End If
Sleep(5000)
Loop
End Sub
Sub Listener_NotificationPosted (SBN As StatusBarNotification)
Log("NotificationPosted, package = " & SBN.PackageName & ", id = " & SBN.Id & _
", text = " & SBN.TickerText)
Dim p As Phone
If p.SdkVersion >= 19 Then
Dim jno As JavaObject = SBN.Notification
Dim extras As JavaObject = jno.GetField("extras")
extras.RunMethod("size", Null)
Log(extras)
Dim title As String = extras.RunMethod("getString", Array As Object("android.title"))
LogColor("Title = " & title, Colors.Blue)
End If
End Sub
Sub Listener_NotificationRemoved (SBN As StatusBarNotification)
Log("NotificationRemoved, package = " & SBN.PackageName & ", id = " & SBN.Id & _
", text = " & SBN.TickerText)
End Sub
Sub Service_TaskRemoved
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub tm_tick
tm.Enabled = False
StartActivity(Main)
End Sub
Sub reconnect_tick
Log("reconnect_tick")
timeCount = timeCount + 1
Log(timeCount)
If timeCount > 3 Then
CallSub(Main, "changeLabel")
ToastMessageShow("ConnectAndReconnect !!!!", False)
ConnectAndReconnect
End If
If timeCount > 19 Then
reconnect.Enabled=False
beepTimer.Enabled=True
CallSub2(Main, "subNoData", timeCount)
Else
End If
reconnect.Enabled=True
End Sub
Sub beepTM_tick
CallSub(Main, "BeepSound")
End Sub
Sub Service_Destroy
End Sub