buba/buba/__main__.py
2025-06-01 19:03:00 +02:00

59 lines
1.7 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.AppConfig import AppConfig
from buba.BubaAnimator import BubaAnimator, BubaTime, BubaCharset
from buba.BubaCmd import BubaCmd
from buba.websocketcomm import WebSocketClients
config = AppConfig()
if config.debug:
logging.basicConfig(level=logging.DEBUG)
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)
animator.add(BubaTime)
animator.add(BubaCharset)
# bottle_helpers = BottleHelpers(auth, group=config.requires_group, allowed=config.allowed)
# update_poller = UpdatePoller(websocket_clients, ccujack, 1 if config.debug else 0.1)
@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:
logging.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)