diff --git a/rpi/controllers/GameController.py b/rpi/controllers/GameController.py index cd3a2c72..f1bf6adb 100644 --- a/rpi/controllers/GameController.py +++ b/rpi/controllers/GameController.py @@ -2,19 +2,23 @@ import base64 import threading import requests -from flask import jsonify, request +from flask import jsonify, request, Flask from models.exceptions.ServiceException import ServiceException from services.game_service import GameService +from services.mqtt_service import MQTTService + class GameController: _game_service : GameService + _broker_service : MQTTService _has_started : bool _auth_token : str - def __init__(self, app): + def __init__(self, app : Flask, broker_service : MQTTService): self._game_service = GameService() + self._broker_service = broker_service self._register_routes(app) self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb" @@ -44,7 +48,12 @@ class GameController: return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500 def stop_game(self): - self._game_service.stop() + try : + game_data = self._game_service.export_game() + self._broker_service.publish("/customer/game/data", game_data, 2) + self._game_service.stop() + except Exception as ex: + print(ex) def make_move(self): try: diff --git a/rpi/main.py b/rpi/main.py index 1b3e65bc..57f02ba1 100644 --- a/rpi/main.py +++ b/rpi/main.py @@ -27,7 +27,24 @@ api_password = os.environ.get("API_PASSWORD") app = Flask(__name__) forwarder_service = None -game_controller = GameController(app) + +local_broker = MQTTService( + local_broker_address, + local_broker_port, + client_id="system", + username=local_username, + password=local_password, +) + +api_broker = MQTTService( + api_broker_address, + api_broker_port, + client_id=client_id, + username=api_username, + password=api_password, +) + +game_controller = GameController(app, api_broker) if __name__ == "__main__": @@ -36,22 +53,6 @@ if __name__ == "__main__": light_sensor_reader = LoraLightSensorReader("/dev/ttyUSB1", 9600) sound_reader = SoundReader(17) - local_broker = MQTTService( - local_broker_address, - local_broker_port, - client_id="system", - username=local_username, - password=local_password, - ) - - api_broker = MQTTService( - api_broker_address, - api_broker_port, - client_id=client_id, - username=api_username, - password=api_password, - ) - forwarder_service = ForwarderService(local_broker, api_broker) forwarder_service.register_forwarder(client_id, "rfid", "/system/sensor/rfid", f"/customer/telemetry/rfid", 2) diff --git a/rpi/models/game.py b/rpi/models/game.py new file mode 100644 index 00000000..874e177a --- /dev/null +++ b/rpi/models/game.py @@ -0,0 +1,22 @@ +class Game: + + _white_name : str + _black_name : str + _time_control : int + _increments : int + _moves : list[str] + _base_fen : str + + def __init__(self, white_name : str, black_name : str, time_control : int, increment : int): + self._white_name = white_name + self._black_name = black_name + self._time_control = time_control + self._increments = increment + self.moves = [] + self._base_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" + + def add_move(self, fen : str): + self.moves.append(fen) + + def get_moves(self): + return self.moves \ No newline at end of file diff --git a/rpi/services/game_service.py b/rpi/services/game_service.py index 5c1c3793..625129b0 100644 --- a/rpi/services/game_service.py +++ b/rpi/services/game_service.py @@ -1,12 +1,16 @@ +import json + from hardware.buzzer.buzzer import Buzzer from hardware.led.led import Led from models.exceptions.ServiceException import ServiceException +from models.game import Game from services.clock_service import ClockService from services.detection_service import DetectionService class GameService: + _game : Game _detection_service : DetectionService _clock_service : ClockService _has_started : bool @@ -19,18 +23,25 @@ class GameService: self._has_started = False self._led = Led(7) self._buzzer = Buzzer(8) + self.game = None def start(self, white_name, back_name, time_control : int, increment : int ) -> None: if self._has_started : raise ServiceException("Game has already started.") - self._clock_service.start(time_control, increment) - self._clock_service.set_on_terminated(self.stop) - self._has_started = True - self._led.on() + try : + self._game = Game(white_name, back_name, time_control, increment) + self._clock_service.start(time_control, increment) + self._clock_service.set_on_terminated(self.stop) + self._led.on() + self._has_started = True + except Exception as e: + print(e) + raise ServiceException(e) def stop(self): self._clock_service.stop() self._detection_service.stop() + self._led.off() self._buzzer.beep() self._has_started = False @@ -39,7 +50,12 @@ class GameService: if not self._has_started : raise Exception("Game hasn't started yet.") self._clock_service.switch() - return self._detection_service.analyze_single_frame() + img, fen = self._detection_service.analyze_single_frame() + self._game.add_move(fen) + return img, fen except Exception as e: print(e) raise ServiceException(e) + + def export_game(self): + return json.dumps(self._game)