Files
Pusher_2.0/subs.bas

217 lines
6.8 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=11
@EndOfDesignText@
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub mueveCamaraMapa(mapFragment As MapFragment, lat As String, lon As String)
'Mueve el mapa a la posicion solicitada
Private gmap As GoogleMap
gmap = mapFragment.GetMap
Dim aa As CameraPosition
aa.Initialize(lat,lon,15)
gmap.AnimateCamera(aa)
End Sub
Sub CreateNotification (Body As String) As Notification 'ignore
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Tracking location", Body, Main)
Return notification
End Sub
Sub removeFomList(myList As List, theItem As String) 'ignore
Dim x As Int = 0
Dim myObject As List
Do While x < myList.Size
myObject = myList.get(x)
If myObject.Get(x) = theItem Then
myList.removeAt(x)
Else
x = x + 1
End If
Loop
End Sub
Sub creaMarcador(mapFragment As MapFragment, lat As String, lon As String, devModel As String, timemarker As Double) 'ignore
' Private gmap As GoogleMap
' gmap = mapFragment.GetMap
' Dim Marker1 As Marker
' Marker1 = gmap.AddMarker(lat, lon, title)
' Marker1.Title = devModel
'
' Dim horaFecha As String = timemarker
' Dim hms As String = horaFecha.SubString(6) 'Tomamos solo la parte de la hora
'' Log("hms="&hms)
' Dim horasMinsSegs As String = hms.SubString2(0,2)&":"&hms.SubString2(2,4)&":"&hms.SubString(4)
'' Log(horasMinsSegs)
' If Main.wifi <> "" And Main.wifi <> Null Then
' Main.wifi = $"Wifi: ${Main.wifi&CRLF}"$
' Else
' Main.wifi = ""
' End If
' Marker1.Snippet = "Last Loc: "&horasMinsSegs&CRLF&"Monto Total: "&montoTotal&CRLF&"Bateria: "&batt&"%"
End Sub
'Convierte una fecha al formato yyMMddHHmmss
Sub fechaKMT(fecha As String) As String 'ignore
' Log(fecha)
Dim OrigFormat As String = DateTime.DateFormat 'save orig date format
DateTime.DateFormat="yyMMddHHmmss"
Dim nuevaFecha As String=DateTime.Date(fecha)
DateTime.DateFormat=OrigFormat 'return to orig date format
' Log(nuevaFecha)
Return nuevaFecha
End Sub
'Hace visible y trae al frente el panel con los parametros "Top" y "Left" dados
Sub panelVisible(panel As Panel, top As Int, left As Int) 'ignore
panel.BringToFront
panel.Visible = True
panel.Top = top
panel.Left = left
End Sub
Sub diaSemanaString(diaSemana As Int) As String 'ignore
If diaSemana = 2 Then Return "Lun"
If diaSemana = 3 Then Return "Mar"
If diaSemana = 4 Then Return "Mie"
If diaSemana = 5 Then Return "Jue"
If diaSemana = 6 Then Return "Vie"
If diaSemana = 7 Then Return "Sab"
If diaSemana = 1 Then Return "Dom"
Return ""
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)
' Log(" +++ +++ pFecha:"&parteFecha&" | pHora:"&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
Sub SetDateFormat(Language As String, Country As String, format As String)
#if B4A or B4J
Dim locale As JavaObject
locale.InitializeNewInstance("java.util.Locale", Array(Language, Country))
Dim DateFormat As JavaObject
DateFormat.InitializeNewInstance("java.text.SimpleDateFormat", Array(format, locale))
Dim r As Reflector
r.Target = r.RunStaticMethod("anywheresoftware.b4a.keywords.DateTime", "getInst", Null, Null)
r.SetField2("dateFormat", DateFormat)
#else if B4i
Dim locale As NativeObject
locale = locale.Initialize("NSLocale").RunMethod("alloc", Null).RunMethod("initWithLocaleIdentifier:", Array(Language & "_" & Country))
DateTime.As(NativeObject).GetField("dateFormat").SetField("locale", locale)
DateTime.DateFormat = format
#End if
End Sub
'Centra un panel dentro de un elemento superior
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.OnGoingEvent = True
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
Sub cancelNoti(id) 'ignore
Private notif As Notification 'ignore
notif.Cancel(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).
'You can add the points clockwise or counterclockwise.
Sub FindInZone( polx As List, poly As List, x As Double, y As Double) As Boolean 'ignore
' polx = list of lats for polygon
' poly = list of lons for polygon
' y = lon to test
' x = lat to test
Dim x1, y1, x2, y2, D As Double
Dim i, ni As Int
ni = 0
x1 = polx.Get(0)
y1 = poly.Get(0)
For i = 0 To polx.Size -1
If i < polx.Size Then
x2 = polx.Get(i)
y2 = poly.Get(i)
Else
x2 = polx.Get(0) ' checks the last line
y2 = poly.Get(0)
End If
If y >= Min(y1, y2) Then
If y <= Max(y1, y2) Then
If x <= Max(x1, x2) Then
If (x = x1 And y = y1) Or (x = x1 And x = x2) Then ' checks vertices and vertical lines
Return True
End If
If y1 <> y2 Then
D = (y - y1) * (x2 - x1) / (y2 - y1) + x1
If x1 = x2 Or x <= D Then
ni = ni + 1
End If
End If
End If
End If
End If
x1 = x2
y1 = y2
Next
If ni Mod 2 = 0 Then
Return False
Else
Return True
End If
End Sub