-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouters.py
82 lines (63 loc) · 2.6 KB
/
routers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import asyncio
import os
import jwt
from dotenv import load_dotenv
from fastapi import APIRouter, Header, HTTPException, WebSocket
from loguru import logger
from microwave import CHANNEL_NAME, Microwave
load_dotenv()
SECRET_KEY = os.environ["SECRET_KEY"]
assert len(SECRET_KEY) > 10
router = APIRouter()
microwave = Microwave()
def validate_jwt(x_token: str = Header(None)):
if x_token is None:
raise HTTPException(status_code=403, detail="Unauthorized")
try:
jwt.decode(x_token, SECRET_KEY, algorithms=["HS256"])
except jwt.PyJWTError:
raise HTTPException(status_code=403, detail="Invalid token or expired")
async def listen_for_incoming_actions(websocket: WebSocket):
while True:
data = await websocket.receive_json()
logger.debug(f"Received action: {data}")
if data["action"] == "power+10":
await microwave.adjust_power(10)
elif data["action"] == "power-10":
await microwave.adjust_power(-10)
elif data["action"] == "counter+10":
await microwave.adjust_counter(10)
elif data["action"] == "counter-10":
await microwave.adjust_counter(-10)
elif data["action"] == "cancel":
if await microwave.get_state() == "ON":
try:
validate_jwt(data["jwt_token"])
except:
pass
else:
await microwave.cancel()
else:
logger.warning(f"Unknown action: {data['action']}")
async def listen_for_redis_changes(websocket: WebSocket):
from redis_connection import redis
pubsub = redis.pubsub()
await pubsub.subscribe(CHANNEL_NAME)
while True:
message = await pubsub.get_message(ignore_subscribe_messages=True)
if message is None:
continue
logger.info(f"Received message: {message}")
microwave_data = await microwave.get_microwave_data()
await websocket.send_text(
f"""<span id="power">{ microwave_data["power"] } </span>
<span id="counter">{ microwave_data["counter"] } </span>
<span id="status">{ microwave_data["state"] } </span>"""
) # TODO: make counter more human readable (e.g. 1:30 instead of 90)
logger.info(f"State broadcast: {microwave_data}")
@router.websocket("/websocket")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
listener_task = asyncio.create_task(listen_for_incoming_actions(websocket))
broadcaster_task = asyncio.create_task(listen_for_redis_changes(websocket))
await asyncio.gather(listener_task, broadcaster_task)