Mercurial > code > home > repos > homeauto
annotate service/piNode/piNode.py @ 639:ad1756af9d99
more piNode wip. treq etc
Ignore-this: 153da9ea2f0e0cf56ec2ccb0c5dd5c5
author | drewp@bigasterisk.com |
---|---|
date | Mon, 12 Aug 2019 10:14:45 -0700 |
parents | 7c04b4f675ec |
children | 23ab244aa649 |
rev | line source |
---|---|
636 | 1 import logging, socket, json, time, pkg_resources |
182 | 2 import cyclone.web |
226 | 3 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
|
4 from rdflib.parser import StringInputSource |
636 | 5 from twisted.internet import reactor |
6 from twisted.internet.defer import inlineCallbacks, maybeDeferred, returnValue | |
347 | 7 from twisted.internet.threads import deferToThread |
182 | 8 from docopt import docopt |
636 | 9 import etcd3 |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
10 from greplin import scales |
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
11 from greplin.scales.cyclonehandler import StatsHandler |
636 | 12 import pigpio |
13 import treq | |
331
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
325
diff
changeset
|
14 |
226 | 15 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
16 from cycloneerr import PrettyErrorHandler |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
17 |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
18 from rdfdb.rdflibpatch import inContext |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
19 from rdfdb.patch import Patch |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
20 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
21 try: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
22 import pigpio |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
23 except ImportError: |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
24 class pigpio(object): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
25 @staticmethod |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
26 def pi(): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
27 return None |
182 | 28 |
29 import devices | |
293 | 30 from export_to_influxdb import InfluxExporter |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
31 |
182 | 32 log = logging.getLogger() |
33 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
|
34 |
182 | 35 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
36 HOST = Namespace('http://bigasterisk.com/ruler/host/') | |
37 | |
38 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
|
39 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
|
40 |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
41 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
|
42 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
|
43 scales.IntStat('pollException'), |
625
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
44 scales.PmfStat('pollAll'), |
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
|
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 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
51 class Config(object): |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
52 def __init__(self, masterGraph, hubHost): |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
53 self.etcd = etcd3.client(host=hubHost, port=9022) |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
54 |
347 | 55 self.masterGraph = masterGraph |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
56 self.hubHost = hubHost |
347 | 57 self.configGraph = ConjunctiveGraph() |
58 self.boards = [] | |
59 self.etcPrefix = 'pi/' | |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
60 self.rereadLater = None |
347 | 61 |
62 self.reread() | |
63 | |
64 deferToThread(self.watchEtcd) | |
65 | |
66 def watchEtcd(self): | |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
67 events, cancel = self.etcd.watch_prefix(self.etcPrefix) |
347 | 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): | |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
78 if self.rereadLater: |
347 | 79 self.rereadLater.cancel() |
80 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
|
81 |
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
|
82 @STATS.configReread.time() |
347 | 83 def reread(self): |
84 self.rereadLater = None | |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
85 log.info('read config') |
347 | 86 self.configGraph = ConjunctiveGraph() |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
87 for v, md in self.etcd.get_prefix(self.etcPrefix): |
347 | 88 log.info(' read file %r', md.key) |
89 self.configGraph.parse(StringInputSource(v), format='n3') | |
90 self.configGraph.bind('', ROOM) | |
91 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
|
92 # config graph is too noisy; maybe make it a separate resource |
347 | 93 #masterGraph.patch(Patch(addGraph=self.configGraph)) |
94 self.setupBoards() | |
624 | 95 |
96 def setupBoards(self): | |
347 | 97 thisHost = Literal(hostname) |
98 for row in self.configGraph.query( | |
99 'SELECT ?board WHERE { ?board a :PiBoard; :hostname ?h }', | |
100 initBindings=dict(h=thisHost)): | |
101 thisBoard = row.board | |
102 break | |
103 else: | |
104 log.warn("config had no board for :hostname %s. Waiting for config update." % | |
105 thisHost) | |
106 self.boards = [] | |
107 return | |
108 | |
109 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
|
110 self.boards = [Board(self.configGraph, self.masterGraph, thisBoard, self.hubHost)] |
347 | 111 |
182 | 112 |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
113 class DeviceRunner(object): |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
114 def __init__(self, dev): |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
115 self.dev = dev |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
116 self.period = getattr(self.dev, 'pollPeriod', .05) |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
117 #self._lastPollTime.get(i.uri, 0) + self.pollPeriod > now): |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
118 |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
119 reactor.callLater(0, self.poll) |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
120 |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
121 @inlineCallbacks |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
122 def poll(self): |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
123 now = time.time() |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
124 try: |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
125 with self.dev.stats.poll.time(): |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
126 new = yield maybeDeferred(self.dev.poll) |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
127 finally: |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
128 reactor.callLater(max(0, time.time() - (now + self.period)), self.poll) |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
129 returnValue(new) |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
130 |
182 | 131 class Board(object): |
132 """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
|
133 def __init__(self, graph, masterGraph, uri, hubHost): |
182 | 134 self.graph, self.uri = graph, uri |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
135 self.hubHost = hubHost |
226 | 136 self.masterGraph = masterGraph |
636 | 137 |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
138 self.masterGraph.setToGraph(self.staticStmts()) |
182 | 139 self.pi = pigpio.pi() |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
140 self._devs = [DeviceRunner(d) for d in devices.makeDevices(graph, self.uri, self.pi)] |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
141 log.debug('found %s devices', len(self._devs)) |
182 | 142 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
|
143 self._lastPollTime = {} # input device uri: time() |
293 | 144 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
|
145 for d in self._devs: |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
146 self.syncMasterGraphToHostStatements(d.dev) |
624 | 147 |
182 | 148 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
|
149 task.LoopingCall(self._poll).start(.05) |
182 | 150 |
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
|
151 @STATS.boardPoll.time() # not differentiating multiple boards here |
182 | 152 def _poll(self): |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
153 try: |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
154 self._pollMaybeError() |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
155 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
|
156 STATS.pollException += 1 |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
157 log.exception("During poll:") |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
158 |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
159 |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
160 @inlineCallbacks |
625
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
161 def _pollOneDev(self, i): |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
162 now = time.time() |
625
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
163 |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
164 new = i.poll() |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
165 if isinstance(new, dict): # new style |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
166 oneshot = new['oneshot'] |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
167 new = new['latest'] |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
168 else: |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
169 oneshot = None |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
170 |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
171 self._updateMasterWithNewPollStatements(i.uri, new) |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
172 |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
173 if oneshot: |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
174 self._sendOneshot(oneshot) |
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
175 self._lastPollTime[i.uri] = now |
624 | 176 |
589
f397ec8bd13d
pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp@bigasterisk.com
parents:
553
diff
changeset
|
177 @inlineCallbacks |
271
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
178 def _pollMaybeError(self): |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
179 pollTime = {} # uri: sec |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
180 yield gatherResults([self._pollOneDev(i.dev, pollTime) |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
181 for i in self._devs], consumeErrors=True) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
182 |
338
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
183 pollResults = map(set, self._statementsFromInputs.values()) |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
184 if pollResults: |
f64e20d3407e
RgbPixelsAnimation and docker build updates
drewp@bigasterisk.com
parents:
331
diff
changeset
|
185 self._influx.exportToInflux(set.union(*pollResults)) |
221
666e0e756ce6
piNode support for temp sensors. proper hostname lookup
drewp@bigasterisk.com
parents:
220
diff
changeset
|
186 |
349
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
187 def _updateMasterWithNewPollStatements(self, dev, new): |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
188 prev = self._statementsFromInputs.get(dev, set()) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
189 |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
190 # it's important that quads from different devices |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
191 # don't clash, since that can lead to inconsistent |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
192 # patches (e.g. |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
193 # dev1 changes value from 1 to 2; |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
194 # dev2 changes value from 2 to 3; |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
195 # 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
|
196 # fail since the '2' statement is gone) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
197 self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
198 inContext(new, dev))) |
88bd46f4e28c
more robust about not accumulating old stmts
drewp@bigasterisk.com
parents:
347
diff
changeset
|
199 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
|
200 |
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
|
201 @STATS.sendOneshot.time() |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
202 def _sendOneshot(self, oneshot): |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
203 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
|
204 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
|
205 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
|
206 d = fetch(method='POST', |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
207 url=url, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
208 headers={'Content-Type': ['text/n3']}, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
209 postdata=body, |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
210 timeout=5) |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
211 def err(e): |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
212 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
|
213 url, e.getErrorMessage()) |
e7a30f72536a
rewrite oneShotPost to ease debugging. add try-block around polling
drewp@bigasterisk.com
parents:
269
diff
changeset
|
214 d.addErrback(err) |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
215 |
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
|
216 @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
|
217 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
|
218 unused = set(stmts) |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
219 for devRunner in self._devs: |
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
220 dev = devRunner.dev |
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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 log.info("output goes to action handler for %s" % dev.uri) |
625
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
231 with dev.stats.output.time(): |
69a84b3d1dfa
frontdoor configs; move device class timing to greplin stats
drewp@bigasterisk.com
parents:
624
diff
changeset
|
232 dev.sendOutput(stmtsForDev) |
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
|
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
|
234 # 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
|
235 # 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
|
236 # 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
|
237 # 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
|
238 # 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 |
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 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 |
226 | 252 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
|
253 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
|
254 |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
255 def description(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
256 """for web page""" |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
257 return { |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
258 'uri': self.uri, |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
259 'devices': [d.dev.description() for d in self._devs], |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
260 'graph': 'http://sticker:9059/graph', #todo |
628
accbbd521ecd
wip for pytype support and separate device run loops on piNode
drewp@bigasterisk.com
parents:
627
diff
changeset
|
261 } |
624 | 262 |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
263 def rdfGraphBody(body, headers): |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
264 g = Graph() |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
265 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
|
266 return g |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
267 |
591 | 268 class OutputPage(PrettyErrorHandler, cyclone.web.RequestHandler): |
182 | 269 def put(self): |
184
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
270 arg = self.request.arguments |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
271 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
|
272 subj = URIRef(arg['s'][-1]) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
273 pred = URIRef(arg['p'][-1]) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
274 turtleLiteral = self.request.body |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
275 try: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
276 obj = Literal(float(turtleLiteral)) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
277 except ValueError: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
278 obj = Literal(turtleLiteral) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
279 stmt = (subj, pred, obj) |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
280 else: |
e052416a2290
piNode allow nt graphs as the body of a PUT /output
drewp@bigasterisk.com
parents:
183
diff
changeset
|
281 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
|
282 assert len(g) == 1, len(g) |
636 | 283 stmt = next(g.triples((None, None, None))) |
182 | 284 |
347 | 285 for b in self.settings.config.boards: |
636 | 286 b.outputStatements({stmt}) |
182 | 287 |
591 | 288 class Boards(PrettyErrorHandler, cyclone.web.RequestHandler): |
183
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
289 def get(self): |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
290 self.set_header('Content-type', 'application/json') |
634d6e477953
get piNode working, for motionsensor at least
drewp@bigasterisk.com
parents:
182
diff
changeset
|
291 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
|
292 'host': hostname, |
347 | 293 '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
|
294 }, indent=2)) |
624 | 295 |
182 | 296 def main(): |
297 arg = docopt(""" | |
298 Usage: piNode.py [options] | |
299 | |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
300 -v Verbose |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
301 --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
|
302 --hub=HOST Hostname for etc3 and oneshot posts. [default: bang.vpn-home.bigasterisk.com] |
182 | 303 """) |
304 log.setLevel(logging.WARN) | |
305 if arg['-v']: | |
306 from twisted.python import log as twlog | |
307 twlog.startLogging(sys.stdout) | |
308 | |
309 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
|
310 |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
311 if arg['--ow']: |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
312 log.setLevel(logging.INFO) |
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
313 for stmt in devices.OneWire().poll(): |
636 | 314 print(stmt) |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
315 return |
624 | 316 |
226 | 317 masterGraph = PatchableGraph() |
408
0787cd64ecf8
cmdline flag on piNode to pick hub host
drewp@bigasterisk.com
parents:
382
diff
changeset
|
318 config = Config(masterGraph, arg['--hub']) |
624 | 319 |
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
|
320 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
|
321 |
182 | 322 reactor.listenTCP(9059, cyclone.web.Application([ |
627
c3f0a98fa65d
lib upgrades; fix a static http server path
drewp@bigasterisk.com
parents:
625
diff
changeset
|
323 (r"/(|output-widgets.html)", 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
|
324 "path": static, "default_filename": "index.html"}), |
553 | 325 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": static}), |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
326 (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
|
327 (r'/boards', Boards), |
226 | 328 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
329 (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}), | |
182 | 330 (r'/output', OutputPage), |
466
1122016d16eb
stats handler, events output fix, build updaets
drewp@bigasterisk.com
parents:
408
diff
changeset
|
331 ], config=config, debug=arg['-v']), interface='::') |
258 | 332 log.warn('serving on 9059') |
182 | 333 reactor.run() |
334 | |
335 main() |