annotate light_bridge.py @ 10:140633abfa2a

route colors from PUT to Light.setColor
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 16:53:35 -0800
parents 05fb0319eb64
children 028ed39aa78f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
drewp@bigasterisk.com
parents:
diff changeset
1 """
drewp@bigasterisk.com
parents:
diff changeset
2 replaces a lot of mqtt_to_rdf and rdf_to_mqtt
drewp@bigasterisk.com
parents:
diff changeset
3 """
drewp@bigasterisk.com
parents:
diff changeset
4 import json
drewp@bigasterisk.com
parents:
diff changeset
5 import logging
7
05fb0319eb64 cleanup
drewp@bigasterisk.com
parents: 6
diff changeset
6 import time
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
7 from functools import partial
10
140633abfa2a route colors from PUT to Light.setColor
drewp@bigasterisk.com
parents: 7
diff changeset
8 from color import Color
0
drewp@bigasterisk.com
parents:
diff changeset
9
drewp@bigasterisk.com
parents:
diff changeset
10 from patchablegraph import PatchableGraph
drewp@bigasterisk.com
parents:
diff changeset
11 from patchablegraph.handler import GraphEvents, StaticGraph
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
12 from sse_starlette.sse import EventSourceResponse
0
drewp@bigasterisk.com
parents:
diff changeset
13 from starlette.applications import Starlette
drewp@bigasterisk.com
parents:
diff changeset
14 from starlette.requests import Request
drewp@bigasterisk.com
parents:
diff changeset
15 from starlette.responses import JSONResponse
drewp@bigasterisk.com
parents:
diff changeset
16 from starlette.routing import Route
drewp@bigasterisk.com
parents:
diff changeset
17 from starlette_exporter import PrometheusMiddleware, handle_metrics
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
18
7
05fb0319eb64 cleanup
drewp@bigasterisk.com
parents: 6
diff changeset
19 from light import Lights
0
drewp@bigasterisk.com
parents:
diff changeset
20
drewp@bigasterisk.com
parents:
diff changeset
21 logging.basicConfig(level=logging.INFO)
drewp@bigasterisk.com
parents:
diff changeset
22 log = logging.getLogger()
drewp@bigasterisk.com
parents:
diff changeset
23
drewp@bigasterisk.com
parents:
diff changeset
24
drewp@bigasterisk.com
parents:
diff changeset
25 async def output(lights: Lights, request: Request) -> JSONResponse:
drewp@bigasterisk.com
parents:
diff changeset
26 light = lights.byName(request.query_params['light'])
10
140633abfa2a route colors from PUT to Light.setColor
drewp@bigasterisk.com
parents: 7
diff changeset
27 body = (await request.body()).decode('utf8')
140633abfa2a route colors from PUT to Light.setColor
drewp@bigasterisk.com
parents: 7
diff changeset
28 await light.setColor(Color.fromHex(body))
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 2
diff changeset
29 return JSONResponse(light.to_dict())
0
drewp@bigasterisk.com
parents:
diff changeset
30
drewp@bigasterisk.com
parents:
diff changeset
31
drewp@bigasterisk.com
parents:
diff changeset
32 async def table(lights: Lights, req: Request) -> EventSourceResponse:
drewp@bigasterisk.com
parents:
diff changeset
33
drewp@bigasterisk.com
parents:
diff changeset
34 async def g():
drewp@bigasterisk.com
parents:
diff changeset
35 async for ping in lights.changes():
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 2
diff changeset
36 yield json.dumps({'now': time.time()} | lights.to_dict())
0
drewp@bigasterisk.com
parents:
diff changeset
37
drewp@bigasterisk.com
parents:
diff changeset
38 return EventSourceResponse(g())
drewp@bigasterisk.com
parents:
diff changeset
39
drewp@bigasterisk.com
parents:
diff changeset
40
drewp@bigasterisk.com
parents:
diff changeset
41 def main():
drewp@bigasterisk.com
parents:
diff changeset
42 lights = Lights()
drewp@bigasterisk.com
parents:
diff changeset
43 graph = PatchableGraph()
drewp@bigasterisk.com
parents:
diff changeset
44 app = Starlette(debug=True,
drewp@bigasterisk.com
parents:
diff changeset
45 routes=[
drewp@bigasterisk.com
parents:
diff changeset
46 Route('/api/output', partial(output, lights), methods=['PUT']),
drewp@bigasterisk.com
parents:
diff changeset
47 Route('/api/table', partial(table, lights)),
drewp@bigasterisk.com
parents:
diff changeset
48 Route('/api/graph', StaticGraph(graph)),
drewp@bigasterisk.com
parents:
diff changeset
49 Route('/api/graph/events', GraphEvents(graph)),
drewp@bigasterisk.com
parents:
diff changeset
50 ])
drewp@bigasterisk.com
parents:
diff changeset
51
drewp@bigasterisk.com
parents:
diff changeset
52 app.add_middleware(PrometheusMiddleware, app_name='light-bridge')
drewp@bigasterisk.com
parents:
diff changeset
53 app.add_route("/metrics", handle_metrics)
drewp@bigasterisk.com
parents:
diff changeset
54
drewp@bigasterisk.com
parents:
diff changeset
55 return app
drewp@bigasterisk.com
parents:
diff changeset
56
drewp@bigasterisk.com
parents:
diff changeset
57
drewp@bigasterisk.com
parents:
diff changeset
58 app = main()