Files
Monitor-Keymon/B4A/Starter.bas
2023-09-07 11:22:06 -06:00

202 lines
7.0 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=9.85
@EndOfDesignText@
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Public rp As RuntimePermissions
Dim reqManager As DBRequestManager
Dim server As String = "http://keymon.lat:1782"
Dim Timer1 As Timer
Dim Interval As Int = 5
Public rp As RuntimePermissions
Dim s As C_Subs
Dim nid As Int = 51042
Dim monitorActivo As Boolean = True
Dim InternetOk As Boolean = True
Dim DBReqServerOk As Boolean = True
Dim DBOk As Boolean = True
Dim logger As Boolean = False
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
Timer1.Initialize("Timer1", Interval * 1000)
Timer1.Enabled = True
#if not(DEBUG)
logger = False
#end if
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
Service.StartForeground(nid, CreateNotification("..."))
reqManager.Initialize(Me, server)
s.Initialize
Timer1_Tick
End Sub
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Tester", Body, Main)
Return notification
End Sub
'Genera una notificacion con importancia alta
Sub notiHigh(title As String, body As String, id As String, activity As Object) 'ignore
activity = Main
Private notif As Notification
notif.Initialize2(notif.IMPORTANCE_HIGH)
notif.Icon = "icon"
notif.Vibrate = False
notif.Sound = False
notif.AutoCancel = True
if logger then Log("notiHigh: "&title)
notif.SetInfo(title, body, activity)
' Log("notiHigh SetInfo")
notif.Notify(id)
End Sub
'Regresa el objeto de una notificacion con importancia baja
Sub notiLowReturn(title As String, Body As String, id As Int) As Notification 'ignore
Private notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
' Log("notiLowReturn: "&title)
notification.Icon = "icon"
notification.Sound = False
notification.Vibrate = False
notification.SetInfo(title, Body, Main)
notification.Notify(id)
' Log("notiLowReturn SetInfo")
Return notification
End Sub
Sub Timer1_Tick
' Log("Next run " & DateTime.Time(DateTime.Now + Interval * 1000))
If monitorActivo Then probamosConexion
End Sub
Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub
Sub Service_Destroy
End Sub
Sub probamosConexion
' Log("probamos probamosConexion")
If B4XPages.IsInitialized Then
B4XPages.MainPage.cb_internet.Checked = False
B4XPages.MainPage.cb_dbreqserver.Checked = False
B4XPages.MainPage.cb_db.Checked = False
End If
If IsConnectedToInternet Then
InternetOk = True
If B4XPages.IsInitialized Then B4XPages.MainPage.cb_internet.Checked = True
Dim cmd As DBCommand
notiLowReturn("Con internet", "Hay conexion a internet", nid)
If B4XPages.IsInitialized Then B4XPages.MainPage.l_status.Text = "Hay conexion a internet!!"
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = "select_conexion"
reqManager.ExecuteQuery(cmd, 0, "select_conexion", 750)
'Reiniciamos el timer para cuando llamamos el Sub desde "seleccion"
Timer1.Enabled = False
Timer1.Interval = Interval * 1000
Timer1.Enabled = True
Else
if logger then Log("Sin conexión a internet!!!")
notiLowReturn("Sin internet", "NO hay conexion a internet", nid)
If B4XPages.IsInitialized Then B4XPages.MainPage.l_status.Text = "NO hay conexion a internet!!"
End If
End Sub
Sub reinicializaReqManager
reqManager.Initialize(Me, server)
' B4XPages.MainPage.reqManager.Initialize(Me, server)
' If logger Then Log(server)
End Sub
'[b4xcollections=Null, B4XPages=Null, defaultscheme=https
'[b4xcollections=null, b4xpages=null, defaultscheme=https
', errormessage=, HttpUtils2Service=Null, invalidurl=https://invalid-url/
', errormessage=, httputils2service=null, invalidurl=https://invalid-url/
', jobname=DBRequest, Main=Null, out=(BufferedOutputStream) java.io.BufferedOutputStream@3406019
', jobname=DBRequest, main=null, out=(BufferedOutputStream) java.io.BufferedOutputStream@3672b93
', password=, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@c06b8de, response=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@4e48abf
', password=, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@8e687d0, response=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@26963c9
', starter=Null, success=True, tag=select_conexion>XXX
', starter=null, success=true, tag=select_conexionXXX
', target=class md.keymon.com.mx.starter, taskid=8, username=
', target=class md.keymon.com.mx.starter, taskid=2, username=
Sub JobDone(Job As HttpJob)
' Log("JOBDONE -- " & Job.Success)
' Log(Job)
If Job.Success = False Then
If Job.JobName = "DBRequest" Then DBReqServerOk = False
notiHigh("Con internet", $"El servidor NO esta respondiendo!!"$, nid, Main)
If Job.ErrorMessage.Contains("failed to connect") Or Job.ErrorMessage.Contains("Failed to connect") Then
ToastMessageShow("¡Hubo un error contactando al servidor, por favor revise su conexión!", True)
End If
Else
If Job.JobName = "DBRequest" Then DBReqServerOk = True
if logger then LogColor("JobDone: '" & reqManager.HandleJob(Job).tag & "' - Registros: " & reqManager.HandleJob(Job).Rows.Size, Colors.Green) 'Mod por CHV - 211027
If Job.JobName = "DBRequest" Then
Dim resultado As DBResult = reqManager.HandleJob(Job)
If resultado.Tag = "select_conexion" Then
For Each records() As Object In resultado.Rows
Private valor As String = records(resultado.Columns.Get("VALOR"))
If valor = "OK" Then
DBOk = True
notiLowReturn("Con internet", $"El servidor esta respondiendo!!"$, nid)
If B4XPages.IsInitialized Then B4XPages.MainPage.l_status.Text = "El servidor esta respondiendo!!"
Else
notiHigh("Con internet", $"El servidor NO esta respondiendo!!"$, nid, Main)
If B4XPages.IsInitialized Then B4XPages.MainPage.l_status.Text = "El servidor NO esta respondiendo!!"
End If
Next
End If
' s.logJobDoneResultados(resultado)
If resultado.Rows.Size = 0 Then DBOk = False
' Log(resultado.Rows.Size)
End If
If B4XPages.IsInitialized Then
B4XPages.MainPage.cb_internet.Checked = InternetOk
B4XPages.MainPage.cb_dbreqserver.Checked = DBReqServerOk
B4XPages.MainPage.cb_db.Checked = DBOk
End If
Job.Release
End If
End Sub
Sub IsConnectedToInternet As Boolean 'ignore
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
r.Target = r.RunMethod("getActiveNetworkInfo")
If r.Target <> Null Then
If logger Then LogColor("isConnectedOrConnecting", Colors.green)
Return r.RunMethod("isConnectedOrConnecting")
End If
if logger then LogColor("Not connected", Colors.red)
Return False
End Sub