Authentication-Results: mail-b.sr.ht; dkim=pass header.d=jankundrat.com header.i=@jankundrat.com; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) by mail-b.sr.ht (Postfix) with ESMTPS id 32ACE11EFBF for <~martijnbraam/openatem@lists.sr.ht>; Sun, 14 Nov 2021 00:14:48 +0000 (UTC) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 29C3532004AE for <~martijnbraam/openatem@lists.sr.ht>; Sat, 13 Nov 2021 19:14:47 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sat, 13 Nov 2021 19:14:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jankundrat.com; h=resent-date:resent-message-id:resent-from:resent-to :message-id:from:date:subject:mime-version:content-type :content-transfer-encoding:to; s=fm3; bh=ILQPNGVrd6LSA92xmxQwBHL CRNyCxT3FVvPu07OEuZo=; b=OFPgG2u8yRMucivAwYHPW8/vQfWybxCTh7Q7NaS TFxRpZ7OEQaDxEJYJF4009hJrPpr2Gcw2d2owsgNRt3OcS3zu/Qhft+N0sdfKrnm wqZrpQGPrW7fyB3JMpBWfRSTzRO9SNgju/pLo19dzOxAegKp3eOWDlYjfOMv2MvY lv/a92fR6BA5zUCu/a5K4VoEY7s+auGyBIzThZz0tINvG5W11ec7OY572HMCstWd 0dKxUoafBsLmpoc1sVK7BflJJJRd1mxCFV3FErQzLD2XlqaALNwru8hlqlX+0Cqv 0WzIY7vrIOL3e/lh8OieaRykqkz1W9j61c3wtk31tDSWd+g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:resent-date:resent-from :resent-message-id:resent-to:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ILQPNGVrd6LSA92xm xQwBHLCRNyCxT3FVvPu07OEuZo=; b=OKvdIERN3VnKrMIr6UnoDXicIbqwtbj6+ 5MCXJLE/0w/HwS17woh74UeRPPcifg/eny0PVF92PkDQsxBlTkoUy5DA/jI1e7Go DPpegtIJ3BuTik142dSxj4+pX+EVuu6TjuO0DvBLC0Ebpez0AKiwWdmG7WglSYuA ZMf037KTlPDLJusN8rqv7eYtdR1moPfCc8hLvywTHuI3nURpy06Q8zdfq33+6Kr1 T1HW+lE9O/Z6eLf41aks/rhHHMaFVfM4Nl45BeASL+eYcrTGuhnwGsBu+BFnpV6E KhTwZY5jJoN5Fd7YKf9O4XSRX8B2zkeovVL3iWWz3CpXJdGMa8PlQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrvdeigddulecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepkffhfffugggtgffvsehtkeertddttd ejnecuhfhrohhmpeflrghnucfmuhhnughrjohtuceojhhkthesjhgrnhhkuhhnughrrght rdgtohhmqeenucggtffrrghtthgvrhhnpeffkefhvddvjeeigeffkeejhfeutdeiheeitd eggfdvteelteetffdukefgiedutdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpehjkhhtsehjrghnkhhunhgurhgrthdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA for <~martijnbraam/openatem@lists.sr.ht>; Sat, 13 Nov 2021 19:14:46 -0500 (EST) Resent-Date: Sun, 14 Nov 2021 01:14:43 +0100 Resent-Message-ID: <4694218c-1889-44c9-9443-1084ef685551@jankundrat.com> Resent-From: =?iso-8859-1?Q?Jan_Kundr=E1t?= Resent-To: openswitcher ML <~martijnbraam/openatem@lists.sr.ht> Message-Id: <1dc20ca284297bb85179dd657abd224f01f7fd23.1636848848.git.jkt@jankundrat.com> From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= Date: Sun, 14 Nov 2021 00:56:13 +0100 Subject: [PATCH] proxy: mqtt: allow arbitrary writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To: "openswitcher ML" <~martijnbraam/openatem@lists.sr.ht> Since there are typically no server-side MQTT ACLs, this has to be enabled explicitly. There's some duplication of code between this implementation and the one in the HTTP backend. The most straightforward way of fixing that is to convert these early returns into proper exceptions, but I'm too lazy for this and I'm gonna be using this feature tomorrow :). Signed-off-by: Jan Kundrát --- openswitcher_proxy/frontend_mqtt.py | 43 ++++++++++++++++++++++++++--- proxy.toml | 1 + 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/openswitcher_proxy/frontend_mqtt.py b/openswitcher_proxy/frontend_mqtt.py index 63538c6..ff0b23a 100644 --- a/openswitcher_proxy/frontend_mqtt.py +++ b/openswitcher_proxy/frontend_mqtt.py @@ -5,6 +5,7 @@ from functools import partial from .error import DependencyError from .frontend_httpapi import FieldEncoder +import pyatem.command as commandmodule try: import paho.mqtt.client as mqtt @@ -74,12 +75,46 @@ class MqttFrontendThread(threading.Thread): def on_mqtt_connect(self, flags, rc): self.status = 'running' logging.info(f'MQTT: connected ({rc})') - # TODO: enable once on_mqtt_message() works - # client.subscribe(f'atem/{userdata.hw_name}/#') + self.client.subscribe(f'atem/+/set/#') def on_mqtt_message(self, msg): - # TODO: propagate to the switcher, eventually - logging.debug(f'MQTT: msg: {msg.topic} {msg.payload}') + if not self.config.get('allow-writes', False): + logging.error('MQTT writes disabled') + return + parts = msg.topic.split('/') + if len(parts) != 4: + logging.error(f'MQTT: malformed command topic: {msg.topic}') + return + hw = parts[1] + if parts[0] != 'atem' or parts[2] != 'set': + logging.error(f'MQTT: malformed command topic: {msg.topic}') + return + if hw not in self.hw_name: + logging.error(f'MQTT: not handling writes for "{hw}"') + return + fieldname = parts[3] + classname = fieldname.title().replace('-', '') + "Command" + if not hasattr(commandmodule, classname): + logging.error(f'MQTT: unrecognized command {fieldname}') + return + arguments = json.loads(msg.payload) + if not isinstance(arguments, dict): + logging.error(f'MQTT: mailformed payload, needs a JSON dict') + return + for key in arguments: + try: + arguments[key] = int(arguments[key]) + except: + pass + if 'source' in arguments: + inputs = self.threadlist['hardware'][hw].switcher.inputs + if arguments['source'] in inputs: + arguments['source'] = inputs[arguments['source']] + try: + cmd = getattr(commandmodule, classname)(**arguments) + self.threadlist['hardware'][hw].switcher.send_commands([cmd]) + except Exception as e: + logging.error(f'MQTT: cannot write {fieldname}: {str(e)}') def get_status(self): if self.status == 'error': diff --git a/proxy.toml b/proxy.toml index bd6b213..1e55bf5 100644 --- a/proxy.toml +++ b/proxy.toml @@ -36,3 +36,4 @@ type = "mqtt" host = "localhost:1883" hardware = "mini" auth = false +allow-writes = false -- 2.33.0