Move inference onto the API
This commit is contained in:
@@ -12,12 +12,14 @@ from services.mqtt_service import MQTTService
|
||||
class GameController:
|
||||
|
||||
_game_service : GameService
|
||||
_api_url : str
|
||||
_broker_service : MQTTService
|
||||
_has_started : bool
|
||||
_auth_token : str
|
||||
|
||||
def __init__(self, app : Flask, broker_service : MQTTService):
|
||||
def __init__(self, app : Flask, api_url : str, broker_service : MQTTService):
|
||||
self._game_service = GameService()
|
||||
self._api_url = api_url
|
||||
self._game_service.set_on_terminated(self._stop_event)
|
||||
self._broker_service = broker_service
|
||||
self._register_routes(app)
|
||||
@@ -61,11 +63,17 @@ class GameController:
|
||||
if auth_token != "Bearer " + self._auth_token:
|
||||
return jsonify({"status": "error", "message": "Invalid authorization token"}), 401
|
||||
|
||||
threading.Thread(
|
||||
target=self._analyze_move(),
|
||||
daemon=True
|
||||
).start()
|
||||
img = self._game_service.make_move()
|
||||
b64_img = base64.b64encode(img).decode('utf-8')
|
||||
payload = {
|
||||
"image": f"data:image/jpeg;base64,{b64_img}"
|
||||
}
|
||||
response = requests.post(self._api_url, json=payload, verify=False)
|
||||
print(response.status_code)
|
||||
|
||||
data = response.json()
|
||||
fen = data.get("fen")
|
||||
self._game_service.add_move(fen)
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
except ServiceException as ex:
|
||||
@@ -74,22 +82,6 @@ class GameController:
|
||||
print(ex)
|
||||
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
|
||||
|
||||
def _analyze_move(self):
|
||||
img, fen = self._game_service.make_move()
|
||||
self._send_detection_result("https://192.168.15.125:1880/party/image", img, fen)
|
||||
|
||||
def _send_detection_result(self, url, img, fen):
|
||||
try:
|
||||
b64_img = base64.b64encode(img).decode('utf-8')
|
||||
payload = {
|
||||
"fen": fen,
|
||||
"image": f"data:image/jpeg;base64,{b64_img}"
|
||||
}
|
||||
response = requests.post(url, json=payload, verify=False)
|
||||
print(response.status_code)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def _stop_event(self, game_data : str):
|
||||
try :
|
||||
print(f"Exporting game data : {game_data}")
|
||||
|
||||
Reference in New Issue
Block a user