predavalnice-kontroler/barco_telnet/barco_G62_control.py

127 lines
No EOL
4.7 KiB
Python

import asyncio
import socket
import aiomqtt
import telnetlib3
import toml
import sys
#GLOBALS
room = "undefined"
position = "undefined"
barcoIP = "undefined"
telnetPort = 3023
mqttPort = 1883
mqttIP = 'localhost'
cmdMap = {
'power': 'POWR',
'shutter': 'PMUT',
'freeze': 'FRZE'
}
reverseCmdMap = {v: k for k, v in cmdMap.items()}
def parse_barco_response(raw: str):
raw = raw[1:-1] # strip square brackets
#print(raw)
if raw.startswith("ERR"):
return None # TODO parse type error - "disabled control" is special case which shouldnt normally happen
cmd, status = raw.split("!", maxsplit=2)
#print(cmd + " " + status)
cmd = reverseCmdMap[cmd]
status = '1' if status == '01' else '0'
return cmd, status
#TODO MAKE THESE USE GLOBAL INSTEAD OF PASSING BARCO POSITION IN MAIN
async def barco_telnet_command(client, writer, select: str):
global room
onSub = f"{room}/projectors/{select}/#"
#print('TEST', onSub)
onMatch = f"{room}/projectors/{select}/command/+"
await client.subscribe(onSub)
#async with client.messages as msgs:
async for mesg in client.messages:
# print(mesg.topic.value)
# print(mesg.payload.decode())
# print('on', select)
if mesg.topic.matches(onMatch):
# print("test")
cmd = mesg.topic.value.split("/")[-1]
#val = '1' if mesg.payload.decode() == 'ON' else '0'
val = '1' if mesg.payload.decode() == '1' else '0' # refactor to direct 0 and 1
barcoCmd = f"[{cmdMap[cmd]}{val}]"
print("Received: [" + mesg.topic.value + "] payload: [" + mesg.payload.decode() + "]")
print("Sending command to Barco: " + barcoCmd)
writer.write(barcoCmd)
async def barco_telnet_read_status(client, reader, select: str):
global room
while True:
output = await reader.readuntil(b']')
raw_response: str = output.decode().strip() # strip not necessary? needed for local netcat testing though
print("Received: " + raw_response + " from Barco (" + select + ')')
parsed = parse_barco_response(raw_response)
if parsed == None:
continue #TODO alert for errors
print(f"Updating topic [{parsed[0]}] with value [{parsed[1]}]")
await client.publish(f"{room}/projectors/{select}/status/{parsed[0]}", payload=parsed[1])
async def barco_telnet_query_status(writer, select: str):
while True:
for val in cmdMap.values():
print(f"Querying Barco {select} with: [{val}?]")
writer.write(f"[{val}?]" + '\r\n') # TODO test if funny CRLF necessary (it probably gets ignored)
await asyncio.sleep(0.2) # sleep between writes necessary, otherwise it gets confused.
# simultaneous commands from control could break this? TODO fix later
await asyncio.sleep(30) # TODO find appropriate period
# async def shell(reader, writer):
# async with aiomqtt.Client('localhost', 1883) as client:
# task_status_query = asyncio.create_task(barco_telnet_query_status(writer))
# task_status_reader = asyncio.create_task(barco_telnet_read_status(client, reader))
# task_control = asyncio.create_task(barco_telnet_command(client, writer))
# await asyncio.gather(task_status_query, task_status_reader, task_control)
async def main():
global barcoPosition, barcoIP, telnetPort, mqttIP, mqttPort
if len(sys.argv) < 2:
sys.exit("No position provided")
else:
barcoPosition = sys.argv[1]
conf = toml.load('./config.toml')
g62Barcos = {k: v for k,v in barcos["barco_G62"].items()}
currentBarco = newBarcos[barcoPosition]
barcoIP = currentBarco['ip']
barcoReader, barcoWriter = await telnetlib3.open_connection(barcoIP, telnetPort)
async with aiomqtt.Client(mqttIP, mqttPort) as client:
task_status_query_barco = asyncio.create_task(barco_telnet_query_status(barcoWriter, barcoPosition))
task_status_reader_barco = asyncio.create_task(barco_telnet_read_status(client, barcoReader, barcoPosition))
task_control_barco = asyncio.create_task(barco_telnet_command(client, barcoWriter, barcoPosition))
await asyncio.gather(task_status_query_barco, task_status_reader_barco, task_control_barco)
### fuj to, ne tk delat
# if __name__ == '__main__':
# loop = asyncio.get_event_loop()
# coro = telnetlib3.open_connection(mainBarcoIP, 3023, shell=shell)
# coro = telnetlib3.open_connection(mainBarcoIP, 3023, shell=shell)
# # coro = telnetlib3.open_connection('localhost', 1234, shell=shell)
# reader, writer = loop.run_until_complete(coro)
# loop.run_until_complete(writer.protocol.waiter_closed)
#mqttIP = sys.argv[1]
#barcoIP = sys.argv[2]
asyncio.run(main())