Narete stvari da delajo

This commit is contained in:
Miha Frangež 2025-09-12 17:43:50 +02:00
parent 64a10b0512
commit 29b2beca5a
45 changed files with 809 additions and 1600 deletions

View file

@ -4,14 +4,17 @@ import aioserial
import aiomqtt
from tse_serial_interpreter import *
from dataclasses import dataclass
import os
import os
import sys
import toml
import toml
import serial.tools.list_ports
#GLOBALS
room = 'undefined' #TODO make be do get fronm file of configuration
# GLOBALS
room: str
platnoBckgdMoving = {
'main': False,
'side': False,
}
# serPath = '/dev/serial/by-id/'
# devLst = os.listdir(serPath)
@ -20,11 +23,16 @@ room = 'undefined' #TODO make be do get fronm file of configuration
# serDev = devLst[0]
portList = serial.tools.list_ports.comports()
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 CHECK FOR TTYUSB0
#TODO if port provided from conf, set, otherwise autodetect magical thing just works
candidates = ('/dev/ttyUSB0', '/dev/ttyACM0', '/dev/ttyUSB1', '/dev/ttyACM1')
serport = ''
# Find first serial port that exists
for port in portList:
if port.device in candidates:
serport = port.device
break
else:
print(portList)
sys.exit("No serial port found")
aser: aioserial.AioSerial = aioserial.AioSerial(
port=serport,
@ -32,10 +40,8 @@ aser: aioserial.AioSerial = aioserial.AioSerial(
parity=serial.PARITY_NONE,
bytesize=serial.EIGHTBITS,
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 = {
"master": 1,
@ -63,10 +69,10 @@ reverse_lookup = {
1: ("power", "master", ""),
2: ("power", "audio", ""),
3: ("power", "projectors", ""),
5: ("platno", "main", "MOVING_DOWN"),
6: ("platno", "main", "MOVING_UP"),
7: ("platno", "side", "MOVING_DOWN"),
8: ("platno", "side", "MOVING_UP"),
@ -113,7 +119,7 @@ async def executeAndPublish(mqttClient, pubTopic, pubPayload, relStat):
await mqttClient.publish(pubTopic, payload=pubPayload)
await asyncio.sleep(0.1) #TODO probably remove
async def handleTsePower(client, sysSelect, cmd):
rel = RelayState(mapping_toggles[sysSelect], cmd == '1')
await executeAndPublish(client, f'{room}/power/{sysSelect}/status', cmd, rel)
@ -129,13 +135,8 @@ async def handleTseSencilo(client, cmd):
await executeAndPublish(client, topicPub, "MOVING_DOWN", rel)
else:
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['UP'], False))
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['DOWN'], False))
await executeAndPublish(client, topicPub, "STOPPED", RelayState(shades_mapping['DOWN'], False))
platnoBckgdMoving = {
'main': False,
'side': False,
} # mucho importante variable prav zares dedoles
async def platnoTimeout(mqttClient, pubTopic, pubPayload, relStat: RelayState, intent, select):
global platnoBckgdMoving
@ -144,12 +145,10 @@ async def platnoTimeout(mqttClient, pubTopic, pubPayload, relStat: RelayState, i
await executeAndPublish(mqttClient, pubTopic, intent, relStat)
platnoBckgdMoving[select] = False #TODO properly document why this is here and what it does
async def handleTsePlatno(client, proj, cmdType, cmd):
global platnoBckgdMoving
#topicSplit = tval.split('/')
# {room} {projectors} {[select]} {platno} {move/goto}
#projector = topicSplit[2]
#command = topicSplit[4]
pubTop = f'{room}/projectors/{proj}/platno/status'
if not (proj == "main" or proj == "side"):
return
@ -203,21 +202,13 @@ async def handleTsePlatno(client, proj, cmdType, cmd):
print('unknown command')
async def task_command2serial(controlClient: aiomqtt.Client):
#print('oge')
await controlClient.subscribe(f"{room}/#")
#print('ogee')
#async with controlClient.messages as msgs:
async for mesg in controlClient.messages:
#print('oge')
#print(mesg, mesg.topic)
#mesg: aiomqtt.Message
async for mesg in controlClient.messages:
topicVal = mesg.topic.value
msgTopic = mesg.topic.value.split('/')
cmnd = mesg.payload.decode()
#print('Received on: ', topicVal, ' with:', cmnd)
#print('bfr')
if mesg.topic.matches(f'{room}/projectors/+/platno/move') or mesg.topic.matches(f'{room}/projectors/+/platno/goto'):
proj = msgTopic[-3]
cmdType = msgTopic[-1] # move / goto
@ -236,7 +227,7 @@ async def task_command2serial(controlClient: aiomqtt.Client):
await handleTseSencilo(controlClient, cmnd)
else:
continue
continue
# code after if block doesnt execute in this case
#print("after")
await asyncio.sleep(0.01) #TODO do we need this? (probably)
@ -244,12 +235,17 @@ async def task_command2serial(controlClient: aiomqtt.Client):
async def main():
global room
conf = toml.load('./malinaConfig.toml')
config_file = os.getenv('MM_CONFIG_PATH', './malinaConfig.toml')
conf = toml.load(config_file)
room = conf['global']['room']
async with aiomqtt.Client('localhost', 1883) as client: #TODO omehčaj kodiranje
mqttHost = conf['global']['mqttHost']
mqttPort = conf['global']['mqttPort']
async with aiomqtt.Client(mqttHost, mqttPort) as client:
task_status = asyncio.create_task(task_status2mqtt(client))
task_control = asyncio.create_task(task_command2serial(client))
await asyncio.gather(task_status, task_control)
if __name__ == '__main__':
asyncio.run(main())
asyncio.run(main())