annotate service/mqtt_graph_bridge/mqtt_graph_bridge.py @ 378:b90d9321d2ce

factor common mqtt code out of mqtt_graph_bridge Ignore-this: 21b54376d0b00f6709f1947c198cb4a8
author drewp@bigasterisk.com
date Wed, 12 Dec 2018 01:10:48 -0800
parents 2158e7ad19b1
children 79d041273e26
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
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
7 import sys, logging
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 = {
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
13 ROOM['kitchenLight']: {'root': '004BD965', 'ctx': ROOM['kitchenH801']}
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
14 }
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
15
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
16 logging.basicConfig()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
17 log = logging.getLogger()
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 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
20 g = Graph()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
21 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
22 return g
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
23
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
24 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
25 def put(self):
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
26 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
27 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
28 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
29 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
30 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
31 try:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
32 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
33 except ValueError:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
34 obj = Literal(turtleLiteral)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
35 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
36 else:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
37 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
38 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
39 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
40 self._onStatement(stmt)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
41
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
42 def _onStatement(self, stmt):
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
43 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
44 if stmt[0:2] == (dev, ROOM['brightness']):
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
45 sw = 'OFF' if stmt[2].toPython() == 0 else 'ON'
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
46 self.settings.mqtt.publish("%s/w1/light/switch" % attrs['root'], sw)
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
47 self.settings.mqtt.publish("%s/rgb/rgb/set" % attrs['root'],
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
48 '200,255,200' if sw == 'ON' else '0,0,0')
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
49 self.settings.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
50 stmt[0], stmt[1], stmt[2])
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
51 return
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
52 log.warn("ignoring %s", stmt)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
53
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
54 if __name__ == '__main__':
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
55 arg = docopt("""
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
56 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
57
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
58 -v Verbose
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
59 """)
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
60 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
61 if arg['-v']:
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
62 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
63 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
64 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
65
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
66 masterGraph = PatchableGraph()
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
67
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
68 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
69
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
70 port = 10008
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
71 reactor.listenTCP(port, cyclone.web.Application([
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
72 (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
73 (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
74 {'masterGraph': masterGraph}),
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
75 (r'/output', OutputPage),
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
76 ], 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
77 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
78
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
79 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
80 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
81 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
82
378
b90d9321d2ce factor common mqtt code out of mqtt_graph_bridge
drewp@bigasterisk.com
parents: 373
diff changeset
83
373
2158e7ad19b1 receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff changeset
84 reactor.run()