Mercurial > code > home > repos > homeauto
comparison service/piNode/piNode.py @ 1048:f3c7f617c335
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
Ignore-this: 4ef38c7a2ed35f77035209bffeffc245
darcs-hash:a7ca5ba671d2919a5de413acc1ca8b8bf1eea31b
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Tue, 02 Feb 2016 22:37:11 -0800 |
parents | ffe6a00c6cef |
children | 254df9f881a6 |
comparison
equal
deleted
inserted
replaced
1047:e3d77b7f16ca | 1048:f3c7f617c335 |
---|---|
55 self.masterGraph.patch(Patch(addQuads=self.staticStmts())) | 55 self.masterGraph.patch(Patch(addQuads=self.staticStmts())) |
56 self.pi = pigpio.pi() | 56 self.pi = pigpio.pi() |
57 self._devs = devices.makeDevices(graph, self.uri, self.pi) | 57 self._devs = devices.makeDevices(graph, self.uri, self.pi) |
58 log.debug('found %s devices', len(self._devs)) | 58 log.debug('found %s devices', len(self._devs)) |
59 self._statementsFromInputs = {} # input device uri: latest statements | 59 self._statementsFromInputs = {} # input device uri: latest statements |
60 self._lastPollTime = {} # input device uri: time() | |
60 self._carbon = CarbonClient(serverHost='bang') | 61 self._carbon = CarbonClient(serverHost='bang') |
61 for d in self._devs: | 62 for d in self._devs: |
62 self.syncMasterGraphToHostStatements(d) | 63 self.syncMasterGraphToHostStatements(d) |
64 | |
63 def startPolling(self): | 65 def startPolling(self): |
64 task.LoopingCall(self._poll).start(.5) | 66 task.LoopingCall(self._poll).start(.05) |
65 | 67 |
66 def _poll(self): | 68 def _poll(self): |
67 for i in self._devs: | 69 for i in self._devs: |
68 prev = inContext(self._statementsFromInputs.get(i.uri, []), CTX) | 70 now = time.time() |
69 new = self._statementsFromInputs[i.uri] = i.poll() | 71 if (hasattr(i, 'pollPeriod') and |
70 new = inContext(new, CTX) | 72 self._lastPollTime.get(i.uri, 0) + i.pollPeriod > now): |
71 self.masterGraph.patch(Patch.fromDiff(prev, new)) | 73 continue |
74 new = i.poll() | |
75 prev = self._statementsFromInputs.get(i.uri, []) | |
76 if new or prev: | |
77 self._statementsFromInputs[i.uri] = new | |
78 # it's important that quads from different devices | |
79 # don't clash, since that can lead to inconsistent | |
80 # patches (e.g. | |
81 # dev1 changes value from 1 to 2; | |
82 # dev2 changes value from 2 to 3; | |
83 # dev1 changes from 2 to 4 but this patch will | |
84 # fail since the '2' statement is gone) | |
85 self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), | |
86 inContext(new, i.uri))) | |
87 self._lastPollTime[i.uri] = now | |
72 self._exportToGraphite() | 88 self._exportToGraphite() |
73 | 89 |
74 def _exportToGraphite(self): | 90 def _exportToGraphite(self): |
75 # note this is writing way too often- graphite is storing at a lower res | 91 # note this is writing way too often- graphite is storing at a lower res |
76 now = time.time() | 92 now = time.time() |