swap to async #1

Merged
asaril merged 10 commits from async into main 2023-06-15 00:32:18 +02:00
Showing only changes of commit 6fa7277813 - Show all commits

View file

@ -1,6 +1,7 @@
""" """
MS Teams controller using a Neokey 1x4 and a RP2040. MS Teams controller using a Neokey 1x4 and a RP2040.
""" """
import logging
import json import json
import time import time
import asyncio import asyncio
@ -8,7 +9,9 @@ from typing import Any
import serial_asyncio import serial_asyncio
from argparse import ArgumentParser from argparse import ArgumentParser
from websockets.client import connect, WebSocketClientProtocol from websockets.client import connect, WebSocketClientProtocol
from pydantic import BaseModel from pydantic import BaseModel, ValidationError
log = logging.getLogger(__name__)
class Button(BaseModel): class Button(BaseModel):
@ -240,25 +243,32 @@ class TeamsCtrl:
async def process_ws(self) -> None: async def process_ws(self) -> None:
async for raw_msg in self.ws: async for raw_msg in self.ws:
msg = TeamsMsg.parse_raw(raw_msg) try:
if self.meeting_state != msg.meetingUpdate.meetingState: msg = TeamsMsg.parse_raw(raw_msg)
print( if self.meeting_state != msg.meetingUpdate.meetingState:
f"state changed:\n{self.meeting_state}\nto\n{msg.meetingUpdate.meetingState}" log.debug(
) f"state changed:\n{self.meeting_state}\nto\n{msg.meetingUpdate.meetingState}"
changes: dict[str, bool] = {} )
if self.meeting_state is None: changes: dict[str, bool] = {}
changes = msg.meetingUpdate.meetingState.dict() if self.meeting_state is None:
changes = msg.meetingUpdate.meetingState.dict()
else:
old_s = self.meeting_state.dict()
new_s = msg.meetingUpdate.meetingState.dict()
for state, old_v in old_s.items():
new_v = new_s[state]
if old_v != new_v:
changes[state] = new_v
next_state = self.current_state.handle_state_change(changes)
self.meeting_state = msg.meetingUpdate.meetingState
if next_state is not None:
self.set_next_state(next_state=next_state)
except ValidationError:
if isinstance(raw_msg, bytes):
m = raw_msg.decode("utf-8")
else: else:
old_s = self.meeting_state.dict() m = raw_msg
new_s = msg.meetingUpdate.meetingState.dict() log.info(f"got unknown meetingState: {m}")
for state, old_v in old_s.items():
new_v = new_s[state]
if old_v != new_v:
changes[state] = new_v
next_state = self.current_state.handle_state_change(changes)
self.meeting_state = msg.meetingUpdate.meetingState
if next_state is not None:
self.set_next_state(next_state=next_state)
async def process_statemachine(self) -> None: async def process_statemachine(self) -> None:
while not self.loop.is_closed(): while not self.loop.is_closed():