buba/buba/__main__.py
Stefan Bethke 64a3c729be Refactor Animation to simplify
Also add some documentation on how to write animations.
2025-06-16 09:33:22 +02:00

58 lines
1.6 KiB
Python

import logging
from bottle import Bottle, static_file, TEMPLATE_PATH, jinja2_view
from bottle_log import LoggingPlugin
from bottle_websocket import websocket, GeventWebSocketServer
from geventwebsocket.websocket import WebSocket
from buba.animationconfig import setup_animations
from buba.appconfig import AppConfig
from buba.bubaanimator import BubaAnimator
from buba.bubacmd import BubaCmd
from buba.websocketcomm import WebSocketClients
config = AppConfig()
if config.debug:
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)
app = Bottle()
if config.debug:
app.config.update({"logging.level": "DEBUG"})
app.install(LoggingPlugin(app.config))
TEMPLATE_PATH.insert(0, config.templatepath)
websocket_clients = WebSocketClients()
buba = BubaCmd(config.serial, websocket_clients.send)
animator = BubaAnimator(buba)
setup_animations(config, animator)
@app.route("/static/<filepath>")
def server_static(filepath):
return static_file(filepath, root=config.staticpath)
@app.get("/")
@jinja2_view("home.html.j2")
def root():
return {}
@app.get('/ws', apply=[websocket])
def websocket_endpoint(ws: WebSocket):
try:
websocket_clients.add(ws)
# ws.send(json.dumps(update_poller.get_locks(True)))
while True:
m = ws.receive()
except Exception as e:
log.debug("error in websocket", exc_info=e)
pass
finally:
websocket_clients.remove(ws)
if __name__ == '__main__':
app.run(host=config.listen_host, port=config.listen_port, server=GeventWebSocketServer, debug=config.debug,
quiet=not config.debug)