Mercurial > code > home > repos > homeauto
annotate service/piNode/piNode.py @ 382:61f1c529a9b7
etcd port change; pigpio download link change
Ignore-this: cf0ce00b51c41c7d897bf5dbefab7480
author | drewp@bigasterisk.com |
---|---|
date | Fri, 28 Dec 2018 01:56:52 -0800 |
parents | 88bd46f4e28c |
children | 0787cd64ecf8 |
rev | line source |
---|---|
182 | 1 from __future__ import division |
347 | 2 import sys, logging, socket, json, time |
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 |
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
|
11 |
182 | 12 logging.basicConfig(level=logging.DEBUG) |
331
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
325
diff
changeset
|
13 |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
325
diff
changeset
|
14 sys.path.append("../../lib") |
226 | 15 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
16 |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
17 from rdfdb.rdflibpatch import inContext |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
18 from rdfdb.patch import Patch |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
19 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
20 try: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
21 import pigpio |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
22 except ImportError: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
23 class pigpio(object): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
24 @staticmethod |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
25 def pi(): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
26 return None |
182 | 27 |
28 import devices | |
293 | 29 from export_to_influxdb import InfluxExporter |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
30 |
182 | 31 log = logging.getLogger() |
32 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
|
33 |
182 | 34 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
35 HOST = Namespace('http://bigasterisk.com/ruler/host/') | |
36 | |
37 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
|
38 CTX = ROOM['pi/%s' % hostname] |
382
61f1c529a9b7
etcd port change; pigpio download link change
drewp@bigasterisk.com
parents:
349
diff
changeset
|
39 etcd = etcd3.client(host='bang6', port=9022) |
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 |
258 | 41 def patchRandid(): |
42 """ | |
43 I'm concerned urandom is slow on raspberry pi, and I'm adding to | |
44 graphs a lot. Unclear what the ordered return values might do to | |
45 the balancing of the graph. | |
46 """ | |
47 _id_serial = [1000] | |
48 def randid(): | |
49 _id_serial[0] += 1 | |
50 return _id_serial[0] | |
51 import rdflib.plugins.memory | |
52 rdflib.plugins.memory.randid = randid | |
53 patchRandid() | |
54 | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
55 class Config(object): |
226 | 56 def __init__(self, masterGraph): |
347 | 57 self.masterGraph = masterGraph |
58 self.configGraph = ConjunctiveGraph() | |
59 self.boards = [] | |
60 self.etcPrefix = 'pi/' | |
61 | |
62 self.reread() | |
63 | |
64 deferToThread(self.watchEtcd) | |
65 | |
66 def watchEtcd(self): | |
67 events, cancel = etcd.watch_prefix(self.etcPrefix) | |
68 reactor.addSystemEventTrigger('before', 'shutdown', cancel) | |
69 for ev in events: | |
70 log.info('%s changed', ev.key) | |
71 reactor.callFromThread(self.configChanged) | |
72 | |
73 def configChanged(self): | |
74 self.cancelRead() | |
75 self.rereadLater = reactor.callLater(.1, self.reread) | |
76 | |
77 def cancelRead(self): | |
78 if getattr(self, 'rereadLater', None): | |
79 self.rereadLater.cancel() | |
80 self.rereadLater = None | |
81 | |
82 def reread(self): | |
83 self.rereadLater = None | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
84 log.info('read config') |
347 | 85 self.configGraph = ConjunctiveGraph() |
86 for v, md in etcd.get_prefix(self.etcPrefix): | |
87 log.info(' read file %r', md.key) | |
88 self.configGraph.parse(StringInputSource(v), format='n3') | |
89 self.configGraph.bind('', ROOM) | |
90 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
|
91 # config graph is too noisy; maybe make it a separate resource |
347 | 92 #masterGraph.patch(Patch(addGraph=self.configGraph)) |
93 self.setupBoards() | |
94 | |
95 def setupBoards(self): | |
96 thisHost = Literal(hostname) | |
97 for row in self.configGraph.query( | |
98 'SELECT ?board WHERE { ?board a :PiBoard; :hostname ?h }', | |
99 initBindings=dict(h=thisHost)): | |
100 thisBoard = row.board | |
101 break | |
102 else: | |
103 log.warn("config had no board for :hostname %s. Waiting for config update." % | |
104 thisHost) | |
105 self.boards = [] | |
106 return | |
107 | |
108 log.info("found config for board %r" % thisBoard) | |
109 self.boards = [Board(self.configGraph, self.masterGraph, thisBoard)] | |
110 self.boards[0].startPolling() | |
111 | |
182 | 112 |
113 class Board(object): | |
114 """similar to arduinoNode.Board but without the communications stuff""" | |
226 | 115 def __init__(self, graph, masterGraph, uri): |
182 | 116 self.graph, self.uri = graph, uri |
226 | 117 self.masterGraph = masterGraph |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
118 self.masterGraph.setToGraph(self.staticStmts()) |
182 | 119 self.pi = pigpio.pi() |
120 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
|
121 log.debug('found %s devices', len(self._devs)) |
182 | 122 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
|
123 self._lastPollTime = {} # input device uri: time() |
293 | 124 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
|
125 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
|
126 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
|
127 |
182 | 128 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
|
129 task.LoopingCall(self._poll).start(.05) |
182 | 130 |
131 def _poll(self): | |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
132 try: |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
133 self._pollMaybeError() |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
134 except Exception: |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
135 log.exception("During poll:") |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
136 |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
137 def _pollMaybeError(self): |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
138 pollTime = {} # uri: sec |
182 | 139 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
|
140 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
|
141 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
|
142 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
|
143 continue |
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
|
144 new = i.poll() |
304
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
145 pollTime[i.uri] = time.time() - now |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
146 if isinstance(new, dict): # new style |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
147 oneshot = new['oneshot'] |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
148 new = new['latest'] |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
149 else: |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
150 oneshot = None |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
151 |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
152 self._updateMasterWithNewPollStatements(i.uri, new) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
153 |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
154 if oneshot: |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
155 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
|
156 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
|
157 if log.isEnabledFor(logging.DEBUG): |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
158 log.debug('poll times:') |
e7cbf250188a
influx output, fade support, switch to Adafruit_DHT, start of Lcd8544
drewp@bigasterisk.com
parents:
293
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
163 pollResults = map(set, self._statementsFromInputs.values()) |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
164 if pollResults: |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
165 self._influx.exportToInflux(set.union(*pollResults)) |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
166 |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
167 def _updateMasterWithNewPollStatements(self, dev, new): |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
168 prev = self._statementsFromInputs.get(dev, set()) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
169 |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
170 # it's important that quads from different devices |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
171 # don't clash, since that can lead to inconsistent |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
172 # patches (e.g. |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
173 # dev1 changes value from 1 to 2; |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
174 # dev2 changes value from 2 to 3; |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
175 # 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
|
176 # fail since the '2' statement is gone) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
177 self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
178 inContext(new, dev))) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
179 self._statementsFromInputs[dev] = new |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
180 |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
181 def _sendOneshot(self, oneshot): |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
182 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
|
183 for s,p,o in oneshot)).encode('utf8') |
347 | 184 url = 'http://bang6:9071/oneShot' |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
185 d = fetch(method='POST', |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
186 url=url, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
187 headers={'Content-Type': ['text/n3']}, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
188 postdata=body, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
189 timeout=5) |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
190 def err(e): |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
191 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
|
192 url, e.getErrorMessage()) |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
193 d.addErrback(err) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
194 |
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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 |
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
|
210 # 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
|
211 # 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
|
212 # 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
|
213 # 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
|
214 # 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 |
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 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 |
226 | 228 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
|
229 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
|
230 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
231 def description(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
232 """for web page""" |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
233 return { |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
234 'uri': self.uri, |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
235 'devices': [d.description() for d in self._devs], |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
236 'graph': 'http://sticker:9059/graph', #todo |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
237 } |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
238 |
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
|
239 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
|
240 def get(self): |
347 | 241 configGraph = self.settings.config.configGraph |
242 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
|
243 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
|
244 |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
245 def rdfGraphBody(body, headers): |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
246 g = Graph() |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
247 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
|
248 return g |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
249 |
182 | 250 class OutputPage(cyclone.web.RequestHandler): |
251 def put(self): | |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
252 arg = self.request.arguments |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
253 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
|
254 subj = URIRef(arg['s'][-1]) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
255 pred = URIRef(arg['p'][-1]) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
256 turtleLiteral = self.request.body |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
257 try: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
258 obj = Literal(float(turtleLiteral)) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
259 except ValueError: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
260 obj = Literal(turtleLiteral) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
261 stmt = (subj, pred, obj) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
262 else: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
263 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
|
264 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
|
265 stmt = g.triples((None, None, None)).next() |
182 | 266 |
347 | 267 for b in self.settings.config.boards: |
268 b.outputStatements([stmt]) | |
182 | 269 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
270 class Boards(cyclone.web.RequestHandler): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
271 def get(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
272 self.set_header('Content-type', 'application/json') |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
273 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
|
274 'host': hostname, |
347 | 275 '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
|
276 }, indent=2)) |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
277 |
182 | 278 def main(): |
279 arg = docopt(""" | |
280 Usage: piNode.py [options] | |
281 | |
282 -v Verbose | |
283 """) | |
284 log.setLevel(logging.WARN) | |
285 if arg['-v']: | |
286 from twisted.python import log as twlog | |
287 twlog.startLogging(sys.stdout) | |
288 | |
289 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
|
290 |
226 | 291 masterGraph = PatchableGraph() |
292 config = Config(masterGraph) | |
182 | 293 |
294 reactor.listenTCP(9059, cyclone.web.Application([ | |
295 (r"/()", cyclone.web.StaticFileHandler, { | |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
296 "path": "static", "default_filename": "index.html"}), |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
297 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}), |
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
|
298 (r'/boards', Boards), |
226 | 299 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
300 (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}), | |
182 | 301 (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
|
302 (r'/dot', Dot), |
347 | 303 ], config=config, debug=arg['-v']), interface='::') |
258 | 304 log.warn('serving on 9059') |
182 | 305 reactor.run() |
306 | |
307 main() |