diff --git a/uhr.py b/uhr.py index 7482221..edf69c9 100644 --- a/uhr.py +++ b/uhr.py @@ -4,7 +4,6 @@ import datetime import os import shlex import subprocess -import sys import threading import time @@ -17,9 +16,8 @@ import board # LED strip configuration LED_COUNT = 60 # Number of LED pixels. LED_PIN = board.D18 # GPIO pin connected to the pixels (must support PWM!). -LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) LED_PIXEL_ORDER = neopixel.GRB # Strip type and colour ordering -LED_BRIGHTNESS = 0.2 +LED_BRIGHTNESS = 0.1 # Zahl stellt den Wert in Minuten dar, wie lange kein Gerät erreichbar sein darf dass die Uhr "abgeschalten" wird ABSCHALTWERT = 4 @@ -40,6 +38,16 @@ std11 = [54, 55, 56] stdliste = [std0, std1, std2, std3, std4, std5, std6, std7, std8, std9, std10, std11] +class Uhr: + def __init__(self, pixels): + self.helligkeit = pixels.brightness + self.pixels = pixels + + def set_helligkeit(self, helligkeit): + self.helligkeit = helligkeit + self.pixels.brightness = self.helligkeit + + # # # # # # # # # # # GPIO: # # # # # # # # # # @@ -123,53 +131,41 @@ def shutdown(): # Threads -def check_anwesenheit(pixels): +def check_anwesenheit(uhr): """Funktion, welche als eigener Thread laeuft, um selbststaendig in einem gewissenen Intervall alle Geraete in der Toml Liste zu pingen arg: Objekt des neopixel LED Ringes toml File: status "anwesend", ist kein Geraet von "anwesend" oder "dimmen" erreichbar, LED Helligkeit auf 0 sobald eine Adresse von status "dimmen" erreichbar ist, wird die Helligkeit verringert""" - def ping_wlan(ip): - """pingt die IP 2x an + + def ping_device(mac): + """pingt das Gerät 2x an return (0 | !0) 0 wenn erreichbar""" - befehl = "ping -c2 -W1 {}".format(ip) + befehl = "sudo arping -c2 {}".format(mac) cmd = shlex.split(befehl) return subprocess.call(cmd) - def ping_bt(bt): - """pingt die IP 2x an - return (0 | !0) 0 wenn erreichbar""" - befehl = "sudo /usr/bin/l2ping -c1 -t1 {}".format(bt) - cmd = shlex.split(befehl) - return subprocess.call(cmd) - # Tomlfile mit den IP Adressen einlesen pfad = os.path.abspath(os.path.dirname(__file__)) configfile = os.path.join(pfad, "bt_wlan.toml") with open(configfile) as conffile: - wlanliste = toml.loads(conffile.read()) + wlanliste = toml.loads(conffile.read())["arping"] status_anwesend_liste = [] delta = datetime.timedelta(seconds=301) last = datetime.datetime.now() status = {} while True: now = datetime.datetime.now() - # Status der IP Adressen ermitteln + # Status der Geräte ermitteln if delta.seconds > 300: for key_status in wlanliste.keys(): - for key_funkart in wlanliste[key_status].keys(): - for ip in wlanliste[key_status][key_funkart]: - if key_funkart == "wlan": - status_return = ping_wlan(ip) - elif key_funkart == "bt": - status_return = ping_bt(ip) - else: - status_return = False - if not status_return: - status[key_status] = True - break - else: - status[key_status] = False + for mac in wlanliste[key_status]: + status_return = ping_device(mac) + if not status_return: + status[key_status] = True + break + else: + status[key_status] = False if status["anwesend"]: status_anwesend_liste = [] # Geraet von anwesend erreichbar helligkeit = LED_BRIGHTNESS @@ -183,7 +179,7 @@ def check_anwesenheit(pixels): helligkeit = LED_BRIGHTNESS * 0.5 if now.hour < 5 and not status["dimmen"]: helligkeit = 0 - pixels.brightness = helligkeit + uhr.set_helligkeit(helligkeit) last = datetime.datetime.now() delta = now - last time.sleep(30) @@ -196,21 +192,20 @@ def main(): pixels = neopixel.NeoPixel(LED_PIN, LED_COUNT, brightness=LED_BRIGHTNESS, auto_write=False, pixel_order=LED_PIXEL_ORDER) - thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(pixels,)) + uhr = Uhr(pixels) + thread_check_wlan = threading.Thread(target=check_anwesenheit, args=(uhr,)) thread_check_wlan.start() alle_led(rgbconf["rgb_leer"][0], rgbconf["rgb_leer"][1], rgbconf["rgb_leer"][2], pixels) - while True: - try: + try: + while True: rgbconf = rgb_standard() now = datetime.datetime.now() if lastsecond != now.second: lastsecond, led_gesetzt = led_calc(now, stdliste, rgbconf, led_gesetzt, pixels) time.sleep(0.2) - except KeyboardInterrupt: - print("KeyboardInterrupt") - pixels.brightness = 0 - alle_led(0, 0, 0, pixels) - sys.exit() + finally: + pixels.brightness = 0 + alle_led(0, 0, 0, pixels) if __name__ == "__main__":