diff options
author | Botond Hende <nettingman@gmail.com> | 2024-09-02 00:21:13 +0200 |
---|---|---|
committer | Botond Hende <nettingman@gmail.com> | 2024-09-02 00:21:13 +0200 |
commit | 1638f40eccc4a1321ee1bf19e3756157b6c965e1 (patch) | |
tree | 99209de68ebcd4f42cafcdf14e563d7916d69e00 /modules | |
parent | 3a390ff218903b8665a99f94d7f3a65357b6e96d (diff) |
refactored code, use get_ship dependency for ship related calls
Diffstat (limited to 'modules')
-rw-r--r-- | modules/daemon.py | 36 | ||||
-rw-r--r-- | modules/ships.py | 46 | ||||
-rw-r--r-- | modules/task_types.py | 21 |
3 files changed, 10 insertions, 93 deletions
diff --git a/modules/daemon.py b/modules/daemon.py deleted file mode 100644 index 085a7f6..0000000 --- a/modules/daemon.py +++ /dev/null @@ -1,36 +0,0 @@ -import openapi_client -from fastapi import FastAPI -from pprint import pprint - -fast_api = FastAPI() - -class Daemon: - def __init__(self, agent_symbol, token): - config = openapi_client.Configuration(access_token=token) - self.api_client = openapi_client.ApiClient(config) - - self.agents_api = openapi_client.AgentsApi(self.api_client) - self.fleet_api = openapi_client.FleetApi(self.api_client) - self.systems_api = openapi_client.SystemsApi(self.api_client) - - self.agent_symbol = agent_symbol - - @fast_api.get("/") - def foo(self): - return "hello" - - def run(self): - try: - api_response = self.agents_api.get_agent(self.agent_symbol) - print("The response of AgentsApi->get_agent:\n") - print(api_response) - - api_response = self.fleet_api.get_my_ships() - for elem in api_response.data: - print(elem.symbol, elem.nav.waypoint_symbol, elem.nav.status) - waypoint = self.systems_api.get_waypoint(elem.nav.system_symbol, elem.nav.waypoint_symbol).data - - print(waypoint.symbol, waypoint.type) - - except Exception as e: - print("Exception when calling AgentsApi->get_agent: %s\n" % e) diff --git a/modules/ships.py b/modules/ships.py index 05ce6c1..9b0d4bd 100644 --- a/modules/ships.py +++ b/modules/ships.py @@ -1,41 +1,15 @@ -from typing import Dict +from typing import Annotated -from . import task_types -from .database import cursor, conn +from fastapi import Depends, HTTPException +from ..apirouters import agents +from ..entities.agent import Agent +from ..entities.ship import Ship -class Ship: - def __init__(self, symbol: str): - self.symbol = symbol - self.callsign = "-".split(symbol)[0] - self.task = None - self.name = None +async def get_ship(ship_symbol: str, agent: Annotated[Agent, Depends(agents.auth_agent)]) -> Ship: + for ship in agent.ships.values(): + if ship.symbol == ship_symbol: + return ship - def get_data(self) -> Dict: - return {"name": self.name, "symbol": self.symbol, "task": self.task} - - def load_task(self): - cursor.execute("SELECT name, task, params FROM ships WHERE symbol = ?", (self.symbol,)) - row = cursor.fetchone() - if row is None: - cursor.execute("INSERT INTO ships (symbol, task, params, name) VALUES (?, ?, ?, ?)", - (self.symbol, task_types.IDLE, None, self.symbol)) - conn.commit() - - self.task = task_types.IDLE - self.task = self.symbol - else: - self.name = row[0] - self.task = row[1] - - def set_task(self, task): - self.task = task - if task != task_types.ERROR: - cursor.execute("UPDATE ships SET task = ? WHERE symbol = ?", (task, self.symbol)) - conn.commit() - - def rename(self, name): - self.name = name - cursor.execute("UPDATE ships SET name = ? WHERE symbol = ?", (name, self.symbol)) - conn.commit() + raise HTTPException(status_code=400, detail="Unknown ship symbol.") diff --git a/modules/task_types.py b/modules/task_types.py deleted file mode 100644 index d23ce55..0000000 --- a/modules/task_types.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import Annotated -from pydantic.functional_validators import AfterValidator - -IDLE = 'IDLE' -MINING = 'MINING' - -MIA = 'MIA' -ERROR = 'ERROR' - -task_types = [ - IDLE, - MINING, -] - - -def is_task_type(task: str): - assert task in task_types, f"'{task}' is not a valid task type." - return task - - -task_type = Annotated[str, AfterValidator(is_task_type)] |