diff --git a/Mapa.bas b/Mapa.bas
index 63fdbe6..eb48142 100644
--- a/Mapa.bas
+++ b/Mapa.bas
@@ -89,6 +89,7 @@ Sub generaMatrizRuteoTiempos(r As String, resp As ServletResponse, ruta As Strin
Loop
Main.db.Close
StopMessageLoop
+ Main.error = ""
resp.ContentType = "text/html"
resp.Write($"Mapa"$)
@@ -112,8 +113,8 @@ Sub tiempos(r As String, resp As ServletResponse, ruta As String, almacen As Str
Log("Iniciamos ruteoCompleto")
ruteoCompleto(r)
-
StopMessageLoop
+ Main.error = ""
resp.ContentType = "text/html"
resp.Write($"Mapa"$)
Return 1
diff --git a/Ruteador-NonUI.b4j b/Ruteador-NonUI.b4j
index 92fe0c4..104c3b1 100644
--- a/Ruteador-NonUI.b4j
+++ b/Ruteador-NonUI.b4j
@@ -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
\ No newline at end of file
diff --git a/Ruteador-NonUI.b4j.meta b/Ruteador-NonUI.b4j.meta
index 7371530..d639cb0 100644
--- a/Ruteador-NonUI.b4j.meta
+++ b/Ruteador-NonUI.b4j.meta
@@ -10,12 +10,12 @@ ModuleBreakpoints2=
ModuleBreakpoints3=
ModuleBreakpoints4=
ModuleBreakpoints5=
-ModuleClosedNodes0=3,4,5,6,8,10,11,14,17,19,21,22,23,24,25
+ModuleClosedNodes0=3,4,5,6,8,11,14,17,19,21,24
ModuleClosedNodes1=
ModuleClosedNodes2=
ModuleClosedNodes3=
ModuleClosedNodes4=
ModuleClosedNodes5=
-NavigationStack=Main,ruteo,283,6,Mapa,Handle,54,0,Main,creaTablas,132,0,Main,generaMatrizLocal,204,1,Main,generaMatrizOSRM,214,1,Main,tiempos,410,3,Ruteador,generaMatrizRuteoTiempos,95,6,Ruteador,tiempos,136,1,Mapa,ruteoCompleto,166,3,rutaCompleta,ruteoCompleto,167,2
+NavigationStack=Main,generaMatrizOSRM,275,0,Main,tiempos,424,5,Mapa,generaMatrizRuteoTiempos,85,0,Mapa,tiempos,108,0,rutaCompleta,generaMatrizRuteoTiempos,85,0,rutaCompleta,tiempos,105,0,Main,calculateDistance3,607,0,Ruteador,tiempos,166,0,Ruteador,generaMatrizRuteoTiempos,103,6,Main,creaTablas,155,6,Main,ruteo,308,0
SelectedBuild=0
VisibleModules=5,3,4,1,2
diff --git a/Ruteador.bas b/Ruteador.bas
index b1e20f2..0df2e3b 100644
--- a/Ruteador.bas
+++ b/Ruteador.bas
@@ -88,31 +88,54 @@ Sub generaMatrizRuteoTiempos(r As String, resp As ServletResponse, ruta As Strin
Log(ts)
Private tempMap As Map
tempMap.Initialize
- Private p As ResultSet = Main.db.ExecQuery($"select * from ${r}_punteo"$)
- Private listCoords As List
- listCoords.Initialize
- 'Ponemos el id de la tienda y las coordenadas en una lista para regresarla en un JSON.
- Do While p.NextRow
- listCoords.Add(CreateMap("pos":p.GetString("pos"), "id":p.GetString("id"), "lat":p.GetString("lat"), "lon":p.GetString("lon")))
- Loop
- Main.db.Close
- tempMap.Put("api", matriz)
- If matriz = "" Then tempMap.Put("api", "Local")
- 'Ponemos la ruta, almacen, tiempos, distancias y la lista de las coordenadas en un mapa para regresarla en un JSON.
- tempMap.Put("code", "OK")
- tempMap.Put("ruta", ruta)
- tempMap.Put("almacen", almacen)
- tempMap.Put("duration", ts.Get("duration"))
- tempMap.Put("distance", ts.Get("distance"))
- tempMap.Put("puntos", ts.Get("puntos"))
- tempMap.Put("coords", listCoords)
- If tempMap.get("puntos") = 0 Then tempMap.Put("code", "KO")
-' Log(tempMap)
- js.Initialize(tempMap)
- StopMessageLoop
- 'Regresamos en un JSON la info del ruteo.
- resp.ContentType = "text/html"
- resp.Write(js.ToString)
+ If checkIfTableExists(r&"_punteo") Then
+ Private p As ResultSet = Main.db.ExecQuery($"select * from ${r}_punteo"$)
+ Private listCoords As List
+ listCoords.Initialize
+ 'Ponemos el id de la tienda y las coordenadas en una lista para regresarla en un JSON.
+ Do While p.NextRow
+ listCoords.Add(CreateMap("pos":p.GetString("pos"), "id":p.GetString("id"), "lat":p.GetString("lat"), "lon":p.GetString("lon")))
+ Loop
+ Main.db.Close
+ tempMap.Put("api", matriz)
+ If matriz = "" Then tempMap.Put("api", "Local")
+ 'Ponemos la ruta, almacen, tiempos, distancias y la lista de las coordenadas en un mapa para regresarla en un JSON.
+ tempMap.Put("code", "OK")
+ tempMap.Put("ruta", ruta)
+ tempMap.Put("almacen", almacen)
+ tempMap.Put("duration", ts.Get("duration"))
+ tempMap.Put("distance", ts.Get("distance"))
+ tempMap.Put("puntos", ts.Get("puntos"))
+ tempMap.Put("coords", listCoords)
+ tempMap.Put("mensaje", Main.msg)
+ If tempMap.get("puntos") = 0 Then tempMap.Put("code", "KO")
+' Log(tempMap)
+ js.Initialize(tempMap)
+ StopMessageLoop
+ Main.error = ""
+ Main.msg = ""
+ 'Regresamos en un JSON la info del ruteo.
+ resp.ContentType = "text/html"
+ resp.Write(js.ToString)
+ Else
+ tempMap.Put("api", "")
+ tempMap.Put("code", "KO")
+ tempMap.Put("error", Main.error)
+ tempMap.Put("ruta", ruta)
+ tempMap.Put("almacen", almacen)
+ tempMap.Put("duration", 0)
+ tempMap.Put("distance", 0)
+ tempMap.Put("puntos", 0)
+ tempMap.Put("coords", "")
+ tempMap.Put("mensaje", Main.msg)
+' Log(tempMap)
+ js.Initialize(tempMap)
+ StopMessageLoop
+ Main.error = ""
+ Main.msg = ""
+ resp.ContentType = "text/html"
+ resp.Write(js.ToString)
+ End If
Return 1
End Sub
@@ -147,10 +170,13 @@ Sub tiempos(r As String, resp As ServletResponse, ruta As String, almacen As Str
tempMap.Put("distance", ts.Get("distance"))
tempMap.Put("puntos", ts.Get("puntos"))
tempMap.Put("coords", listCoords)
+ tempMap.Put("mensaje", Main.msg)
If tempMap.get("puntos") = 0 Then tempMap.Put("code", "KO")
' Log(tempMap)
js.Initialize(tempMap)
StopMessageLoop
+ Main.error = ""
+ Main.msg = ""
resp.ContentType = "text/html"
resp.Write(js.ToString)
Log("###################################################################" & CRLF)
diff --git a/rutaCompleta.bas b/rutaCompleta.bas
index 746dc4d..075af22 100644
--- a/rutaCompleta.bas
+++ b/rutaCompleta.bas
@@ -90,8 +90,8 @@ Sub generaMatrizRuteoTiempos(r As String, resp As ServletResponse, ruta As Strin
Loop
Main.db.Close
StopMessageLoop
+ Main.error = ""
resp.ContentType = "text/html"
-
resp.Write($"Mapa"$)
Return 1
End Sub
@@ -110,11 +110,10 @@ Sub tiempos(r As String, resp As ServletResponse, ruta As String, almacen As Str
End If
Loop
Main.db.Close
-
' Log("Iniciamos ruteoCompleto")
' ruteoCompleto(r)
-
StopMessageLoop
+ Main.error = ""
resp.ContentType = "text/html"
resp.Write($"Mapa"$)
Return 1