You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
7.2 KiB
Python
218 lines
7.2 KiB
Python
#!/usr/bin/python3
|
|
|
|
import datetime
|
|
import os
|
|
import shlex
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
import gpiozero
|
|
import toml
|
|
import neopixel
|
|
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
|
|
|
|
# Zahl stellt den Wert in Minuten dar, wie lange kein Gerät erreichbar sein darf dass die Uhr "abgeschalten" wird
|
|
ABSCHALTWERT = 4
|
|
|
|
# LED Mapping
|
|
std0 = [0, 1, 59]
|
|
std1 = [4, 5, 6]
|
|
std2 = [9, 10, 11]
|
|
std3 = [14, 15, 16]
|
|
std4 = [19, 20, 21]
|
|
std5 = [24, 25, 26]
|
|
std6 = [29, 30, 31]
|
|
std7 = [34, 35, 36]
|
|
std8 = [39, 40, 41]
|
|
std9 = [44, 45, 46]
|
|
std10 = [49, 50, 51]
|
|
std11 = [54, 55, 56]
|
|
stdliste = [std0, std1, std2, std3, std4, std5, std6, std7, std8, std9, std10, std11]
|
|
|
|
|
|
# # # # # # # # # #
|
|
# GPIO:
|
|
# # # # # # # # # #
|
|
I_MODE_TASTER = gpiozero.Button(3)
|
|
I_PLUS_TASTER = gpiozero.Button(4)
|
|
I_MINUS_TASTER = gpiozero.Button(14)
|
|
|
|
|
|
# # # # # # # # # #
|
|
# Funktionen
|
|
# # # # # # # # # #
|
|
# Farbkonfiguration
|
|
def rgb_standard():
|
|
rgb_leer = [0, 0, 0]
|
|
rgb_s = [0, 180, 0]
|
|
rgb_min = [32, 178, 170]
|
|
rgb_std = [255, 0, 255]
|
|
rgbconf = {"rgb_leer": rgb_leer, "rgb_s": rgb_s, "rgb_min": rgb_min, "rgb_std": rgb_std}
|
|
return rgbconf
|
|
|
|
|
|
def alle_led(r, g, b, pixels):
|
|
pixels.fill((r, g, b))
|
|
pixels.show()
|
|
|
|
|
|
def led_calc(zeit, stdliste, rgbdict, led_gesetzt, pixels):
|
|
if zeit.hour > 11:
|
|
index = zeit.hour - 12
|
|
else:
|
|
index = zeit.hour
|
|
hour = stdliste[index]
|
|
minute = zeit.minute
|
|
second = zeit.second
|
|
|
|
# Schnittfarben berechnen falls "Zeiger" übereinander liegen
|
|
# Stunde mit Minute vergleichen
|
|
|
|
if bool(set(hour) & {minute}):
|
|
for counter in range(0, 3):
|
|
rgbdict["rgb_min"][counter] = int((rgbdict["rgb_std"][counter] + rgbdict["rgb_min"][counter]) / 2)
|
|
# Stunde mit Sekunde vergleichen
|
|
if set(hour) & {second}:
|
|
for counter in range(0, 3):
|
|
rgbdict["rgb_s"][counter] = int((rgbdict["rgb_std"][counter] + rgbdict["rgb_s"][counter]) / 2)
|
|
# Minute mit Sekunde vergleichen
|
|
if {minute} & {second}:
|
|
for counter in range(0, 3):
|
|
rgbdict["rgb_s"][counter] = int((rgbdict["rgb_min"][counter] + rgbdict["rgb_s"][counter]) / 2)
|
|
led_gesetzt = led_setzen(hour, minute, second, rgbdict, led_gesetzt, pixels)
|
|
return zeit.second, led_gesetzt
|
|
|
|
|
|
def led_setzen(hour, minute, second, rgbdict, led_gesetzt, pixels):
|
|
if not isinstance(led_gesetzt, list):
|
|
led_gesetzt = [led_gesetzt]
|
|
# Stunden setzen
|
|
for stunde in hour:
|
|
pixels[stunde] = rgbdict["rgb_std"][0], rgbdict["rgb_std"][1], rgbdict["rgb_std"][2]
|
|
|
|
# Minute setzen
|
|
pixels[minute] = rgbdict["rgb_min"][0], rgbdict["rgb_min"][1], rgbdict["rgb_min"][2]
|
|
|
|
# Sekunde setzen
|
|
pixels[second] = rgbdict["rgb_s"][0], rgbdict["rgb_s"][1], rgbdict["rgb_s"][2]
|
|
|
|
# Neue Leere LED berechnen
|
|
led_gesetzt_neu = set(hour) | {minute} | {second}
|
|
leerliste = list(set(led_gesetzt) - led_gesetzt_neu)
|
|
for leer in leerliste:
|
|
pixels[leer] = rgbdict["rgb_leer"][0], rgbdict["rgb_leer"][1], rgbdict["rgb_leer"][2]
|
|
|
|
pixels.show()
|
|
return list(led_gesetzt_neu)
|
|
|
|
|
|
def shutdown():
|
|
cmd = "sudo shutdown now"
|
|
cmd = shlex.split(cmd)
|
|
subprocess.call(cmd)
|
|
|
|
|
|
# Threads
|
|
def check_anwesenheit(pixels):
|
|
"""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
|
|
return (0 | !0) 0 wenn erreichbar"""
|
|
befehl = "ping -c2 -W1 {}".format(ip)
|
|
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())
|
|
status_anwesend_liste = []
|
|
delta = datetime.timedelta(seconds=301)
|
|
last = datetime.datetime.now()
|
|
status = {}
|
|
while True:
|
|
now = datetime.datetime.now()
|
|
# Status der IP Adressen 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
|
|
if status["anwesend"]:
|
|
status_anwesend_liste = [] # Geraet von anwesend erreichbar
|
|
helligkeit = LED_BRIGHTNESS
|
|
elif not status["anwesend"] and not status["dimmen"]: # Wenn kein Geraet erreichbar ist
|
|
if len(status_anwesend_liste) < ABSCHALTWERT + 1:
|
|
status_anwesend_liste.append(status["anwesend"])
|
|
print("Nichts los daheim die ", len(status_anwesend_liste), ".")
|
|
if len(status_anwesend_liste) > ABSCHALTWERT:
|
|
helligkeit = 0
|
|
if status["dimmen"]:
|
|
helligkeit = LED_BRIGHTNESS * 0.5
|
|
if now.hour < 5 and not status["dimmen"]:
|
|
helligkeit = 0
|
|
pixels.brightness = helligkeit
|
|
last = datetime.datetime.now()
|
|
delta = now - last
|
|
time.sleep(30)
|
|
|
|
|
|
def main():
|
|
lastsecond = None
|
|
led_gesetzt = 0
|
|
rgbconf = rgb_standard()
|
|
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,))
|
|
thread_check_wlan.start()
|
|
alle_led(rgbconf["rgb_leer"][0], rgbconf["rgb_leer"][1], rgbconf["rgb_leer"][2], pixels)
|
|
while True:
|
|
try:
|
|
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()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|