diff --git a/Files/layout.bal b/Files/layout.bal index c148cb0..f409732 100644 Binary files a/Files/layout.bal and b/Files/layout.bal differ diff --git a/FirebaseMessaging.bas b/FirebaseMessaging.bas index 931b3b5..046e9ad 100644 --- a/FirebaseMessaging.bas +++ b/FirebaseMessaging.bas @@ -6,10 +6,12 @@ Version=10.5 @EndOfDesignText@ Sub Process_Globals Private fm As FirebaseMessaging + Dim db As SQL End Sub Sub Service_Create fm.Initialize("fm") + db = Subs.dbInit End Sub Public Sub SubscribeToTopics @@ -49,8 +51,32 @@ Sub fm_MessageArrived (Message As RemoteMessage) 'Si el mensaje es de ubicacion recibida If Message.GetData.ContainsKey("t") And (Message.GetData.Get("t") = "u" Or Message.GetData.Get("t") = "au") Then Log("Recibimos ubicacion") + Private ubi As Location Log("Llamamos UbicacionRecibida") - If Message.GetData.Get("t") = "au" Then ToastMessageShow("Ubicacion recibida:"&Message.GetData.Get("d"),False) + If Message.GetData.Get("t") = "au" Then +' ToastMessageShow("Ubicacion recibida:"&Message.GetData.Get("body"),False) + Private coords() As String = Regex.split(",", Message.GetData.Get("body")) + ubi.Initialize + ubi.Latitude = coords(0) + ubi.Longitude = coords(1) + Private c As Cursor = db.ExecQuery("select * from geocercas") 'Traemos las geocercas + Private estaGC As Location + If c.RowCount > 0 Then + For i = 0 To c.RowCount - 1 + c.Position = i + estaGC.Initialize + estaGC.Latitude = c.GetString("lat") + estaGC.Longitude = c.GetString("lon") + Log(ubi & "|" & estaGC) + Log(ubi.DistanceTo(estaGC)) + If ubi.DistanceTo(estaGC) < 300 Then 'Revisamos si la ubicaccion recibida esta dentro de alguna geocerca. +' ToastMessageShow($"Dentro de ${c.GetString("nombre")}"$, False) + Subs.notiHigh("Dentro de Geocerca", $"${Message.GetData.Get("d")} esta dentro de ${c.GetString("nombre")}"$, 777, "Main") + End If + Next + End If +' Subs.notiHigh("Dentro de Geocerca", $"${Message.GetData.Get("d")} esta dentro de ${c.GetString("nombre")}"$, 777, "Main") + End If CallSub2(Main, "ubicacionRecibida", Message.GetData) End If diff --git a/Pusher.b4a b/Pusher.b4a index 338b314..588edd3 100644 --- a/Pusher.b4a +++ b/Pusher.b4a @@ -45,11 +45,12 @@ Library14=json Library15=okhttputils2 Library16=phone Library17=randomaccessfile -Library18=runtimepermissions -Library19=xcustomlistview +Library18=reflection +Library19=runtimepermissions Library2=byteconverter -Library20=xui -Library21=reflection +Library20=xcustomlistview +Library21=xui +Library22=sql Library3=compressstrings Library4=core Library5=dateutils @@ -65,7 +66,7 @@ Module4=MyPopup Module5=Starter Module6=Subs NumberOfFiles=18 -NumberOfLibraries=21 +NumberOfLibraries=22 NumberOfModules=6 Version=12.5 @EndOfDesignText@ @@ -103,6 +104,7 @@ Sub Process_Globals 'Api Private const API_KEY As String = "AAAAv1qt3Lk:APA91bECIR-pHn6ul53eYyoVlpPuOo85RO-0zcAgEXwE7vqw8DFSbBtCaCINiqWQAkBBZXxHtQMdpU6B-jHIqgFKVL196UgwHv0Gw6_IgmipfV_NiItjzlH9d2QNpGLp9y_JUKVjUEhP" Private rp As RuntimePermissions + Dim db As SQL Dim phn As Phone Dim devModel As String Dim dUbicados As Int 'Dispositivos con ubicacion @@ -124,6 +126,7 @@ Sub Process_Globals Private hInicio, hFinal As String Private distanciaRecorrida As String = "0" Private usuario As String + dim punto as LatLng End Sub Sub Globals @@ -181,20 +184,25 @@ Sub Globals Private et_gc_nombre As EditText Private b_gc_cancelar As Button Private b_gc_ok As Button + Private b_geocercas As Button + Private p_geocercas As Panel + Private lv_geocercas As ListView + Private b_cerrar As Button End Sub Sub Activity_Create(FirstTime As Boolean) 'ignore Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Wait For Activity_PermissionResult (Permission As String, Result As Boolean) - Dim p As String + db = Subs.dbInit + Dim p As String 'ignore If File.ExternalWritable Then p = File.DirRootExternal Else p = File.DirInternal End If - Log("rootExternal="&p) - Log("File.DirInternal="&File.DirInternal) - Log("File.DirRootExternal="&File.DirRootExternal) +' Log("rootExternal="&p) +' Log("File.DirInternal="&File.DirInternal) +' Log("File.DirRootExternal="&File.DirRootExternal) Activity.LoadLayout("Layout") clientes.Initialize @@ -222,6 +230,9 @@ Sub Activity_Create(FirstTime As Boolean) 'ignore topInicialBuscador = p_buscador.top s_tracker.AddAll(clientes) cb_puntosIntermedios.Checked = True + + p_geocercas.BringToFront + p_geocercas.Left = (Activity.Width / 2) - (p_geocercas.Width / 2) ' reqManager.Initialize(Me, "http://10.0.0.205:1782") ' s_hora.AddAll(Array As String("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23")) End Sub @@ -304,6 +315,7 @@ Sub JobDone(Job As HttpJob) Subs.SetDateFormat("es", "MX", "MMM d") distanciaRecorrida = 0 cont = 0 + Private c As Cursor = db.ExecQuery("select * from geocercas") For Each records() As Object In result.Rows ' Log(records(result.Columns.Get("FECHA"))) If cont = 0 Then @@ -329,12 +341,29 @@ Sub JobDone(Job As HttpJob) End If End If cont = cont + 1 + coordsAnt.Latitude = estasCoords.Latitude coordsAnt.Longitude = estasCoords.Longitude fechaFinRutaGPS = Subs.fechaKMT2Ticks(records(result.Columns.Get("FECHA"))) DateTime.timeformat = "HH:mm" fechaFinRutaGPS = DateTime.Date(fechaFinRutaGPS) & ", " & DateTime.Time(fechaFinRutaGPS) Next + Private estaGC As Location + If c.RowCount > 0 Then + For i = 0 To c.RowCount - 1 + c.Position = i + estaGC.Initialize + estaGC.Latitude = c.GetString("lat") + estaGC.Longitude = c.GetString("lon") +' Log(estasCoords & "|" & thisGC) + Log(estasCoords.DistanceTo(estaGC)) + If estasCoords.DistanceTo(estaGC) < 300 Then + ToastMessageShow($"Dentro de ${c.GetString("nombre")}"$, False) +' Subs.notiHigh("Dentro de Geocerca", $"${usuario} esta dentro de ${c.GetString("nombre")}"$, 777, "Main") + End If + Next + End If + c.Close ToastMessageShow("Recibimos ruta con " & reqManager.HandleJob(Job).Rows.Size & " puntos.", False) ' Log(rutaGPS) LogColor($"Distancia recorrida: $1.1{distanciaRecorrida/1000} kms."$, Colors.Magenta) @@ -533,6 +562,7 @@ Sub MapFragment2_LongClick(Point As LatLng) p_addGeofence.BringToFront p_addGeofence.Left = (Activity.Width / 2) - (p_addGeofence.Width / 2) p_addGeofence.Visible = True + punto = Point End Sub Sub InfoWindowAdapter1_GetInfoContents(Marker1 As Marker) As View @@ -681,6 +711,7 @@ Sub b_buscar_longClick p_ruta.Height = Activity.Height p_periodoInfo.Top = Activity.Height - p_periodoInfo.Height - 5 b_regresar.Top = Activity.Height - b_regresar.Height - 5 + b_geocercas.Top = Activity.Height - b_geocercas.Height - 130 l_periodoTitulo.Top = Activity.Height - (Activity.Height - p_mapa2.Height) l_periodo.Top = Activity.Height - (Activity.Height - p_mapa2.Height) + 10 s_rutas.Top = l_periodoTitulo.Top + l_periodoTitulo.Height @@ -787,8 +818,48 @@ End Sub Private Sub b_gc_ok_Click p_addGeofence.Visible = False + db.ExecNonQuery($"insert into geocercas (nombre, lat, lon) values ('${et_gc_nombre.Text}', '${punto.Latitude}', '${punto.Longitude}')"$) + et_gc_nombre.Text = "" End Sub Private Sub b_gc_cancelar_Click p_addGeofence.Visible = False +End Sub + +Private Sub b_geocercas_Click + Private c As Cursor = db.ExecQuery("select * from geocercas") + Dim nombre As Label = lv_geocercas.SingleLineLayout.Label + nombre.TextColor = Colors.Black + nombre.TextSize = 14 + lv_geocercas.SingleLineLayout.ItemHeight = 30dip + lv_geocercas.Clear + If c.RowCount > 1 Then + For i = 0 To c.RowCount - 1 + c.Position = i + lv_geocercas.AddSingleLine($"${c.GetString("nombre")}"$) + Next + End If + c.Close + p_geocercas.BringToFront + p_geocercas.Visible = True +End Sub + +Private Sub lv_geocercas_ItemClick (Position As Int, Value As Object) + ToastMessageShow("Clic largo para borrar", False) +End Sub + + +Private Sub lv_geocercas_ItemLongClick (Position As Int, Value As Object) + If Msgbox2($"Seguro que desea borrar la geocerca "${Value}""$, "AVISO", "Aceptar", "Cancelar", "", Null) = DialogResponse.POSITIVE Then 'ignore + db.ExecNonQuery($"delete from geocercas where nombre = '${Value}'"$) + End If + p_geocercas.Visible = False +End Sub + +Private Sub p_geocercas_Click + +End Sub + +Private Sub b_cerrar_Click + p_geocercas.Visible = False End Sub \ No newline at end of file diff --git a/subs.bas b/subs.bas index 7071c5d..c09e490 100644 --- a/subs.bas +++ b/subs.bas @@ -138,6 +138,31 @@ Sub centraPanel(elemento As Panel, anchoElementoSuperior As Int) 'ignore elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2) End Sub +'Inicializa la BD con "kmt.db" en File.DirInternal, si el archivo no existe, lo copia desde File.DirAssets. +'Dispara el evento "dbOk" cuando termina. +Sub dbInit As SQL + Private db As SQL +' If File.Exists(File.DirInternal, "push.db") = False Then File.Copy(File.DirAssets, "push.db", File.DirInternal, "kmt.db") + db.Initialize(File.DirInternal,"push.db", True) + db.ExecNonQuery("CREATE TABLE IF NOT EXISTS geocercas (nombre TEXT, lat TEXT, lon TEXT)") + Return db +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 + 'Geo-Zone Determination (Point in Polygon) 'Use this to determine If a vehicle is within a defined zone made of 5 or more lat/lon coordinates. 'Point 1 Is also Point 5 (first point And last point are same value).