mirror of
https://github.com/KeymonSoft/Monitor-Keymon.git
synced 2026-04-17 21:06:19 +00:00
09/09/23 - Panel de configuración y servicio al arranque
Se agregó´un panel para cambiar algunos parametros de la aplicación y se agregó´un servicio para que el monitor arranque junto con el dispositivo, para esto es necesario darle permisos para accesar las notificaciones del sistema.
This commit is contained in:
171
B4A/monitor.bas
Normal file
171
B4A/monitor.bas
Normal file
@@ -0,0 +1,171 @@
|
||||
B4A=true
|
||||
Group=Default Group
|
||||
ModulesStructureVersion=1
|
||||
Type=Service
|
||||
Version=12.2
|
||||
@EndOfDesignText@
|
||||
#Region Service Attributes
|
||||
#StartAtBoot: 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.
|
||||
Dim reqManager As DBRequestManager
|
||||
' Dim DBReqServer As String = "http://keymon.lat:1782"
|
||||
' Dim skmt As SQL
|
||||
Dim Timer1 As Timer
|
||||
Private lock As PhoneWakeState
|
||||
Dim Interval As Int
|
||||
Dim s As C_Subs
|
||||
Dim nid As Int = 51043
|
||||
Dim monitorActivo As Boolean = True
|
||||
Dim InternetOk As Boolean = True
|
||||
Dim DBReqServerOk As Boolean = True
|
||||
Dim DBOk As Boolean = True
|
||||
Dim logger As Boolean = True
|
||||
Dim timeout As Int
|
||||
End Sub
|
||||
|
||||
Sub Service_Create
|
||||
s.Initialize
|
||||
Interval = s.traeIntervaloDeBD
|
||||
timeout = s.traeTimeoutDeBD
|
||||
Timer1.Initialize("Timer1", Interval * 1000)
|
||||
Timer1.Enabled = True
|
||||
#if not(DEBUG)
|
||||
logger = False
|
||||
#end if
|
||||
lock.PartialLock
|
||||
' probamosConexion
|
||||
End Sub
|
||||
|
||||
Sub Service_Start (StartingIntent As Intent)
|
||||
Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
|
||||
Service.StartForeground(nid, s.CreateNotification("..."))
|
||||
StartServiceAt(Me, DateTime.Now + 1 * DateTime.TicksPerMinute, True)
|
||||
reqManager.Initialize(Me, Starter.DBReqServer)
|
||||
Timer1_Tick
|
||||
If logger Then LogColor("*************** Monitor Iniciado 👍🏽 **********", Colors.red)
|
||||
End Sub
|
||||
|
||||
Sub Service_Destroy
|
||||
|
||||
End Sub
|
||||
|
||||
Sub Timer1_Tick
|
||||
' Log("Next run " & DateTime.Time(DateTime.Now + Interval * 1000))
|
||||
DateTime.DateFormat = "dd/MM HH:mm:ss"
|
||||
If B4XPages.IsInitialized Then B4XPages.MainPage.l_ultimoPing.Text = $"Último ping: ${DateTime.Date(DateTime.now)}"$
|
||||
If monitorActivo Then probamosConexion
|
||||
End Sub
|
||||
|
||||
Sub probamosConexion
|
||||
If logger Then 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 cd1 As ColorDrawable
|
||||
cd1.Initialize(Colors.RGB(16, 141, 0), 10dip)
|
||||
B4XPages.MainPage.b_ping.Background = cd1
|
||||
B4XPages.MainPage.b_ping.Text = "Internet Ok"
|
||||
End If
|
||||
Dim cmd As DBCommand
|
||||
s.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", timeout)
|
||||
'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!!!")
|
||||
s.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 'ignore
|
||||
reqManager.Initialize(Me, Starter.DBReqServer)
|
||||
If logger Then Log("Inicializamos con: " & Starter.DBReqServer)
|
||||
End Sub
|
||||
|
||||
Sub JobDone(Job As HttpJob)
|
||||
' Log("JOBDONE -- " & Job.Success)
|
||||
' Log(Job)
|
||||
If Job.Success = False Then
|
||||
If Job.JobName = "DBRequest" Then DBReqServerOk = False
|
||||
|
||||
Dim cd1 As ColorDrawable
|
||||
cd1.Initialize(Colors.Red, 10dip)
|
||||
B4XPages.MainPage.b_ping.Background = cd1
|
||||
B4XPages.MainPage.b_ping.Text = "DBReqServer KO"
|
||||
s.notiHigh("Con internet", $"El servidor ${Starter.DBReqServer} NO responde!!"$, 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
|
||||
s.notiLowReturn("Con internet", $"El servidor esta respondiendo!!"$, nid)
|
||||
If B4XPages.IsInitialized Then
|
||||
B4XPages.MainPage.l_status.Text = "El servidor esta respondiendo!!"
|
||||
Dim cd1 As ColorDrawable
|
||||
cd1.Initialize(Colors.RGB(16, 141, 0), 10dip)
|
||||
B4XPages.MainPage.b_ping.Background = cd1
|
||||
B4XPages.MainPage.b_ping.Text = "Servidor Ok"
|
||||
End If
|
||||
Else
|
||||
s.notiHigh("Con internet", $"El servidor NO esta respondiendo!!"$, nid, Main)
|
||||
If B4XPages.IsInitialized Then
|
||||
B4XPages.MainPage.l_status.Text = "El servidor NO esta respondiendo!!"
|
||||
Dim cd1 As ColorDrawable
|
||||
cd1.Initialize(Colors.Red, 10dip)
|
||||
B4XPages.MainPage.b_ping.Background = cd1
|
||||
B4XPages.MainPage.b_ping.Text = "Servidor KO"
|
||||
End If
|
||||
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
|
||||
Reference in New Issue
Block a user