This commit is contained in:
2023-10-16 19:01:04 -06:00
parent eb20c14c48
commit 57ef651054
112 changed files with 1988 additions and 7132 deletions

View File

@@ -2,7 +2,7 @@
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=11
Version=10.2
@EndOfDesignText@
#Region Service Attributes
#StartAtBoot: True
@@ -33,23 +33,17 @@ Version=11
'#AdditionalJar: com.google.android.gms:play-services-location
Sub Process_Globals
' Private nid As Int = 1
Private nid As Int = 1
Private 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
Dim logger As Boolean
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
UUGCoords.Initialize
logger = False
'Para FusedLocationProvider (2 lineas)
FLP.Initialize("flp")
FLP.Connect
@@ -58,36 +52,36 @@ Sub Service_Create
End Sub
Sub flp_ConnectionSuccess
' If logger Then Log("Connected to location provider")
If Starter.logger Then Log("Connected to location provider")
'FLP.GetLastKnownLocation
End Sub
Sub flp_ConnectionFailed(ConnectionResult1 As Int)
If logger Then Log("Failed to connect to location provider")
If Starter.logger Then Log("Failed to connect to location provider")
End Sub
Sub flp_ConnectionSuspended(ConnectionResult1 As Int)
If logger Then Log("FLP conection suspended")
If Starter.logger Then Log("FLP conection suspended")
StartFLP
End Sub
Sub Service_Start (StartingIntent As Intent)
LogColor("Iniciando Tracker ...", Colors.Green)
If Starter.logger Then Log("Tracker Service Started")
'Para FusedLocationProvider (1 linea)
Service.StopAutomaticForeground
' Service.StartForeground(51042, Subs.notiLowReturn("Kelloggs", "Activo", 51042))
StartServiceAt(Me, DateTime.Now + 10 * DateTime.TicksPerMinute, True)
Service.StartForeground(nid, CreateNotification("..."))
Track
If Not(Starter.Logger) Then logger = False
StartServiceAt(Me, DateTime.Now + 5 * DateTime.TicksPerMinute, True)
End Sub
Public Sub Track
If logger Then Log("Inicia Track - Tracking : "&Tracking)
If Starter.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 logger Then Log("Sin permisos de ublicación.")
If Starter.logger Then Log("No permission")
Return
End If
StartFLP 'Iniciamos FusedLocationProvider
@@ -95,46 +89,34 @@ Public Sub Track
End Sub
Public Sub StartFLP
If logger Then Log("StartFLP - flpStarted="&flpStarted)
Log("StartFLP - flpStarted="&flpStarted)
Do While FLP.IsConnected = False
Sleep(500)
If logger Then Log("sleeping")
If Starter.logger Then Log("sleeping")
Loop
' If flpStarted = False Then
' If logger Then Log("RequestLocationUpdates")
If Starter.logger Then Log("RequestLocationUpdates")
FLP.RequestLocationUpdates(CreateLocationRequest) 'Buscamos ubicacion
If logger Then LogColor("Buscamos ubicacion (movimientoMinimo = "&actualLR.GetSmallestDisplacement&")", Colors.Magenta)
' If logger Then Log(actualLR.GetSmallestDisplacement)
If Starter.logger Then Log("Buscamos ubicacion")
If Starter.logger Then Log(actualLR.GetSmallestDisplacement)
flpStarted = True
' End If
End Sub
Public Sub StartFLP2
If logger Then Log("StartFLP2 - flpStarted="&flpStarted)
Public Sub StartFLP2Reqs
If Starter.logger Then Log("StartFLP - flpStarted="&flpStarted)
Do While FLP.IsConnected = False
Sleep(500)
If logger Then Log("sleeping")
If Starter.logger Then Log("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
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)
FLP.RequestLocationUpdates(CreateLocationRequest2times) 'Buscamos ubicacion 2 peticiones
If Starter.logger Then Log("Buscamos ubicacion 2 peticiones")
If Starter.logger Then Log(actualLR.GetSmallestDisplacement)
End Sub
Private Sub CreateLocationRequest As LocationRequest
' If logger Then Log("CreateLocationRequest")
If Starter.logger Then Log("CreateLocationRequest")
Dim lr As LocationRequest
lr.Initialize
lr.SetInterval(10000) 'Intervalo deseado para actualizaciones de ubicacion
@@ -145,27 +127,14 @@ Private Sub CreateLocationRequest As LocationRequest
Return lr
End Sub
Private Sub CreateLocationRequest2 As LocationRequest
If logger Then Log("Iniciamos CreateLocationRequest2")
Private Sub CreateLocationRequest2times As LocationRequest
If Starter.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(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
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(0) 'Solo registra cambio de ubicacion si es mayor a XX mts
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
@@ -173,10 +142,9 @@ End Sub
Sub dameUltimaUbicacionConocida
If FLP.GetLastKnownLocation.IsInitialized Then 'Mandamos ultima ubicacion guardada
' If 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)
If Starter.logger Then Log("Mandamos GetLastKnownLocation : "&formatoFecha(FLP.GetLastKnownLocation.Time))
Dim coords As String = FLP.GetLastKnownLocation.Latitude&","&FLP.GetLastKnownLocation.Longitude&","&formatoFecha(FLP.GetLastKnownLocation.Time)
CallSubDelayed2(FirebaseMessaging,"mandamosLoc",coords)
End If
End Sub
@@ -189,58 +157,50 @@ Public Sub StopFLP
End Sub
Sub flp_LocationChanged (Location1 As Location)
LogColor($"Location changed lat=${Location1.Latitude}, lon=${Location1.Longitude}, Acc=${Location1.Accuracy}, SD=$1.0{actualLR.GetSmallestDisplacement}"$, Colors.green)
UUGCoords = Location1
' If logger Then Log("SmallestDisplacement="&actualLR.GetSmallestDisplacement)
If Starter.logger Then Log("SmallestDisplacement="&actualLR.GetSmallestDisplacement)
' CallSub2(Starter, "GPS_LocationChanged", Location1)
' CallSub2(gestion, "GPS_LocationChanged", Location1)
' CallSub2(MAPA_RUTAS, "GPS_LocationChanged", Location1)
' CallSub2(fila, "GPS_LocationChanged", Location1)
Starter.lat_gps = Location1.Latitude
Starter.lon_gps = Location1.Longitude
Starter.ubicacionActual = Location1
'Si las coordenadas estan en CERO o NULL entonces usamos "GetLastKnownLocation"
If Starter.lat_gps = 0 Or Starter.lat_gps = Null And FLP.GetLastKnownLocation.IsInitialized Then
Starter.lat_gps = FLP.GetLastKnownLocation.Latitude
Starter.lon_gps = FLP.GetLastKnownLocation.Longitude
Starter.ubicacionActual = FLP.GetLastKnownLocation
LogColor("Coords en CERO - Guardamos ultima ubicacion conocida", Colors.red)
End If
' Starter.ubicacionActual.Latitude = Starter.lat_gps
' Starter.ubicacionActual.Longitude = Starter.lon_gps
' Starter.ubicacionActual.Accuracy = Location1.Accuracy
'/////// para la ultima ubicacion FL
'/////// 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)
If Starter.lat_gps <> 0 And Starter.lat_gps <> Null Then
Try
Starter.db.ExecNonQuery("DELETE FROM HIST_GPS")
Starter.db.ExecNonQuery2("INSERT INTO HIST_GPS (HGDATE, HGLAT, HGLON) VALUES(?,?,?) ", Array As Object (sDate & sTime, Starter.lat_gps, Starter.lon_gps))
Catch
If logger Then Log("Error al borrar o insertar nuevas coordendas en HIST_GPS")
End Try
End If
Dim origFormat As String = DateTime.TimeFormat 'Guardamos formato de fecha.
DateTime.TimeFormat = "HHmmss" ' Modificamos formato de fecha.
Try
Starter.skmt.ExecNonQuery("DELETE FROM HIST_GPS")
Starter.skmt.ExecNonQuery2("INSERT INTO HIST_GPS (HGDATE, HGLAT, HGLON) VALUES(?,?,?) ", Array As Object (sDate & sTime, Starter.lat_gps, Starter.lon_gps))
Catch
If Starter.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)
If Starter.logger Then Log("Loc changed : "&Location1.Latitude&","&Location1.Longitude&"|"&Starter.usuario&"|")
If Starter.logger Then Log("Mandamos Ubicacion")
If Starter.logger Then Log(FirebaseMessaging.locRequest)
' Solo mandamos la ubicacion si la precision es dentro de XX mts
If Location1.Accuracy < 300 Then CallSubDelayed2(FirebaseMessaging,"mandamosLoc",coords)
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) - Starter.ultimaActualizacionGPS
' If logger Then Log("UltimaAct="&Starter.ultimaActualizacionGPS&" MinsDif="&minsDif)
If Location1.Accuracy < 100 And minsDif > 240 Then 'Si precision de 100 y 4 min transcurridos manda a web.
If logger Then Log("Actualizamos Ubicacion Web")
' If Starter.logger Then Log("UltimaAct="&Starter.ultimaActualizacionGPS&" MinsDif="&minsDif)
If Location1.Accuracy < 100 And minsDif > 240 Then 'Si precision de 100 y 4 min transcurridos manda a web
If Starter.logger Then Log("actualizamos Ubicacion")
CallSubDelayed(Starter, "ENVIA_ULTIMA_GPS")
End If
DateTime.TimeFormat = origFormat 'Regresamos formato de fecha original.
B4XPages.MainPage.cliente.GPS_LocationChanged(Location1)
B4XPages.MainPage.nuevoCliente.GPS_LocationChanged(Location1)
If Not(IsPaused(MAPA_RUTAS)) Then CallSubDelayed2(MAPA_RUTAS, "GPS_LocationChanged", Location1)
DateTime.TimeFormat = origFormat 'Regresamos formato de fecha original
' CallSubDelayed2(FirebaseMessaging,"mandamosLoc",coords)
End Sub
Sub CreateNotification (Body As String) As Notification 'ignore
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("This", Body, Main)
notification.SetInfo("Durakelo", Body, Main)
If Starter.logger Then Log("Creamos notificacion")
Return notification
End Sub
@@ -252,7 +212,7 @@ Sub Service_Destroy
lock.ReleasePartialLock
End Sub
Sub formatoFecha(fecha As String) As String 'ignore 'Convierte una fecha al formato yyMMddHHmmss
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"