simulator/simulator.py
2024-05-07 23:05:48 +02:00

49 lines
1.5 KiB
Python
Executable file

import json
import asyncio
from websockets.server import serve
import os
import time
teams = {"5a:45:55:53:00:01","5a:45:55:53:00:02","5a:45:55:53:00:03","5a:45:55:53:00:04","5a:45:55:53:00:06","5a:45:55:53:00:07","5a:45:55:53:00:09","5a:45:55:53:00:0a","5a:45:55:53:00:0b","5a:45:55:53:00:0c","5a:45:55:53:00:0d","5a:45:55:53:00:0f","5a:45:55:53:00:10","5a:45:55:53:00:11","5a:45:55:53:00:12","5a:45:55:53:00:14","5a:45:55:53:00:15","5a:45:55:53:00:16","5a:45:55:53:00:18"}
start = int(os.getenv('START',1682499937))
clock = start
step = 0.01
async def run_clock():
global clock
while True:
clock += step
await asyncio.sleep(step)
async def ws(ws, path):
global clock
id = json.loads(await ws.recv())["lastId"]
with open(f"dumps{path}.json") as f:
detections = json.loads(f.read())['detections']
for i in range(len(detections) - 1):
if detections[i]["id"] >= id and detections[i]["mac"] in teams and detections[i]["detection_timestamp"] > start:
timestamp = detections[i]["detection_timestamp"]
while timestamp > clock: await asyncio.sleep(timestamp - clock)
await ws.send(json.dumps([detections[i]]))
async def run_serve():
async with serve(ws, "0.0.0.0", 8080):
await asyncio.Future() # run forever
async def main():
_ = asyncio.create_task(run_clock())
_ = asyncio.create_task(run_serve())
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())