added basic api routes

This commit is contained in:
Patrick762
2023-05-03 14:51:39 +02:00
parent dc13848f96
commit 22fe988591
2 changed files with 63 additions and 40 deletions

View File

@@ -9,7 +9,7 @@ here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
long_description = "\n" + fh.read()
DESCRIPTION = 'Stream Deck API Library'
DESCRIPTION = "Stream Deck API Library"
# Setting up
setup(
@@ -22,8 +22,14 @@ setup(
long_description=long_description,
url="https://github.com/Patrick762/streamdeckapi",
packages=find_packages(),
install_requires=["requests==2.28.2", "websockets==11.0.2",
"streamdeck==0.9.3", "pillow>=9.4.0,<10.0.0"],
install_requires=[
"requests==2.28.2",
"websockets==11.0.2",
"aiohttp==3.8.4",
"human-readable-ids==0.1.3",
"streamdeck==0.9.3",
"pillow>=9.4.0,<10.0.0",
],
keywords=[],
entry_points={
"console_scripts": ["streamdeckapi-server = streamdeckapi.server:start"]
@@ -35,5 +41,5 @@ setup(
"Operating System :: Unix",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
]
],
)

View File

@@ -1,48 +1,65 @@
"""Stream Deck API Server."""
from StreamDeck.DeviceManager import DeviceManager
import aiohttp
import asyncio
from aiohttp import web, WSCloseCode
# from StreamDeck.DeviceManager import DeviceManager
from streamdeckapi.const import PLUGIN_ICON, PLUGIN_INFO, PLUGIN_PORT
# Prints diagnostic information about a given StreamDeck.
def print_deck_info(index, deck):
image_format = deck.key_image_format()
flip_description = {
(False, False): "not mirrored",
(True, False): "mirrored horizontally",
(False, True): "mirrored vertically",
(True, True): "mirrored horizontally/vertically",
}
async def api_info_handler(request: web.Request):
return web.Response(text="Info")
print("Deck {} - {}.".format(index, deck.deck_type()))
print("\t - ID: {}".format(deck.id()))
print("\t - Serial: '{}'".format(deck.get_serial_number()))
print("\t - Firmware Version: '{}'".format(deck.get_firmware_version()))
print("\t - Key Count: {} (in a {}x{} grid)".format(
deck.key_count(),
deck.key_layout()[0],
deck.key_layout()[1]))
if deck.is_visual():
print("\t - Key Images: {}x{} pixels, {} format, rotated {} degrees, {}".format(
image_format['size'][0],
image_format['size'][1],
image_format['format'],
image_format['rotation'],
flip_description[image_format['flip']]))
else:
print("\t - No Visual Output")
def start():
streamdecks = DeviceManager().enumerate()
async def api_icon_get_handler(request: web.Request):
btnId = request.match_info["btnId"]
return web.Response(text="Icon get")
print("Found {} Stream Deck(s).\n".format(len(streamdecks)))
async def api_icon_set_handler(request: web.Request):
btnId = request.match_info["btnId"]
body = await request.text()
print(body)
return web.Response(text="Icon set")
async def websocket_handler(request: web.Request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == "close":
await ws.close()
else:
await ws.send_str("some websocket message payload")
elif msg.type == aiohttp.WSMsgType.ERROR:
print("ws connection closed with exception %s" % ws.exception())
return ws
def create_runner():
app = web.Application()
app.add_routes(
[
web.get("/", websocket_handler),
web.get(PLUGIN_INFO, api_info_handler),
web.get(PLUGIN_ICON + "/{btnId}", api_icon_get_handler),
web.post(PLUGIN_ICON + "/{btnId}", api_icon_set_handler),
]
)
return web.AppRunner(app)
async def start_server(host="0.0.0.0", port=PLUGIN_PORT):
runner = create_runner()
await runner.setup()
site = web.TCPSite(runner, host, port)
await site.start()
print("Started Stream Deck API server on port", PLUGIN_PORT)
for index, deck in enumerate(streamdecks):
deck.open()
deck.reset()
print_deck_info(index, deck)
deck.close()
def start():
loop = asyncio.get_event_loop()
loop.run_until_complete(start_server())
loop.run_forever()