Mercurial > code > home > repos > homeauto
comparison service/arduinoNode/arduinoNode.py @ 256:d2c60552fb13
polling timing and error catching
Ignore-this: 2d78d67d367c3286aec4e990a396eee7
author | drewp@bigasterisk.com |
---|---|
date | Mon, 21 Mar 2016 04:21:35 -0700 |
parents | 254df9f881a6 |
children | fc0e42933baa |
comparison
equal
deleted
inserted
replaced
255:3657e3f0d5d0 | 256:d2c60552fb13 |
---|---|
103 | 103 |
104 def open(self): | 104 def open(self): |
105 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, | 105 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, |
106 timeout=2) | 106 timeout=2) |
107 | 107 |
108 def startPolling(self): | 108 def startPolling(self, period=.5): |
109 task.LoopingCall(self._poll).start(.5) | 109 task.LoopingCall(self._poll).start(period) |
110 | 110 |
111 def _poll(self): | 111 def _poll(self): |
112 """ | 112 """ |
113 even boards with no inputs need some polling to see if they're | 113 even boards with no inputs need some polling to see if they're |
114 still ok | 114 still ok |
124 | 124 |
125 def _pollWork(self): | 125 def _pollWork(self): |
126 t1 = time.time() | 126 t1 = time.time() |
127 self.ser.write("\x60\x00") # "poll everything" | 127 self.ser.write("\x60\x00") # "poll everything" |
128 for i in self._polledDevs: | 128 for i in self._polledDevs: |
129 now = time.time() | 129 try: |
130 new = i.readFromPoll(self.ser.read) | 130 now = time.time() |
131 if isinstance(new, dict): # new style | 131 new = i.readFromPoll(self.ser.read) |
132 oneshot = new['oneshot'] | 132 if isinstance(new, dict): # new style |
133 new = new['latest'] | 133 oneshot = new['oneshot'] |
134 else: | 134 new = new['latest'] |
135 oneshot = None | 135 else: |
136 prev = self._statementsFromInputs.get(i.uri, []) | 136 oneshot = None |
137 if new or prev: | 137 prev = self._statementsFromInputs.get(i.uri, []) |
138 self._statementsFromInputs[i.uri] = new | 138 if new or prev: |
139 # it's important that quads from different devices | 139 self._statementsFromInputs[i.uri] = new |
140 # don't clash, since that can lead to inconsistent | 140 # it's important that quads from different devices |
141 # patches (e.g. | 141 # don't clash, since that can lead to inconsistent |
142 # dev1 changes value from 1 to 2; | 142 # patches (e.g. |
143 # dev2 changes value from 2 to 3; | 143 # dev1 changes value from 1 to 2; |
144 # dev1 changes from 2 to 4 but this patch will | 144 # dev2 changes value from 2 to 3; |
145 # fail since the '2' statement is gone) | 145 # dev1 changes from 2 to 4 but this patch will |
146 self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), | 146 # fail since the '2' statement is gone) |
147 inContext(new, i.uri))) | 147 self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri), |
148 if oneshot: | 148 inContext(new, i.uri))) |
149 self._sendOneshot(oneshot) | 149 if oneshot: |
150 self._lastPollTime[i.uri] = now | 150 self._sendOneshot(oneshot) |
151 | 151 self._lastPollTime[i.uri] = now |
152 except: | |
153 log.warn('while polling %r:', i.uri) | |
154 raise | |
152 #plus statements about succeeding or erroring on the last poll | 155 #plus statements about succeeding or erroring on the last poll |
153 byte = self.ser.read(1) | 156 byte = self.ser.read(1) |
154 if byte != 'x': | 157 if byte != 'x': |
155 raise ValueError("after poll, got %x instead of 'x'" % byte) | 158 raise ValueError("after poll, got %x instead of 'x'" % byte) |
156 elapsed = time.time() - t1 | 159 elapsed = time.time() - t1 |
377 for b in boards: | 380 for b in boards: |
378 b.deployToArduino() | 381 b.deployToArduino() |
379 | 382 |
380 log.info('open boards') | 383 log.info('open boards') |
381 for b in boards: | 384 for b in boards: |
382 b.startPolling() | 385 b.startPolling(period=.1 if not arg['-v'] else 10) |
383 | 386 |
384 | 387 |
385 reactor.listenTCP(9059, cyclone.web.Application([ | 388 reactor.listenTCP(9059, cyclone.web.Application([ |
386 (r"/()", cyclone.web.StaticFileHandler, { | 389 (r"/()", cyclone.web.StaticFileHandler, { |
387 "path": "static", "default_filename": "index.html"}), | 390 "path": "static", "default_filename": "index.html"}), |