Mercurial > code > home > repos > light-bridge
annotate light_bridge.py @ 15:61d4ccecfed8
rough refactor
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 21:18:01 -0800 |
parents | e3dbd04dab96 |
children | cee43f550577 |
rev | line source |
---|---|
0 | 1 """ |
2 replaces a lot of mqtt_to_rdf and rdf_to_mqtt | |
3 """ | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
4 import asyncio |
0 | 5 import json |
6 import logging | |
7 | 7 import time |
2 | 8 from functools import partial |
10
140633abfa2a
route colors from PUT to Light.setColor
drewp@bigasterisk.com
parents:
7
diff
changeset
|
9 from color import Color |
0 | 10 |
11 from patchablegraph import PatchableGraph | |
12 from patchablegraph.handler import GraphEvents, StaticGraph | |
2 | 13 from sse_starlette.sse import EventSourceResponse |
0 | 14 from starlette.applications import Starlette |
15 from starlette.requests import Request | |
16 from starlette.responses import JSONResponse | |
17 from starlette.routing import Route | |
18 from starlette_exporter import PrometheusMiddleware, handle_metrics | |
2 | 19 |
7 | 20 from light import Lights |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
11
diff
changeset
|
21 from mqtt_io import MqttIo |
0 | 22 logging.basicConfig(level=logging.INFO) |
23 log = logging.getLogger() | |
24 | |
25 | |
26 async def output(lights: Lights, request: Request) -> JSONResponse: | |
27 light = lights.byName(request.query_params['light']) | |
10
140633abfa2a
route colors from PUT to Light.setColor
drewp@bigasterisk.com
parents:
7
diff
changeset
|
28 body = (await request.body()).decode('utf8') |
140633abfa2a
route colors from PUT to Light.setColor
drewp@bigasterisk.com
parents:
7
diff
changeset
|
29 await light.setColor(Color.fromHex(body)) |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
2
diff
changeset
|
30 return JSONResponse(light.to_dict()) |
0 | 31 |
32 | |
33 async def table(lights: Lights, req: Request) -> EventSourceResponse: | |
34 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
35 def updateMessage(): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
36 return json.dumps({'now': time.time()} | lights.to_dict()) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
37 |
0 | 38 async def g(): |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
39 yield updateMessage() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
40 async for ping in lights.changes(): # broken if there's more than one caller! |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
41 log.info('send table event') |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
42 yield updateMessage() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
43 await asyncio.sleep(.5) # slow down the inf loop |
0 | 44 |
45 return EventSourceResponse(g()) | |
46 | |
47 | |
48 def main(): | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
11
diff
changeset
|
49 mqtt = MqttIo() |
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
11
diff
changeset
|
50 lights = Lights(mqtt) |
0 | 51 graph = PatchableGraph() |
52 app = Starlette(debug=True, | |
53 routes=[ | |
54 Route('/api/output', partial(output, lights), methods=['PUT']), | |
55 Route('/api/table', partial(table, lights)), | |
56 Route('/api/graph', StaticGraph(graph)), | |
57 Route('/api/graph/events', GraphEvents(graph)), | |
58 ]) | |
59 | |
60 app.add_middleware(PrometheusMiddleware, app_name='light-bridge') | |
61 app.add_route("/metrics", handle_metrics) | |
62 | |
63 return app | |
64 | |
65 | |
66 app = main() |