- Se agregaron mas TRY/CATCH y se agregó el parametro "f", para especifiar un punto que debe ser el punto final (destino)

This commit is contained in:
2024-04-25 01:50:43 -06:00
parent 156e3b1d94
commit c6a5220797
5 changed files with 195 additions and 137 deletions

View File

@@ -50,6 +50,8 @@ Sub Process_Globals
Dim inicio As String = "", final As String = ""
' Dim coords As List
' Dim estePunto() As String
Dim error As String = ""
dim msg as string = ""
End Sub
Sub AppStart (Args() As String)
@@ -153,38 +155,47 @@ Sub creaTablas(params As Map)
Private estasCoords As String = params.Get("coords")
Private hash As String = params.Get("hash")
Private ruta As String = $"${params.Get("ruta")}A${almacen}_${hash}"$
db.BeginTransaction
Try
' Log($"drop table if exists ${ruta}_matriz"$)
db.ExecNonQuery($"drop table if exists ${ruta}_matriz"$)
' Log($"drop table if exists ${ruta}_matriz"$
db.ExecNonQuery($"drop table if exists ${ruta}_matriz"$)
' Log($"drop table if exists ${ruta}_matrizOSRM"$)
db.ExecNonQuery($"drop table if exists ${ruta}_matrizOSRM"$)
db.ExecNonQuery($"drop table if exists ${ruta}_matrizOSRM"$)
' Log($"drop table if exists ${ruta}_puntos"$)
db.ExecNonQuery($"drop table if exists ${ruta}_puntos"$)
Catch
Log(LastException)
End Try
db.ExecNonQuery($"create table if not exists ${ruta}_puntos (id TEXT, nombre TEXT, lat TEXT, lon TEXT)"$)
db.ExecNonQuery($"drop table if exists ${ruta}_puntos"$)
db.ExecNonQuery($"create table if not exists ${ruta}_puntos (id TEXT, nombre TEXT, lat TEXT, lon TEXT)"$)
' Log($"create table if not exists ${ruta}_puntos (id TEXT, nombre TEXT, lat TEXT, lon TEXT)"$)
db.ExecNonQuery($"create table if not exists ${ruta}_punteo (pos TEXT, id TEXT, nombre TEXT, lat TEXT, lon TEXT)"$)
db.ExecNonQuery($"create table if not exists ${ruta}_punteo (pos TEXT, id TEXT, nombre TEXT, lat TEXT, lon TEXT)"$)
' Log($"create table if not exists ${ruta}_punteo (pos TEXT, id TEXT, nombre TEXT, lat TEXT, lon TEXT)"$)
Private f() As String = Regex.Split(";", estasCoords)
For i = 0 To f.Length - 1
Private f() As String = Regex.Split(";", estasCoords)
For i = 0 To f.Length - 1
' Log(f(i))
Private pars() As String = Regex.Split(",", f(i))
If pars.Length < 3 Then Log("####################" & CRLF & "Se necesita el id del cliente, la longitud y la latitud" & CRLF & "##############################")
' Log($"(${i}, ${pars(0)}, 'a', ${pars(2)}, ${pars(1)})"$)
db.ExecNonQuery($"insert into ${ruta}_puntos (id, nombre, lat, lon) values ('CC${pars(0)}','a', ${pars(2)}, ${pars(1)})"$)
Next
Private pars() As String = Regex.Split(",", f(i))
If pars(2) > 0 Or pars(2) > 0 Then
If pars.Length < 3 Then Log("####################" & CRLF & "Se necesita el id del cliente, la longitud y la latitud" & CRLF & "##############################")
' Log($"(${i}, ${pars(0)}, 'a', ${pars(2)}, ${pars(1)})"$)
db.ExecNonQuery($"insert into ${ruta}_puntos (id, nombre, lat, lon) values ('_${pars(0)}','a', ${pars(2)}, ${pars(1)})"$)
Else
msg = "Hay puntos con coordenadas en CERO, se eliminaron."
End If
Next
Private colsMatriz As String = "idT TEXT"
Private pp As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos"$)
Do While pp.NextRow
colsMatriz = $"${colsMatriz},'${pp.GetString("id")}' REAL"$
Loop
Private colsMatriz As String = "idT TEXT"
Private pp As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos"$)
Do While pp.NextRow
colsMatriz = $"${colsMatriz},'${pp.GetString("id")}' REAL"$
Loop
' Log(colsMatriz)
db.ExecNonQuery($"create table if not exists ${ruta}_matriz (${colsMatriz})"$)
db.ExecNonQuery($"create table if not exists ${ruta}_matrizOSRM (${colsMatriz})"$)
db.ExecNonQuery($"create table if not exists ${ruta}_matriz (${colsMatriz})"$)
db.ExecNonQuery($"create table if not exists ${ruta}_matrizOSRM (${colsMatriz})"$)
db.TransactionSuccessful
Catch
Log(LastException)
If error = "" Then error = LastException
db.Rollback
End Try
End Sub
Private Sub b_generaMatriz_Click
@@ -231,6 +242,7 @@ Sub generaMatrizLocal(ruta As String) 'ignore
Log("Matriz LOCAL generada")
Catch
Log(LastException)
If error = "" Then error = LastException
End Try
' generaMatrizOSRM
End Sub
@@ -289,6 +301,7 @@ Sub generaMatrizOSRM(ruta As String) As ResumableSub 'ignore
Log("Matriz OSRM generada")
Catch
Log(LastException)
If error = "" Then error = LastException
End Try
End Sub
@@ -301,44 +314,46 @@ Sub ruteo(ruta As String, matriz As String) 'ignore
Log("#####################################################")
Log("############# Main/ruteo ####################")
Log("#####################################################")
db.ExecNonQuery($"delete from ${ruta}_punteo"$)
punteoLista.Initialize
Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos limit 1"$)
Do While c.NextRow
db.ExecNonQuery($"insert into ${ruta}_punteo (pos, id, nombre, lat, lon) values ('0', '${c.GetString("id")}', 'Almacen', ${c.Getdouble("lat")}, ${c.Getdouble("lon")})"$)
punteoLista.Add(c.GetString("id")) 'Lista para ruteoNearestInsertion, ponemos el primer punto de la lista de coordenadas.
Loop
' Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos where id = 'CCINCIO' limit 1"$)
' Do While c.NextRow
' db.ExecNonQuery($"insert into ${ruta}_punteo (pos, id, nombre, lat, lon) values ('0', '${c.GetString("id")}', 'Almacen', ${c.Getdouble("lat")}, ${c.Getdouble("lon")})"$)
' Loop
' c.Close
' punteoLista.Add("CCINICIO")
If final <> "" Then ' Si en los parametros se especifica un punto de destino (punto final), se agrega aqui a la lista, se agrega como segundo punto, porque todos los demas puntos de la lista se van a agregar ENTRE estos dos puntos iniciales.
Log("Agregamos punto final.")
Private pf() As String = Regex.Split(",", final) ' Obtenemos id, lon y lat.
Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos where id = 'CC${pf(0)}' limit 1"$)
If checkIfTableExists(ruta&"_puntos") Then
db.ExecNonQuery($"delete from ${ruta}_punteo"$)
punteoLista.Initialize
Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos limit 1"$)
Do While c.NextRow
db.ExecNonQuery($"insert into ${ruta}_punteo (pos, id, nombre, lat, lon) values ('0', '${c.GetString("id")}', 'Almacen', ${c.Getdouble("lat")}, ${c.Getdouble("lon")})"$)
punteoLista.Add(c.GetString("id")) 'Lista para ruteoNearestInsertion, ponemos el primer punto de la lista de coordenadas.
Loop
punteoLista.Add("CC" & pf(0))
c.Close
End If
Private cualAlgoritmo As String = "" 'ignore
Log($"Usamos matriz |${matriz}|"$)
If algoritmo = 1 Then
ruteoNearestInsertion(ruta, matriz)
cualAlgoritmo = " (NI)"
Else If algoritmo = 0 Then
Log(3)
ruteoNearestNeighbor(ruta, matriz)
cualAlgoritmo = " (NN)"
Else
ruteoNearestInsertion2
cualAlgoritmo = " (NI2)"
' Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos where id = 'CCINCIO' limit 1"$)
' Do While c.NextRow
' db.ExecNonQuery($"insert into ${ruta}_punteo (pos, id, nombre, lat, lon) values ('0', '${c.GetString("id")}', 'Almacen', ${c.Getdouble("lat")}, ${c.Getdouble("lon")})"$)
' Loop
' c.Close
' punteoLista.Add("CCINICIO")
If final <> "" Then ' Si en los parametros se especifica un punto de destino (punto final), se agrega aqui a la lista, se agrega como segundo punto, porque todos los demas puntos de la lista se van a agregar ENTRE estos dos puntos iniciales.
Log("Agregamos punto final.")
Private pf() As String = Regex.Split(",", final) ' Obtenemos id, lon y lat.
Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_puntos where id = '_${pf(0)}' limit 1"$)
Do While c.NextRow
db.ExecNonQuery($"insert into ${ruta}_punteo (pos, id, nombre, lat, lon) values ('0', '${c.GetString("id")}', 'Almacen', ${c.Getdouble("lat")}, ${c.Getdouble("lon")})"$)
Loop
punteoLista.Add("_" & pf(0))
c.Close
End If
Private cualAlgoritmo As String = "" 'ignore
Log($"Usamos matriz |${matriz}|"$)
If algoritmo = 1 Then
ruteoNearestInsertion(ruta, matriz)
cualAlgoritmo = " (NI)"
Else If algoritmo = 0 Then
Log(3)
ruteoNearestNeighbor(ruta, matriz)
cualAlgoritmo = " (NN)"
Else
ruteoNearestInsertion2
cualAlgoritmo = " (NI2)"
End If
End If
End Sub
@@ -385,57 +400,59 @@ Sub tiempos(ruta As String) As ResumableSub 'ignore
Log("############# Main/tiempos ####################")
Log("#######################################################")
' Log("Tiempos: " & ruta)
Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_punteo"$)
Private estasCoords As String = ""
Do While c.NextRow
If estasCoords = "" Then
estasCoords = $"${c.GetString("lon")},${c.GetString("lat")}"$
Else
estasCoords = $"${estasCoords};${c.GetString("lon")},${c.GetString("lat")}"$
If checkIfTableExists(ruta&"_punteo") Then
Private c As ResultSet = db.ExecQuery($"select * from ${ruta}_punteo"$)
Private estasCoords As String = ""
Do While c.NextRow
If estasCoords = "" Then
estasCoords = $"${c.GetString("lon")},${c.GetString("lat")}"$
Else
estasCoords = $"${estasCoords};${c.GetString("lon")},${c.GetString("lat")}"$
End If
Loop
Private d() As String
d = Regex.Split(";", estasCoords)
' Log("$$$$$$ " & d.Length)
Log($"http://router.project-osrm.org/route/v1/driving/${estasCoords}"$)
Dim j As HttpJob
j.Initialize("", Me)
j.Download($"http://router.project-osrm.org/route/v1/driving/${estasCoords}"$)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("RESPONSE:")
Log(j.GetString)
Private j0 As String = j.GetString
End If
Loop
' fx.ShowExternalDocument($"http://router.project-osrm.org/route/v1/driving/${estasCoords}"$)
Private d() As String
d = Regex.Split(";", estasCoords)
' Log("$$$$$$ " & d.Length)
Log($"http://router.project-osrm.org/route/v1/driving/${estasCoords}"$)
Dim j As HttpJob
j.Initialize("", Me)
j.Download($"http://router.project-osrm.org/route/v1/driving/${estasCoords}"$)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("RESPONSE:")
Log(j.GetString)
Private j0 As String = j.GetString
j.Release
' StopMessageLoop
Private js As JSONParser
js.Initialize(j0)
Log(j0)
' Log("*****************************************")
' Log(js)
'' For Each colroot As Map In js
'' Log(colroot)
'' Next
' Log("*****************************************")
ts.Put(ruta, CreateMap("code":"KO", "duration":0, "distance":0, "puntos":0))
Try
Private m As Map = js.NextObject
Log(m)
Private estatus As String = m.Get("code")
Private rutas As Map = m.Get("routes").as(List).Get(0)
Private waypoints As List = m.Get("waypoints")
Log("Response: " & estatus)
' Log("Duration: " & rutas.Get("duration"))
' Log("Distance: " & rutas.Get("distance"))
' Log("Legs: " & rutas.Get("legs").As(List).Size)
' Log("Waypoints: " & waypoints.Size)
ts.Put(ruta, CreateMap("code":"OK", "duration":rutas.Get("duration"), "distance":rutas.Get("distance"), "puntos":rutas.Get("legs").As(List).Size))
' Log(">>>>>>>>>>>>>>>>>>>>>>>>>>>" & ts)
Catch
Log(LastException)
If error = "" Then error = LastException
End Try
End If
j.Release
' StopMessageLoop
Private js As JSONParser
js.Initialize(j0)
' Log(j0)
' Log("*****************************************")
' Log(js)
'' For Each colroot As Map In js
'' Log(colroot)
'' Next
' Log("*****************************************")
ts.Put(ruta, CreateMap("code":"KO", "duration":0, "distance":0, "puntos":0))
Try
Private m As Map = js.NextObject
Log(m)
Private estatus As String = m.Get("code")
Private rutas As Map = m.Get("routes").as(List).Get(0)
Private waypoints As List = m.Get("waypoints")
Log("Response: " & estatus)
' Log("Duration: " & rutas.Get("duration"))
' Log("Distance: " & rutas.Get("distance"))
' Log("Legs: " & rutas.Get("legs").As(List).Size)
' Log("Waypoints: " & waypoints.Size)
ts.Put(ruta, CreateMap("code":"OK", "duration":rutas.Get("duration"), "distance":rutas.Get("distance"), "puntos":rutas.Get("legs").As(List).Size))
' Log(">>>>>>>>>>>>>>>>>>>>>>>>>>>" & ts)
Catch
Log(LastException)
End Try
Return 1
End Sub
@@ -616,6 +633,7 @@ Public Sub calculateDistance3(lat1 As Double, lon1 As Double, lat2 As Double, lo
Return Round(Yards)
Catch
Log("CalcDistance " & LastException)
if error = "" then error = LastException
Return -1
End Try
End Sub
@@ -640,4 +658,18 @@ End Sub
Private Sub cb_api_SelectedIndexChanged(Index As Int, Value As Object)
' api = Index
' If Index = 0 Then matriz = "" Else matriz = "OSRM"
End Sub
'Revisamos que la tabla exista.
Sub checkIfTableExists(table As String) As Boolean
' B4XPages.MainPage.db.InitializeSQLite(File.DirApp, "kmt.db", True)
Private r As ResultSet = db.ExecQuery($"Select name FROM sqlite_master WHERE Type='table' AND name='${table}'"$)
If r.NextRow Then
' B4XPages.MainPage.db.close
' Log($"NAME: ${r.GetString("name")}"$)
Return True
Else
' B4XPages.MainPage.db.close
Return False
End If
End Sub