Move request handling

This commit is contained in:
2025-12-31 16:44:17 +01:00
parent 9d795c426e
commit 36526bfc86
2 changed files with 28 additions and 29 deletions

View File

@@ -1,3 +1,6 @@
import threading
import requests
from flask import jsonify, request
from services.game_service import GameService
@@ -45,9 +48,29 @@ class GameController:
if auth_token != "Bearer " + self.auth_token:
return jsonify({"status": "error", "message": "Invalid authorization token"}), 401
self._game_service.make_move()
img, fen = self._game_service.make_move()
threading.Thread(
target=self.__send_detection_result,
args=("https://192.168.15.125:1880/party/image", img, fen),
daemon=True
).start()
return jsonify({"status": "ok"}), 200
except Exception as ex:
print(ex)
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
def __send_detection_result(self, url, img, fen):
try:
headers = {'Content-Type': 'image/jpeg'}
body = {'frame': img, 'fen': fen}
response = requests.post(
url,
data=body,
headers=headers,
verify=False)
print(response.status_code)
except Exception as e:
print(e)

View File

@@ -1,8 +1,3 @@
import threading
import requests
import _thread
from services.clock_service import ClockService
from services.detection_service import DetectionService
@@ -24,28 +19,9 @@ class GameService:
self.clock_service.stop()
self.detection_service.stop()
def make_move(self) -> None:
def make_move(self) -> tuple[bytes, str] | None:
try :
frame, fen = self.detection_service.analyze_single_frame()
print(fen)
url = "http://192.168.15.125:1880/party/image"
"""threading.Thread(
target=self.__send_detection_result,
args=(url, frame, fen),
daemon=True
).start()"""
return self.detection_service.analyze_single_frame()
except Exception as e:
print(e)
def __send_detection_result(self, url, img, fen):
try:
headers = {'Content-Type': 'image/jpeg'}
body = {'frame': img, 'fen': fen}
response = requests.post(
url,
data=body,
headers=headers,
verify=False)
print(response.status_code)
except Exception as e:
print(e)
return None