pushi commit kreten sploh ni res.. upam da crknes
This commit is contained in:
parent
1249c29956
commit
8a3eb2de70
3 changed files with 97 additions and 31 deletions
|
@ -1,72 +1,106 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import socket
|
||||||
import aiomqtt
|
import aiomqtt
|
||||||
import telnetlib3
|
import telnetlib3
|
||||||
|
import toml
|
||||||
|
from socket import gethostname
|
||||||
|
|
||||||
|
mainBarcoIP = '192.168.192.12'
|
||||||
|
sideBarcoIP = '192.168.192.13'
|
||||||
|
|
||||||
|
# TODO all the checks and stuff
|
||||||
|
# TODO MAKE THIS CONFIGURALBE
|
||||||
|
|
||||||
cmdMap = {
|
cmdMap = {
|
||||||
'POWER': 'POWR',
|
'power': 'POWR',
|
||||||
'SHUTTER': 'PMUT',
|
'shutter': 'PMUT',
|
||||||
'FREEZE': 'FRZE'
|
'freeze': 'FRZE'
|
||||||
}
|
}
|
||||||
reverseCmdMap = {v: k for k, v in cmdMap.items()}
|
reverseCmdMap = {v: k for k, v in cmdMap.items()}
|
||||||
|
|
||||||
|
|
||||||
def parse_barco_response(raw: str):
|
def parse_barco_response(raw: str):
|
||||||
raw = raw[1:-1] # strip square brackets
|
raw = raw[1:-1] # strip square brackets
|
||||||
#print(raw)
|
#print(raw)
|
||||||
if raw.startswith("ERR"):
|
if raw.startswith("ERR"):
|
||||||
return None # TODO parse type error - "disabled control" is special case
|
return None # TODO parse type error - "disabled control" is special case which shouldnt normally happen
|
||||||
cmd, status = raw.split("!", maxsplit=2)
|
cmd, status = raw.split("!", maxsplit=2)
|
||||||
#print(cmd + " " + status)
|
#print(cmd + " " + status)
|
||||||
cmd = reverseCmdMap[cmd]
|
cmd = reverseCmdMap[cmd]
|
||||||
status = 'ON' if status == '01' else 'OFF'
|
status = '1' if status == '01' else '0'
|
||||||
return cmd, status
|
return cmd, status
|
||||||
|
|
||||||
|
|
||||||
async def barco_telnet_command(client, writer):
|
async def barco_telnet_command(client, writer, select: str):
|
||||||
await client.subscribe("p1/projectors/main/#")
|
await client.subscribe(f"p1/projektorji/{select}/#")
|
||||||
async with client.messages() as msgs:
|
async with client.messages() as msgs:
|
||||||
async for mesg in msgs:
|
async for mesg in msgs:
|
||||||
if mesg.topic.matches('p1/projectors/main/command/+'):
|
if mesg.topic.matches('p1/projektorji/{select}/ukaz/+'):
|
||||||
cmd = mesg.topic.value.split("/")[-1]
|
cmd = mesg.topic.value.split("/")[-1]
|
||||||
val = '1' if mesg.payload.decode() == 'ON' else '0'
|
#val = '1' if mesg.payload.decode() == 'ON' else '0'
|
||||||
|
val = mesg.payload.decode()
|
||||||
barcoCmd = f"[{cmdMap[cmd]}{val}]"
|
barcoCmd = f"[{cmdMap[cmd]}{val}]"
|
||||||
print("Received: [" + mesg.topic.value + "] payload: [" + mesg.payload.decode() + "]")
|
print("Received: [" + mesg.topic.value + "] payload: [" + mesg.payload.decode() + "]")
|
||||||
print("Sending command to Barco: " + barcoCmd)
|
print("Sending command to Barco: " + barcoCmd)
|
||||||
writer.write(barcoCmd)
|
writer.write(barcoCmd)
|
||||||
|
|
||||||
|
|
||||||
|
async def barco_telnet_read_status(client, reader, select: str):
|
||||||
async def barco_telnet_read_status(client, reader):
|
|
||||||
while True:
|
while True:
|
||||||
output = await reader.readuntil(b']')
|
output = await reader.readuntil(b']')
|
||||||
raw_response: str = output.decode().strip() # strip not necessary? needed for local netcat testing though
|
raw_response: str = output.decode().strip() # strip not necessary? needed for local netcat testing though
|
||||||
print("Received: " + raw_response + " from Barco")
|
print("Received: " + raw_response + " from Barco (" + select + ')')
|
||||||
parsed = parse_barco_response(raw_response)
|
parsed = parse_barco_response(raw_response)
|
||||||
if parsed == None:
|
if parsed == None:
|
||||||
continue #TODO alert for errors
|
continue #TODO alert for errors
|
||||||
print(f"Updating topic [{parsed[0]}] with value [{parsed[1]}]")
|
print(f"Updating topic [{parsed[0]}] with value [{parsed[1]}]")
|
||||||
await client.publish(f"p1/projectors/main/status/{parsed[0]}", payload=parsed[1])
|
await client.publish(f"p1/projektorji/{select}/status/{parsed[0]}", payload=parsed[1])
|
||||||
|
|
||||||
|
|
||||||
async def barco_telnet_query_status(writer):
|
async def barco_telnet_query_status(writer, select: str):
|
||||||
while True:
|
while True:
|
||||||
for val in cmdMap.values():
|
for val in cmdMap.values():
|
||||||
print(f"Querying Barco with: [{val}?]")
|
print(f"Querying Barco {select} with: [{val}?]")
|
||||||
writer.write(f"[{val}?]" + '\r\n') # TODO test if funny CRLF necessary (it probably gets ignored)
|
writer.write(f"[{val}?]" + '\r\n') # TODO test if funny CRLF necessary (it probably gets ignored)
|
||||||
await asyncio.sleep(1) # sleep between writes necessary, otherwise it gets confused.
|
await asyncio.sleep(1) # sleep between writes necessary, otherwise it gets confused.
|
||||||
# simultaneous commands from control could break this? TODO fix later
|
# simultaneous commands from control could break this? TODO fix later
|
||||||
await asyncio.sleep(1000) # TODO find appropriate period
|
await asyncio.sleep(1000) # TODO find appropriate period
|
||||||
|
|
||||||
|
|
||||||
async def shell(reader, writer):
|
# 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():
|
||||||
|
#conf = toml.load('config.toml')
|
||||||
|
#mainBarcoIP = conf[gethostname()]['projektor_glavni']
|
||||||
|
#sideBarcoIP = conf[gethostname()]['projektor_stranski']
|
||||||
|
mainReader, mainWriter = await telnetlib3.open_connection(mainBarcoIP, 3023)
|
||||||
|
sideReader, sideWriter = await telnetlib3.open_connection(sideBarcoIP, 3023)
|
||||||
async with aiomqtt.Client('localhost', 1883) as client:
|
async with aiomqtt.Client('localhost', 1883) as client:
|
||||||
task_status_query = asyncio.create_task(barco_telnet_query_status(writer))
|
task_status_query_main = asyncio.create_task(barco_telnet_query_status(mainWriter, 'glavni'))
|
||||||
task_status_reader = asyncio.create_task(barco_telnet_read_status(client, reader))
|
task_status_reader_main = asyncio.create_task(barco_telnet_read_status(client, mainReader, 'glavni'))
|
||||||
task_control = asyncio.create_task(barco_telnet_command(client, writer))
|
task_control_main = asyncio.create_task(barco_telnet_command(client, mainWriter, 'glavni'))
|
||||||
await asyncio.gather(task_status_query, task_status_reader, task_control)
|
task_status_query_side = asyncio.create_task(barco_telnet_query_status(sideWriter, 'stranski'))
|
||||||
|
task_status_reader_side = asyncio.create_task(barco_telnet_read_status(client, sideReader, 'stranski'))
|
||||||
|
task_control_side = asyncio.create_task(barco_telnet_command(client, sideWriter, 'stranski'))
|
||||||
|
|
||||||
|
await asyncio.gather(task_status_query_main, task_status_reader_main, task_control_main,
|
||||||
|
task_status_query_side, task_status_reader_side, task_control_side)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
### fuj to, ne tk delat
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
#coro = telnetlib3.open_connection('192.168.192.12', 3023, shell=shell)
|
# if __name__ == '__main__':
|
||||||
coro = telnetlib3.open_connection('localhost', 1234, shell=shell)
|
|
||||||
reader, writer = loop.run_until_complete(coro)
|
# loop = asyncio.get_event_loop()
|
||||||
loop.run_until_complete(writer.protocol.waiter_closed)
|
# #coro = telnetlib3.open_connection('192.168.192.12', 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)
|
||||||
|
|
||||||
|
asyncio.run(main())
|
|
@ -7,11 +7,11 @@ from extron_audio_matrix_telnet_interpreter import *
|
||||||
|
|
||||||
|
|
||||||
async def extron_audio_telnet_status(client, reader):
|
async def extron_audio_telnet_status(client, reader):
|
||||||
while True:
|
while True: #TODO TODO TODO
|
||||||
output = await reader.readuntil(b']')
|
output = await reader.readuntil(b']')
|
||||||
raw_response: str = output.decode().strip() # strip not necessary? needed for local netcat testing though
|
raw_response: str = output.decode().strip() # strip not necessary? needed for local netcat testing though
|
||||||
print("Received: " + raw_response + " from Barco")
|
print("Received: " + raw_response + " from Barco")
|
||||||
parsed = parse_barco_response(raw_response)
|
parsed = eam_telnet_code_to_field(raw_response)
|
||||||
if parsed == None:
|
if parsed == None:
|
||||||
continue # TODO alert for errors
|
continue # TODO alert for errors
|
||||||
print(f"Updating topic [{parsed[0]}] with value [{parsed[1]}]")
|
print(f"Updating topic [{parsed[0]}] with value [{parsed[1]}]")
|
||||||
|
@ -44,14 +44,14 @@ async def extron_audio_telnet_control(client, writer):
|
||||||
# TODO add initial pull
|
# TODO add initial pull
|
||||||
async def shell(reader, writer):
|
async def shell(reader, writer):
|
||||||
async with aiomqtt.Client('localhost', 1883) as client:
|
async with aiomqtt.Client('localhost', 1883) as client:
|
||||||
#task_status_query = asyncio.create_task(extron_audio_telnet_query_status(client, writer, reader))
|
task_status_query = asyncio.create_task(extron_audio_telnet_status(client, writer, reader))
|
||||||
task_control = asyncio.create_task(extron_audio_telnet_control(client, writer))
|
task_control = asyncio.create_task(extron_audio_telnet_control(client, writer))
|
||||||
await asyncio.gather(task_control)
|
await asyncio.gather(task_control)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
coro = telnetlib3.open_connection('localhost', 1234, shell=shell)
|
#coro = telnetlib3.open_connection('localhost', 1234, shell=shell)
|
||||||
#coro = telnetlib3.open_connection('192.168.192.14', 23, shell=shell)
|
coro = telnetlib3.open_connection('192.168.192.14', 23, shell=shell)
|
||||||
reader, writer = loop.run_until_complete(coro)
|
reader, writer = loop.run_until_complete(coro)
|
||||||
loop.run_until_complete(writer.protocol.waiter_closed)
|
loop.run_until_complete(writer.protocol.waiter_closed)
|
||||||
|
|
32
mqtt_init_topics.py
Normal file
32
mqtt_init_topics.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import aiomqtt
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with aiomqtt.Client('localhost', 1883) as client:
|
||||||
|
await client.publish(f"p1/projektorji/glavni/ukaz/power", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/glavni/ukaz/shutter", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/glavni/ukaz/freeze", payload=0, retain=True)
|
||||||
|
|
||||||
|
await client.publish(f"p1/projektorji/stranski/ukaz/power", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/stranski/ukaz/shutter", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/stranski/ukaz/freeze", payload=0, retain=True)
|
||||||
|
|
||||||
|
await client.publish(f"p1/projektorji/glavni/dvigalo/delovni_gor", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/glavni/dvigalo/delovni_dol", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/glavni/dvigalo/servis_gor", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/glavni/dvigalo/servis_dol", payload=0, retain=True)
|
||||||
|
|
||||||
|
await client.publish(f"p1/projektorji/stranski/dvigalo/delovni_gor", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/stranski/dvigalo/delovni_dol", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/stranski/dvigalo/servis_gor", payload=0, retain=True)
|
||||||
|
await client.publish(f"p1/projektorji/stranski/dvigalo/servis_dol", payload=0, retain=True)
|
||||||
|
for i in range(1, 13):
|
||||||
|
await client.publish(f"p1/releji/r{i}/ukaz", payload=0, retain=True)
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue