Same in 1og
This commit is contained in:
1 parent
cb4220458d
commit
c0b36e8035
1 file changed
+39
-20
+39
-20
@@ -61,9 +61,12 @@ class Door:
|
||||
|
||||
|
||||
class FoodoorMQTT:
|
||||
MESSAGE_TIMEOUT = 24 * 60 * 60
|
||||
|
||||
def __init__(self, areas):
|
||||
self.areas = areas
|
||||
self.message_cbs = []
|
||||
self.last_message_at = time.monotonic()
|
||||
|
||||
self.client = mqtt.Client()
|
||||
self.client.on_connect = self.on_connect
|
||||
@@ -94,6 +97,7 @@ class FoodoorMQTT:
|
||||
self._connect_lock.notify()
|
||||
|
||||
def on_message(self, client, userdata, msg):
|
||||
self.last_message_at = time.monotonic()
|
||||
print(f"MQTT-Server Message: {msg.topic} {msg.payload.decode()}")
|
||||
self._data[msg.topic] = msg.payload.decode()
|
||||
for cb in self.message_cbs:
|
||||
@@ -120,6 +124,7 @@ class StateMenu:
|
||||
self.anim = []
|
||||
self.timeout = None
|
||||
self.next_state = None
|
||||
self._force_redraw = True
|
||||
self.reset_state()
|
||||
|
||||
self.strip = neopixel.NeoPixel(board.D12, 36, brightness=0.5, pixel_order=neopixel.GRB, auto_write=False)
|
||||
@@ -140,7 +145,7 @@ class StateMenu:
|
||||
}
|
||||
|
||||
self.mqtt = FoodoorMQTT(["oben", "unten"])
|
||||
self.mqtt.message_cbs.append(lambda *_: self.draw())
|
||||
self.mqtt.message_cbs.append(lambda *_: self.redraw())
|
||||
self.mqtt.connect()
|
||||
|
||||
def reset_state(self):
|
||||
@@ -150,6 +155,7 @@ class StateMenu:
|
||||
self.anim.clear()
|
||||
self.timeout = None
|
||||
self.next_state = None
|
||||
self._force_redraw = True
|
||||
|
||||
def button_pressed(self, btn):
|
||||
if self.state == StateMenu.OFF:
|
||||
@@ -189,7 +195,7 @@ class StateMenu:
|
||||
elif self.state == StateMenu.PROGRESS:
|
||||
pass
|
||||
|
||||
self.draw()
|
||||
self.redraw()
|
||||
|
||||
def set_state(self, state, timeout=None):
|
||||
self.state = state
|
||||
@@ -197,9 +203,24 @@ class StateMenu:
|
||||
if timeout is not None:
|
||||
self.timeout = time.monotonic() + timeout
|
||||
|
||||
def draw(self):
|
||||
def redraw(self):
|
||||
self._force_redraw = True
|
||||
|
||||
def _draw(self):
|
||||
self.anim.clear()
|
||||
|
||||
if self.state == StateMenu.PROGRESS or (self.change_target and self.doors[self.change_target].inprogress):
|
||||
self.strip_center.fill(color.BLACK)
|
||||
if self.change_target == "aerie":
|
||||
self.strip_cellar.fill(color.GREEN if self.mqtt.is_open("unten") else color.RED)
|
||||
self.anim = [Chase(self.strip_aerie, 0.05, size=1,
|
||||
color=color.GREEN if self.target_state == "open" else color.RED)]
|
||||
elif self.change_target == "cellar":
|
||||
self.strip_aerie.fill(color.GREEN if self.mqtt.is_open("oben") else color.RED)
|
||||
self.anim = [Chase(self.strip_cellar, 0.05, size=1,
|
||||
color=color.GREEN if self.target_state == "open" else color.RED)]
|
||||
return
|
||||
|
||||
if self.state == StateMenu.OFF:
|
||||
self.strip.fill(color.BLACK)
|
||||
elif self.state == StateMenu.SHOW_STATE:
|
||||
@@ -217,28 +238,23 @@ class StateMenu:
|
||||
Chase(self.strip_aerie, 0.5, color=color.GREEN, size=1, spacing=11),
|
||||
Chase(self.strip_center, 0.5, color=color.RED, size=1, spacing=11),
|
||||
]
|
||||
elif self.state == StateMenu.PROGRESS:
|
||||
self.strip_center.fill(color.BLACK)
|
||||
if self.change_target == "aerie":
|
||||
self.strip_cellar.fill(color.GREEN if self.mqtt.is_open("unten") else color.RED)
|
||||
self.anim = [Chase(self.strip_aerie, 0.05, size=1,
|
||||
color=color.GREEN if self.target_state == "open" else color.RED)]
|
||||
elif self.change_target == "cellar":
|
||||
self.strip_aerie.fill(color.GREEN if self.mqtt.is_open("oben") else color.RED)
|
||||
self.anim = [Chase(self.strip_cellar, 0.05, size=1,
|
||||
color=color.GREEN if self.target_state == "open" else color.RED)]
|
||||
elif self.state == StateMenu.RAINBOW:
|
||||
self.strip.fill(color.BLACK)
|
||||
self.anim = [RainbowComet(self.strip_loop, 0.05, ring=True)]
|
||||
|
||||
def tick(self):
|
||||
if self.timeout and self.timeout < time.monotonic() and not (self.change_target and self.doors[self.change_target].inprogress):
|
||||
if self.next_state is not None:
|
||||
self.set_state(self.next_state, 10)
|
||||
self.next_state = None
|
||||
else:
|
||||
self.reset_state()
|
||||
self.draw()
|
||||
if not (self.change_target and self.doors[self.change_target].inprogress): # Freeze timeout while in progress
|
||||
if self.timeout and self.timeout < time.monotonic():
|
||||
if self.next_state is not None:
|
||||
self.set_state(self.next_state, 10)
|
||||
self.next_state = None
|
||||
self.redraw()
|
||||
else:
|
||||
self.reset_state()
|
||||
|
||||
if self._force_redraw:
|
||||
self._force_redraw = False
|
||||
self._draw()
|
||||
|
||||
for a in self.anim:
|
||||
a.animate(show=False)
|
||||
@@ -264,6 +280,9 @@ def main():
|
||||
|
||||
try:
|
||||
while True:
|
||||
if time.monotonic() - menu.mqtt.last_message_at >= menu.mqtt.MESSAGE_TIMEOUT:
|
||||
raise RuntimeError("No MQTT message received for 24 hours")
|
||||
|
||||
if btn_cellar.value:
|
||||
if not btn_locked:
|
||||
menu.button_pressed("cellar")
|
||||
|
||||
Reference in new issue
Block a user