Mercurial > code > home > repos > homeauto
changeset 1061:ef635e9b9dce
polling timing and error catching
Ignore-this: 2d78d67d367c3286aec4e990a396eee7
darcs-hash:bb100f6ba2dec95f0beb03cf8bf9ce733d1e6cc5
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 21 Mar 2016 04:21:35 -0700 |
parents | 0cd92c2f8cf4 |
children | 6ab7a933c2a8 |
files | service/arduinoNode/arduinoNode.py |
diffstat | 1 files changed, 29 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/service/arduinoNode/arduinoNode.py Mon Mar 21 04:21:06 2016 -0700 +++ b/service/arduinoNode/arduinoNode.py Mon Mar 21 04:21:35 2016 -0700 @@ -105,8 +105,8 @@ self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, timeout=2) - def startPolling(self): - task.LoopingCall(self._poll).start(.5) + def startPolling(self, period=.5): + task.LoopingCall(self._poll).start(period) def _poll(self): """ @@ -126,29 +126,32 @@ t1 = time.time() self.ser.write("\x60\x00") # "poll everything" for i in self._polledDevs: - now = time.time() - new = i.readFromPoll(self.ser.read) - if isinstance(new, dict): # new style - oneshot = new['oneshot'] - new = new['latest'] - else: - oneshot = None - prev = self._statementsFromInputs.get(i.uri, []) - if new or prev: - self._statementsFromInputs[i.uri] = new - # it's important that quads from different devices - # don't clash, since that can lead to inconsistent - # patches (e.g. - # dev1 changes value from 1 to 2; - # dev2 changes value from 2 to 3; - # dev1 changes from 2 to 4 but this patch will - # fail since the '2' statement is gone) - self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), - inContext(new, i.uri))) - if oneshot: - self._sendOneshot(oneshot) - self._lastPollTime[i.uri] = now - + try: + now = time.time() + new = i.readFromPoll(self.ser.read) + if isinstance(new, dict): # new style + oneshot = new['oneshot'] + new = new['latest'] + else: + oneshot = None + prev = self._statementsFromInputs.get(i.uri, []) + if new or prev: + self._statementsFromInputs[i.uri] = new + # it's important that quads from different devices + # don't clash, since that can lead to inconsistent + # patches (e.g. + # dev1 changes value from 1 to 2; + # dev2 changes value from 2 to 3; + # dev1 changes from 2 to 4 but this patch will + # fail since the '2' statement is gone) + self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), + inContext(new, i.uri))) + if oneshot: + self._sendOneshot(oneshot) + self._lastPollTime[i.uri] = now + except: + log.warn('while polling %r:', i.uri) + raise #plus statements about succeeding or erroring on the last poll byte = self.ser.read(1) if byte != 'x': @@ -379,7 +382,7 @@ log.info('open boards') for b in boards: - b.startPolling() + b.startPolling(period=.1 if not arg['-v'] else 10) reactor.listenTCP(9059, cyclone.web.Application([