diff options
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/apps/kvmd/server.py | 18 | ||||
-rw-r--r-- | kvmd/apps/kvmd/ugpio.py | 60 |
2 files changed, 52 insertions, 26 deletions
diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index 4003bf2b..1b3e2363 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -250,12 +250,13 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins await client.ws.prepare(request) await self.__register_ws_client(client) try: - await self.__broadcast_event("gpio_model_state", await self.__user_gpio.get_model()) + await self.__send_event(client.ws, "gpio_model_state", await self.__user_gpio.get_model()) await asyncio.gather(*[ - self.__broadcast_event(component.event_type, await component.get_state()) + self.__send_event(client.ws, component.event_type, await component.get_state()) for component in self.__components if component.get_state ]) + await self.__send_event(client.ws, "loop", {}) async for msg in client.ws: if msg.type == aiohttp.web.WSMsgType.TEXT: try: @@ -278,7 +279,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins @exposed_ws("ping") async def __ws_ping_handler(self, ws: aiohttp.web.WebSocketResponse, _: Dict) -> None: - await ws.send_str(json.dumps({"event_type": "pong", "event": {}})) + await self.__send_event(ws, "pong", {}) # ===== SYSTEM STUFF @@ -363,13 +364,16 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins except Exception: logger.exception("Cleanup error on %s", component.name) + async def __send_event(self, ws: aiohttp.web.WebSocketResponse, event_type: str, event: Optional[Dict]) -> None: + await ws.send_str(json.dumps({ + "event_type": event_type, + "event": event, + })) + async def __broadcast_event(self, event_type: str, event: Optional[Dict]) -> None: if self.__ws_clients: await asyncio.gather(*[ - client.ws.send_str(json.dumps({ - "event_type": event_type, - "event": event, - })) + self.__send_event(client.ws, event_type, event) for client in list(self.__ws_clients) if ( not client.ws.closed diff --git a/kvmd/apps/kvmd/ugpio.py b/kvmd/apps/kvmd/ugpio.py index cb328115..d4e9d0af 100644 --- a/kvmd/apps/kvmd/ugpio.py +++ b/kvmd/apps/kvmd/ugpio.py @@ -322,29 +322,51 @@ class UserGpio: items: List[Dict] = [] for item in map(str.strip, row): if item.startswith("#") or len(item) == 0: - items.append({ - "type": "label", - "text": item[1:].strip(), - }) + items.append(self.__make_view_label(item)) else: - parts = list(map(str.strip, item.split("|", 1))) + parts = list(map(str.strip, item.split("|", 2))) if parts: - channel: str = parts[0] - param: Optional[str] = (parts[1] if len(parts) > 1 else None) - if channel in self.__inputs: - items.append({ - "type": UserGpioModes.INPUT, - "channel": channel, - "color": (param if param in ["green", "yellow", "red"] else "green"), - }) - elif channel in self.__outputs: - items.append({ - "type": UserGpioModes.OUTPUT, - "channel": parts[0], - "text": (param if param is not None else "Click"), - }) + if parts[0] in self.__inputs: + items.append(self.__make_view_input(parts)) + elif parts[0] in self.__outputs: + items.append(self.__make_view_output(parts)) table.append(items) + return { "header": self.__view["header"], "table": table, } + + def __make_view_label(self, item: str) -> Dict: + assert item.startswith("#") + return { + "type": "label", + "text": item[1:].strip(), + } + + def __make_view_input(self, parts: List[str]) -> Dict: + assert len(parts) >= 1 + color = (parts[1] if len(parts) > 1 else None) + if color not in ["green", "yellow", "red"]: + color = "green" + return { + "type": UserGpioModes.INPUT, + "channel": parts[0], + "color": color, + } + + def __make_view_output(self, parts: List[str]) -> Dict: + assert len(parts) >= 1 + confirm = False + text = "Click" + if len(parts) == 2: + text = parts[1] + elif len(parts) == 3: + confirm = (parts[1] == "confirm") + text = parts[2] + return { + "type": UserGpioModes.OUTPUT, + "channel": parts[0], + "confirm": confirm, + "text": text, + } |