diff --git a/1og/button.py b/1og/button.py index 5c556de..cb3b80c 100644 --- a/1og/button.py +++ b/1og/button.py @@ -1,20 +1,37 @@ -import json import subprocess import threading import time -import urllib.request -import RPi.GPIO as gpio import board +import digitalio import neopixel +import paho.mqtt.client as mqtt +from adafruit_led_animation import color +from adafruit_led_animation.animation.chase import Chase +from adafruit_led_animation.animation.rainbowcomet import RainbowComet -def ssh(host, name): - def ssh_(host_, name_): - print("[ssh]", f"{name_}@{host_}") - subprocess.check_call(["ssh", f"{name_}@{host_}"], stdin=subprocess.DEVNULL) +class SubStrip: + def __init__(self, strip, pixel: list[int]): + self.strip: neopixel.NeoPixel = strip + self._pixel_map = pixel - threading.Thread(target=ssh_, args=(host, name)).start() + def __len__(self): + return len(self._pixel_map) + + def __setitem__(self, key, value): + if isinstance(key, slice): + for i, v in zip(range(*key.indices(len(self._pixel_map))), value): + self.strip[self._pixel_map[i]] = v + else: + self.strip[self._pixel_map[key]] = value + + def show(self): + self.strip.show() + + def fill(self, color): + for p in self._pixel_map: + self.strip[p] = color class Door: @@ -22,194 +39,244 @@ class Door: self.host = host self.user_open = user_open self.user_close = user_close + self.inprogress = False + + def _ssh(self, host, name): + def _ssh_(host_, name_): + self.inprogress = True + try: + print("[ssh]", f"{name_}@{host_}") + subprocess.check_call(["ssh", f"{name_}@{host_}"], stdin=subprocess.DEVNULL) + except subprocess.CalledProcessError as ex: + print("[ssh]", ex) + self.inprogress = False + + threading.Thread(target=_ssh_, args=(host, name)).start() def open(self): - ssh(self.host, self.user_open) + self._ssh(self.host, self.user_open) def close(self): - ssh(self.host, self.user_close) + self._ssh(self.host, self.user_close) -class ButtonStrip: - def __init__(self, strip, leds): - self.leds = leds - self.strip = strip +class FoodoorMQTT: + def __init__(self, areas): + self.areas = areas + self.message_cbs = [] - def draw(self, color): - for led in self.leds: - self.strip[led] = color + self.client = mqtt.Client() + self.client.on_connect = self.on_connect + self.client.on_message = self.on_message + + self._connect_lock = threading.Condition() + self._data = {} + + def _subscribe(self): + for area in self.areas: + self.client.subscribe(f"foobar/{area}/foodoor/status", 1) + + def connect(self, host="mqtt.chaospott.de"): + try: + self.client.connect(host) + self.client.loop_start() + with self._connect_lock: + self._connect_lock.wait() + except Exception as e: + print(f"Verbindungsfehler zu MQTT-Server: {e}") + + def disconnect(self): + self.client.loop_stop() + + def on_connect(self, client, userdata, flags, rc): + self._subscribe() + with self._connect_lock: + self._connect_lock.notify() + + def on_message(self, client, userdata, msg): + print(f"MQTT-Server Message: {msg.topic} {msg.payload.decode()}") + self._data[msg.topic] = msg.payload.decode() + for cb in self.message_cbs: + cb(msg.topic) + + def get_door(self, area): + return self._data.get(f"foobar/{area}/foodoor/status") + + def is_open(self, area): + return self.get_door(area) == "open" -class ButtonHandler(threading.Thread): - def __init__(self, pin, func, edge='both', bouncetime=100): - super().__init__(daemon=True) +class StateMenu: + OFF = 0 + SHOW_STATE = 1 + CHANGE = 2 + PROGRESS = 3 + RAINBOW = 4 - self.pin = pin - self.func = func - self.edge = edge - self.bouncetime = bouncetime / 1000 + def __init__(self): + self.state = StateMenu.OFF + self.change_target = None + self.target_state = None + self.anim = [] + self.timeout = None + self.next_state = None + self.reset_state() - self.lastpinval = gpio.input(self.pin) - self.lock = threading.Lock() + self.mqtt = FoodoorMQTT(["oben", "unten"]) + self.mqtt.message_cbs.append(lambda *_: self.draw()) + self.mqtt.connect() - def __call__(self, *args): - if not self.lock.acquire(blocking=False): - return + self.strip = neopixel.NeoPixel(board.D18, 36, brightness=0.5, pixel_order=neopixel.GRB, auto_write=False) + self.strip_cellar = SubStrip(self.strip, pixel=list(range(12))) + self.strip_center = SubStrip(self.strip, pixel=list(range(12, 24))) + self.strip_aerie = SubStrip(self.strip, pixel=list(range(24, 36))) - t = threading.Timer(self.bouncetime, self.read, args=args) - t.start() + self.doors = { + "cellar": Door("10.42.1.20", "open", "close"), + "aerie": Door("10.42.1.28", "open", "close"), + } - def read(self, *args): - pinval = gpio.input(self.pin) + def reset_state(self): + self.state = StateMenu.SHOW_STATE + self.change_target = None + self.target_state = None + self.anim.clear() + self.timeout = None + self.next_state = None - if ((pinval == 0 and self.lastpinval == 1 and self.edge in ['falling', 'both']) - or (pinval == 1 and self.lastpinval == 0 and self.edge in ['rising', 'both'])): - self.func(*args) + def button_pressed(self, btn): + if self.state == StateMenu.OFF: + self.set_state(StateMenu.SHOW_STATE) + elif self.state == StateMenu.RAINBOW and btn == "center": + self.reset_state() + elif self.state == StateMenu.SHOW_STATE or self.state == StateMenu.RAINBOW: + if btn == "center": + self.set_state(StateMenu.RAINBOW, 10) + elif btn in ("aerie", "cellar"): + self.set_state(StateMenu.CHANGE, 10) + self.change_target = btn + elif self.state == StateMenu.CHANGE: + if btn == self.change_target: + self.reset_state() + elif self.change_target == "aerie": + if btn == "center": + self.target_state = "open" + elif btn == "cellar": + self.target_state = "close" + elif self.change_target == "cellar": + if btn == "aerie": + self.target_state = "open" + elif btn == "center": + self.target_state = "close" - self.lastpinval = pinval - self.lock.release() + if self.change_target is not None: + self.set_state(StateMenu.PROGRESS, 5) + self.next_state = StateMenu.SHOW_STATE + print(self.change_target, self.target_state) + if self.target_state == "open": + self.doors[self.change_target].open() + elif self.target_state == "close": + self.doors[self.change_target].close() -class ButtonSensor: - def __init__(self, pin, callback): - self.pin = pin + elif self.state == StateMenu.PROGRESS: + pass - gpio.setup(pin, gpio.IN, pull_up_down=gpio.PUD_UP) - self.handler = ButtonHandler(pin, callback, edge='falling', bouncetime=100) - self.handler.start() - gpio.add_event_detect(pin, gpio.BOTH, callback=self.handler) + self.draw() + def set_state(self, state, timeout=None): + self.state = state + self.timeout = None + if timeout is not None: + self.timeout = time.monotonic() + timeout -class ButtonPress(Exception): - def __init__(self, pin): - super().__init__() - self.pin = pin + def draw(self): + self.anim.clear() + if self.state == StateMenu.OFF: + self.strip.fill(color.BLACK) + elif self.state == StateMenu.SHOW_STATE: + self.strip_aerie.fill(color.GREEN if self.mqtt.is_open("oben") else color.RED) + self.strip_cellar.fill(color.GREEN if self.mqtt.is_open("unten") else color.RED) + self.strip_center.fill(color.BLACK) + elif self.state == StateMenu.CHANGE: + if self.change_target == "aerie": + self.anim = [ + Chase(self.strip_center, 0.5, color=color.GREEN, size=1, spacing=11), + Chase(self.strip_cellar, 0.5, color=color.RED, size=1, spacing=11), + ] + elif self.change_target == "cellar": + self.anim = [ + 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_aerie.fill(color.GREEN if self.mqtt.is_open("oben") else color.RED) + self.strip_cellar.fill(color.GREEN if self.mqtt.is_open("unten") else color.RED) + self.anim = [RainbowComet(self.strip_center, 0.05, ring=True)] -class MultiSensor: - def __init__(self, pins): - self.pressed = None - self.buttons = [ButtonSensor(pin, self.press) for pin in pins] + 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() - def press(self, pin): - self.pressed = pin + for a in self.anim: + a.animate(show=False) + self.strip.show() - def sleepSecond(self): - for i in range(100): - if self.pressed is not None: - pressed_pin = self.pressed - self.pressed = None - raise ButtonPress(pressed_pin) - time.sleep(0.01) - - def sleep(self, delay): - for i in range(delay): - self.sleepSecond() - - -class APILoader(threading.Thread): - def __init__(self, delay): - super().__init__() - self.delay = delay - self.data = None - self.loadData() - - def getData(self): - if not self.is_alive(): - print("API thread is dead. Trying to restart...") - self.start() - return self.data - - def run(self): - while True: - try: - self.loadData() - except Exception as e: - print("Unable to load API data:", e) - - time.sleep(self.delay) - - def loadData(self): - # Load api data - print("Loading API data...") - response = urllib.request.urlopen("https://status-v2.chaospott.de/status.json").read() - data = json.loads(response.decode("utf-8")) - - self.data = {door["location"]: not door["value"] for door in data["sensors"]["door_locked"]} + def deinit(self): + self.strip.deinit() + self.mqtt.disconnect() def main(): - print("[*] Initializing...") - # Initialize GPIO board - gpio.setmode(gpio.BCM) + btn_cellar = digitalio.DigitalInOut(board.D22) + btn_cellar.switch_to_input(digitalio.Pull.DOWN) - # Create LED-Strip handle - gpio.setup(4, gpio.IN, pull_up_down=gpio.PUD_UP) - strip = neopixel.NeoPixel(board.D18, 36, brightness=0.5, pixel_order=neopixel.GRB, auto_write=False) + btn_center = digitalio.DigitalInOut(board.D24) + btn_center.switch_to_input(digitalio.Pull.DOWN) - # Create sub-handle for each button handle - leds = { - "cellar": ButtonStrip(strip, list(range(1, 12))), - "center": ButtonStrip(strip, list(range(12, 24))), - "aerie": ButtonStrip(strip, list(range(24, 36))), - } + btn_aerie = digitalio.DigitalInOut(board.D23) + btn_aerie.switch_to_input(digitalio.Pull.DOWN) - # Create button press sensor - buttons = { - 22: "cellar", - 23: "aerie", - 24: "center", - } - sensor = MultiSensor(buttons.keys()) + menu = StateMenu() + btn_locked = False # For waiting while a button is still pressed - doors = { - "aerie": Door("10.42.1.28", "open", "close"), - "cellar": Door("10.42.1.20", "open", "close"), - } - - # Initialize API interface - loader = APILoader(4) - loader.start() - - print("[*] Starting...") - while True: - for led in leds: - leds[led].draw((0, 0, 0)) - - data = loader.getData() - for door in data: - if door in leds: - leds[door].draw((0, 255, 0) if data[door] else (255, 0, 0)) - - strip.show() - - # Wait for a button press - try: - sensor.sleep(2) - except ButtonPress as press: - selection = buttons[press.pin] - - if selection == "aerie": - leds["cellar"].draw((255, 0, 0)) - elif selection == "cellar": - leds["aerie"].draw((255, 0, 0)) + try: + while True: + if not btn_cellar.value: + if not btn_locked: + menu.button_pressed("cellar") + btn_locked = True + elif not btn_center.value: + if not btn_locked: + menu.button_pressed("center") + btn_locked = True + elif not btn_aerie.value: + if not btn_locked: + menu.button_pressed("aerie") + btn_locked = True else: - continue + btn_locked = False - leds["center"].draw((0, 255, 0)) - leds[selection].draw((0, 0, 0)) - - strip.show() - - try: - sensor.sleep(5) - except ButtonPress as press: - action = buttons[press.pin] - - if action == "center": - doors[selection].open() - elif action != selection: - doors[selection].close() + menu.tick() + time.sleep(.01) + except KeyboardInterrupt: + menu.deinit() if __name__ == '__main__': diff --git a/1og/requirements.txt b/1og/requirements.txt new file mode 100644 index 0000000..0fd44c7 --- /dev/null +++ b/1og/requirements.txt @@ -0,0 +1,5 @@ +adafruit-circuitpython-neopixel +adafruit-circuitpython-led-animation +paho-mqtt +RPi.GPIO +rpi-ws281x diff --git a/1og/service.sh b/1og/service.sh index 304ebe4..0829953 100644 --- a/1og/service.sh +++ b/1og/service.sh @@ -3,13 +3,25 @@ FILE=/etc/systemd/system/buttond.service -cat > $FILE << EOF +button=$(realpath button.py) +if [[ ! -f $button ]]; then + echo button.py not found + exit 1 +fi + +vpython=$(realpath venv/)/bin/python +if [[ ! -e $vpython ]]; then + echo venv not found + exit 1 +fi + +cat > "$FILE" << EOF [Unit] Description = buttonctl [Service] Type = simple -ExecStart = /usr/bin/python3 $PWD/button.py +ExecStart = $vpython $button [Install] WantedBy=multi-user.target diff --git a/ug/button.py b/ug/button.py index 9c33007..df9f4fd 100644 --- a/ug/button.py +++ b/ug/button.py @@ -126,6 +126,7 @@ class DoorLocal(Door): class FoodoorMQTT: def __init__(self, areas): self.areas = areas + self.message_cbs = [] self.client = mqtt.Client() self.client.on_connect = self.on_connect @@ -158,6 +159,8 @@ class FoodoorMQTT: def on_message(self, client, userdata, msg): print(f"MQTT-Server Message: {msg.topic} {msg.payload.decode()}") self._data[msg.topic] = msg.payload.decode() + for cb in self.message_cbs: + cb(msg.topic) def get_door(self, area): return self._data.get(f"foobar/{area}/foodoor/status") @@ -179,8 +182,10 @@ class StateMenu: self.anim = [] self.timeout = None self.next_state = None + self.reset_state() self.mqtt = FoodoorMQTT(["oben", "unten"]) + self.mqtt.message_cbs.append(lambda *_: self.draw()) if SIM: self.mqtt.connect("127.0.0.1") else: @@ -233,9 +238,11 @@ class StateMenu: self.draw() - def set_state(self, state, timeout): + def set_state(self, state, timeout=None): self.state = state - self.timeout = time.monotonic() + timeout + self.timeout = None + if timeout is not None: + self.timeout = time.monotonic() + timeout def draw(self): self.anim.clear() @@ -251,7 +258,6 @@ class StateMenu: Chase(self.strip_unten, 0.5, color=color.RED, size=1, spacing=11), ] elif self.state == StateMenu.PROGRESS: - self.strip_oben.fill(color.GREEN if self.mqtt.is_open("oben") else color.RED) if self.change_target == "oben": self.strip_unten.fill(color.GREEN if self.mqtt.is_open("unten") else color.RED) self.anim = [Chase(self.strip_oben, 0.05, size=1,