vortexiot: use json config, minor fixes

master
informatic 2024-02-25 18:52:10 +01:00
parent 621a1b908d
commit b1d41a3473
No known key found for this signature in database
5 changed files with 63 additions and 37 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.pyc
config.py
config.json

12
config.json.dist Normal file
View File

@ -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"]
}
}

View File

@ -1,10 +0,0 @@
INPUTS = {
'mic1': [5],
'aux1': [1, 2],
'aux2': [3, 4],
}
OUTPUTS = {
'main': ['A', 'B'],
'recording': ['C', 'D'],
}

View File

@ -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:

View File

@ -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()