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