annotate light_bridge.py @ 6:fc8ed0efcd72

move init to Lights
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 16:02:31 -0800
parents 7eeda7f4f9cd
children 05fb0319eb64
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
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
6 from functools import partial
1
42a494b8ee1a show report time
drewp@bigasterisk.com
parents: 0
diff changeset
7 import time
0
drewp@bigasterisk.com
parents:
diff changeset
8
drewp@bigasterisk.com
parents:
diff changeset
9 import background_loop
drewp@bigasterisk.com
parents:
diff changeset
10 from patchablegraph import PatchableGraph
drewp@bigasterisk.com
parents:
diff changeset
11 from patchablegraph.handler import GraphEvents, StaticGraph
drewp@bigasterisk.com
parents:
diff changeset
12 from rdflib import Namespace
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
13 from sse_starlette.sse import EventSourceResponse
0
drewp@bigasterisk.com
parents:
diff changeset
14 from starlette.applications import Starlette
drewp@bigasterisk.com
parents:
diff changeset
15 from starlette.requests import Request
drewp@bigasterisk.com
parents:
diff changeset
16 from starlette.responses import JSONResponse
drewp@bigasterisk.com
parents:
diff changeset
17 from starlette.routing import Route
drewp@bigasterisk.com
parents:
diff changeset
18 from starlette_exporter import PrometheusMiddleware, handle_metrics
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
19
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
20 from light import Color, Light, Lights
0
drewp@bigasterisk.com
parents:
diff changeset
21
drewp@bigasterisk.com
parents:
diff changeset
22 EX = Namespace('http://example.com/')
drewp@bigasterisk.com
parents:
diff changeset
23
drewp@bigasterisk.com
parents:
diff changeset
24 logging.basicConfig(level=logging.INFO)
drewp@bigasterisk.com
parents:
diff changeset
25 log = logging.getLogger()
drewp@bigasterisk.com
parents:
diff changeset
26
drewp@bigasterisk.com
parents:
diff changeset
27
drewp@bigasterisk.com
parents:
diff changeset
28 async def output(lights: Lights, request: Request) -> JSONResponse:
drewp@bigasterisk.com
parents:
diff changeset
29 light = lights.byName(request.query_params['light'])
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 2
diff changeset
30 return JSONResponse(light.to_dict())
0
drewp@bigasterisk.com
parents:
diff changeset
31
drewp@bigasterisk.com
parents:
diff changeset
32
drewp@bigasterisk.com
parents:
diff changeset
33 async def table(lights: Lights, req: Request) -> EventSourceResponse:
drewp@bigasterisk.com
parents:
diff changeset
34
drewp@bigasterisk.com
parents:
diff changeset
35 async def g():
drewp@bigasterisk.com
parents:
diff changeset
36 async for ping in lights.changes():
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 2
diff changeset
37 yield json.dumps({'now': time.time()} | lights.to_dict())
0
drewp@bigasterisk.com
parents:
diff changeset
38
drewp@bigasterisk.com
parents:
diff changeset
39 return EventSourceResponse(g())
drewp@bigasterisk.com
parents:
diff changeset
40
drewp@bigasterisk.com
parents:
diff changeset
41
drewp@bigasterisk.com
parents:
diff changeset
42 def main():
drewp@bigasterisk.com
parents:
diff changeset
43 lights = Lights()
drewp@bigasterisk.com
parents:
diff changeset
44 graph = PatchableGraph()
drewp@bigasterisk.com
parents:
diff changeset
45 app = Starlette(debug=True,
drewp@bigasterisk.com
parents:
diff changeset
46 routes=[
drewp@bigasterisk.com
parents:
diff changeset
47 Route('/api/output', partial(output, lights), methods=['PUT']),
drewp@bigasterisk.com
parents:
diff changeset
48 Route('/api/table', partial(table, lights)),
drewp@bigasterisk.com
parents:
diff changeset
49 Route('/api/graph', StaticGraph(graph)),
drewp@bigasterisk.com
parents:
diff changeset
50 Route('/api/graph/events', GraphEvents(graph)),
drewp@bigasterisk.com
parents:
diff changeset
51 ])
drewp@bigasterisk.com
parents:
diff changeset
52
drewp@bigasterisk.com
parents:
diff changeset
53 app.add_middleware(PrometheusMiddleware, app_name='light-bridge')
drewp@bigasterisk.com
parents:
diff changeset
54 app.add_route("/metrics", handle_metrics)
drewp@bigasterisk.com
parents:
diff changeset
55 # app.state.loop = background_loop.loop_forever(lambda first_run=False: update(graph), 1)
drewp@bigasterisk.com
parents:
diff changeset
56
drewp@bigasterisk.com
parents:
diff changeset
57 return app
drewp@bigasterisk.com
parents:
diff changeset
58
drewp@bigasterisk.com
parents:
diff changeset
59
drewp@bigasterisk.com
parents:
diff changeset
60 app = main()