diff --git a/server.py b/server.py new file mode 100644 index 0000000..64dd4da --- /dev/null +++ b/server.py @@ -0,0 +1,47 @@ +import asyncio +import json +from websockets import serve + +clients = { + "FD3ZJFHQ7r" : { + "doorOpen" : False, + "lockdown" : False, + "lastOpened" : 1, #unix timestamp + "secLevel" : 1, + "openedBy" : "user" + } +} + +async def echo(websocket): + print("Connection established") + authenticated = False + await websocket.send(json.dumps({ + "type" : "SAuthReq" + })) + + async for msg in websocket: + msg_json = json.loads(msg) + if authenticated == False: + if msg_json["type"] == "CAuth": + if msg_json["token"] in clients: + await websocket.send(json.dumps({ + "type" : "SAuthStatus", + "failed" : False, + "reason" : "Authentication Successful" + })) + authenticated = True + else: + await websocket.send(json.dumps({ + "type" : "SAuthStatus", + "failed" : True, + "reason" : "Invalid token" + })) + + + + +async def main(): + async with serve(echo, "localhost", 8765): + await asyncio.get_running_loop().create_future() + +asyncio.run(main()) \ No newline at end of file