Old Barco projectors (initial implementation)
This commit is contained in:
parent
f41dfc4f86
commit
a9820001b4
4 changed files with 314 additions and 0 deletions
67
barco_rlmw_http/barco_rlwm_http.py
Normal file
67
barco_rlmw_http/barco_rlwm_http.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
import re
|
||||
import asyncio
|
||||
import aiohttp
|
||||
from asyncio.exceptions import CancelledError
|
||||
|
||||
PORT = 43680
|
||||
RE_STATUS = re.compile(r".stats.txt(.+).value='(.+)';")
|
||||
REMOTEKEY_TIMEOUT = aiohttp.ClientTimeout(total=1)
|
||||
|
||||
|
||||
class BarcoRLM_Control:
|
||||
def __init__(self, projector_ip):
|
||||
self.projector_ip = projector_ip
|
||||
self.session = None
|
||||
|
||||
async def _request(self, method, path, *args, **kwargs):
|
||||
if not self.session:
|
||||
self.session = aiohttp.ClientSession()
|
||||
url = f"http://{self.projector_ip}{path}"
|
||||
resp = await self.session.request(method, url, *args, **kwargs)
|
||||
resp.raise_for_status()
|
||||
return await resp.text()
|
||||
|
||||
async def get_status(self):
|
||||
resp = await self._request("GET", "/tgi/firststatus.tgi")
|
||||
matches = RE_STATUS.findall(resp)
|
||||
status = dict(matches)
|
||||
return status
|
||||
|
||||
async def toggle_power(self):
|
||||
await self._request("GET", "/tgi/general.tgi?powertog")
|
||||
|
||||
async def click_remote(self, key):
|
||||
try:
|
||||
await self._request("GET", f"/tgi/remote.tgi?{key}", timeout=REMOTEKEY_TIMEOUT, raise_for_status=False)
|
||||
except TimeoutError:
|
||||
pass
|
||||
|
||||
async def set_shutter(self, shutter):
|
||||
endpoint = f"/tgi/general.tgi?pause{onoff(shutter)}"
|
||||
await self._request("GET", endpoint)
|
||||
|
||||
async def set_power(self, power):
|
||||
key = onoff(power) + "ky"
|
||||
await self.click_remote(key)
|
||||
|
||||
async def set_input(self, input):
|
||||
endpoint = f"/tgi/input.tgi?{input}"
|
||||
await self._request("GET", endpoint)
|
||||
|
||||
|
||||
def onoff(state: bool) -> str:
|
||||
if state:
|
||||
return "on"
|
||||
else:
|
||||
return "off"
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
async def main():
|
||||
barco = BarcoRLM_Control("192.168.192.12")
|
||||
status = await barco.get_status()
|
||||
print(status)
|
||||
|
||||
await barco.click_remote("kymenu")
|
||||
|
||||
asyncio.run(main())
|
79
barco_rlmw_http/main.py
Normal file
79
barco_rlmw_http/main.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
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
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
barco = BarcoRLM_MQTT(PROJECTOR_IP, MQTT_PREFIX)
|
||||
asyncio.run(barco.run())
|
Loading…
Add table
Add a link
Reference in a new issue