From 82789854786ce482a1b12e9d701363a5e5303058 Mon Sep 17 00:00:00 2001 From: tmjoumaa Date: Thu, 7 Aug 2025 17:25:20 -0400 Subject: [PATCH] Added support for queue notifications in backend. --- CartInformationDisplays/main.py | 26 ++++++++++++++++++++++---- CartInformationDisplays/tba.py | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CartInformationDisplays/main.py b/CartInformationDisplays/main.py index 6355a63..4da7a82 100644 --- a/CartInformationDisplays/main.py +++ b/CartInformationDisplays/main.py @@ -2,20 +2,38 @@ import uvicorn import threading from app import app -from tba import getMatches +from tba import getMatchQueueInfo, getMatches from communicationBus import communicationBus import time -def threadMasterMatchDataSender(): - matches = getMatches() +def threadMatchDataHandler(): + matches = getMatches(fresh=True) + queue_info = getMatchQueueInfo(matches) + last_match_queued = 0 while True: time.sleep(10) matches = getMatches() + # Match data was updated by TBA if matches != []: + queue_info = getMatchQueueInfo(matches) asyncio.run(communicationBus.sendMissionController({"type": "matchPackage", "data": matches})) + for q in queue_info: + if q['time'] is not None: + current_unix_time = int(time.time()) + if last_match_queued < q['matchId'] and q['time'] - current_unix_time <= 300 and q['time'] - current_unix_time > 0: + last_match_queued = q['matchId'] + data = { + "color" : q['color'], + "position" : q['position'], + "matchId": q['matchId'], + } + asyncio.run(communicationBus.sendL({"type": "matchQueuePackage", "data": data})) + asyncio.run(communicationBus.sendR({"type": "matchQueuePackage", "data": data})) + + if __name__ == "__main__": - masterMatchDataThread = threading.Thread(target=threadMasterMatchDataSender, daemon=True) + masterMatchDataThread = threading.Thread(target=threadMatchDataHandler, daemon=True) masterMatchDataThread.start() uvicorn.run(app, host="0.0.0.0", port=1701) \ No newline at end of file diff --git a/CartInformationDisplays/tba.py b/CartInformationDisplays/tba.py index d10ea4a..03eed57 100644 --- a/CartInformationDisplays/tba.py +++ b/CartInformationDisplays/tba.py @@ -2,12 +2,36 @@ from enum import Enum import requests import os +import time load_dotenv(override=True) apikey = os.getenv("TBA_API_KEY") previous_etag = "" +def getMatchQueueInfo(matches): + queue_info = [] + + for match in matches: + myTeam = "" + if "1701" not in match['alliances']['red']['team_keys'] and "1701" not in match['alliances']['blue']['team_keys']: + continue + #TODO: Just add a field returned by getMatches to avoid this loop (didn't wanna mess with Swift side) + for team in match['alliances']['red']['team_keys']: + team = team.strip("frc") + if team == "1701": + myTeam = "red" + if myTeam == "": + myTeam = "blue" + queue_info.append({ + "matchId": match['matchId'], + "time": match['time'], + "color" : myTeam, + "position" : str(match['alliances'][myTeam]['team_keys'].index("1701") + 1) + }) + + return queue_info + def getMatches(fresh=False): global previous_etag, apikey