minor changes and fixes, adjusted frontend
This commit is contained in:
parent
d00651a66c
commit
acc38950e7
11 changed files with 99 additions and 121 deletions
|
@ -24,7 +24,7 @@ if len(portList) < 1:
|
|||
sys.exit("No serial port found")
|
||||
#TODO if multiple ports idk, shouldn't ever happen but still
|
||||
(serport, serdesc, serhwid) = portList[0]
|
||||
|
||||
#TODO if port provided from conf, set, otherwise autodetect magical thing just works
|
||||
|
||||
aser: aioserial.AioSerial = aioserial.AioSerial(
|
||||
port=serport,
|
||||
|
@ -34,6 +34,7 @@ aser: aioserial.AioSerial = aioserial.AioSerial(
|
|||
stopbits=serial.STOPBITS_ONE
|
||||
)
|
||||
|
||||
|
||||
#TODO get this from file da ni hardcoded?
|
||||
# altho itak je ta script za tist specific tse box in so vsi isti
|
||||
mapping_toggles = {
|
||||
|
@ -59,38 +60,47 @@ shades_mapping = {
|
|||
}
|
||||
|
||||
reverse_lookup = {
|
||||
1: "master",
|
||||
2: "audio",
|
||||
3: "projectors",
|
||||
1: ("power", "master", ""),
|
||||
2: ("power", "audio", ""),
|
||||
3: ("power", "projectors", ""),
|
||||
|
||||
5: "main_down",
|
||||
6: "main_up",
|
||||
5: ("platno", "main", "MOVING_DOWN"),
|
||||
6: ("platno", "main", "MOVING_UP"),
|
||||
|
||||
7: "side_down",
|
||||
8: "side_up",
|
||||
7: ("platno", "side", "MOVING_DOWN"),
|
||||
8: ("platno", "side", "MOVING_UP"),
|
||||
|
||||
9: "shades_down",
|
||||
10: "shades_up"
|
||||
9: ("shades", "MOVING_DOWN"),
|
||||
10: ("shades", "MOVING_UP")
|
||||
}
|
||||
|
||||
|
||||
#TODO finish this
|
||||
#TODO add doc comment to every task funciton
|
||||
async def task_status2mqtt(statusClient: aiomqtt.Client):
|
||||
while True:
|
||||
data = await aser.read_until_async()
|
||||
data = data.decode(errors='ignore').strip()
|
||||
print("TSE box sent: " + data)
|
||||
relState = resp_to_relay_state(data)
|
||||
if relState.relay_id == None:
|
||||
continue # TODO handling
|
||||
command = reverse_lookup[relState.relay_id]
|
||||
|
||||
action = relState.state
|
||||
|
||||
if relState.relay_id is None:
|
||||
continue # TODO handling - nebi smelo do tega prit anyway
|
||||
publishTopic = f"{room}/"
|
||||
publishPayload = "ON" if relState.state else "OFF"
|
||||
publishPayload = ""
|
||||
lookup = reverse_lookup[relState.relay_id]
|
||||
if lookup[0] == "power":
|
||||
publishTopic += f"{lookup[0]}/{lookup[1]}/status"
|
||||
publishPayload = '1' if relState.state else '0'
|
||||
elif lookup[0] == "shades":
|
||||
publishTopic += f"{lookup[0]}/status"
|
||||
publishPayload = 'STOPPED' if not relState.state else lookup[1]
|
||||
elif lookup[0] == "platno":
|
||||
publishTopic += f"projectors/{lookup[1]}/{lookup[0]}/status"
|
||||
publishPayload = 'UNKNOWN' if not relState.state else lookup[2]
|
||||
#publishTopic = f"{room}/projectors/{}"
|
||||
#publishPayload = "1" if relState.state else "0"
|
||||
print("Publishing [" + publishPayload + "] to topic [" + publishTopic + "]")
|
||||
await statusClient.publish(publishTopic, payload=publishPayload)
|
||||
await asyncio.sleep(10)
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
|
||||
async def executeAndPublish(mqttClient, pubTopic, pubPayload, relStat):
|
||||
|
@ -99,9 +109,9 @@ async def executeAndPublish(mqttClient, pubTopic, pubPayload, relStat):
|
|||
print("Sending to TSE box: " + setRelayCmd)
|
||||
print(f"Also publishing topic [{pubTopic}] with status [{pubPayload}]")
|
||||
print()
|
||||
#await aser.write_async(bytes(setRelayCmd + '\r\n', "ascii"))
|
||||
await aser.write_async(bytes(setRelayCmd + '\r\n', "ascii"))
|
||||
await mqttClient.publish(pubTopic, payload=pubPayload)
|
||||
await asyncio.sleep(1)
|
||||
await asyncio.sleep(0.1) #TODO probably remove
|
||||
|
||||
|
||||
async def handleTsePower(client, sysSelect, cmd):
|
||||
|
@ -126,10 +136,10 @@ platnoBckgdMoving = False # mucho importante variable prav zares dedoles
|
|||
|
||||
async def platnoTimeout(mqttClient, pubTopic, pubPayload, relStat: RelayState, intent):
|
||||
global platnoBckgdMoving
|
||||
await asyncio.sleep(3) #TODO time actual delay
|
||||
await asyncio.sleep(25) #TODO time actual delay
|
||||
relStat.state = False
|
||||
await executeAndPublish(mqttClient, pubTopic, intent, relStat)
|
||||
platnoBckgdMoving = False
|
||||
platnoBckgdMoving = False #TODO properly document why this is here and what it does
|
||||
|
||||
async def handleTsePlatno(client, proj, cmdType, cmd):
|
||||
global platnoBckgdMoving
|
||||
|
@ -137,7 +147,7 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
|
|||
# {room} {projectors} {[select]} {platno} {move/goto}
|
||||
#projector = topicSplit[2]
|
||||
#command = topicSplit[4]
|
||||
pubTop = f'{room}/projectors/{proj}/status'
|
||||
pubTop = f'{room}/projectors/{proj}/platno/status'
|
||||
|
||||
if platnoBckgdMoving:
|
||||
if cmd == 'STOP':
|
||||
|
@ -148,7 +158,7 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
|
|||
await executeAndPublish(client,pubTop, pubPld, rel2)
|
||||
platnoBckgdMoving = False
|
||||
else:
|
||||
return # ignore any other move commands while moving
|
||||
return #TODO print ignore any other move commands while moving
|
||||
|
||||
#movement cmds
|
||||
elif cmdType == 'move':
|
||||
|
@ -159,10 +169,11 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
|
|||
rel = RelayState(platno_mapping[proj]['DOWN'], True)
|
||||
else:
|
||||
return # in case of invalid input skip
|
||||
platnoBckgdMoving = True
|
||||
platnoBckgdMoving = True #TODO rename to moving, add comment how it works
|
||||
pubPld = 'MOVING'
|
||||
await executeAndPublish(client, pubTop, pubPld, rel)
|
||||
|
||||
#TODO WTF HAPPENS IF YOU SEND UP AND DOWN AT ONCE?? (screenshot?)
|
||||
#TODO daj ignore print ko je locked up
|
||||
elif cmdType == 'goto':
|
||||
# print('received GOTO')
|
||||
rel: RelayState
|
||||
|
@ -197,10 +208,10 @@ async def task_command2serial(controlClient: aiomqtt.Client):
|
|||
cmnd = mesg.payload.decode()
|
||||
#print('Received on: ', topicVal, ' with:', cmnd)
|
||||
#print('bfr')
|
||||
if mesg.topic.matches(f'{room}/projectors/+/platno/+'):
|
||||
if mesg.topic.matches(f'{room}/projectors/+/platno/#'):
|
||||
proj = msgTopic[2] # glavni / stranski
|
||||
cmdType = msgTopic[4] # move / goto
|
||||
await handleTsePlatno(controlClient, proj, cmdType, cmnd)
|
||||
await handleTsePlatno(controlClient, proj, cmdType, cmnd) #TODO odzadi index
|
||||
|
||||
elif mesg.topic.matches(f'{room}/power/+/set'):
|
||||
systype = msgTopic[2]
|
||||
|
@ -213,14 +224,14 @@ async def task_command2serial(controlClient: aiomqtt.Client):
|
|||
continue
|
||||
# code after if block doesnt execute in this case
|
||||
#print("after")
|
||||
await asyncio.sleep(0.01)
|
||||
await asyncio.sleep(0.01) #TODO do we need this? (probably)
|
||||
|
||||
|
||||
async def main():
|
||||
global room
|
||||
conf = toml.load('./malinaConfig.toml')
|
||||
room = conf['global']['room']
|
||||
async with aiomqtt.Client('localhost', 1883) as client:
|
||||
async with aiomqtt.Client('localhost', 1883) as client: #TODO omehčaj kodiranje
|
||||
task_status = asyncio.create_task(task_status2mqtt(client))
|
||||
task_control = asyncio.create_task(task_command2serial(client))
|
||||
await asyncio.gather(task_status, task_control)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue