vortexiot: use json config, minor fixes
parent
621a1b908d
commit
b1d41a3473
|
@ -1,2 +1,3 @@
|
||||||
*.pyc
|
*.pyc
|
||||||
config.py
|
config.py
|
||||||
|
config.json
|
||||||
|
|
|
@ -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"]
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +0,0 @@
|
||||||
INPUTS = {
|
|
||||||
'mic1': [5],
|
|
||||||
'aux1': [1, 2],
|
|
||||||
'aux2': [3, 4],
|
|
||||||
}
|
|
||||||
|
|
||||||
OUTPUTS = {
|
|
||||||
'main': ['A', 'B'],
|
|
||||||
'recording': ['C', 'D'],
|
|
||||||
}
|
|
|
@ -47,10 +47,16 @@ class VortexConnection(object):
|
||||||
while data:
|
while data:
|
||||||
try:
|
try:
|
||||||
data = self.socket.recv(512)
|
data = self.socket.recv(512)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
raise
|
||||||
except:
|
except:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
self.logger.warning("Recv failed")
|
self.logger.warning("Recv failed")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
raise Exception("Connection closed")
|
||||||
|
|
||||||
buf += data
|
buf += data
|
||||||
|
|
||||||
while buf.find(b"\r") != -1:
|
while buf.find(b"\r") != -1:
|
||||||
|
|
71
vortexiot.py
71
vortexiot.py
|
@ -1,9 +1,10 @@
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import itertools
|
||||||
|
import logging
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import vortex
|
import vortex
|
||||||
import logging
|
|
||||||
import itertools
|
|
||||||
|
|
||||||
import config
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
@ -11,12 +12,15 @@ logging.basicConfig(level=logging.INFO)
|
||||||
class VortexSpejsIOTClient(mqtt.Client):
|
class VortexSpejsIOTClient(mqtt.Client):
|
||||||
topic_prefix = "iot/polycom/"
|
topic_prefix = "iot/polycom/"
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, config, *args, **kwargs):
|
||||||
super(VortexSpejsIOTClient, self).__init__(*args, **kwargs)
|
super(VortexSpejsIOTClient, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
self.config = config
|
||||||
self.logger = logging.getLogger(self.__class__.__name__)
|
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.vortex.handlers.append(self.on_vortex_message)
|
||||||
self.device_id = ""
|
self.device_id = ""
|
||||||
|
|
||||||
|
@ -27,6 +31,7 @@ class VortexSpejsIOTClient(mqtt.Client):
|
||||||
self.vortex[self.device_id].call_single("BLAUTO0")
|
self.vortex[self.device_id].call_single("BLAUTO0")
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
self.connect(**self.config["mqtt"])
|
||||||
self.loop_start()
|
self.loop_start()
|
||||||
self.vortex.loop()
|
self.vortex.loop()
|
||||||
|
|
||||||
|
@ -57,34 +62,40 @@ class VortexSpejsIOTClient(mqtt.Client):
|
||||||
node, attrib, _ = topic.split("/")
|
node, attrib, _ = topic.split("/")
|
||||||
msg.payload = msg.payload.decode("utf-8")
|
msg.payload = msg.payload.decode("utf-8")
|
||||||
|
|
||||||
if node in config.INPUTS:
|
if node in self.config["inputs"]:
|
||||||
if attrib == "gain":
|
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":
|
elif attrib == "mute":
|
||||||
self.multi_call(
|
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":
|
elif attrib == "mode":
|
||||||
self.multi_call(
|
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":
|
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":
|
elif attrib == "mute":
|
||||||
self.multi_call(
|
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:
|
elif ":" in node:
|
||||||
# This is matrix operation...
|
# This is matrix operation...
|
||||||
inp, _, out = node.partition(":")
|
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)
|
self.logger.warning("Invalid route: %r", node)
|
||||||
return
|
return
|
||||||
|
|
||||||
inp_chs = config.INPUTS[inp]
|
inp_chs = self.config["inputs"][inp]
|
||||||
out_chs = config.OUTPUTS[out]
|
out_chs = self.config["outputs"][out]
|
||||||
|
|
||||||
pairs = zip(itertools.cycle(inp_chs), out_chs)
|
pairs = zip(itertools.cycle(inp_chs), out_chs)
|
||||||
|
|
||||||
|
@ -97,37 +108,39 @@ class VortexSpejsIOTClient(mqtt.Client):
|
||||||
self.logger.info("vortex -> %r", msg)
|
self.logger.info("vortex -> %r", msg)
|
||||||
device_id, msg = msg[:3], msg[3:]
|
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)
|
self.logger.debug("%r/%r: Invalid device id", device_id, self.device_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
if msg.startswith("MMUTE"):
|
if msg.startswith(b"MMUTE"):
|
||||||
inp, out, muted = msg[5:].split(",")
|
inp, out, muted = msg[5:].decode().split(",")
|
||||||
inp_label = self.input_from_channel(inp)
|
inp_label = self.input_from_channel(inp)
|
||||||
out_label = self.output_from_channel(out)
|
out_label = self.output_from_channel(out)
|
||||||
self.notify("%s:%s" % (inp_label, out_label), "mute", muted == "1")
|
self.notify("%s:%s" % (inp_label, out_label), "mute", muted == "1")
|
||||||
|
|
||||||
elif msg.startswith("MGAIN"):
|
elif msg.startswith(b"MGAIN"):
|
||||||
inp, out, gain = msg[5:].split(",")
|
inp, out, gain = msg[5:].decode().split(",")
|
||||||
inp_label = self.input_from_channel(inp)
|
inp_label = self.input_from_channel(inp)
|
||||||
out_label = self.output_from_channel(out)
|
out_label = self.output_from_channel(out)
|
||||||
self.notify("%s:%s" % (inp_label, out_label), "gain", gain)
|
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:]
|
channel_id, gain = msg[5], msg[6:]
|
||||||
label = self.input_from_channel(channel_id)
|
label = self.input_from_channel(channel_id)
|
||||||
self.notify(label, "gain", gain)
|
self.notify(label, "gain", gain)
|
||||||
|
|
||||||
elif msg.startswith("MUTEI"):
|
elif msg.startswith(b"MUTEI"):
|
||||||
|
msg = msg.decode()
|
||||||
channel_id, muted = msg[5], msg[6:]
|
channel_id, muted = msg[5], msg[6:]
|
||||||
label = self.input_from_channel(channel_id)
|
label = self.input_from_channel(channel_id)
|
||||||
self.notify(label, "mute", muted == "1")
|
self.notify(label, "mute", muted == "1")
|
||||||
|
|
||||||
def input_from_channel(self, channel):
|
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):
|
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):
|
def find_label(self, labels, channel):
|
||||||
for label, channels in labels.items():
|
for label, channels in labels.items():
|
||||||
|
@ -138,8 +151,12 @@ class VortexSpejsIOTClient(mqtt.Client):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
client = VortexSpejsIOTClient()
|
config = {}
|
||||||
client.connect("localhost")
|
|
||||||
|
with open(os.environ["CONFIG_PATH"], "rb") as fd:
|
||||||
|
config = json.load(fd)
|
||||||
|
|
||||||
|
client = VortexSpejsIOTClient(config)
|
||||||
client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue