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

@ -6,6 +6,7 @@ import aiomqtt
import asyncio
#from i2cpy import I2C
from smbus2 import SMBus
import toml
#import i2cpy
#i2cset -y 1 0x11 0x11 0x42
#set i2c address from 0x11 to 0x42
@ -14,8 +15,11 @@ from smbus2 import SMBus
#Device.pin_factory = gpiozero.pins.mock.MockFactory()
#relays = [LED(17), LED(27), LED(22), LED(23), LED(5), LED(6), LED(24), LED(25)]
relayBoardMain = 0x42
relayBoardSide = 0x43
main_I2C_addr = 0 #default
side_I2C_addr = 0
room = ""
use_offset = False
""" 0 1 2 3 """
relayMasks = [0b0001, 0b0010, 0b0100, 0b1000] #probably ne rabim
@ -29,8 +33,8 @@ relMapping = {
}
currentState = {
'glavni': 0b0000,
'stranski': 0b0000
'main': 0b0000,
'side': 0b0000
}
async def msgRelayBoard(projSelect, command, state: bool):
@ -39,10 +43,16 @@ async def msgRelayBoard(projSelect, command, state: bool):
# register 0x10 za releje
I2CAddr = 0x42 #glavni
if projSelect == 'stranski':
I2CAddr += 0x1
I2CAddr = 0 #glavni
match projSelect:
case 'main':
I2CAddr = main_I2C_addr
case 'side':
I2CAddr = side_I2C_addr
#return #TODO TEMPORARY, REMOVE LATER#
case default:
return #ignore if unknown position
maskShift = relMapping[command]
mask = (1 << maskShift)
if state:
@ -51,8 +61,8 @@ async def msgRelayBoard(projSelect, command, state: bool):
currentState[projSelect] = currentState[projSelect] & ~mask
bus.write_byte_data(I2CAddr, 0x10, currentState[projSelect])
print('testirovano jako')
print("Command sent")
#print('testirovano jako')
"""
@ -71,32 +81,42 @@ SIDE: 4 5 6 7
#dej like bolš to podukumentiraj or smth
async def task_command2relays(controlClient: aiomqtt.Client):
global room
#relayCtrl = lambda cmd, relay: relays[relay].on() if cmd == 1 else relays[relay].off()
#relayCtrl = lambda cmd, relay: [relays[r].on() if cmd == 1 and r == relay else relays[r].off for r in range(len(relays))]
relayCtrl = lambda x, y: print(x, y)
await controlClient.subscribe("p01/projectors/+/lift/#")
await controlClient.subscribe(f"{room}/projectors/+/lift/#")
msgs = controlClient.messages
async for mesg in msgs:
mesg: aiomqtt.Message
if mesg.topic.matches('p01/projectors/+/lift/move/#'):
if mesg.topic.matches(f'{room}/projectors/+/lift/move/#'):
msgTopicArr = mesg.topic.value.split('/')
state = mesg.payload.decode()
print("Received: " + str(msgTopicArr) + " payload: [" + state + "]")
#testCase = (msgTopicArr[2], msgTopicArr[4])
projSel = msgTopicArr[2]
if projSel != 'glavni' and projSel != 'stranski':
continue
if projSel != 'main' and projSel != 'side':
continue #TODO error hnadling?
command = msgTopicArr[5]
await msgRelayBoard(projSel, command, state == '1')
await controlClient.publish(f'{room}/projectors/{projSel}/lift/status', payload="", qos=1, retain=True)
#print("Pushing \'off\' to other relays to prevent conflicts")
await asyncio.sleep(0.01)
async def main():
global main_I2C_addr, side_I2C_addr, room
conf = toml.load('./malinaConfig.toml')
projMotors = conf.get("projector_motors")
mainMotor = projMotors.get("main")
sideMotor = projMotors.get("side")
main_I2C_addr = mainMotor['i2c_address']
side_I2C_addr = sideMotor['i2c_address']
room = conf["global"]["room"]
async with aiomqtt.Client('localhost', 1883) as client:
task_control = asyncio.create_task(task_command2relays(client))
await asyncio.gather(task_control)