mirror of
https://github.com/KeymonSoft/Intmex_Reparto_2026.git
synced 2026-04-17 12:56:12 +00:00
331 lines
13 KiB
QBasic
331 lines
13 KiB
QBasic
B4A=true
|
|
Group=Default Group
|
|
ModulesStructureVersion=1
|
|
Type=Service
|
|
Version=10.2
|
|
@EndOfDesignText@
|
|
#Region Service Attributes
|
|
#StartAtBoot: True
|
|
#End Region
|
|
'******************************************************************************
|
|
'No olvidar agregar esta linea al editor de manifiesto:
|
|
' SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
|
|
'
|
|
'En Starter agregar estas lineas en Process_Globals
|
|
' Public rp As RuntimePermissions
|
|
' Public FLP As FusedLocationProvider
|
|
' Private flpStarted As Boolean
|
|
'
|
|
'En Main agregar estas lineas a Activity_Resume
|
|
' Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
|
|
' Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
|
|
' If Result Then
|
|
' StartService(Tracker)
|
|
' Log("Start Tracker")
|
|
' Else
|
|
' ToastMessageShow("No permission", True)
|
|
' End If
|
|
'
|
|
'Se necesitan las librerias FusedLocationProvider, GPS, Phone y RunTimePermissions
|
|
'
|
|
'Y en Main agregar estas dos lineas:
|
|
'#AdditionalJar: com.android.support:support-v4
|
|
'#AdditionalJar: com.google.android.gms:play-services-location
|
|
|
|
Sub Process_Globals
|
|
Private nid As Int = 1
|
|
Dim Tracking As Boolean
|
|
Private lock As PhoneWakeState
|
|
'Para FusedLocationProvider (2 lineas)
|
|
Public FLP As FusedLocationProvider
|
|
Dim actualLR As LocationRequest
|
|
Private flpStarted As Boolean
|
|
Dim locRequest As String
|
|
Dim UUGCoords As Location 'Ultima Ubicacion Guardada
|
|
Dim trackerActividad, pushServiceActividad As String
|
|
End Sub
|
|
|
|
Public Sub StartFLPSmall
|
|
' Log("StartFLPSmall - flpStarted="&flpStarted)
|
|
Do While FLP.IsConnected = False
|
|
Sleep(500)
|
|
Log("sleeping")
|
|
Loop
|
|
dameUltimaUbicacionConocida 'Regresamos ultima ubicacion conocida
|
|
FLP.RequestLocationUpdates(CreateLocationRequestSmallD) 'Buscamos ubicacion 2 peticiones
|
|
' Log("Buscamos ubicacion Small displacement")
|
|
' Log("GPSSmallestDisplacement = " & actualLR.GetSmallestDisplacement)
|
|
End Sub
|
|
|
|
Private Sub CreateLocationRequestSmallD As LocationRequest
|
|
' Log("Iniciamos CreateLocationRequestSmallD")
|
|
Dim lr As LocationRequest
|
|
lr.Initialize
|
|
lr.SetInterval(2000) 'Intervalo deseado para actualizaciones de ubicacion
|
|
lr.SetFastestInterval(lr.GetInterval / 2) 'Intervalo minimo para actualizaciones de ubicacion
|
|
lr.setNumUpdates(2) 'Solicitamos solo 2 actualizaciones con estos parametros
|
|
lr.SetSmallestDisplacement(1) 'Solo registra cambio de ubicacion si es mayor a XX mts
|
|
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
|
actualLR=lr
|
|
Return lr
|
|
End Sub
|
|
|
|
|
|
Sub Service_Create
|
|
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
|
|
UUGCoords.Initialize
|
|
UUGCoords.Latitude = 0
|
|
UUGCoords.Longitude = 0
|
|
'Para FusedLocationProvider (2 lineas)
|
|
FLP.Initialize("flp")
|
|
FLP.Connect
|
|
lock.PartialLock
|
|
StartFLP
|
|
End Sub
|
|
|
|
Sub flp_ConnectionSuccess
|
|
' If B4XPages.MainPage.logger Then Log("Connected to location provider")
|
|
'FLP.GetLastKnownLocation
|
|
End Sub
|
|
|
|
Sub flp_ConnectionFailed(ConnectionResult1 As Int)
|
|
If B4XPages.MainPage.logger Then Log("Failed to connect to location provider")
|
|
End Sub
|
|
|
|
Sub flp_ConnectionSuspended(ConnectionResult1 As Int)
|
|
If B4XPages.MainPage.logger Then Log("FLP conection suspended")
|
|
StartFLP
|
|
End Sub
|
|
|
|
Sub Service_Start (StartingIntent As Intent)
|
|
LogColor("Iniciando Tracker ...", Colors.Green)
|
|
Service.StopAutomaticForeground
|
|
' Service.StartForeground(51042, Subs.notiLowReturn("THIS (T)", PushService.wsStatus, 51042))
|
|
StartServiceAt(Me, DateTime.Now + 10 * DateTime.TicksPerMinute, True)
|
|
Track
|
|
Starter.trackerActividad = Subs.fechaKMT(DateTime.Now)
|
|
' Log("trackerActividad="&Starter.trackerActividad&"|"& Subs.fechaKMT(DateTime.Now))
|
|
End Sub
|
|
|
|
Public Sub Track
|
|
If B4XPages.MainPage.logger Then Log("Inicia Track - Tracking : "&Tracking)
|
|
If Tracking Then
|
|
' Log(actualLR.GetSmallestDisplacement)
|
|
Return 'Si ya estamos "rastreando" no hacemos nada (return)
|
|
End If
|
|
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
|
|
If B4XPages.MainPage.logger Then Log("No permission")
|
|
Return
|
|
End If
|
|
StartFLP 'Iniciamos FusedLocationProvider
|
|
Tracking = True
|
|
End Sub
|
|
|
|
Public Sub StartFLP
|
|
' If B4XPages.MainPage.logger Then Log("StartFLP - flpStarted="&flpStarted)
|
|
Do While FLP.IsConnected = False
|
|
Sleep(500)
|
|
' If Main.logger Then Log("sleeping")
|
|
Loop
|
|
' If flpStarted = False Then
|
|
' If Main.logger Then Log("RequestLocationUpdates")
|
|
FLP.RequestLocationUpdates(CreateLocationRequest) 'Buscamos ubicacion
|
|
' If Main.logger Then Log("Buscamos ubicacion")
|
|
' If Main.logger Then Log(actualLR.GetSmallestDisplacement)
|
|
flpStarted = True
|
|
' End If
|
|
End Sub
|
|
|
|
Public Sub StartFLP2Reqs
|
|
If B4XPages.MainPage.logger Then Log("StartFLP - flpStarted="&flpStarted)
|
|
Do While FLP.IsConnected = False
|
|
Sleep(500)
|
|
If B4XPages.MainPage.logger Then Log("sleeping GR")
|
|
Loop
|
|
dameUltimaUbicacionConocida 'Regresamos ultima ubicacion conocida
|
|
FLP.RequestLocationUpdates(CreateLocationRequest2times) 'Buscamos ubicacion 2 peticiones
|
|
If B4XPages.MainPage.logger Then Log("Buscamos ubicacion 2 peticiones")
|
|
If B4XPages.MainPage.logger Then Log(actualLR.GetSmallestDisplacement)
|
|
End Sub
|
|
|
|
Private Sub CreateLocationRequest As LocationRequest
|
|
If B4XPages.MainPage.logger Then Log("CreateLocationRequest")
|
|
Dim lr As LocationRequest
|
|
lr.Initialize
|
|
lr.SetInterval(10000) 'Intervalo deseado para actualizaciones de ubicacion
|
|
lr.SetFastestInterval(lr.GetInterval / 2) 'Intervalo minimo para actualizaciones de ubicacion
|
|
lr.SetSmallestDisplacement(75) 'Solo registra cambio de ubicacion si es mayor a XX mts
|
|
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
|
actualLR=lr
|
|
Return lr
|
|
End Sub
|
|
|
|
Private Sub CreateLocationRequest2times As LocationRequest
|
|
If B4XPages.MainPage.logger Then Log("Iniciamos CreateLocationRequest2times")
|
|
Dim lr As LocationRequest
|
|
lr.Initialize
|
|
lr.SetInterval(2000) 'Intervalo deseado para actualizaciones de ubicacion
|
|
lr.SetFastestInterval(lr.GetInterval / 2) 'Intervalo minimo para actualizaciones de ubicacion
|
|
lr.setNumUpdates(2) 'Solicitamos solo 2 actualizaciones con estos parametros
|
|
lr.SetSmallestDisplacement(10) 'Solo registra cambio de ubicacion si es mayor a XX mts
|
|
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
|
actualLR=lr
|
|
Return lr
|
|
End Sub
|
|
|
|
Sub dameUltimaUbicacionConocida
|
|
If FLP.GetLastKnownLocation.IsInitialized Then 'Mandamos ultima ubicacion guardada
|
|
' If Main.logger Then Log("Mandamos UUC : "&formatoFecha(FLP.GetLastKnownLocation.Time))
|
|
If B4XPages.MainPage.Logger Then LogColor($"Mandamos UUC "${Subs.fechaKMT(FLP.GetLastKnownLocation.Time)}|Acc:$0.2{FLP.GetLastKnownLocation.Accuracy}|$0.8{FLP.GetLastKnownLocation.Latitude}|$0.8{FLP.GetLastKnownLocation.Longitude}|Spd:$0.2{FLP.GetLastKnownLocation.Speed}|"$, Colors.RGB(255,112,35))
|
|
Dim coords As String = FLP.GetLastKnownLocation.Latitude&","&FLP.GetLastKnownLocation.Longitude&","&formatoFecha(FLP.GetLastKnownLocation.Time)
|
|
' CallSubDelayed2(FirebaseMessaging,"mandamosLoc",coords)
|
|
Subs.mandamosLoc(coords)
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub StopFLP
|
|
'Log("StopFLP")
|
|
If flpStarted Then
|
|
FLP.RemoveLocationUpdates 'Eliminamos todas las solicitudes de ubicacion
|
|
flpStarted = False
|
|
End If
|
|
End Sub
|
|
|
|
Sub flp_LocationChanged (Location1 As Location)
|
|
' Starter.trackerActividad = Subs.fechaKMT(DateTime.Now)
|
|
' UUGCoords = Location1
|
|
'' If Main.logger Then Log("SmallestDisplacement="&actualLR.GetSmallestDisplacement)
|
|
' CallSub2(Starter, "GPS_LocationChanged", Location1)
|
|
'' CallSub2(gestion, "GPS_LocationChanged", Location1)
|
|
'' CallSubDelayed2(gestion, "GPS_LocationChanged", Location1)
|
|
' B4XPages.MainPage.lat_gps = Location1.Latitude
|
|
' B4XPages.MainPage.lon_gps = Location1.Longitude
|
|
''/////// para la ultima ubicacion FL
|
|
' Dim sDate,sTime As String
|
|
' DateTime.DateFormat = "MM/dd/yyyy"
|
|
' sDate=DateTime.Date(DateTime.Now)
|
|
' sTime=DateTime.Time(DateTime.Now)
|
|
' Try
|
|
' Starter.skmt.ExecNonQuery("DELETE FROM HIST_GPS")
|
|
' Starter.skmt.ExecNonQuery2("INSERT INTO HIST_GPS (HGDATE, HGLAT, HGLON) VALUES(?,?,?) ", Array As Object (sDate & sTime, B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps))
|
|
' Catch
|
|
' If B4XPages.MainPage.logger Then Log("Error al borrar o insertar nuevas coordendas en HIST_GPS")
|
|
' End Try
|
|
''///////
|
|
' Dim coords As String = Location1.Latitude&","&Location1.Longitude&","&formatoFecha(Location1.Time)
|
|
' Log("Loc changed : "&Location1.Latitude&","&Location1.Longitude)
|
|
' If B4XPages.MainPage.logger Then Log("Mandamos Ubicacion")
|
|
' If B4XPages.MainPage.logger Then Log(locRequest)
|
|
' ' Solo mandamos la ubicacion si la precision es menor a XX mts
|
|
' If Location1.Accuracy < 100 Then
|
|
' Subs.guardaInfoEnBD(coords)'Escribimos coordenadas y fecha en BD
|
|
'' CallSubDelayed2(FirebaseMessaging,"mandamosLoc",coords)
|
|
' Subs.mandamosLoc(coords)
|
|
' End If
|
|
' Dim origFormat As String = DateTime.TimeFormat 'Guardamos formato de fecha
|
|
' DateTime.TimeFormat = "HHmmss" ' Modificamos formato de fecha
|
|
' Dim minsDif As Int = DateTime.Time(DateTime.Now) - B4XPages.MainPage.ultimaActualizacionGPS
|
|
'' If Main.logger Then Log("UltimaAct="&Main.ultimaActualizacionGPS&" MinsDif="&minsDif)
|
|
' If Location1.Accuracy < 100 And minsDif > 240 Then 'Si precision de 100 y 4 min transcurridos manda a web
|
|
' If B4XPages.MainPage.logger Then Log("actualizamos Ubicacion Web")
|
|
' CallSubDelayed(Starter, "ENVIA_ULTIMA_GPS")
|
|
' End If
|
|
' DateTime.TimeFormat = origFormat 'Regresamos formato de fecha original
|
|
' 'Revisamos servicios
|
|
' Subs.monitor
|
|
|
|
' ToastMessageShow("Loc changed", False)
|
|
' Log($"Loc changed:${Location1.Longitude},${Location1.Latitude}"$)
|
|
B4XPages.MainPage.lat_gps = Location1.Latitude
|
|
B4XPages.MainPage.lon_gps = Location1.Longitude
|
|
UUGCoords = Location1
|
|
' Log("SmallestDisplacement="&actualLR.GetSmallestDisplacement)
|
|
' If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
|
|
' Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
|
|
' n.Notify(nid)
|
|
' LastUpdateTime = DateTime.Now
|
|
' End If
|
|
'/////// para la ultima localización FL
|
|
Dim sDate,sTime As String
|
|
DateTime.DateFormat = "MM/dd/yyyy"
|
|
sDate=DateTime.Date(DateTime.Now)
|
|
sTime=DateTime.Time(DateTime.Now)
|
|
' If Not(Starter.skmt.IsInitialized) Then Starter.skmt.Initialize(Starter.ruta,"kmt.db", True)
|
|
' Try
|
|
' B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO HIST_GPS (HGDATE, HGLAT, HGLON) VALUES(?,?,?) ", Array As Object (sDate & sTime, B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps))
|
|
' B4XPages.MainPage.skmt.ExecNonQuery("DELETE FROM HIST_GPS")
|
|
' Catch
|
|
' LogColor(LastException, Colors.Red)
|
|
' End Try
|
|
'///////
|
|
Dim coords As String = Location1.Latitude&","&Location1.Longitude&","&formatoFecha(Location1.Time)
|
|
' Log("Loc changed : "&Location1.Latitude&","&Location1.Longitude&"|"&B4XPages.MainPage.usuario&"|")
|
|
' Log("Mandamos Ubicacion")
|
|
' Log(FirebaseMessaging.locRequest)
|
|
' Solo mandamos la ubicacion si la precision es menor a XX mts
|
|
If Location1.Accuracy < 100 Then
|
|
' CallSubDelayed2(FirebaseMessaging,"mandamosLoc",coords)
|
|
End If
|
|
|
|
CallSub2(Starter, "GPS_LocationChanged", Location1)
|
|
CallSub2(B4XPages.MainPage.cliente, "GPS_LocationChanged", Location1)
|
|
' CallSub2(gestion, "GPS_LocationChanged", Location1)
|
|
If B4XPages.MainPage.cliente.IsInitialized Then
|
|
CallSub2(B4XPages.GetPage("Cliente"), "GPS_LocationChanged", Location1)
|
|
End If
|
|
' CallSub2(nuevocliente, "GPS_LocationChanged", Location1)
|
|
|
|
End Sub
|
|
|
|
Public Sub StartFLP2
|
|
Private logger As Boolean = True
|
|
If logger Then Log("StartFLP2 - flpStarted="&flpStarted)
|
|
Do While FLP.IsConnected = False
|
|
Sleep(500)
|
|
If logger Then Log("kll - sleeping")
|
|
Loop
|
|
dameUltimaUbicacionConocida 'Regresamos ultima ubicacion conocida
|
|
FLP.RequestLocationUpdates(CreateLocationRequest2) 'Buscamos ubicacion 2 peticiones
|
|
If logger Then LogColor("Buscamos ubicacion (movimientoMinimo = "&actualLR.GetSmallestDisplacement&")", Colors.Magenta)
|
|
' If logger Then Log(actualLR.GetSmallestDisplacement)
|
|
End Sub
|
|
|
|
Private Sub CreateLocationRequest2 As LocationRequest
|
|
Private logger As Boolean = True
|
|
If logger Then Log("Iniciamos CreateLocationRequest2")
|
|
Dim lr As LocationRequest
|
|
lr.Initialize
|
|
lr.SetInterval(1000) 'Intervalo deseado para actualizaciones de ubicacion
|
|
lr.SetFastestInterval(lr.GetInterval / 2) 'Intervalo minimo para actualizaciones de ubicacion
|
|
lr.setNumUpdates(2) 'Solicitamos solo 2 actualizaciones con estos parametros
|
|
lr.SetSmallestDisplacement(1) 'Solo registra cambio de ubicacion si es mayor a XX mts
|
|
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
|
actualLR=lr
|
|
Return lr
|
|
End Sub
|
|
|
|
Sub CreateNotification (Body As String) As Notification 'ignore
|
|
Dim notification As Notification
|
|
notification.Initialize2(notification.IMPORTANCE_LOW)
|
|
notification.Icon = "icon"
|
|
notification.SetInfo("INTMEX REPARTO", Body, Main)
|
|
Return notification
|
|
End Sub
|
|
|
|
Sub Service_Destroy
|
|
If Tracking Then
|
|
StopFLP
|
|
End If
|
|
Tracking = False
|
|
lock.ReleasePartialLock
|
|
End Sub
|
|
|
|
Sub formatoFecha(fecha As String) As String 'Convierte una fecha al formato yyMMddHHmmss
|
|
' Log(fecha)
|
|
Dim OrigFormat As String = DateTime.DateFormat 'save orig date format
|
|
DateTime.DateFormat="yyMMddHHmmss"
|
|
Dim lastUpdate As String=DateTime.Date(fecha)
|
|
DateTime.DateFormat=OrigFormat 'return to orig date format
|
|
' Log(lastUpdate)
|
|
Return lastUpdate
|
|
End Sub |