Fix QLC+ Integration

This commit is contained in:
Mattallmighty 2021-11-15 07:56:41 +11:00
parent adfea1d9fa
commit ec5f19ac8d
4 changed files with 78 additions and 79 deletions

8
.vscode/tasks.json vendored
View File

@ -24,9 +24,11 @@
{
"label": "[3]-[Frontend]:3000-start",
"type": "shell",
// "command": "runuser -l vscode -c 'cd /workspaces/${workspaceFolderBasename}/frontend; yarn; yarn start'",
"command": "cd /workspaces/${workspaceFolderBasename}/frontend; yarn; yarn start",
"group": "build",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
@ -60,7 +62,7 @@
"type": "shell",
"command": "rm -rf /workspaces/${workspaceFolderBasename}/ledfx_frontend/*; runuser -l vscode -c 'cd /workspaces/${workspaceFolderBasename}/frontend; yarn build'",
"problemMatcher": []
},
}
],
"inputs": [
{

View File

@ -50,7 +50,7 @@ function ConfirmationDialogRaw(props) {
// console.log("qlcInfo - Response: ", qlcInfo);
const effectNames = qlcInfo && qlcInfo.event_types && qlcInfo.event_types.effect_set.event_filters.effect_name
//const effectCleared = qlcInfo && qlcInfo.event_types && qlcInfo.event_types.effect_cleared.event_name
const SceneSet = qlcInfo && qlcInfo.event_types && qlcInfo.event_types.scene_set.event_filters.scene_name
const SceneSet = qlcInfo && qlcInfo.event_types && qlcInfo.event_types.scene_activated.event_filters.scene_name
const QLCWidgets = qlcInfo && qlcInfo.qlc_widgets && qlcInfo.qlc_widgets.sort((a,b) => parseInt(a[0]) - parseInt(b[0]) )
const EVENT_TYPES= qlcInfo && qlcInfo.event_types && qlcInfo.event_types
// console.log("test3",EVENT_TYPES);

View File

@ -1,5 +1,4 @@
import logging
from json import JSONDecodeError
from aiohttp import web
@ -15,15 +14,23 @@ class QLCEndpoint(RestEndpoint):
ENDPOINT_PATH = "/api/integrations/qlc/{integration_id}"
async def get(self, integration_id) -> web.Response:
async def get(self, integration_id, request) -> web.Response:
"""Get info from QLC+ integration"""
integration = self._ledfx.integrations.get(integration_id)
if (integration is None) or (integration.type != "qlc"):
response = {"not found": 404}
return web.json_response(data=response, status=404)
response = {}
data = await request.json()
info = data.get("info")
if info is None:
response = {
"status": "failed",
"reason": 'Required attribute "info" was not provided',
}
return web.json_response(data=response, status=500)
if info == "event_types":
# generate dict of {effect_id: effect_name}
effect_names = []
for effect_type, effect in self._ledfx.effects.classes().items():
@ -33,7 +40,7 @@ class QLCEndpoint(RestEndpoint):
for scene in self._ledfx.config["scenes"]:
scene_names.append(self._ledfx.config["scenes"][scene]["name"])
response["event_types"] = {
response = {
Event.EFFECT_SET: {
"event_name": "Effect Set",
"event_filters": {"effect_name": effect_names},
@ -48,9 +55,18 @@ class QLCEndpoint(RestEndpoint):
},
}
response["qlc_widgets"] = await integration.get_widgets()
elif info == "qlc_widgets":
response = await integration.get_widgets()
response["qlc_listeners"] = integration.data
elif info == "qlc_listeners":
response = integration.data
else:
response = {
"status": "failed",
"reason": f'Unknown info parameter "{info}"',
}
return web.json_response(data=response, status=500)
return web.json_response(data=response, status=200)
@ -61,14 +77,7 @@ class QLCEndpoint(RestEndpoint):
response = {"not found": 404}
return web.json_response(data=response, status=404)
try:
data = await request.json()
except JSONDecodeError:
response = {
"status": "failed",
"reason": "JSON Decoding failed",
}
return web.json_response(data=response, status=400)
event_type = data.get("event_type")
event_filter = data.get("event_filter")
@ -77,21 +86,21 @@ class QLCEndpoint(RestEndpoint):
"status": "failed",
"reason": 'Required attribute "event_type" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if event_filter is None:
response = {
"status": "failed",
"reason": 'Required attribute "event_filter" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if type(event_filter) is not dict:
response = {
"status": "failed",
"reason": f'Invalid filter "{event_filter}", should be dictionary eg. {{ "scene_name" : "my scene" }} ',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
# toggle the event listener
if not integration.toggle_event(event_type, event_filter):
@ -99,7 +108,7 @@ class QLCEndpoint(RestEndpoint):
"status": "failed",
"reason": f"Could not find event with type {event_type} and filter {event_filter}",
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
# Save the configuration (integration will handle modifying "data")
for _integration in self._ledfx.config["integrations"]:
@ -121,14 +130,7 @@ class QLCEndpoint(RestEndpoint):
response = {"not found": 404}
return web.json_response(data=response, status=404)
try:
data = await request.json()
except JSONDecodeError:
response = {
"status": "failed",
"reason": "JSON Decoding failed",
}
return web.json_response(data=response, status=400)
event_type = data.get("event_type")
event_filter = data.get("event_filter")
qlc_payload = data.get("qlc_payload")
@ -138,28 +140,28 @@ class QLCEndpoint(RestEndpoint):
"status": "failed",
"reason": 'Required attribute "event_type" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if event_filter is None:
response = {
"status": "failed",
"reason": 'Required attribute "event_filter" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if type(event_filter) is not dict:
response = {
"status": "failed",
"reason": f'Invalid filter "{event_filter}", should be dictionary eg. {{ "scene_name" : "my scene" }} ',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if qlc_payload is None:
response = {
"status": "failed",
"reason": 'Required attribute "qlc_payload" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
# Create a link between ledfx event and sending the payload
integration.create_event(event_type, event_filter, True, qlc_payload)
@ -184,14 +186,7 @@ class QLCEndpoint(RestEndpoint):
response = {"not found": 404}
return web.json_response(data=response, status=404)
try:
data = await request.json()
except JSONDecodeError:
response = {
"status": "failed",
"reason": "JSON Decoding failed",
}
return web.json_response(data=response, status=400)
event_type = data.get("event_type")
event_filter = data.get("event_filter")
@ -200,21 +195,21 @@ class QLCEndpoint(RestEndpoint):
"status": "failed",
"reason": 'Required attribute "event_type" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if event_filter is None:
response = {
"status": "failed",
"reason": 'Required attribute "event_filter" was not provided',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
if type(event_filter) is not dict:
response = {
"status": "failed",
"reason": f'Invalid filter "{event_filter}", should be dictionary eg. {{ "scene_name" : "my scene" }} ',
}
return web.json_response(data=response, status=400)
return web.json_response(data=response, status=500)
# Delete the listener and event from data
integration.delete_event(event_type, event_filter)

View File

@ -238,12 +238,14 @@ class QLCWebsocketClient:
self.websocket = None
self.url = url
self.domain = domain
self.session = aiohttp.ClientSession()
async def connect(self):
"""Connect to the WebSocket."""
while True:
try:
self.websocket = await aiohttp.ClientSession.ws_connect(self.domain)
self.websocket = await self.session.ws_connect(self.url)
#self.websocket = await self.ws_connect(self.url)
return True
except aiohttp.client_exceptions.ClientConnectorError:
_LOGGER.info(
@ -276,7 +278,7 @@ class QLCWebsocketClient:
await self.websocket.send_str(message)
# Every call to the logger is a performance hit
# _LOGGER.debug(f"Sent message {message} to {self.domain}")
_LOGGER.debug(f"Sent message {message} to {self.domain}")
async def receive(self):
"""Receive one message from the WebSocket."""