80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
import asyncio
|
|
import aiomqtt
|
|
from collections import defaultdict
|
|
|
|
from barco_legacy import BarcoRLM_Control
|
|
|
|
# TODO MAKE THIS CONFIGURALBE
|
|
PROJECTOR_IP = "192.168.192.12"
|
|
MQTT_PREFIX = "p22/projektorji/glavni"
|
|
MQTT_HOST = "-----"
|
|
POLLING_PERIOD_SEC = 10
|
|
|
|
|
|
class BarcoRLM_MQTT:
|
|
def __init__(self, projector_ip, mqtt_prefix):
|
|
self.projector_ip = projector_ip
|
|
self.mqtt_prefix = mqtt_prefix
|
|
self.barco = BarcoRLM_Control(projector_ip)
|
|
self.last_status = defaultdict(lambda: '')
|
|
|
|
async def run(self):
|
|
async with aiomqtt.Client(MQTT_HOST, 1883) as client:
|
|
self.client = client
|
|
task_polling = asyncio.create_task(self.task_polling())
|
|
task_control = asyncio.create_task(self.task_control())
|
|
await asyncio.gather(task_control, task_polling)
|
|
|
|
async def task_control(self):
|
|
topicMatch = f"{self.mqtt_prefix}/ukaz/+"
|
|
await self.client.subscribe(topicMatch)
|
|
|
|
async for mesg in self.client.messages:
|
|
cmd = mesg.topic.value.split("/")[-1]
|
|
val = mesg.payload.decode()
|
|
|
|
if cmd == "power":
|
|
await self.barco.set_power(onoff(val))
|
|
elif cmd == "shutter":
|
|
await self.barco.set_shutter(onoff(val))
|
|
|
|
async def task_polling(self):
|
|
while True:
|
|
status = await self.barco.get_status()
|
|
for key, val in status.items():
|
|
if self.last_status[key] != val:
|
|
print(f"Status change {key}={val}")
|
|
await self.on_status_change(key, val)
|
|
|
|
self.last_status = status
|
|
await asyncio.sleep(POLLING_PERIOD_SEC)
|
|
|
|
async def on_status_change(self, key, val):
|
|
mkey = mval = None
|
|
if key == "status":
|
|
mkey = "power"
|
|
if val == "Imaging":
|
|
mval = "1"
|
|
if val == "Standby":
|
|
mval = "0"
|
|
if key == "src":
|
|
mkey = "input"
|
|
mval = val
|
|
if key == "fmr":
|
|
mkey = "input_format"
|
|
mval = val
|
|
if mkey is not None and mval is not None:
|
|
await self.client.publish(f"{self.mqtt_prefix}/status/{mkey}", payload=mval)
|
|
|
|
|
|
def onoff(input):
|
|
#if input == "1":
|
|
# return True
|
|
#elif input == "0":
|
|
# return False
|
|
return input == "1"
|
|
|
|
|
|
if __name__ == '__main__':
|
|
barco = BarcoRLM_MQTT(PROJECTOR_IP, MQTT_PREFIX)
|
|
asyncio.run(barco.run())
|