annotate service/mqtt_graph_bridge/mqtt_graph_bridge.py @ 1400:31aed1a0af9c

adjust kitchen PWM freqs. add comments and proposed contents of n3 configs Ignore-this: 8fc4659c1d8830e1ed66b0702b1e69fd darcs-hash:026328acb60195c0d4410ce1cc583ac120d1c6a3
author drewp <drewp@bigasterisk.com>
date Sat, 13 Jul 2019 19:58:50 -0700
parents 928b1833de0f
children 925bc4137c93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1400
31aed1a0af9c adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp <drewp@bigasterisk.com>
parents: 1389
diff changeset
1 """
31aed1a0af9c adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp <drewp@bigasterisk.com>
parents: 1389
diff changeset
2 We get output statements that are like light9's deviceAttrs (:dev1 :color "#ff0000"),
31aed1a0af9c adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp <drewp@bigasterisk.com>
parents: 1389
diff changeset
3 convert those to outputAttrs (:dev1 :red 255; :green 0; :blue 0) and post them to mqtt.
31aed1a0af9c adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp <drewp@bigasterisk.com>
parents: 1389
diff changeset
4
31aed1a0af9c adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp <drewp@bigasterisk.com>
parents: 1389
diff changeset
5 This is like light9/bin/collector.
31aed1a0af9c adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp <drewp@bigasterisk.com>
parents: 1389
diff changeset
6 """
1384
a29a55f3429c mqtt_graph_bridge to new build rules and to py3
drewp <drewp@bigasterisk.com>
parents: 1263
diff changeset
7 import json
a29a55f3429c mqtt_graph_bridge to new build rules and to py3
drewp <drewp@bigasterisk.com>
parents: 1263
diff changeset
8
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 from docopt import docopt
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 from rdflib import Namespace, URIRef, Literal, Graph
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 from rdflib.parser import StringInputSource
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 from twisted.internet import reactor
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 import cyclone.web
1384
a29a55f3429c mqtt_graph_bridge to new build rules and to py3
drewp <drewp@bigasterisk.com>
parents: 1263
diff changeset
14
1183
6561367aa60a factor common mqtt code out of mqtt_graph_bridge
drewp <drewp@bigasterisk.com>
parents: 1178
diff changeset
15 from mqtt_client import MqttClient
1384
a29a55f3429c mqtt_graph_bridge to new build rules and to py3
drewp <drewp@bigasterisk.com>
parents: 1263
diff changeset
16 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler
a29a55f3429c mqtt_graph_bridge to new build rules and to py3
drewp <drewp@bigasterisk.com>
parents: 1263
diff changeset
17 from standardservice.logsetup import log, verboseLogging
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 ROOM = Namespace('http://projects.bigasterisk.com/room/')
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 devs = {
1263
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
22 ROOM['kitchenLight']: {
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
23 'root': 'h801_skylight',
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
24 'ctx': ROOM['kitchenH801']
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
25 },
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
26 ROOM['kitchenCounterLight']: {
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
27 'root': 'h801_counter',
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
28 'ctx': ROOM['kitchenH801']
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
29 },
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 }
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 def rdfGraphBody(body, headers):
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 g = Graph()
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 g.parse(StringInputSource(body), format='nt')
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 return g
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 class OutputPage(cyclone.web.RequestHandler):
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 def put(self):
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 arg = self.request.arguments
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 if arg.get('s') and arg.get('p'):
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 subj = URIRef(arg['s'][-1])
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 pred = URIRef(arg['p'][-1])
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 turtleLiteral = self.request.body
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44 try:
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 obj = Literal(float(turtleLiteral))
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46 except ValueError:
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 obj = Literal(turtleLiteral)
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48 stmt = (subj, pred, obj)
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 else:
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 g = rdfGraphBody(self.request.body, self.request.headers)
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 assert len(g) == 1, len(g)
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 stmt = g.triples((None, None, None)).next()
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 self._onStatement(stmt)
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 def _onStatement(self, stmt):
1197
d8acab2b01f5 mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents: 1183
diff changeset
56 ignored = True
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57 for dev, attrs in devs.items():
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 if stmt[0:2] == (dev, ROOM['brightness']):
1263
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
59 for chan, scale in [('w1', 1),
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
60 ('r', 1),
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
61 ('g', .8),
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
62 ('b', .8)]:
1389
928b1833de0f fix str/bytes to mqtt client
drewp <drewp@bigasterisk.com>
parents: 1384
diff changeset
63 out = stmt[2].toPython() * scale
928b1833de0f fix str/bytes to mqtt client
drewp <drewp@bigasterisk.com>
parents: 1384
diff changeset
64 topic = f"{attrs['root']}/light/kit_{chan}/command"
1263
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
65 self.settings.mqtt.publish(
1389
928b1833de0f fix str/bytes to mqtt client
drewp <drewp@bigasterisk.com>
parents: 1384
diff changeset
66 topic.encode('ascii'),
1263
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
67 json.dumps({
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
68 'state': 'ON',
1389
928b1833de0f fix str/bytes to mqtt client
drewp <drewp@bigasterisk.com>
parents: 1384
diff changeset
69 'brightness': int(out * 255)}).encode('ascii'))
1263
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
70 self.settings.masterGraph.patchObject(
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
71 attrs['ctx'],
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
72 stmt[0], stmt[1], stmt[2])
1197
d8acab2b01f5 mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents: 1183
diff changeset
73 ignored = False
d8acab2b01f5 mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents: 1183
diff changeset
74 if ignored:
d8acab2b01f5 mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents: 1183
diff changeset
75 log.warn("ignoring %s", stmt)
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 if __name__ == '__main__':
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 arg = docopt("""
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 Usage: mqtt_graph_bridge.py [options]
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 -v Verbose
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 """)
1384
a29a55f3429c mqtt_graph_bridge to new build rules and to py3
drewp <drewp@bigasterisk.com>
parents: 1263
diff changeset
83 verboseLogging(arg['-v'])
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 masterGraph = PatchableGraph()
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86
1389
928b1833de0f fix str/bytes to mqtt client
drewp <drewp@bigasterisk.com>
parents: 1384
diff changeset
87 mqtt = MqttClient(clientId='mqtt_graph_bridge', brokerPort=1883)
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 port = 10008
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 reactor.listenTCP(port, cyclone.web.Application([
1263
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
91 (r"/()", cyclone.web.StaticFileHandler,
82fe44eadf00 build updates. hack in r/g/b and some fixed multipliers
drewp <drewp@bigasterisk.com>
parents: 1197
diff changeset
92 {"path": ".", "default_filename": "index.html"}),
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}),
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 (r"/graph/events", CycloneGraphEventsHandler,
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 {'masterGraph': masterGraph}),
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 (r'/output', OutputPage),
1183
6561367aa60a factor common mqtt code out of mqtt_graph_bridge
drewp <drewp@bigasterisk.com>
parents: 1178
diff changeset
97 ], mqtt=mqtt, masterGraph=masterGraph, debug=arg['-v']), interface='::')
1178
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98 log.warn('serving on %s', port)
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 for dev, attrs in devs.items():
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 masterGraph.patchObject(attrs['ctx'],
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 dev, ROOM['brightness'], Literal(0.0))
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103
e3991af5bd39 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 reactor.run()