Mercurial > code > home > repos > homeauto
annotate service/mqtt_graph_bridge/mqtt_graph_bridge.py @ 1178:e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
Ignore-this: 3b180d528a9bd7f2f330e565311013d6
darcs-hash:ac3b164fbf124a1569f147e677b952c23043144b
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sat, 08 Dec 2018 01:48:37 -0800 |
parents | |
children | b90d9321d2ce |
rev | line source |
---|---|
1178
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 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
|
2 from mqtt.client.factory import MQTTFactory |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
3 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
4 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
|
5 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
|
6 from twisted.application.internet import ClientService, backoffPolicy |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
7 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
|
8 from twisted.internet.defer import inlineCallbacks |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 from twisted.internet.endpoints import clientFromString |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
10 import cyclone.web |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
11 import sys, logging |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 BROKER = "tcp:bang:1883" |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 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
|
15 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 devs = { |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 ROOM['kitchenLight']: {'root': '004BD965', 'ctx': ROOM['kitchenH801']} |
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 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 logging.basicConfig() |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 log = logging.getLogger() |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 class MQTTService(ClientService): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 def __init(self, endpoint, factory): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 ClientService.__init__(self, endpoint, factory, retryPolicy=backoffPolicy()) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 def startService(self): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 self.whenConnected().addCallback(self.connectToBroker) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 ClientService.startService(self) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 @inlineCallbacks |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 def connectToBroker(self, protocol): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 self.protocol = protocol |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 self.protocol.onDisconnection = self.onDisconnection |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 # We are issuing 3 publish in a row |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 # if order matters, then set window size to 1 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 # Publish requests beyond window size are enqueued |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 self.protocol.setWindowSize(1) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 try: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
43 yield self.protocol.connect("TwistedMQTT-pub", keepalive=60) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 except Exception as e: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 log.error("Connecting to {broker} raised {excp!s}", |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 broker=BROKER, excp=e) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 else: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 log.info("Connected to {broker}".format(broker=BROKER)) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 def onDisconnection(self, reason): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 log.warn("Connection to broker lost: %r", reason) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 self.whenConnected().addCallback(self.connectToBroker) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 def publish(self, topic, msg): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 def _logFailure(failure): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
56 log.warn("publish failed: %s", failure.getErrorMessage()) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
57 return failure |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
58 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
59 return self.protocol.publish(topic=topic, qos=0, message=msg).addErrback(_logFailure) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
60 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
61 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
|
62 g = Graph() |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
63 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
|
64 return g |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
65 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 try: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
74 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
|
75 except ValueError: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
76 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
|
77 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
|
78 else: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 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
|
83 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
84 def _onStatement(self, stmt): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
85 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
|
86 if stmt[0:2] == (dev, ROOM['brightness']): |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
87 sw = 'OFF' if stmt[2].toPython() == 0 else 'ON' |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
88 serv.publish("%s/w1/light/switch" % attrs['root'], sw) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
89 self.settings.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
|
90 stmt[0], stmt[1], stmt[2]) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
91 return |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
92 log.warn("ignoring %s", stmt) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
93 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
94 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
|
95 arg = docopt(""" |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
96 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
|
97 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
98 -v Verbose |
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 log.setLevel(logging.WARN) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
101 if arg['-v']: |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
102 from twisted.python import log as twlog |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
103 twlog.startLogging(sys.stdout) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
104 log.setLevel(logging.DEBUG) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
105 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
106 masterGraph = PatchableGraph() |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
107 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
108 factory = MQTTFactory(profile=MQTTFactory.PUBLISHER) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
109 myEndpoint = clientFromString(reactor, BROKER) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
110 serv = MQTTService(myEndpoint, factory) |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
111 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
112 port = 10008 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
113 reactor.listenTCP(port, cyclone.web.Application([ |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
114 (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
|
115 (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
|
116 {'masterGraph': masterGraph}), |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
117 (r'/output', OutputPage), |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
118 ], serv=serv, masterGraph=masterGraph, debug=arg['-v']), interface='::') |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
119 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
|
120 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
125 serv.startService() |
e3991af5bd39
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
126 reactor.run() |