#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
' Dim TimerService As Timer
Dim Counter As Int
Dim lm As ESLocation2
Dim myList As List
Dim LatN As String
Dim LonN As String
Dim ISNet As Boolean
Dim ISGPS As Boolean
Dim MyIMEI As String
Dim RtaServidor As String
Dim DateT As String
Counter=0
End Sub
Sub Service_Create
'TimerService.Initialize("TimerService",1000)
'TimerService.Enabled=True
'lm.Initialize("Location")
End Sub
Sub Service_Start (StartingIntent As Intent)
lm.Initialize("Location")
myList.Initialize
' LastLocation
Log("pido la locacion")' ver el tema del gps
Try
lm.requestNetworkLocation(0,0)
lm.requestGPSLocation(0,0)
Catch
Log("algun error")
End Try
Counter=Counter+1
Log ("servicio start: " & Counter)
ISGPS=False
ISNet=False
RtaServidor=""
EnviarPosicion'' arranco las tareas
StartServiceAt("", DateTime.Now+60*1000, True)
End Sub
Sub Service_Destroy
End Sub
'Sub TimerService_Tick
' 'busco la ultima posicion
' Dim paquetePos As String
' paquetePos=""
' ISGPS=False
' ISNet=False
' RtaServidor=""
' LastLocation 'tomo la long y lat y la presicion
' MyIMEI=GetDeviceId ' tomo el serial o id del equipo
' 'armo el jason
' DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
'
' DateT =DateTime.Date(DateTime.Now)
'' DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
' If ISGPS Then
' paquetePos=CadenaLastPosicion(MyIMEI,DateT,LatN, LonN, "GPS")
' Else If ISNet Then
' paquetePos=CadenaLastPosicion(MyIMEI,DateT,LatN, LonN, "NET")
' End If
' ' lo envio al servidor
' EnviarPaqueteAlServidor(paquetePos)
'
'
'' ToastMessageShow("Servicio: _ " & Counter, False)
' Log (RtaServidor)
' StartServiceAt("", DateTime.Now+60*1000, True)
' TimerService.Interval=70000
'End Sub
Sub LastLocation
Dim saux As String
Log("lastlocation")
myList = lm.findLastLocation("null")
Try
lm.stopNetworkListening'' si no trae siempre lo mismo
lm.stopGPSListening
Catch
End Try
Log("paro de escuchar")
Try
LatN=myList.Get(2)
LonN=myList.Get(3)
ISNet=True
saux="NET"
Catch
ISNet=False
End Try
Try
If myList.Size > 10 Then
LatN=myList.Get(12)
LonN=myList.Get(13)
ISGPS=True
saux="GPS"
End If
Catch
ISGPS=False
End Try
'ToastMessageShow(LatN &" : " & LonN & " : " & saux,True)
''aca lo deberia guardar en una base de datos para luego comparar
ToastMessageShow(saux,False)
End Sub
Sub CadenaLastPosicion (IMEI As String, Fecha As String, LaTi As String, LoNGi As String, presicion As String) As String
'IMEI:String
' Latitud: String
'Longitud: String
'Precision:String
Dim cadenaJson As String
Dim jcadena As Map
Dim JSONGenerator As JSONGenerator
'creo el paquete con el formato json {"nombreDeUsuario":"demo","clave":"demo"}, si quiero con [] uso el JSONList
jcadena.Initialize
jcadena.Put("IMEI",IMEI)
jcadena.Put("Fecha",Fecha)
jcadena.Put("Latitud",LaTi)
jcadena.Put("Longitud",LoNGi)
jcadena.Put("Precision",presicion)
JSONGenerator.Initialize(jcadena)
cadenaJson = JSONGenerator.ToString
Log (cadenaJson)
Return cadenaJson
End Sub
Sub GetDeviceId As String
Dim r As Reflector
Dim Api As Int
Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
If Api < 9 Then
'Old device
If File.Exists(File.DirInternal, "__id") Then
Return File.ReadString(File.DirInternal, "__id")
Else
Dim id As Int
id = Rnd(0x10000000, 0x7FFFFFFF)
File.WriteString(File.DirInternal, "__id", id)
Return id
End If
Else
'New device
Try
Return r.GetStaticField("android.os.Build", "SERIAL")
' Log(r.GetStaticField("android.os.Build", "SERIAL"))
Catch
Return "SIN_IMEI"
End Try
End If
End Sub
Sub EnviarPaqueteAlServidor (paquete As String)
Dim httpTest As HttpJob
httpTest.Initialize("EnviarPosicion",Me)
'`SPGuardarPosicion`(aimei text, alat text, along text, apres text)
httpTest.Download("http://"& Main.sErvidor & Main.puErto & "/api/v2/mysql_fdlh/_proc/SPGuardarPosicion("& paquete)
httpTest.GetRequest.SetHeader("X-DreamFactory-Api-Key", "36fda24fe5588fa4285ac6c6c2fdfbdb6b6bc9834699774c9bf777f706d05a88")
Log (paquete)
' ProgressDialogShow2("Please wait while we fetch your information", True)
End Sub
Sub JobDone(Job As HttpJob)
' ProgressDialogHide
If Job.Success Then
Dim res As String
res = Job.GetString
Log("Back from Job:" & Job.JobName )
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)''agarro la respuesta del servidor
Select Job.JobName
Case "EnviarPosicion"
Dim jListOfPersons As List
Dim RCount As Map
RCount.Initialize
jListOfPersons.Initialize
jListOfPersons= parser.NextArray 'me viene solo un map con la respuesta y no una lista
If jListOfPersons.Size=0 Then
RtaServidor="0"
Else
'
For i = 0 To jListOfPersons.Size - 1
RCount=jListOfPersons.Get(i)
RtaServidor= RCount.Get("ROW_COUNT()")
Next
End If
Log(RtaServidor)
End Select
Else
' ToastMessageShow("Error: " & Job.ErrorMessage, True)
ToastMessageShow("Error", False)
End If
Job.Release
' ToastMessageShow(RtaServidor,True)
End Sub
Sub EnviarPosicion() As String
Dim paquetePos As String
paquetePos=""
ISGPS=False
ISNet=False
RtaServidor=""
LastLocation 'tomo la long y lat y la presicion
MyIMEI=GetDeviceId ' tomo el serial o id del equipo
'armo el jason
' DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
'' deberia verificar si cambio y lo guardo mas de 1 mt
DateT = DateTime.GetYear(DateTime.Now)& "/" & DateTime.GetMonth(DateTime.Now)& "/" & DateTime.GetDayOfMonth(DateTime.Now) & " " & DateTime.GetHour(DateTime.Now) & ":" & DateTime.GetMinute(DateTime.Now)& ":" & DateTime.GetSecond(DateTime.Now)
' DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
Log (DateT)
If ISGPS Then
'paquetePos=CadenaLastPosicion(MyIMEI,DateT,LatN, LonN, "GPS")
paquetePos= MyIMEI & "," & LatN & "," & LonN & "," & "GPS)"
Else If ISNet Then
'paquetePos=CadenaLastPosicion(MyIMEI,DateT,LatN, LonN, "NET")
paquetePos= MyIMEI & "," & LatN & "," & LonN & "," & "NET)"
End If
' lo envio al servidor
EnviarPaqueteAlServidor(paquetePos)
' ToastMessageShow("Servicio: _ " & Counter, False)
Log (RtaServidor)
Return RtaServidor
' TimerService.Interval=70000
End Sub