From b1d41a3473a49ac9ef5771c8e704f33181989d92 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Sun, 25 Feb 2024 18:52:10 +0100 Subject: [PATCH] vortexiot: use json config, minor fixes --- .gitignore | 1 + config.json.dist | 12 ++++++++ config.py.dist | 10 ------- vortex.py | 6 ++++ vortexiot.py | 71 ++++++++++++++++++++++++++++++------------------ 5 files changed, 63 insertions(+), 37 deletions(-) create mode 100644 config.json.dist delete mode 100644 config.py.dist diff --git a/.gitignore b/.gitignore index df81b2c..588758b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc config.py +config.json diff --git a/config.json.dist b/config.json.dist new file mode 100644 index 0000000..2e31c7d --- /dev/null +++ b/config.json.dist @@ -0,0 +1,12 @@ +{ + "mqtt": {"host": "localhost"}, + "control": {"host": "localhost", "port": 10001}, + "inputs": { + "aux1": ["1", "2"], + "sound": ["A", "B"], + "hdmi": ["C", "D"] + }, + "outputs": { + "main": ["1", "2"] + } +} diff --git a/config.py.dist b/config.py.dist deleted file mode 100644 index c5a523e..0000000 --- a/config.py.dist +++ /dev/null @@ -1,10 +0,0 @@ -INPUTS = { - 'mic1': [5], - 'aux1': [1, 2], - 'aux2': [3, 4], - } - -OUTPUTS = { - 'main': ['A', 'B'], - 'recording': ['C', 'D'], - } diff --git a/vortex.py b/vortex.py index c4fcd78..91324f9 100644 --- a/vortex.py +++ b/vortex.py @@ -47,10 +47,16 @@ class VortexConnection(object): while data: try: data = self.socket.recv(512) + except KeyboardInterrupt: + raise except: time.sleep(0.1) self.logger.warning("Recv failed") continue + + if not data: + raise Exception("Connection closed") + buf += data while buf.find(b"\r") != -1: diff --git a/vortexiot.py b/vortexiot.py index e9296ad..2b3b45e 100644 --- a/vortexiot.py +++ b/vortexiot.py @@ -1,9 +1,10 @@ +import os +import json +import itertools +import logging + import paho.mqtt.client as mqtt import vortex -import logging -import itertools - -import config logging.basicConfig(level=logging.INFO) @@ -11,12 +12,15 @@ logging.basicConfig(level=logging.INFO) class VortexSpejsIOTClient(mqtt.Client): topic_prefix = "iot/polycom/" - def __init__(self, *args, **kwargs): + def __init__(self, config, *args, **kwargs): super(VortexSpejsIOTClient, self).__init__(*args, **kwargs) + self.config = config self.logger = logging.getLogger(self.__class__.__name__) - self.vortex = vortex.VortexConnection("localhost", 10001) + self.vortex = vortex.VortexConnection( + self.config["control"]["host"], self.config["control"]["port"] + ) self.vortex.handlers.append(self.on_vortex_message) self.device_id = "" @@ -27,6 +31,7 @@ class VortexSpejsIOTClient(mqtt.Client): self.vortex[self.device_id].call_single("BLAUTO0") def run(self): + self.connect(**self.config["mqtt"]) self.loop_start() self.vortex.loop() @@ -57,34 +62,40 @@ class VortexSpejsIOTClient(mqtt.Client): node, attrib, _ = topic.split("/") msg.payload = msg.payload.decode("utf-8") - if node in config.INPUTS: + if node in self.config["inputs"]: if attrib == "gain": - self.multi_call("GAINI", config.INPUTS[node], msg.payload) + self.multi_call("GAINI", self.config["inputs"][node], msg.payload) elif attrib == "mute": self.multi_call( - "MUTEI", config.INPUTS[node], "1" if msg.payload == "true" else "0" + "MUTEI", + self.config["inputs"][node], + "1" if msg.payload == "true" else "0", ) elif attrib == "mode": self.multi_call( - "MIC", config.INPUTS[node], "1" if msg.payload == "mic" else "0" + "MIC", + self.config["inputs"][node], + "1" if msg.payload == "mic" else "0", ) - elif node in config.OUTPUTS: + elif node in self.config["outputs"]: if attrib == "gain": - self.multi_call("GAINO", config.OUTPUTS[node], msg.payload) + self.multi_call("GAINO", self.config["outputs"][node], msg.payload) elif attrib == "mute": self.multi_call( - "MUTEO", config.OUTPUTS[node], "1" if msg.payload == "true" else "0" + "MUTEO", + self.config["outputs"][node], + "1" if msg.payload == "true" else "0", ) elif ":" in node: # This is matrix operation... inp, _, out = node.partition(":") - if inp not in config.INPUTS or out not in config.OUTPUTS: + if inp not in self.config["inputs"] or out not in self.config["outputs"]: self.logger.warning("Invalid route: %r", node) return - inp_chs = config.INPUTS[inp] - out_chs = config.OUTPUTS[out] + inp_chs = self.config["inputs"][inp] + out_chs = self.config["outputs"][out] pairs = zip(itertools.cycle(inp_chs), out_chs) @@ -97,37 +108,39 @@ class VortexSpejsIOTClient(mqtt.Client): self.logger.info("vortex -> %r", msg) device_id, msg = msg[:3], msg[3:] - if self.device_id != device_id: + if self.device_id != device_id.decode(): self.logger.debug("%r/%r: Invalid device id", device_id, self.device_id) return - if msg.startswith("MMUTE"): - inp, out, muted = msg[5:].split(",") + if msg.startswith(b"MMUTE"): + inp, out, muted = msg[5:].decode().split(",") inp_label = self.input_from_channel(inp) out_label = self.output_from_channel(out) self.notify("%s:%s" % (inp_label, out_label), "mute", muted == "1") - elif msg.startswith("MGAIN"): - inp, out, gain = msg[5:].split(",") + elif msg.startswith(b"MGAIN"): + inp, out, gain = msg[5:].decode().split(",") inp_label = self.input_from_channel(inp) out_label = self.output_from_channel(out) self.notify("%s:%s" % (inp_label, out_label), "gain", gain) - elif msg.startswith("GAINI"): + elif msg.startswith(b"GAINI"): + msg = msg.decode() channel_id, gain = msg[5], msg[6:] label = self.input_from_channel(channel_id) self.notify(label, "gain", gain) - elif msg.startswith("MUTEI"): + elif msg.startswith(b"MUTEI"): + msg = msg.decode() channel_id, muted = msg[5], msg[6:] label = self.input_from_channel(channel_id) self.notify(label, "mute", muted == "1") def input_from_channel(self, channel): - return self.find_label(config.INPUTS, channel) + return self.find_label(self.config["inputs"], channel) def output_from_channel(self, channel): - return self.find_label(config.OUTPUTS, channel) + return self.find_label(self.config["outputs"], channel) def find_label(self, labels, channel): for label, channels in labels.items(): @@ -138,8 +151,12 @@ class VortexSpejsIOTClient(mqtt.Client): def main(): - client = VortexSpejsIOTClient() - client.connect("localhost") + config = {} + + with open(os.environ["CONFIG_PATH"], "rb") as fd: + config = json.load(fd) + + client = VortexSpejsIOTClient(config) client.run()