diff --git a/buba/__main__.py b/buba/__main__.py index 29cf283..1984073 100644 --- a/buba/__main__.py +++ b/buba/__main__.py @@ -5,6 +5,7 @@ from bottle_log import LoggingPlugin from bottle_websocket import websocket, GeventWebSocketServer from geventwebsocket.websocket import WebSocket +from buba.animations.clubassistant import Clubassistant from buba.animations.dbf import DBFAnimation from buba.animations.icalevents import IcalEvents from buba.animations.snake import SnakeAnimation @@ -36,8 +37,10 @@ animator.add(DBFAnimation, ds100="AHS", station="Altona", count=9) animator.add(IcalEvents, url="https://cloud.hamburg.ccc.de/remote.php/dav/public-calendars/QJAdExziSnNJEz5g?export", title="CCCHH Events") animator.add(Spaceapi, "https://spaceapi.hamburg.ccc.de", "CCCHH") +animator.add(Clubassistant, "https://club-assistant.ccchh.net", config.clubassistant_token) animator.add(SnakeAnimation) + @app.route("/static/") def server_static(filepath): return static_file(filepath, root=config.staticpath) diff --git a/buba/animations/clubassistant.py b/buba/animations/clubassistant.py new file mode 100644 index 0000000..06984ee --- /dev/null +++ b/buba/animations/clubassistant.py @@ -0,0 +1,62 @@ +""" +Data from club-assistant.hamburg.ccc.de +""" +import json +from threading import Thread +from time import sleep + +import requests + +from buba.bubaanimator import BubaAnimation + +default_endpoints = [ + { + "endpoint": "sensor.total_measured_power", + "name": "Current Power", + }, + { + "endpoint": "sensor.hauptraum_scd41_co2", + "name": "Hauptraum CO2", + }, + { + "endpoint": "sensor.werkstatt_scd41_co2", + "name": "Werkstatt CO2", + }, + { + "endpoint": "sensor.temperatur_und_feuchtigkeitssensor_hauptraum_humidity", + "name": "Hauptraum Luftfeuchte", + }, + { + "endpoint": "sensor.temperatur_und_feuchtigkeitssensor_lotschlauch_humidity", + "name": "Lötschlauch Luftfeuchte", + } +] + +class Clubassistant(BubaAnimation): + def __init__(self, buba, url, token, endpoints=None): + if endpoints is None: + endpoints = default_endpoints + super().__init__(buba) + self.url = url + self.token = token + self.endpoints = endpoints + self.data = [] + self.load() + Thread(target=self.update, daemon=True).start() + + def load(self): + data = [] + for e in self.endpoints: + res = requests.get(self.url + "/api/states/" + e["endpoint"], headers={'Authorization': 'Bearer ' + self.token}) + res.raise_for_status() + js = json.loads(res.text) + data.append([e["name"], f"{js['state']}{js['attributes']['unit_of_measurement']}"]) + self.data = data + + def update(self): + while True: + self.load() + sleep(60) + + def run(self): + self.pages("Club Assistant", self.data) \ No newline at end of file diff --git a/buba/appconfig.py b/buba/appconfig.py index 9124a9d..8f96d3f 100644 --- a/buba/appconfig.py +++ b/buba/appconfig.py @@ -16,6 +16,7 @@ class AppConfig: self.url = getenv('BUBA_URL', 'http://localhost:3000') (self.listen_host, self.listen_port) = getenv('BUBA_LISTEN', '127.0.0.1:3000').split(':') self.serial = getenv('BUBA_SERIAL', '/dev/ttyUSB0') + self.clubassistant_token = getenv('BUBA_CLUBASSISTANT_TOKEN', None) if self.debug is not None and self.debug.lower not in ('0', 'f', 'false'): self.debug = True diff --git a/buba/bubaanimator.py b/buba/bubaanimator.py index 27b9fd9..6ea2272 100644 --- a/buba/bubaanimator.py +++ b/buba/bubaanimator.py @@ -1,4 +1,5 @@ import logging +import random from dataclasses import dataclass from datetime import datetime, timedelta from itertools import islice @@ -139,14 +140,20 @@ class BubaAnimator: Thread(target=self.run, daemon=True).start() def run(self): + last = -1 while True: if len(self.animations) == 0: self.log.debug("No animations, sleeping...") sleep(2) else: - for a in self.animations: - self.log.debug(f"Starting animation: {a}") - a.run() + while True: + next = random.randint(0, len(self.animations) - 1) + if next != last: + break + last = next + a = self.animations[next] + self.log.debug(f"Starting animation: {a}") + a.run() def add(self, animation, *args, **kwargs): self.animations.append(animation(self.buba, *args, **kwargs))