Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions CartInformationDisplays/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 24 additions & 0 deletions CartInformationDisplays/tba.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down