Mercurial > code > home > repos > homeauto
annotate service/piNode/piNode.py @ 553:36e4304762ae
fix /static path dir
Ignore-this: 464d6e4dfc28f03240fde8cf1f047e41
author | drewp@bigasterisk.com |
---|---|
date | Thu, 25 Apr 2019 23:52:22 -0700 |
parents | 98384c3ccd33 |
children | f397ec8bd13d |
rev | line source |
---|---|
182 | 1 from __future__ import division |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
2 import sys, logging, socket, json, time, pkg_resources |
182 | 3 import cyclone.web |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
4 from cyclone.httpclient import fetch |
226 | 5 from rdflib import Namespace, URIRef, Literal, Graph, RDF, ConjunctiveGraph |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
6 from rdflib.parser import StringInputSource |
182 | 7 from twisted.internet import reactor, task |
347 | 8 from twisted.internet.threads import deferToThread |
182 | 9 from docopt import docopt |
347 | 10 import etcd3 |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
11 from greplin import scales |
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
12 from greplin.scales.cyclonehandler import StatsHandler |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
13 |
182 | 14 logging.basicConfig(level=logging.DEBUG) |
331
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
325
diff
changeset
|
15 |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
325
diff
changeset
|
16 sys.path.append("../../lib") |
226 | 17 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
18 |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
19 from rdfdb.rdflibpatch import inContext |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
20 from rdfdb.patch import Patch |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
21 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
22 try: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
23 import pigpio |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
24 except ImportError: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
25 class pigpio(object): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
26 @staticmethod |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
27 def pi(): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
28 return None |
182 | 29 |
30 import devices | |
293 | 31 from export_to_influxdb import InfluxExporter |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
32 |
182 | 33 log = logging.getLogger() |
34 logging.getLogger('serial').setLevel(logging.WARN) | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
35 |
182 | 36 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
37 HOST = Namespace('http://bigasterisk.com/ruler/host/') | |
38 | |
39 hostname = socket.gethostname() | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
40 CTX = ROOM['pi/%s' % hostname] |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
41 |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
42 STATS = scales.collection('/root', |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
43 scales.PmfStat('configReread'), |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
44 scales.IntStat('pollException'), |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
45 scales.PmfStat('boardPoll'), |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
46 scales.PmfStat('sendOneshot'), |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
47 scales.PmfStat('outputStatements'), |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
48 |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
49 ) |
258 | 50 def patchRandid(): |
51 """ | |
52 I'm concerned urandom is slow on raspberry pi, and I'm adding to | |
53 graphs a lot. Unclear what the ordered return values might do to | |
54 the balancing of the graph. | |
55 """ | |
56 _id_serial = [1000] | |
57 def randid(): | |
58 _id_serial[0] += 1 | |
59 return _id_serial[0] | |
60 import rdflib.plugins.memory | |
61 rdflib.plugins.memory.randid = randid | |
62 patchRandid() | |
63 | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
64 class Config(object): |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
65 def __init__(self, masterGraph, hubHost): |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
66 self.etcd = etcd3.client(host=hubHost, port=9022) |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
67 |
347 | 68 self.masterGraph = masterGraph |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
69 self.hubHost = hubHost |
347 | 70 self.configGraph = ConjunctiveGraph() |
71 self.boards = [] | |
72 self.etcPrefix = 'pi/' | |
73 | |
74 self.reread() | |
75 | |
76 deferToThread(self.watchEtcd) | |
77 | |
78 def watchEtcd(self): | |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
79 events, cancel = self.etcd.watch_prefix(self.etcPrefix) |
347 | 80 reactor.addSystemEventTrigger('before', 'shutdown', cancel) |
81 for ev in events: | |
82 log.info('%s changed', ev.key) | |
83 reactor.callFromThread(self.configChanged) | |
84 | |
85 def configChanged(self): | |
86 self.cancelRead() | |
87 self.rereadLater = reactor.callLater(.1, self.reread) | |
88 | |
89 def cancelRead(self): | |
90 if getattr(self, 'rereadLater', None): | |
91 self.rereadLater.cancel() | |
92 self.rereadLater = None | |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
93 |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
94 @STATS.configReread.time() |
347 | 95 def reread(self): |
96 self.rereadLater = None | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
97 log.info('read config') |
347 | 98 self.configGraph = ConjunctiveGraph() |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
99 for v, md in self.etcd.get_prefix(self.etcPrefix): |
347 | 100 log.info(' read file %r', md.key) |
101 self.configGraph.parse(StringInputSource(v), format='n3') | |
102 self.configGraph.bind('', ROOM) | |
103 self.configGraph.bind('rdf', RDF) | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
104 # config graph is too noisy; maybe make it a separate resource |
347 | 105 #masterGraph.patch(Patch(addGraph=self.configGraph)) |
106 self.setupBoards() | |
107 | |
108 def setupBoards(self): | |
109 thisHost = Literal(hostname) | |
110 for row in self.configGraph.query( | |
111 'SELECT ?board WHERE { ?board a :PiBoard; :hostname ?h }', | |
112 initBindings=dict(h=thisHost)): | |
113 thisBoard = row.board | |
114 break | |
115 else: | |
116 log.warn("config had no board for :hostname %s. Waiting for config update." % | |
117 thisHost) | |
118 self.boards = [] | |
119 return | |
120 | |
121 log.info("found config for board %r" % thisBoard) | |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
122 self.boards = [Board(self.configGraph, self.masterGraph, thisBoard, self.hubHost)] |
347 | 123 self.boards[0].startPolling() |
124 | |
182 | 125 |
126 class Board(object): | |
127 """similar to arduinoNode.Board but without the communications stuff""" | |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
128 def __init__(self, graph, masterGraph, uri, hubHost): |
182 | 129 self.graph, self.uri = graph, uri |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
130 self.hubHost = hubHost |
226 | 131 self.masterGraph = masterGraph |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
132 self.masterGraph.setToGraph(self.staticStmts()) |
182 | 133 self.pi = pigpio.pi() |
134 self._devs = devices.makeDevices(graph, self.uri, self.pi) | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
135 log.debug('found %s devices', len(self._devs)) |
182 | 136 self._statementsFromInputs = {} # input device uri: latest statements |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
137 self._lastPollTime = {} # input device uri: time() |
293 | 138 self._influx = InfluxExporter(self.graph) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
139 for d in self._devs: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
140 self.syncMasterGraphToHostStatements(d) |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
141 |
182 | 142 def startPolling(self): |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
143 task.LoopingCall(self._poll).start(.05) |
182 | 144 |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
145 @STATS.boardPoll.time() # not differentiating multiple boards here |
182 | 146 def _poll(self): |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
147 try: |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
148 self._pollMaybeError() |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
149 except Exception: |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
150 STATS.pollException += 1 |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
151 log.exception("During poll:") |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
152 |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
153 def _pollMaybeError(self): |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
154 pollTime = {} # uri: sec |
182 | 155 for i in self._devs: |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
156 now = time.time() |
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
157 if (hasattr(i, 'pollPeriod') and |
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
158 self._lastPollTime.get(i.uri, 0) + i.pollPeriod > now): |
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
159 continue |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
160 #need something like: |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
161 # with i.pollTiming.time(): |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
162 new = i.poll() |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
163 pollTime[i.uri] = time.time() - now |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
164 if isinstance(new, dict): # new style |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
165 oneshot = new['oneshot'] |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
166 new = new['latest'] |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
167 else: |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
168 oneshot = None |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
169 |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
170 self._updateMasterWithNewPollStatements(i.uri, new) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
171 |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
172 if oneshot: |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
173 self._sendOneshot(oneshot) |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
174 self._lastPollTime[i.uri] = now |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
175 if log.isEnabledFor(logging.DEBUG): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
176 log.debug('poll times:') |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
177 for u, s in sorted(pollTime.items()): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
178 log.debug(" %.4f ms %s", s * 1000, u) |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
179 log.debug('total poll time: %f ms', sum(pollTime.values()) * 1000) |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
180 |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
181 pollResults = map(set, self._statementsFromInputs.values()) |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
182 if pollResults: |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
183 self._influx.exportToInflux(set.union(*pollResults)) |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
184 |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
185 def _updateMasterWithNewPollStatements(self, dev, new): |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
186 prev = self._statementsFromInputs.get(dev, set()) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
187 |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
188 # it's important that quads from different devices |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
189 # don't clash, since that can lead to inconsistent |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
190 # patches (e.g. |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
191 # dev1 changes value from 1 to 2; |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
192 # dev2 changes value from 2 to 3; |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
193 # dev1 changes from 2 to 4 but this patch will |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
194 # fail since the '2' statement is gone) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
195 self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
196 inContext(new, dev))) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
197 self._statementsFromInputs[dev] = new |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
198 |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
199 @STATS.sendOneshot.time() |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
200 def _sendOneshot(self, oneshot): |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
201 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
202 for s,p,o in oneshot)).encode('utf8') |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
203 url = 'http://%s:9071/oneShot' % self.hubHost |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
204 d = fetch(method='POST', |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
205 url=url, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
206 headers={'Content-Type': ['text/n3']}, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
207 postdata=body, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
208 timeout=5) |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
209 def err(e): |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
210 log.info('oneshot post to %r failed: %s', |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
211 url, e.getErrorMessage()) |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
212 d.addErrback(err) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
213 |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
214 @STATS.outputStatements.time() |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
215 def outputStatements(self, stmts): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
216 unused = set(stmts) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
217 for dev in self._devs: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
218 stmtsForDev = [] |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
219 for pat in dev.outputPatterns(): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
220 if [term is None for term in pat] != [False, False, True]: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
221 raise NotImplementedError |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
222 for stmt in stmts: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
223 if stmt[:2] == pat[:2]: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
224 stmtsForDev.append(stmt) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
225 unused.discard(stmt) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
226 if stmtsForDev: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
227 log.info("output goes to action handler for %s" % dev.uri) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
228 dev.sendOutput(stmtsForDev) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
229 |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
230 # Dev *could* change hostStatements at any time, and |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
231 # we're not currently tracking that, but the usual is |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
232 # to change them in response to sendOutput so this |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
233 # should be good enough. The right answer is to give |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
234 # each dev the masterGraph for it to write to. |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
235 self.syncMasterGraphToHostStatements(dev) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
236 log.info("output and masterGraph sync complete") |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
237 if unused: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
238 log.info("Board %s doesn't care about these statements:", self.uri) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
239 for s in unused: |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
240 log.warn("%r", s) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
241 |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
242 def syncMasterGraphToHostStatements(self, dev): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
243 hostStmtCtx = URIRef(dev.uri + '/host') |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
244 newQuads = inContext(dev.hostStatements(), hostStmtCtx) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
245 p = self.masterGraph.patchSubgraph(hostStmtCtx, newQuads) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
246 log.debug("patch master with these host stmts %s", p) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
247 |
226 | 248 def staticStmts(self): |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
249 return [(HOST[hostname], ROOM['connectedTo'], self.uri, CTX)] |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
250 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
251 def description(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
252 """for web page""" |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
253 return { |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
254 'uri': self.uri, |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
255 'devices': [d.description() for d in self._devs], |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
256 'graph': 'http://sticker:9059/graph', #todo |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
257 } |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
258 |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
259 class Dot(cyclone.web.RequestHandler): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
260 def get(self): |
347 | 261 configGraph = self.settings.config.configGraph |
262 dot = dotrender.render(configGraph, self.settings.config.boards) | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
263 self.write(dot) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
264 |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
265 def rdfGraphBody(body, headers): |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
266 g = Graph() |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
267 g.parse(StringInputSource(body), format='nt') |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
268 return g |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
269 |
182 | 270 class OutputPage(cyclone.web.RequestHandler): |
271 def put(self): | |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
272 arg = self.request.arguments |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
273 if arg.get('s') and arg.get('p'): |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
274 subj = URIRef(arg['s'][-1]) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
275 pred = URIRef(arg['p'][-1]) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
276 turtleLiteral = self.request.body |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
277 try: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
278 obj = Literal(float(turtleLiteral)) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
279 except ValueError: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
280 obj = Literal(turtleLiteral) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
281 stmt = (subj, pred, obj) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
282 else: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
283 g = rdfGraphBody(self.request.body, self.request.headers) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
284 assert len(g) == 1, len(g) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
285 stmt = g.triples((None, None, None)).next() |
182 | 286 |
347 | 287 for b in self.settings.config.boards: |
288 b.outputStatements([stmt]) | |
182 | 289 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
290 class Boards(cyclone.web.RequestHandler): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
291 def get(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
292 self.set_header('Content-type', 'application/json') |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
293 self.write(json.dumps({ |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
294 'host': hostname, |
347 | 295 'boards': [b.description() for b in self.settings.config.boards] |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
296 }, indent=2)) |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
297 |
182 | 298 def main(): |
299 arg = docopt(""" | |
300 Usage: piNode.py [options] | |
301 | |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
302 -v Verbose |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
303 --ow Just report onewire device URIs and readings, then exit. |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
304 --hub=HOST Hostname for etc3 and oneshot posts. [default: bang.vpn-home.bigasterisk.com] |
182 | 305 """) |
306 log.setLevel(logging.WARN) | |
307 if arg['-v']: | |
308 from twisted.python import log as twlog | |
309 twlog.startLogging(sys.stdout) | |
310 | |
311 log.setLevel(logging.DEBUG) | |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
312 |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
313 if arg['--ow']: |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
314 log.setLevel(logging.INFO) |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
315 for stmt in devices.OneWire().poll(): |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
316 print stmt |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
317 return |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
318 |
226 | 319 masterGraph = PatchableGraph() |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
320 config = Config(masterGraph, arg['--hub']) |
182 | 321 |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
322 static = pkg_resources.resource_filename('homeauto_anynode', 'static/') |
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
323 |
182 | 324 reactor.listenTCP(9059, cyclone.web.Application([ |
325 (r"/()", cyclone.web.StaticFileHandler, { | |
552
98384c3ccd33
update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp@bigasterisk.com
parents:
466
diff
changeset
|
326 "path": static, "default_filename": "index.html"}), |
553 | 327 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": static}), |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
328 (r'/stats/(.*)', StatsHandler, {'serverName': 'piNode'}), |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
329 (r'/boards', Boards), |
226 | 330 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
331 (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}), | |
182 | 332 (r'/output', OutputPage), |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
333 (r'/dot', Dot), |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
334 ], config=config, debug=arg['-v']), interface='::') |
258 | 335 log.warn('serving on 9059') |
182 | 336 reactor.run() |
337 | |
338 main() |