Compare commits

..

No commits in common. "6fa72778130dcf7be570b291fb77cfe099178f84" and "a6b2992cc29b5bd5cd94c6885a28d56dd9f393ca" have entirely different histories.

View file

@ -1,7 +1,6 @@
"""
MS Teams controller using a Neokey 1x4 and a RP2040.
"""
import logging
import json
import time
import asyncio
@ -9,9 +8,7 @@ from typing import Any
import serial_asyncio
from argparse import ArgumentParser
from websockets.client import connect, WebSocketClientProtocol
from pydantic import BaseModel, ValidationError
log = logging.getLogger(__name__)
from pydantic import BaseModel
class Button(BaseModel):
@ -21,10 +18,10 @@ class Button(BaseModel):
class BUTTONS:
cam = Button(num=0, color_off=(0, 0, 30), color_on=(0, 0, 255))
mic = Button(num=1, color_off=(30, 0, 0), color_on=(255, 0, 0))
hand = Button(num=2, color_off=(30, 10, 0), color_on=(255, 192, 0))
call = Button(num=3, color_off=(0, 30, 0), color_on=(255, 64, 0))
cam = Button(num=0, color_off=(0, 0, 10), color_on=(0, 0, 255))
mic = Button(num=1, color_off=(10, 0, 0), color_on=(255, 0, 0))
hand = Button(num=2, color_off=(10, 10, 0), color_on=(255, 192, 0))
call = Button(num=3, color_off=(0, 10, 0), color_on=(255, 64, 0))
all = [cam, mic, hand, call]
@ -243,44 +240,37 @@ class TeamsCtrl:
async def process_ws(self) -> None:
async for raw_msg in self.ws:
try:
msg = TeamsMsg.parse_raw(raw_msg)
if self.meeting_state != 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 = 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")
msg = TeamsMsg.parse_raw(raw_msg)
if self.meeting_state != msg.meetingUpdate.meetingState:
print(
f"state changed:\n{self.meeting_state}\nto\n{msg.meetingUpdate.meetingState}"
)
changes: dict[str, bool] = {}
if self.meeting_state is None:
changes = msg.meetingUpdate.meetingState.dict()
else:
m = raw_msg
log.info(f"got unknown meetingState: {m}")
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)
async def process_statemachine(self) -> None:
while not self.loop.is_closed():
await self.current_state.process()
def gather_all(self) -> asyncio.Future[tuple[None, None, None]]:
def gather_all(self):
return asyncio.gather(
self.process_serial(), self.process_ws(), self.process_statemachine()
)
async def amain() -> None:
async def amain():
loop = asyncio.get_event_loop()
serial = await serial_asyncio.open_serial_connection(
loop=loop, url=args.port, baudrate=115200