Mercurial > code > home > repos > homeauto
annotate service/mqtt_graph_bridge/mqtt_graph_bridge.py @ 694:925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
Ignore-this: 74eba63f3a29f48b48c6fb1ae74780d3
author | drewp@bigasterisk.com |
---|---|
date | Fri, 31 Jan 2020 23:54:20 -0800 |
parents | e1ee6661329a |
children | c52b172c0824 |
rev | line source |
---|---|
597
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
1 """ |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
2 We get output statements that are like light9's deviceAttrs (:dev1 :color "#ff0000"), |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
3 convert those to outputAttrs (:dev1 :red 255; :green 0; :blue 0) and post them to mqtt. |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
4 |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
5 This is like light9/bin/collector. |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
6 """ |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
7 import json |
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
8 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
9 from docopt import docopt |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
10 from rdflib import Namespace, Literal |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
11 from twisted.internet import reactor |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
12 import cyclone.web |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
13 |
378
b90d9321d2ce
factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents:
373
diff
changeset
|
14 from mqtt_client import MqttClient |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
15 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
16 from standardservice.logsetup import log, verboseLogging |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
17 import rdf_over_http |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
18 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
19 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
20 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
21 devs = { |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
22 ROOM['kitchenLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
23 'root': 'h801_skylight', |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
24 'ctx': ROOM['kitchenH801'] |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
25 }, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
26 ROOM['kitchenCounterLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
27 'root': 'h801_counter', |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
28 'ctx': ROOM['kitchenH801'] |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
29 }, |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
30 } |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
31 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
33 class OutputPage(cyclone.web.RequestHandler): |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
34 def put(self): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
35 for stmt in rdf_over_http.rdfStatementsFromRequest( |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
36 self.request.arguments, |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
37 self.request.body, |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
38 self.request.headers): |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
39 self._onStatement(stmt) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
40 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
41 def _onStatement(self, stmt): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
42 log.info(f'incoming statement: {stmt}') |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
43 ignored = True |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
44 for dev, attrs in devs.items(): |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
45 if stmt[0:2] == (dev, ROOM['brightness']): |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
46 for chan, scale in [('w1', 1), |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
47 ('r', 1), |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
48 ('g', .8), |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
49 ('b', .8)]: |
586 | 50 out = stmt[2].toPython() * scale |
51 topic = f"{attrs['root']}/light/kit_{chan}/command" | |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
52 self.settings.mqtt.publish( |
586 | 53 topic.encode('ascii'), |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
54 json.dumps({ |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
55 'state': 'ON', |
586 | 56 'brightness': int(out * 255)}).encode('ascii')) |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
57 self.settings.masterGraph.patchObject( |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
58 attrs['ctx'], |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
59 stmt[0], stmt[1], stmt[2]) |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
60 ignored = False |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
61 if ignored: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
62 log.warn("ignoring %s", stmt) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
63 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
64 if __name__ == '__main__': |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
65 arg = docopt(""" |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
66 Usage: mqtt_graph_bridge.py [options] |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
67 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
68 -v Verbose |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
69 """) |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
70 verboseLogging(arg['-v']) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
71 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
72 masterGraph = PatchableGraph() |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
73 |
586 | 74 mqtt = MqttClient(clientId='mqtt_graph_bridge', brokerPort=1883) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
75 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
76 port = 10008 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
77 reactor.listenTCP(port, cyclone.web.Application([ |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
78 (r"/()", cyclone.web.StaticFileHandler, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
79 {"path": ".", "default_filename": "index.html"}), |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
80 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
81 (r"/graph/events", CycloneGraphEventsHandler, |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
82 {'masterGraph': masterGraph}), |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
83 (r'/output', OutputPage), |
378
b90d9321d2ce
factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents:
373
diff
changeset
|
84 ], mqtt=mqtt, masterGraph=masterGraph, debug=arg['-v']), interface='::') |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
85 log.warn('serving on %s', port) |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
86 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
87 for dev, attrs in devs.items(): |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
88 masterGraph.patchObject(attrs['ctx'], |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
89 dev, ROOM['brightness'], Literal(0.0)) |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
90 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
91 reactor.run() |