Added Favorites to geocache.db (needs rework)
and more
This commit is contained in:
@@ -642,10 +642,8 @@ class TunneldRunnerSio:
|
||||
"command": "start",
|
||||
"message": "Simulation started",
|
||||
"data": {
|
||||
|
||||
"simulation_active": self.context.simulation_active,
|
||||
"simulation_queue_state": self.context.simulation_queue_state
|
||||
}
|
||||
"simulation_queue": get_simulation_status(),
|
||||
},
|
||||
}
|
||||
else:
|
||||
data = {
|
||||
@@ -654,9 +652,8 @@ class TunneldRunnerSio:
|
||||
"command": "start",
|
||||
"message": "Simulation already running",
|
||||
"data": {
|
||||
"simulation_active": self.context.simulation_active,
|
||||
"simulation_queue_state": self.context.simulation_queue_state
|
||||
}
|
||||
"simulation_queue": get_simulation_status(),
|
||||
},
|
||||
}
|
||||
return data
|
||||
|
||||
@@ -763,6 +760,7 @@ class TunneldRunnerSio:
|
||||
{
|
||||
"type": "simulation_crash",
|
||||
"udid": self.context.udid,
|
||||
"udid": self.context.udid,
|
||||
"error": traceback.format_exc(),
|
||||
},
|
||||
namespace="/",
|
||||
@@ -885,7 +883,6 @@ class TunneldRunnerSio:
|
||||
"command": "delete",
|
||||
"message": f"Location {loc_id} deleted from the queue",
|
||||
"data": {
|
||||
"simulation_queue": get_simulation_status(),
|
||||
"simulation_queue": get_simulation_status(),
|
||||
"location_item": self.context.simulation_queue_data[loc_id],
|
||||
},
|
||||
@@ -1179,12 +1176,69 @@ class TunneldRunnerSio:
|
||||
else getattr(data, "longitude", 999)
|
||||
)
|
||||
if latitude != 999 and longitude != 999:
|
||||
coords = f"{latitude}, {longitude}"
|
||||
rev_geocode = await self.context.reverse_geocode.get_address(latitude, longitude)
|
||||
return rev_geocode
|
||||
else:
|
||||
return None
|
||||
|
||||
async def get_favorites():
|
||||
favorites = await self.context.reverse_geocode.get_favorites()
|
||||
return favorites
|
||||
|
||||
async def clear_favorite(data):
|
||||
latitude = float(
|
||||
data.get("latitude", 999)
|
||||
if isinstance(data, dict)
|
||||
else getattr(data, "latitude", 999)
|
||||
)
|
||||
longitude = float(
|
||||
data.get("longitude", 999)
|
||||
if isinstance(data, dict)
|
||||
else getattr(data, "longitude", 999)
|
||||
)
|
||||
if latitude != 999 and longitude != 999:
|
||||
clear_status = await self.context.reverse_geocode.clear_favorite(latitude, longitude)
|
||||
if clear_status:
|
||||
return "OK", "Favorite cleared"
|
||||
else:
|
||||
return "ERROR", "Failed to clear favorite"
|
||||
else:
|
||||
return "ERROR", "Invalid latitude and longitude"
|
||||
|
||||
async def set_favorite(data):
|
||||
favorite = (
|
||||
data.get("favorite")
|
||||
if isinstance(data, dict)
|
||||
else getattr(data, "favorite", None)
|
||||
)
|
||||
if favorite is not None:
|
||||
name = (
|
||||
favorite.get("name")
|
||||
if isinstance(favorite, dict)
|
||||
else getattr(favorite, "name", None)
|
||||
)
|
||||
logger.info("Adding favorite: %s", name)
|
||||
latitude = float(
|
||||
favorite.get("latitude", 999)
|
||||
if isinstance(favorite, dict)
|
||||
else getattr(favorite, "latitude", 999)
|
||||
)
|
||||
longitude = float(
|
||||
favorite.get("longitude", 999)
|
||||
if isinstance(favorite, dict)
|
||||
else getattr(favorite, "longitude", 999)
|
||||
)
|
||||
if latitude != 999 and longitude != 999 and favorite:
|
||||
set_status = await self.context.reverse_geocode.set_favorite(latitude, longitude, favorite)
|
||||
if set_status:
|
||||
return "OK", "Favorite added"
|
||||
else:
|
||||
return "ERROR", "Failed to add favorite"
|
||||
else:
|
||||
return "ERROR", "Invalid latitude and longitude"
|
||||
else:
|
||||
return "ERROR", "Invalid favorite data"
|
||||
|
||||
""" FastAPI HTTP Functions"""
|
||||
|
||||
def generate_http_response(
|
||||
@@ -1738,6 +1792,20 @@ class TunneldRunnerSio:
|
||||
}
|
||||
return resp
|
||||
|
||||
case "reset":
|
||||
""" Reset the simulation queue"""
|
||||
reset_queue()
|
||||
resp = {
|
||||
"command": command,
|
||||
"command_class": "simulation_control",
|
||||
"command_status": "OK",
|
||||
"message": "Simulation queue reset",
|
||||
"data": {
|
||||
"simulation_queue": get_simulation_status(),
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case "pause":
|
||||
""" Pause the simulation queue"""
|
||||
await pause_simulation_queue()
|
||||
@@ -1751,6 +1819,7 @@ class TunneldRunnerSio:
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case "resume":
|
||||
""" Resume the simulation queue"""
|
||||
await resume_simulation_queue()
|
||||
@@ -1764,6 +1833,7 @@ class TunneldRunnerSio:
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case "end":
|
||||
""" End the simulation queue"""
|
||||
logger.info(
|
||||
@@ -1779,12 +1849,14 @@ class TunneldRunnerSio:
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case "start":
|
||||
""" Start the simulation queue"""
|
||||
logger.info(
|
||||
"Start location simulation request from %s", sid)
|
||||
data = await start_simulation_queue()
|
||||
return data
|
||||
|
||||
case "gps-noise":
|
||||
""" Toggle GPS noise"""
|
||||
before_toggle = self.context.simulation_noise
|
||||
@@ -1952,6 +2024,27 @@ class TunneldRunnerSio:
|
||||
"command_status": "ERROR",
|
||||
"message": f"Error starting tunneld: {e}",
|
||||
}
|
||||
case "restart":
|
||||
"""Restart Tunneld"""
|
||||
logger.info("Restart tunneld request from %s: %s", sid, data)
|
||||
try:
|
||||
self._tunneld_core.clear()
|
||||
await asyncio.sleep(2)
|
||||
self._tunneld_core.start()
|
||||
return {
|
||||
"command_status": "OK",
|
||||
"command_class": "tunneld_control",
|
||||
"command": command,
|
||||
"message": "Tunneld started successfully",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("Error restarting tunneld: %s", e)
|
||||
return {
|
||||
"command_status": "ERROR",
|
||||
"command_class": "tunneld_control",
|
||||
"command": command,
|
||||
"message": f"Error starting tunneld: {e}",
|
||||
}
|
||||
|
||||
case "start-watcher":
|
||||
""" Start Tunneld Watcher """
|
||||
@@ -2060,6 +2153,76 @@ class TunneldRunnerSio:
|
||||
rev_geocode = await get_reverse_geocode(data)
|
||||
return rev_geocode
|
||||
|
||||
@self.context.sio.event
|
||||
async def favorite_control(sid, data: dict):
|
||||
command = (
|
||||
data.get("command")
|
||||
if isinstance(data, dict)
|
||||
else getattr(data, "command", None)
|
||||
)
|
||||
logger.info(
|
||||
"Favorite Control command: %s requested from %s", command, sid
|
||||
)
|
||||
match command:
|
||||
case "set":
|
||||
""" Add a favorite location to database"""
|
||||
cstat, cmessage = await set_favorite(data)
|
||||
if cstat == "ERROR":
|
||||
logger.error('Favorite set failed: %s', cmessage)
|
||||
resp = {
|
||||
"command": command,
|
||||
"command_class": "favorite_control",
|
||||
"command_status": cstat,
|
||||
"message": cmessage,
|
||||
"data": {
|
||||
"favorites": await get_favorites(),
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case "clear":
|
||||
""" Clear a favorite location from database"""
|
||||
cstat, cmessage = await clear_favorite(data)
|
||||
resp = {
|
||||
"command": command,
|
||||
"command_class": "favorite_control",
|
||||
"command_status": cstat,
|
||||
"message": cmessage,
|
||||
"data": {
|
||||
"favorites": await get_favorites(),
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case "get":
|
||||
""" Get favorite locations from database"""
|
||||
favs = await get_favorites()
|
||||
if favs:
|
||||
cstat = "OK"
|
||||
cmessage = "Favorites retrieved successfully"
|
||||
else:
|
||||
cstat = "ERROR"
|
||||
cmessage = "No favorites found"
|
||||
resp = {
|
||||
"command": command,
|
||||
"command_class": "favorite_control",
|
||||
"command_status": cstat,
|
||||
"message": cmessage,
|
||||
"data": {
|
||||
"favorites": favs,
|
||||
}
|
||||
}
|
||||
return resp
|
||||
|
||||
case _:
|
||||
return {
|
||||
"command": command,
|
||||
"command_class": "favorite_control",
|
||||
"command_status": "ERROR",
|
||||
"message": f"Invalid command: {command}",
|
||||
}
|
||||
|
||||
|
||||
|
||||
self._vue_app.include_router(self._app, prefix="/api")
|
||||
self._vue_app.mount(
|
||||
|
||||
Reference in New Issue
Block a user