Mercurial > code > home > repos > homeauto
comparison service/piNode/piNode.py @ 1154:5da3a85b59e4
more robust about not accumulating old stmts
Ignore-this: ee8618c8341d028fd7bd3596cd16a43d
darcs-hash:447cf599c2a1633481bc68fc3bc2fa5b8352fbf5
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 16 Apr 2018 22:16:15 -0700 |
parents | 6d2eba4d0dfd |
children | 61f1c529a9b7 |
comparison
equal
deleted
inserted
replaced
1153:e4f49cd9dda3 | 1154:5da3a85b59e4 |
---|---|
113 class Board(object): | 113 class Board(object): |
114 """similar to arduinoNode.Board but without the communications stuff""" | 114 """similar to arduinoNode.Board but without the communications stuff""" |
115 def __init__(self, graph, masterGraph, uri): | 115 def __init__(self, graph, masterGraph, uri): |
116 self.graph, self.uri = graph, uri | 116 self.graph, self.uri = graph, uri |
117 self.masterGraph = masterGraph | 117 self.masterGraph = masterGraph |
118 self.masterGraph.patch(Patch(addQuads=self.staticStmts())) | 118 self.masterGraph.setToGraph(self.staticStmts()) |
119 self.pi = pigpio.pi() | 119 self.pi = pigpio.pi() |
120 self._devs = devices.makeDevices(graph, self.uri, self.pi) | 120 self._devs = devices.makeDevices(graph, self.uri, self.pi) |
121 log.debug('found %s devices', len(self._devs)) | 121 log.debug('found %s devices', len(self._devs)) |
122 self._statementsFromInputs = {} # input device uri: latest statements | 122 self._statementsFromInputs = {} # input device uri: latest statements |
123 self._lastPollTime = {} # input device uri: time() | 123 self._lastPollTime = {} # input device uri: time() |
146 if isinstance(new, dict): # new style | 146 if isinstance(new, dict): # new style |
147 oneshot = new['oneshot'] | 147 oneshot = new['oneshot'] |
148 new = new['latest'] | 148 new = new['latest'] |
149 else: | 149 else: |
150 oneshot = None | 150 oneshot = None |
151 prev = self._statementsFromInputs.get(i.uri, set()) | 151 |
152 | 152 self._updateMasterWithNewPollStatements(i.uri, new) |
153 if new or prev: | |
154 self._statementsFromInputs[i.uri] = new | |
155 # it's important that quads from different devices | |
156 # don't clash, since that can lead to inconsistent | |
157 # patches (e.g. | |
158 # dev1 changes value from 1 to 2; | |
159 # dev2 changes value from 2 to 3; | |
160 # dev1 changes from 2 to 4 but this patch will | |
161 # fail since the '2' statement is gone) | |
162 self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), | |
163 inContext(new, i.uri))) | |
164 | 153 |
165 if oneshot: | 154 if oneshot: |
166 self._sendOneshot(oneshot) | 155 self._sendOneshot(oneshot) |
167 self._lastPollTime[i.uri] = now | 156 self._lastPollTime[i.uri] = now |
168 if log.isEnabledFor(logging.DEBUG): | 157 if log.isEnabledFor(logging.DEBUG): |
173 | 162 |
174 pollResults = map(set, self._statementsFromInputs.values()) | 163 pollResults = map(set, self._statementsFromInputs.values()) |
175 if pollResults: | 164 if pollResults: |
176 self._influx.exportToInflux(set.union(*pollResults)) | 165 self._influx.exportToInflux(set.union(*pollResults)) |
177 | 166 |
167 def _updateMasterWithNewPollStatements(self, dev, new): | |
168 prev = self._statementsFromInputs.get(dev, set()) | |
169 | |
170 # it's important that quads from different devices | |
171 # don't clash, since that can lead to inconsistent | |
172 # patches (e.g. | |
173 # dev1 changes value from 1 to 2; | |
174 # dev2 changes value from 2 to 3; | |
175 # dev1 changes from 2 to 4 but this patch will | |
176 # fail since the '2' statement is gone) | |
177 self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), | |
178 inContext(new, dev))) | |
179 self._statementsFromInputs[dev] = new | |
180 | |
178 def _sendOneshot(self, oneshot): | 181 def _sendOneshot(self, oneshot): |
179 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) | 182 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) |
180 for s,p,o in oneshot)).encode('utf8') | 183 for s,p,o in oneshot)).encode('utf8') |
181 url = 'http://bang6:9071/oneShot' | 184 url = 'http://bang6:9071/oneShot' |
182 d = fetch(method='POST', | 185 d = fetch(method='POST', |