annotate service/mqtt_graph_bridge/mqtt_graph_bridge.py @ 460:7051b8b4766a

build updates. hack in r/g/b and some fixed multipliers Ignore-this: 155d402cd863cf9be2b59f5595131c0d
author drewp@bigasterisk.com
date Sat, 20 Apr 2019 23:32:36 -0700
parents 79d041273e26
children 30022797642e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
1 from docopt import docopt
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
2 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
3 from rdflib import Namespace, URIRef, Literal, Graph
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
4 from rdflib.parser import StringInputSource
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
5 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
6 import cyclone.web
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 378
diff changeset
7 import sys, logging, json
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
8 from mqtt_client import MqttClient
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
9
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
10 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
11
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
12 devs = {
460
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
13 ROOM['kitchenLight']: {
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
14 'root': 'h801_skylight',
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
15 'ctx': ROOM['kitchenH801']
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
16 },
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
17 ROOM['kitchenCounterLight']: {
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
18 'root': 'h801_counter',
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
19 'ctx': ROOM['kitchenH801']
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
20 },
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
21 }
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
22
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
23 logging.basicConfig()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
24 log = logging.getLogger()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
25
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
26 def rdfGraphBody(body, headers):
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
27 g = Graph()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
28 g.parse(StringInputSource(body), format='nt')
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
29 return g
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 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
32 def put(self):
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
33 arg = self.request.arguments
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
34 if arg.get('s') and arg.get('p'):
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
35 subj = URIRef(arg['s'][-1])
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
36 pred = URIRef(arg['p'][-1])
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
37 turtleLiteral = self.request.body
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
38 try:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
39 obj = Literal(float(turtleLiteral))
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
40 except ValueError:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
41 obj = Literal(turtleLiteral)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
42 stmt = (subj, pred, obj)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
43 else:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
44 g = rdfGraphBody(self.request.body, self.request.headers)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
45 assert len(g) == 1, len(g)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
46 stmt = g.triples((None, None, None)).next()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
47 self._onStatement(stmt)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
48
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
49 def _onStatement(self, stmt):
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 378
diff changeset
50 ignored = True
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
51 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
52 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
53 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
54 ('r', 1),
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
55 ('g', .8),
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
56 ('b', .8)]:
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
57 out = stmt[2].toPython() * scale
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
58 self.settings.mqtt.publish(
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
59 "%s/light/kit_%s/command" % (attrs['root'], chan),
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
60 json.dumps({
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
61 'state': 'ON',
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
62 'brightness': int(out * 255)}))
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
63 self.settings.masterGraph.patchObject(
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
64 attrs['ctx'],
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
65 stmt[0], stmt[1], stmt[2])
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 378
diff changeset
66 ignored = False
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 378
diff changeset
67 if ignored:
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 378
diff changeset
68 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
69
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
70 if __name__ == '__main__':
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
71 arg = docopt("""
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
72 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
73
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
74 -v Verbose
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 log.setLevel(logging.WARN)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
77 if arg['-v']:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
78 from twisted.python import log as twlog
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
79 twlog.startLogging(sys.stdout)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
80 log.setLevel(logging.DEBUG)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
81
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
82 masterGraph = PatchableGraph()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
83
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
84 mqtt = MqttClient(brokerPort=1883)
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
85
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
86 port = 10008
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
87 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
88 (r"/()", cyclone.web.StaticFileHandler,
7051b8b4766a build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents: 392
diff changeset
89 {"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
90 (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
91 (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
92 {'masterGraph': masterGraph}),
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
93 (r'/output', OutputPage),
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
94 ], 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
95 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
96
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
97 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
98 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
99 dev, ROOM['brightness'], Literal(0.0))
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
100
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
101
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
102 reactor.run()