From 78ce8ff512f569101cf425c0d34384d7224c9221 Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Wed, 3 May 2023 17:22:13 +0200 Subject: [PATCH] added hardware handling --- streamdeckapi/server.py | 119 +++++++++++++++++++++++++++++++++------- 1 file changed, 98 insertions(+), 21 deletions(-) diff --git a/streamdeckapi/server.py b/streamdeckapi/server.py index 49189d7..2195be9 100644 --- a/streamdeckapi/server.py +++ b/streamdeckapi/server.py @@ -1,14 +1,33 @@ """Stream Deck API Server.""" +import re import aiohttp import asyncio import platform +import human_readable_ids as hri from jsonpickle import encode from aiohttp import web +from StreamDeck.DeviceManager import DeviceManager +from StreamDeck.Devices.StreamDeck import StreamDeck -# from StreamDeck.DeviceManager import DeviceManager from streamdeckapi.const import PLUGIN_ICON, PLUGIN_INFO, PLUGIN_PORT -from streamdeckapi.types import SDApplication, SDButton, SDDevice +from streamdeckapi.types import SDApplication, SDButton, SDButtonPosition, SDDevice + + +DEFAULT_ICON = re.sub( + "\r\n|\n|\r", + "", + """ + + + + + +Configure + +""", +) + application: SDApplication = SDApplication( { @@ -23,27 +42,29 @@ devices: list[SDDevice] = [] buttons: dict[str, SDButton] = {} # Examples -devices.append( - SDDevice( - { - "id": "08B602C026FC8D1989FDF80EB8658612", - "name": "Stream Deck", - "size": {"columns": 5, "rows": 3}, - "type": 0, - } - ) -) -buttons["576e8e7fc6ac2a37fa436ed3dc76652b"] = SDButton( - { - "uuid": "kind-sloth-97", - "device": "08B602C026FC8D1989FDF80EB8658612", - "position": {"x": 0, "y": 0}, - "svg": 'offPhilips Hue Huelight', - } -) +# devices.append( +# SDDevice( +# { +# "id": "08B602C026FC8D1989FDF80EB8658612", +# "name": "Stream Deck", +# "size": {"columns": 5, "rows": 3}, +# "type": 0, +# } +# ) +# ) +# buttons["576e8e7fc6ac2a37fa436ed3dc76652b"] = SDButton( +# { +# "uuid": "kind-sloth-97", +# "device": "08B602C026FC8D1989FDF80EB8658612", +# "position": {"x": 0, "y": 0}, +# "svg": 'offPhilips Hue Huelight', +# } +# ) -async def api_info_handler(request: web.Request): +async def api_info_handler( + request: web.Request, +): # FIXME: can result in unparseable json (different keys, f.ex. x - x_pos) json_data = encode( {"devices": devices, "application": application, "buttons": buttons}, unpicklable=False, @@ -115,7 +136,63 @@ async def start_server(host="0.0.0.0", port=PLUGIN_PORT): print("Started Stream Deck API server on port", PLUGIN_PORT) +def get_position(deck: StreamDeck, key: int) -> SDButtonPosition: + """Get the position of a key.""" + return SDButtonPosition({"x": int(key / deck.KEY_COLS), "y": key % deck.KEY_COLS}) + + +def on_key_change(deck: StreamDeck, key: int, state: bool): + """Handle key change callbacks.""" + position = get_position(deck, key) + print(f"Key at {position.x_pos}|{position.y_pos} is state {state}") + + +def init_all(): + """Init Stream Deck devices.""" + # TODO: Load buttons from storage and save asap + + streamdecks: list[StreamDeck] = DeviceManager().enumerate() + print("Found {} Stream Deck(s).\n".format(len(streamdecks))) + + for deck in streamdecks: + if not deck.is_visual(): + continue + + deck.open() + + serial = deck.get_serial_number() + + devices.append( + SDDevice( + { + "id": serial, + "name": deck.deck_type(), + "size": {"columns": deck.KEY_COLS, "rows": deck.KEY_ROWS}, + "type": 20, + } + ) + ) + + for key in range(deck.key_count()): + # FIXME: only add if not already in dict + position = get_position(deck, key) + buttons[key] = SDButton( + { + "uuid": hri.get_new_id().lower().replace(" ", "-"), + "device": serial, + "position": {"x": position.x_pos, "y": position.y_pos}, + "svg": DEFAULT_ICON, + } + ) + + # TODO: write svg to buttons + + deck.set_key_callback(on_key_change) + + def start(): + init_all() + loop = asyncio.get_event_loop() loop.run_until_complete(start_server()) loop.run_forever()