mirror of
https://github.com/cheveguerra/FLP_2.0.git
synced 2026-04-17 19:36:42 +00:00
Cambio de puerto
This commit is contained in:
145
Subs.bas
145
Subs.bas
@@ -34,9 +34,9 @@ Sub getPhnId 'Pone el valor de phn.Model en la variable global "devModel"
|
||||
End If
|
||||
If elId.Length < 3 Then elId = $"dev${DateTime.GetHour(DateTime.Now)}"$
|
||||
File.WriteString(File.DirInternal, "phnId.txt", elId) 'Sobreescribimos archivo phnId.txt with elId
|
||||
If Starter.logger Then Log("Escribimos phnId: "&elId&" a "&File.DirInternal&"/phnId.txt")
|
||||
' If Starter.logger Then Log("Escribimos phnId: "&elId&" a "&File.DirInternal&"/phnId.txt")
|
||||
Starter.devModel = elId
|
||||
If Starter.logger Then Log(Starter.devModel)
|
||||
' If Starter.logger Then Log(Starter.devModel)
|
||||
End Sub
|
||||
|
||||
Sub compress(str As String) As String ' Compresion
|
||||
@@ -91,16 +91,16 @@ Sub guardaInfoEnArchivo(coords As String) 'Escribimos coordenadas y fecha a un a
|
||||
out.Close
|
||||
End Sub
|
||||
|
||||
Sub guardaInfoEnBD(coords As String) 'Escribimos coordenadas y fecha a una BD
|
||||
If Starter.logger Then Log("Guardamos ubicacion en db (" & coords & ")")
|
||||
Dim latlon() As String = Regex.Split(",", coords)
|
||||
If gps_hist.IsInitialized = False Then gps_hist.Initialize(Starter.ruta, "gps_hist.db", True)
|
||||
gps_hist.ExecNonQuery2("INSERT INTO RUTA_GPS(fecha, lat, lon) VALUES (?,?,?)", Array As Object (latlon(2),latlon(0),latlon(1)))
|
||||
'Guarda la ubicacion dada en la tabla "RUTA_GPS".
|
||||
Sub guardaInfoEnBD(loc As Location) 'Escribimos coordenadas y fecha a una BD
|
||||
revisaBD
|
||||
If Starter.logger Then Log($"Guardamos ubicacion en db (${loc.Latitude},${loc.Longitude})"$)
|
||||
gps_hist.ExecNonQuery2("INSERT INTO RUTA_GPS(fecha, lat, lon, acc, time) VALUES (?,?,?,?,?)", Array As Object (fechaKMT(loc.time),loc.Latitude,loc.Longitude, loc.Accuracy, loc.time))
|
||||
End Sub
|
||||
|
||||
Sub dameRuta As String
|
||||
Dim c As Cursor
|
||||
If gps_hist.IsInitialized = False Then gps_hist.Initialize(Starter.ruta, "gps_hist.db", True)
|
||||
If gps_hist.IsInitialized = False Then gps_hist.Initialize(File.DirInternal, "gps_hist.db", True)
|
||||
c = gps_hist.ExecQuery("select FECHA, LAT, LON from RUTA_GPS order by FECHA desc limit 380")
|
||||
c.Position = 0
|
||||
Dim ruta2 As String = ""
|
||||
@@ -131,11 +131,10 @@ Sub borramosArchivoGPS
|
||||
End Sub
|
||||
|
||||
Sub revisaBD
|
||||
' Starter.ruta = File.DirInternal
|
||||
If File.Exists(Starter.ruta, "gps_hist.db") = False Then
|
||||
File.Copy(File.DirAssets, "gps_hist.db", Starter.ruta, "gps_hist.db")
|
||||
If Starter.logger Then Log("No existe gps_hist, copiamos gps_hist.db")
|
||||
End If
|
||||
If Not(File.Exists(File.DirInternal, "gps_hist.db")) Then File.Copy(File.DirAssets, "gps_hist.db", File.DirInternal, "gps_hist.db")
|
||||
If Not(gps_hist.IsInitialized) Then gps_hist.Initialize(File.DirInternal, "gps_hist.db", True)
|
||||
gps_hist.ExecNonQuery("CREATE TABLE IF NOT EXISTS BITACORA(RUTA TEXT, TEXTO TEXT, FECHA TEXT)")
|
||||
gps_hist.ExecNonQuery("CREATE TABLE IF NOT EXISTS RUTA_GPS(FECHA INTEGER, LAT TEXT, LON TEXT, ACC INT, TIME INT)")
|
||||
End Sub
|
||||
|
||||
Sub getSSID
|
||||
@@ -155,6 +154,29 @@ Sub fechaKMT(fecha As String) As String 'ignore
|
||||
Return nuevaFecha
|
||||
End Sub
|
||||
|
||||
'Convierte una fecha en formato YYMMDDHHMMSS a Ticks
|
||||
Sub fechaKMT2Ticks(fKMT As String) As Long 'ignore
|
||||
Try
|
||||
If fKMT.Length = 12 Then
|
||||
Private parteFecha As String = fKMT.SubString2(0,6)
|
||||
Private parteHora As String = fKMT.SubString(6)
|
||||
Private OrigFormat As String = DateTime.DateFormat 'save original date format
|
||||
DateTime.DateFormat="yymmdd"
|
||||
DateTime.TimeFormat="HHmmss"
|
||||
Private ticks As Long = DateTime.DateTimeParse(parteFecha,parteHora)
|
||||
DateTime.DateFormat=OrigFormat 'return to original date format
|
||||
Return ticks
|
||||
Else
|
||||
Log("Formato de fecha incorrecto, debe de ser 'YYMMDDHHMMSS', no '"&fKMT&"' largo="&fKMT.Length)
|
||||
Return 0
|
||||
End If
|
||||
Catch
|
||||
Log(LastException)
|
||||
LogColor($"Fecha dada: ${fKMT}, Parte Fecha: ${parteFecha}, Parte Hora: ${parteHora}"$, Colors.Red)
|
||||
Return 0
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
'Convierte una fecha al formato yyMMddHHmmss
|
||||
Sub fechaNormal(fecha As String) As String 'ignore
|
||||
' Log(fecha)
|
||||
@@ -165,18 +187,6 @@ Sub fechaNormal(fecha As String) As String 'ignore
|
||||
Return nuevaFecha
|
||||
End Sub
|
||||
|
||||
'Sub dameUltimaUbicacionConocida(lastLocation As Location)
|
||||
' Log(0)
|
||||
' If Tracker.FLP.GetLastKnownLocation.IsInitialized Then 'Mandamos ultima ubicacion guardada
|
||||
' Log("dameUltimaUbicacionConocida")
|
||||
' If Tracker.FLP.GetLastKnownLocation.Accuracy < 30 And Tracker.FLP.GetLastKnownLocation.DistanceTo(lastLocation) > 50 Then
|
||||
' Starter.UUC = Tracker.FLP.GetLastKnownLocation
|
||||
' mandaLocAServer(Tracker.FLP.GetLastKnownLocation, Starter.devModel)
|
||||
' Log($"UUC: ${Starter.UUC.Latitude},${Starter.UUC.Longitude}"$)
|
||||
' End If
|
||||
' End If
|
||||
'End Sub
|
||||
|
||||
Sub mandaLocAServer(loc As Location, id As String)
|
||||
Starter.lastLocUpdate = DateTime.Now
|
||||
If Not(IsPaused(Main)) Then CallSubDelayed(Main, "actualizaLabelUU")
|
||||
@@ -189,6 +199,14 @@ Sub mandaLocAServer(loc As Location, id As String)
|
||||
Starter.reqManager.ExecuteCommand(Starter.cmd,"guardaDatos")
|
||||
End Sub
|
||||
|
||||
'Manda la ubicacion al servidor de BD y a FirebaseMessage.
|
||||
Sub mandaLoc2(loc As Location, id As String) 'ignore
|
||||
mandaLocAServer(loc, id)
|
||||
FirebaseMessaging.locRequest = "Activa"
|
||||
CallSubDelayed2(FirebaseMessaging, "mandaLocFM", loc)
|
||||
guardaInfoEnBD(loc)'Escribimos coordenadas y fecha a una bd
|
||||
End Sub
|
||||
|
||||
Sub ConvertMillisecondsToString(t As Long) As String 'ignore
|
||||
Dim hours, minutes, seconds As Int
|
||||
hours = t / DateTime.TicksPerHour
|
||||
@@ -197,15 +215,86 @@ Sub ConvertMillisecondsToString(t As Long) As String 'ignore
|
||||
Return $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
|
||||
End Sub
|
||||
|
||||
'Convierte ticks a minutos.
|
||||
Sub ticksAMins(ts As Long) As Long 'ignore
|
||||
Private m As Long = ((ts/1000)/60)
|
||||
Return m
|
||||
End Sub
|
||||
|
||||
Sub bitacora(texto As String) 'ignore
|
||||
revisaBD
|
||||
' Log(fechaNormal(DateTime.now))
|
||||
gps_hist.ExecNonQuery($"insert into BITACORA (RUTA, TEXTO, FECHA) values ('${Starter.devModel}', '${texto}', '${fechaNormal(DateTime.now)}')"$)
|
||||
End Sub
|
||||
|
||||
'Borramos renglones extra de la tabla de errores
|
||||
Sub borraArribaDe10000Bitacora 'ignore
|
||||
Sub borraArribaDeXXXBitacora(limite As Int) 'ignore
|
||||
If Starter.logger Then LogColor("Recortamos la tabla de bitacora, limite de 10,000", Colors.Magenta)
|
||||
gps_hist.ExecNonQuery("DELETE FROM bitacora WHERE fecha NOT in (SELECT fecha FROM bitacora ORDER BY fecha desc LIMIT 9999 )")
|
||||
gps_hist.ExecNonQuery($"DELETE FROM bitacora WHERE fecha NOT in (SELECT fecha FROM bitacora ORDER BY fecha desc LIMIT ${limite})"$)
|
||||
gps_hist.ExecNonQuery("vacuum;")
|
||||
' if starter.logger then Log("Borramos mas de 100 de errorLog")
|
||||
End Sub
|
||||
End Sub
|
||||
|
||||
'Regresa la ultima ubicacion guardada o una ubicacion sin inicializar si no encuentra nada.
|
||||
Sub traeUltimaUbicacionGuardada As Location 'ignore
|
||||
Private loc As Location
|
||||
Private c As Cursor = gps_hist.ExecQuery($"select FECHA, LAT, LON, ACC, TIME from RUTA_GPS order by fecha desc limit 1"$)
|
||||
If c.RowCount > 0 Then
|
||||
c.Position = 0
|
||||
loc.Initialize
|
||||
loc.Latitude = c.GetString("LAT")
|
||||
loc.Longitude = c.GetString("LON")
|
||||
loc.Accuracy = 0
|
||||
If c.GetString("ACC") <> Null Then loc.Accuracy = c.GetString("ACC")
|
||||
loc.Time = 0
|
||||
If c.GetString("TIME") <> Null Then loc.Time = c.GetString("TIME")
|
||||
End If
|
||||
c.Close
|
||||
Return loc
|
||||
End Sub
|
||||
|
||||
'Busca la ultima ubicacion guardada en la table GPS_HIST, y dependiendo del tiempo transcurrido hace lo siguiente:
|
||||
' - Mas de 10 minutos -> Pide actualzación de ubicacion.
|
||||
' - Mas de 20 minutos -> Apaga y prende el servicio de FLP.
|
||||
' - Mas de 30 minutos -> Reinicia la aplicación.
|
||||
Sub revisaYmandaUUC
|
||||
Private ultimaLoc As Location = traeUltimaUbicacionGuardada
|
||||
If ultimaLoc.IsInitialized Then
|
||||
Starter.UUC = ultimaLoc
|
||||
Private minsTranscurridos As String = ticksAMins(DateTime.Now - ultimaLoc.Time) 'Minutos transcurridos desde la ultima ubicacion guardada.
|
||||
Log($"Ultima ubicacion guardada hace ${minsTranscurridos} mins."$)
|
||||
If minsTranscurridos > 10 And minsTranscurridos < 20 Then
|
||||
bitacora("Mas de 10 mins - REQ-UPDATE")
|
||||
LogColor($"Ubicacion vieja (mas de 10 mins.)"$, Colors.Red)
|
||||
LogColor("Pedimos actualizacion!", Colors.blue)
|
||||
If Tracker.flp.IsInitialized And Tracker.locReqSmall.IsInitialized Then
|
||||
bitacora("REQ-UPDATE") : LogColor("REQ-UPDATE", Colors.magenta)
|
||||
Tracker.flp.RequestLocationUpdates(Tracker.locReqSmall)
|
||||
Else
|
||||
bitacora("TRACKER APAGADO - RESTART-TRACKER") : LogColor("RESTART-TRACKER", Colors.magenta)
|
||||
StopService(Tracker)
|
||||
Sleep(5000)
|
||||
StartService(Tracker)
|
||||
End If
|
||||
else if minsTranscurridos >= 20 And minsTranscurridos < 30 Then
|
||||
LogColor("RESTART-TRACKER", Colors.red)
|
||||
bitacora("Mas de 20 mins - RESTART-TRACKER")
|
||||
' bitacora(Tracker.flp.SuspendedCause)
|
||||
StopService(Tracker)
|
||||
Sleep(5000)
|
||||
StartService(Tracker)
|
||||
else If minsTranscurridos >= 30 Then
|
||||
If Tracker.flp.IsInitialized Then LogColor($"FLP.Connected: ${Tracker.flp.IsConnected}"$, Colors.Red)
|
||||
If Tracker.flp.IsInitialized and Not(Tracker.flp.IsConnecting) Then 'Si NO esta en proceso de conectarse ...
|
||||
LogColor("RESTART-APP", Colors.red)
|
||||
bitacora("Mas de 30 mins - RESTART-APP")
|
||||
' bitacora(Tracker.flp.SuspendedCause) 'ignore
|
||||
Starter.UUC.Time = DateTime.Now
|
||||
guardaInfoEnBD(Starter.UUC) 'Guardamos la ultima ubicacion con la hora actual, para que no se reinicie la app si no consigue una ubicacion nueva.
|
||||
Sleep(1000)
|
||||
ExitApplication
|
||||
End If
|
||||
End If
|
||||
' LogColor($"Ultima loc: ${Subs.fechaNormal(ultimaLoc.Time)}"$, Colors.blue)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Reference in New Issue
Block a user