import asyncio import aiomqtt from collections import defaultdict from barco_rlmw_tcp import BarcoRLMW_TCP, ValueUpdate, Status, ACK, ValueType # TODO MAKE THIS CONFIGURALBE PROJECTOR_IP = "192.168.192.12" MQTT_PREFIX = "p22/projektorji/glavni" MQTT_HOST = "-------" POLLING_PERIOD_SEC = 10 class BarcoRLMW_TCP_MQTT: def __init__(self, projector_ip, mqtt_prefix): self.projector_ip = projector_ip self.mqtt_prefix = mqtt_prefix self.barco = BarcoRLMW_TCP(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: await self.barco.query("status") await asyncio.sleep(POLLING_PERIOD_SEC) async def write_status(self, status, value): await self.client.publish(f"{self.mqtt_prefix}/status/{status}", payload=value) async def task_process_messages(self): async for msg in self.barco.iter_messages(): # We only care about value updates if isinstance(msg, ValueUpdate): # Power status if msg.key == ValueType.status: if msg.value in (Status.imaging, Status.warmup): self.write_status("power", 1) elif msg.value in (Status.standby, Status.cooling): self.write_status("power", 0) # Video mute if msg.key == ValueType.picture_mute: self.write_status("shutter", msg.value) def onoff(input): if input == "1": return True elif input == "0": return False if __name__ == '__main__': barco = BarcoRLMW_TCP_MQTT(PROJECTOR_IP, MQTT_PREFIX) asyncio.run(barco.run())