rtmc-be/server.py
2024-09-08 23:54:52 +00:00

47 lines
1.2 KiB
Python

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())