Added lighting preset recall on backend and frontend, fixed things in other scripts - things should *mostly* be working now

This commit is contained in:
katsu 2025-06-19 15:21:44 +02:00
parent 61a8aa8ebc
commit d00651a66c
14 changed files with 851 additions and 74 deletions

View file

@ -9,7 +9,9 @@ import sys
import toml
import serial.tools.list_ports
room = 'p01' #TODO make be do get fronm file of configuration
#GLOBALS
room = 'undefined' #TODO make be do get fronm file of configuration
# serPath = '/dev/serial/by-id/'
# devLst = os.listdir(serPath)
@ -18,7 +20,7 @@ room = 'p01' #TODO make be do get fronm file of configuration
# serDev = devLst[0]
portList = serial.tools.list_ports.comports()
if portList < 1:
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]
@ -41,19 +43,19 @@ mapping_toggles = {
}
# 4 is not connected to anything
platno_mapping = {
"glavni": {
"dol": 5,
"gor": 6
"main": {
"DOWN": 5,
"UP": 6
},
"stranski": {
"dol": 7,
"gor": 8
"side": {
"DOWN": 7,
"UP": 8
}
}
shades_mapping = {
"dol": 9,
"gor": 10
"DOWN": 9,
"UP": 10
}
reverse_lookup = {
@ -61,14 +63,14 @@ reverse_lookup = {
2: "audio",
3: "projectors",
5: "glavni_dol",
6: "glavni_gor",
5: "main_down",
6: "main_up",
7: "glavni_dol",
8: "glavni_gor",
7: "side_down",
8: "side_up",
9: "shades_dol",
10: "shades_gor"
9: "shades_down",
10: "shades_up"
}
@ -84,11 +86,11 @@ async def task_status2mqtt(statusClient: aiomqtt.Client):
action = relState.state
#publishTopic = f"{room}/"
#publishPayload = "ON" if relState.state else "OFF"
#print("Publishing [" + publishPayload + "] to topic [" + publishTopic + "]")
#await statusClient.publish(publishTopic, payload=publishPayload)
await asyncio.sleep(0.01)
publishTopic = f"{room}/"
publishPayload = "ON" if relState.state else "OFF"
print("Publishing [" + publishPayload + "] to topic [" + publishTopic + "]")
await statusClient.publish(publishTopic, payload=publishPayload)
await asyncio.sleep(10)
async def executeAndPublish(mqttClient, pubTopic, pubPayload, relStat):
@ -97,26 +99,27 @@ 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)
async def handleTsePower(client, sysSelect, cmd):
rel = RelayState(mapping_toggles[sysSelect], cmd == '1')
await executeAndPublish(client, f'{room}/power/{sysSelect}/platno/status', cmd, rel)
await executeAndPublish(client, f'{room}/power/{sysSelect}/status', cmd, rel)
async def handleTseSencilo(client, cmd):
#relName = tval.split('/')[3]
topicPub = f'{room}/firanki/status'
if cmd == "MOVE_UP":
rel = RelayState(shades_mapping['gor'], True)
rel = RelayState(shades_mapping['UP'], True)
await executeAndPublish(client, topicPub, "MOVING_UP", rel)
elif cmd == "MOVE_DOWN":
rel = RelayState(shades_mapping['dol'], True)
rel = RelayState(shades_mapping['DOWN'], True)
await executeAndPublish(client, topicPub, "MOVING_DOWN", rel)
else:
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['gor'], False))
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['dol'], False))
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['UP'], False))
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['DOWN'], False))
platnoBckgdMoving = False # mucho importante variable prav zares dedoles
@ -139,8 +142,8 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
if platnoBckgdMoving:
if cmd == 'STOP':
pubPld = 'UNKNOWN'
rel1 = RelayState(platno_mapping[proj]['gor'], False)
rel2 = RelayState(platno_mapping[proj]['dol'], False)
rel1 = RelayState(platno_mapping[proj]['UP'], False)
rel2 = RelayState(platno_mapping[proj]['DOWN'], False)
await executeAndPublish(client,pubTop, pubPld, rel1)
await executeAndPublish(client,pubTop, pubPld, rel2)
platnoBckgdMoving = False
@ -151,9 +154,9 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
elif cmdType == 'move':
rel: RelayState
if cmd == 'UP':
rel = RelayState(platno_mapping[proj]['gor'], True)
rel = RelayState(platno_mapping[proj]['UP'], True)
elif cmd == 'DOWN':
rel = RelayState(platno_mapping[proj]['dol'], True)
rel = RelayState(platno_mapping[proj]['DOWN'], True)
else:
return # in case of invalid input skip
platnoBckgdMoving = True
@ -164,10 +167,10 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
# print('received GOTO')
rel: RelayState
if cmd == 'UP':
rel = RelayState(platno_mapping[proj]['gor'], True)
rel = RelayState(platno_mapping[proj]['UP'], True)
intent = 'UP'
elif cmd == 'DOWN':
rel = RelayState(platno_mapping[proj]['dol'], True)
rel = RelayState(platno_mapping[proj]['DOWN'], True)
intent = 'DOWN'
else:
return # in case of invalid input skip
@ -186,6 +189,7 @@ async def task_command2serial(controlClient: aiomqtt.Client):
#async with controlClient.messages as msgs:
async for mesg in controlClient.messages:
#print('oge')
#print(mesg, mesg.topic)
#mesg: aiomqtt.Message
topicVal = mesg.topic.value
@ -193,16 +197,16 @@ 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)
elif mesg.topic.matches(f'{room}/power/#'):
elif mesg.topic.matches(f'{room}/power/+/set'):
systype = msgTopic[2]
await handleTsePower(controlClient, systype, cmnd)
elif mesg.topic.matches(f'{room}/firanki/move/#'):
elif mesg.topic.matches(f'{room}/firanki/move'):
await handleTseSencilo(controlClient, cmnd)
else:
@ -213,6 +217,9 @@ async def task_command2serial(controlClient: aiomqtt.Client):
async def main():
global room
conf = toml.load('./malinaConfig.toml')
room = conf['global']['room']
async with aiomqtt.Client('localhost', 1883) as client:
task_status = asyncio.create_task(task_status2mqtt(client))
task_control = asyncio.create_task(task_command2serial(client))