- VERSION 4.07.28

- Se agregó el nuevo ruteo dinamico
This commit is contained in:
Jose Alberto Guerra Ugalde
2024-07-30 01:11:14 -06:00
parent 2500ffea11
commit 42d6e32f32
9 changed files with 221 additions and 42 deletions

View File

@@ -33,6 +33,7 @@ Sub Class_Globals
Dim listaWayPoints As List
Dim lv1Top As String
Private b_limpiarRuta As Button
Dim rutaGenerada As Boolean = False
End Sub
'You can add more parameters here.
@@ -90,7 +91,7 @@ Sub B4XPage_Appear
cs2.Initialize
' t1 = Starter.waypointsOrdered.Get(k)
' c.GetString("codigo")
Log(c.GetString("indice"))
' Log(c.GetString("indice"))
ListView1.AddTwoLines(cs.Color(Colors.RGB(100,149,237)).Append(c.GetString("codigo")).PopAll, cs2.append(c.GetString("CAT_CL_NOMBRE")).Color(Colors.RGB(100,149,237)).Append(" Calle: ").Pop.Append(c.GetString("CAT_CL_CALLE").Trim & " " & c.GetString("CAT_CL_NOEXT")).PopAll )
Next
Else
@@ -105,11 +106,36 @@ Sub B4XPage_Appear
Subs.centraEtiqueta(lfila, Root.Width)
b_getRutaInfo.Visible = True
b_getRutaInfo.BringToFront
If Starter.cedisLocation.Latitude = "0" Then
Private x As Cursor = Starter.skmt.ExecQuery("select * from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'COORDS_ALMACEN'")
If x.RowCount > 0 Then
x.Position = 0
Private y() As String
Starter.cedisLocation.Latitude = Regex.Split(",", x.GetString("CAT_VA_VALOR"))(1)
Starter.cedisLocation.Longitude = Regex.Split(",", x.GetString("CAT_VA_VALOR"))(0)
End If
End If
Log(Starter.cedisLocation)
End Sub
Private Sub B4XPage_CloseRequest As ResumableSub
' BACK key pressed
'Return True to close, False to cancel
Log(entro)
If rutaGenerada Or entro = 3 Then ' Si ya generamos el ruteo o entramos a una de las rutas, nos regresamos!
b_limpiarRuta_Click
l_rutaInfo.Text = ""
rutaGenerada = False
else if entro = 2 Then ' Si estamos mostrando las rutas, nos regresamos a la principal.
B4XPages.ShowPage("Principal")
End If
Return False
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
' Log($"Entro= ${entro}"$)
Log(Value)
ListView1.Clear
Sleep(50)
Subs.SetDivider(ListView1, Colors.LightGray, 2)
@@ -121,14 +147,14 @@ Sub ListView1_ItemClick (Position As Int, Value As Object)
l_rutaInfo.Visible = False
b_GetDirs.Visible = False
If entro = "2" Then
b_GetDirs.Visible = True
img_getDirs.Visible = True
b_getRutaInfo.Visible = False
' b_GetDirs.Visible = True
' img_getDirs.Visible = True
' b_getRutaInfo.Visible = False
Private lrt As String
lrt = Value
laRuta = lrt.SubString(6) 'Quitamos el texto "Ruta: " para obtener el numero de la ruta.
' Log($"Original: ${Value} - Mod: |${lrt.SubString(6)}| - laRuta: ${laRuta}"$)
c2=Starter.skmt.ExecQuery2("select CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO, CAT_CL_LAT, CAT_CL_LONG from kmt_info where CAT_CL_RUTA = ? and gestion = 0 order by CAT_CL_NOMBRE ", Array As String(laRuta))
c2 = Starter.skmt.ExecQuery2("select CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO, CAT_CL_LAT, CAT_CL_LONG from kmt_info where CAT_CL_RUTA = ? and gestion = 0 order by CAT_CL_NOMBRE ", Array As String(laRuta))
Private thisLoc As Location
Private label1 As Label
Private label2 As Label
@@ -138,14 +164,14 @@ Sub ListView1_ItemClick (Position As Int, Value As Object)
label2 = ListView1.TwoLinesLayout.SecondLabel
label2.TextSize = 15
label2.TextColor = Colors.black
label2.Height = 38dip
label2.Height = 57dip
thisLoc.Initialize
If entro = 2 Then ListView1.TwoLinesLayout.ItemHeight = 75dip
If entro = 2 Then ListView1.TwoLinesLayout.ItemHeight = 90dip
lfila.text = "Nombre y Calle"
distOrderedMap.Initialize
If c2.RowCount>0 Then
For i=0 To c2.RowCount -1 'Generamos mapa de clientes
c2.Position=i
If c2.RowCount > 0 Then
For i = 0 To c2.RowCount -1 'Generamos mapa de clientes
c2.Position = i
thisLoc.Latitude = c2.GetString("CAT_CL_LAT")
thisLoc.Longitude = c2.GetString("CAT_CL_LONG")
' Log(Tracker.UUGCoords)
@@ -159,12 +185,14 @@ Sub ListView1_ItemClick (Position As Int, Value As Object)
For Each k As Object In distOrderedMap.Keys 'Generamos el listView con el mapa ordenada.
m1 = distOrderedMap.Get(k)
m1.Get("codigo")
Log(m1.Get("nomDirDist"))
ListView1.AddTwoLines(m1.Get("codigo"), m1.Get("nomDirDist"))
Next
End If
c2.Close
entro = "3"
Else If entro = "3" Then
' LogColor("Guardamos CLIENTE " & Value, Colors.red)
Starter.skmt.ExecNonQuery("delete from CUENTAA")
Starter.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?)", Array As Object(Value))
B4XPages.ShowPage("Cliente")
@@ -407,12 +435,14 @@ Sub traeRutaDia(aVisitar As B4XOrderedMap)
Private r As Int = 1
Private r1, wps As Map
Starter.skmt.ExecNonQuery("delete from waypoints")
Log("BORRAMOS WAYPOINTS")
For Each k As Object In clientesMapaO.Keys 'Guardamos en la BD el orden de los waypoints para luego generar el listview.
If listaWayPoints.Size > 0 Then
r1 = clientesMapaO.Get(k)
r1.Get("codigo")
Log(listaWayPoints.Get(r) & "|" & r1.Get("coords") & "|" & r1.Get("calle"))
wps = listaWayPoints.Get(r)
Starter.skmt.ExecNonQuery($"update kmt_info set SECUENCIA = ${wps.get("waypoint_index")} where CAT_CL_CODIGO = '${r1.Get("codigo")}'"$)
Starter.skmt.ExecNonQuery2("insert into waypoints values (?,?)", Array As Object(r1.Get("codigo"), wps.get("waypoint_index")))
r = r + 1
End If
@@ -546,7 +576,8 @@ End Sub
'Traemos la ruta de visitas via el API de OSRM usando el sub "traeRutaDia(traeTodosAVisitar)".
Private Sub b_getRutaInfo_Click
traeRutaDia(traeTodosAVisitar)
' traeRutaDia(traeTodosAVisitar)
caculaRutaGPS(todosAVisitar)
End Sub
'Mostramos u ocultamos el boton para borrar los waypoints de la ruta.
@@ -563,4 +594,96 @@ Private Sub b_limpiarRuta_Click
Starter.skmt.ExecNonQuery("delete from waypoints")
b_limpiarRuta.Visible = False
B4XPage_Appear
End Sub
End Sub
Sub caculaRutaGPS(coords As List)
Private tmpList As List
Dim cs, cs2 As CSBuilder
tmpList.Initialize
tmpList.AddAll(Array As String("ACTUAL", Tracker.UUGCoords.Longitude, Tracker.UUGCoords.Latitude))
coords.InsertAt(0,tmpList)
Log("=================================")
' Log(coords)
Private coordsStr As String = ""
For Each tienda As List In coords
' Log(tienda)
If coordsStr = "" Then
coordsStr = $"${tienda.get(0)},${tienda.get(1)},${tienda.get(2)}"$
Else
coordsStr = $"${coordsStr};${tienda.get(0)},${tienda.get(1)},${tienda.get(2)}"$
End If
Next
Log(coordsStr)
ProgressDialogShow2("Calculando distancia y tiempo, un momento por favor.", False)
Dim j As HttpJob
j.Initialize("trip", Me) 'Calculamos el resto de la ruta.
j.Download($"http://keymon.lat:9002/ruteador?m=OSRM&r=${Subs.traeRutaReparto}&a=${Subs.traeAlmacen}&f=CEDIS,${Starter.cedisLocation.Longitude},${Starter.cedisLocation.Latitude}&c=${coordsStr}"$)
Log($"http://keymon.lat:9002/ruteador?m=OSRM&r=${Subs.traeRutaReparto}&a=${Subs.traeAlmacen}&f=CEDIS,${Starter.cedisLocation.Longitude},${Starter.cedisLocation.Latitude}&c=${coordsStr}"$)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim jp As JSONParser
jp.Initialize(j.GetString)
Dim m As Map = jp.NextObject
Log(m)
If m.Get("code") = "OK" Then
Log("===== OK")
rutaGenerada = True
Dim puntos As Int = m.Get("puntos")
Private coords As List = m.Get("coords")
Private duration As String = m.Get("duration")
Private distance As String = m.Get("distance")
Private mensaje As String = m.Get("mensaje")
Starter.skmt.ExecNonQuery("delete from waypoints")
Starter.skmt.ExecNonQuery("update kmt_info set SECUENCIA = 0")
For i = 0 To coords.Size - 1
Private ti As Map = coords.Get(i)
Starter.skmt.ExecNonQuery($"update kmt_info set SECUENCIA = ${ti.Get("pos")} where CAT_CL_CODIGO = '${ti.Get("id").As(String).SubString(1)}'"$) ' Ponemos la secuencia con el orden del ruteo para poder verla en el mapa.
Starter.skmt.ExecNonQuery($"insert into waypoints (codigo, indice) values ('${ti.Get("id").As(String).SubString(1)}', '${ti.Get("pos")}')"$)
Next
c = Starter.skmt.ExecQuery("select codigo, indice, CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_NOEXT from waypoints inner join kmt_info on waypoints.codigo = kmt_info.CAT_CL_CODIGO where gestion = 0 order by indice")
If c.RowCount > 0 Then 'Ya hay waypoints en la base de datos.
c.Position = 0
conMapa = True
ListView1.Clear
Dim cs, cs2 As CSBuilder
entro = 3
For i=0 To c.RowCount -1 'Generamos el listView con la lista ordenada.
c.Position = i
cs.Initialize
cs2.Initialize
ListView1.AddTwoLines(cs.Color(Colors.RGB(100,149,237)).Append(c.GetString("codigo")).PopAll, cs2.append(c.GetString("CAT_CL_NOMBRE")).Color(Colors.RGB(100,149,237)).Append(" Calle: ").Pop.Append(c.GetString("CAT_CL_CALLE").Trim & " " & c.GetString("CAT_CL_NOEXT")).PopAll )
Next
End If
c.Close
Log($"Distancia total: $1.1{distance/1000} kms, tiempo aprox: $1.1{duration/60} mins. ($1.1{duration/60/60} hrs)"$)
l_rutaInfo.Text = $"Distancia: $1.1{distance/1000} kms, tiempo aprox: $1.1{duration/60/60} hrs${CRLF}Visitas restantes: ${puntos - 1}"$
l_rutaInfo.Width = Root.Width * 0.9
Subs.centraEtiqueta(l_rutaInfo, Root.Width)
l_rutaInfo.Visible = True
l_rutaInfo.BringToFront
ListView1.Top = lv1Top + 100
End If
Else
LogColor("**************** Error! ******************", Colors.red)
End If
j.Release
ProgressDialogHide
End Sub
'Regresa lista de los clientes a visitar y sus coordenadas, iniciando con el cedis.
Sub todosAVisitar As List
Log(laRuta)
Log(entro)
Private sqlDeRuta As String = ""
If entro = 3 Then sqlDeRuta = $"and CAT_CL_RUTA = '${laRuta}'"$
Private aVisitar, tmpList As List
aVisitar.Initialize
Private coords As ResultSet = Starter.skmt.ExecQuery($"select CAT_CL_CODIGO, CAT_CL_LAT, CAT_CL_LONG from kmt_info where gestion = 0 ${sqlDeRuta}"$)
Do While coords.NextRow
tmpList.Initialize
tmpList.AddAll(Array As String(coords.GetString("CAT_CL_CODIGO"), coords.GetString("CAT_CL_LONG"), coords.GetString("CAT_CL_LAT")))
aVisitar.Add(tmpList)
Loop
Log(aVisitar)
Return aVisitar
End Sub