commit dd9f68198c719f4e33ac7cb64c11bf00febc38bd Author: Xander Bil Date: Tue May 7 23:05:48 2024 +0200 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77e1422 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv +env +dumps diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..98948c3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.12-alpine3.19 + +WORKDIR /simulator + +COPY . . + +RUN pip install -r requirements.txt + +CMD ["python","-u","simulator.py"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..14774b4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +websockets diff --git a/simulator.py b/simulator.py new file mode 100755 index 0000000..c717d5b --- /dev/null +++ b/simulator.py @@ -0,0 +1,49 @@ +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())