Mercurial > code > home > repos > light-bridge
annotate light_bridge.py @ 25:cee43f550577
add /lightNames
author | drewp@bigasterisk.com |
---|---|
date | Fri, 02 Feb 2024 20:52:09 -0800 |
parents | e3dbd04dab96 |
children | 32cfefe3155b |
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 |
25 | 32 async def lightNames(lights: Lights, request: Request) -> JSONResponse: |
33 return JSONResponse({'lightNames': lights.allNames()}) | |
34 | |
0 | 35 |
36 async def table(lights: Lights, req: Request) -> EventSourceResponse: | |
37 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
38 def updateMessage(): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
39 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
|
40 |
0 | 41 async def g(): |
11
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 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
|
44 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
|
45 yield updateMessage() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
10
diff
changeset
|
46 await asyncio.sleep(.5) # slow down the inf loop |
0 | 47 |
48 return EventSourceResponse(g()) | |
49 | |
50 | |
51 def main(): | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
11
diff
changeset
|
52 mqtt = MqttIo() |
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
11
diff
changeset
|
53 lights = Lights(mqtt) |
0 | 54 graph = PatchableGraph() |
55 app = Starlette(debug=True, | |
56 routes=[ | |
57 Route('/api/output', partial(output, lights), methods=['PUT']), | |
25 | 58 Route('/api/lightNames', partial(lightNames, lights), methods=['GET']), |
0 | 59 Route('/api/table', partial(table, lights)), |
60 Route('/api/graph', StaticGraph(graph)), | |
61 Route('/api/graph/events', GraphEvents(graph)), | |
62 ]) | |
63 | |
64 app.add_middleware(PrometheusMiddleware, app_name='light-bridge') | |
65 app.add_route("/metrics", handle_metrics) | |
66 | |
67 return app | |
68 | |
69 | |
70 app = main() |