Mercurial > code > home > repos > homeauto
annotate service/arduinoNode/arduinoNode.py @ 170:376599552a4c
polymer board debug page with working output widgets
Ignore-this: 3157d0c47a91afe47b30a5f182629d93
author | drewp@bigasterisk.com |
---|---|
date | Mon, 13 Apr 2015 23:30:12 -0700 |
parents | d228105749ac |
children | f81c4d3d774b |
rev | line source |
---|---|
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
1 """ |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
2 depends on packages: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
3 arduino-mk |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
4 indent |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
5 """ |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
6 from __future__ import division |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
7 import glob, sys, logging, subprocess, socket, os, hashlib, time, tempfile |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
8 import shutil, json |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
9 import serial |
164 | 10 import cyclone.web |
11 from rdflib import Graph, Namespace, URIRef, Literal, RDF | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
12 from rdflib.parser import StringInputSource |
164 | 13 from twisted.internet import reactor, task |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
14 |
164 | 15 import devices |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
16 import dotrender |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
17 import rdflib_patch |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
18 rdflib_patch.fixQnameOfUriWithTrailingSlash() |
164 | 19 |
20 logging.basicConfig(level=logging.DEBUG) | |
21 | |
22 from loggingserial import LoggingSerial | |
23 | |
24 sys.path.append("/my/site/magma") | |
25 from stategraph import StateGraph | |
26 | |
27 log = logging.getLogger() | |
28 logging.getLogger('serial').setLevel(logging.WARN) | |
29 | |
30 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
31 HOST = Namespace('http://bigasterisk.com/ruler/host/') |
164 | 32 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
33 ACTION_BASE = 10 # higher than any of the fixed command numbers |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
34 |
164 | 35 class Config(object): |
36 def __init__(self): | |
37 self.graph = Graph() | |
38 log.info('read config') | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
39 self.graph.parse('config.n3', format='n3') |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
40 self.graph.bind('', ROOM) # not working |
164 | 41 self.graph.bind('rdf', RDF) |
42 | |
43 def serialDevices(self): | |
44 return dict([(row.dev, row.board) for row in self.graph.query( | |
45 """SELECT ?board ?dev WHERE { | |
46 ?board :device ?dev; | |
47 a :ArduinoBoard . | |
48 }""", initNs={'': ROOM})]) | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
49 |
164 | 50 class Board(object): |
51 """an arduino connected to this computer""" | |
52 baudrate = 115200 | |
53 def __init__(self, dev, graph, uri, onChange): | |
54 """ | |
55 each connected thing has some pins. | |
56 | |
57 We'll call onChange when we know the currentGraph() has | |
58 changed (and not just in creation time). | |
59 """ | |
60 self.uri = uri | |
61 self.graph = graph | |
62 self.dev = dev | |
63 | |
64 # The order of this list needs to be consistent between the | |
65 # deployToArduino call and the poll call. | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
66 self._devs = devices.makeDevices(graph, self.uri) |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
67 self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
68 for devIndex, dev in enumerate(self._devs)) |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
69 self._polledDevs = [d for d in self._devs if d.generatePollCode()] |
164 | 70 |
71 self._statementsFromInputs = {} # input uri: latest statements | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
72 |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
73 self.open() |
164 | 74 |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
75 def description(self): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
76 """for web page""" |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
77 return { |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
78 'uri': self.uri, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
79 'dev': self.dev, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
80 'baudrate': self.baudrate, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
81 'devices': [d.description() for d in self._devs], |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
82 } |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
83 |
164 | 84 def open(self): |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
85 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
86 timeout=2) |
164 | 87 |
88 def startPolling(self): | |
89 task.LoopingCall(self._poll).start(.5) | |
90 | |
91 def _poll(self): | |
92 """ | |
93 even boards with no inputs need some polling to see if they're | |
94 still ok | |
95 """ | |
96 try: | |
97 self._pollWork() | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
98 except serial.SerialException: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
99 reactor.crash() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
100 raise |
164 | 101 except Exception as e: |
102 log.warn("poll: %r" % e) | |
103 | |
104 def _pollWork(self): | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
105 t1 = time.time() |
164 | 106 self.ser.write("\x60\x00") |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
107 for i in self._polledDevs: |
164 | 108 self._statementsFromInputs[i.uri] = i.readFromPoll(self.ser.read) |
109 #plus statements about succeeding or erroring on the last poll | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
110 byte = self.ser.read(1) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
111 if byte != 'x': |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
112 raise ValueError("after poll, got %x instead of 'x'" % byte) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
113 elapsed = time.time() - t1 |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
114 if elapsed > 1.0: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
115 log.warn('poll took %.1f seconds' % elapsed) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
116 |
164 | 117 |
118 def currentGraph(self): | |
119 g = Graph() | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
120 |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
121 g.add((HOST[socket.gethostname()], ROOM['connectedTo'], self.uri)) |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
122 |
164 | 123 for si in self._statementsFromInputs.values(): |
124 for s in si: | |
125 g.add(s) | |
126 return g | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
127 |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
128 def outputStatements(self, stmts): |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
129 unused = set(stmts) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
130 for dev in self._devs: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
131 stmtsForDev = [] |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
132 for pat in dev.outputPatterns(): |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
133 if [term is None for term in pat] != [False, False, True]: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
134 raise NotImplementedError |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
135 for stmt in stmts: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
136 if stmt[:2] == pat[:2]: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
137 stmtsForDev.append(stmt) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
138 unused.discard(stmt) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
139 if stmtsForDev: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
140 log.info("output goes to action handler for %s" % dev.uri) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
141 self.ser.write("\x60" + chr(self._devCommandNum[dev.uri])) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
142 dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
143 if self.ser.read(1) != 'k': |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
144 raise ValueError( |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
145 "%s sendOutput/generateActionCode didn't use " |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
146 "matching output bytes" % dev.__class__) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
147 log.info("success") |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
148 if unused: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
149 log.warn("No devices cared about these statements:") |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
150 for s in unused: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
151 log.warn(repr(s)) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
152 |
164 | 153 def generateArduinoCode(self): |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
154 generated = { |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
155 'baudrate': self.baudrate, |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
156 'includes': '', |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
157 'global': '', |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
158 'setups': '', |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
159 'polls': '', |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
160 'actions': '', |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
161 } |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
162 for attr in ['includes', 'global', 'setups', 'polls', 'actions']: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
163 for dev in self._devs: |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
164 if attr == 'includes': |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
165 gen = '\n'.join('#include "%s"\n' % inc |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
166 for inc in dev.generateIncludes()) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
167 elif attr == 'global': gen = dev.generateGlobalCode() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
168 elif attr == 'setups': gen = dev.generateSetupCode() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
169 elif attr == 'polls': gen = dev.generatePollCode() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
170 elif attr == 'actions': |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
171 code = dev.generateActionCode() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
172 if code: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
173 gen = '''else if (cmd == %(cmdNum)s) { |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
174 %(code)s |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
175 Serial.write('k'); |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
176 } |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
177 ''' % dict(cmdNum=self._devCommandNum[dev.uri], |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
178 code=code) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
179 else: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
180 gen = '' |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
181 else: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
182 raise NotImplementedError |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
183 |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
184 if gen: |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
185 generated[attr] += '// for %s\n%s\n' % (dev.uri, gen) |
164 | 186 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
187 code = ''' |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
188 %(includes)s |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
189 |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
190 %(global)s |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
191 |
164 | 192 void setup() { |
193 Serial.begin(%(baudrate)d); | |
194 Serial.flush(); | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
195 %(setups)s |
164 | 196 } |
197 | |
198 void loop() { | |
199 byte head, cmd; | |
200 if (Serial.available() >= 2) { | |
201 head = Serial.read(); | |
202 if (head != 0x60) { | |
203 Serial.flush(); | |
204 return; | |
205 } | |
206 cmd = Serial.read(); | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
207 if (cmd == 0x00) { // poll |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
208 %(polls)s |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
209 Serial.write('x'); |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
210 } else if (cmd == 0x01) { // get code checksum |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
211 Serial.write("CODE_CHECKSUM"); |
164 | 212 } |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
213 %(actions)s |
164 | 214 } |
215 } | |
216 ''' % generated | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
217 try: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
218 with tempfile.SpooledTemporaryFile() as codeFile: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
219 codeFile.write(code) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
220 codeFile.seek(0) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
221 code = subprocess.check_output([ |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
222 'indent', |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
223 '-linux', |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
224 '-fc1', # ok to indent comments |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
225 '-i4', # 4-space indent |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
226 '-sob' # swallow blanks (not working) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
227 ], stdin=codeFile) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
228 except OSError as e: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
229 log.warn("indent failed (%r)", e) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
230 cksum = hashlib.sha1(code).hexdigest() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
231 code = code.replace('CODE_CHECKSUM', cksum) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
232 return code, cksum |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
233 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
234 def _readBoardChecksum(self, length): |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
235 # this is likely right after reset, so it might take 2 seconds |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
236 for tries in range(6): |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
237 self.ser.write("\x60\x01") |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
238 try: |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
239 return self.ser.read(length) |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
240 except ValueError: |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
241 if tries == 5: |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
242 raise |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
243 time.sleep(.5) |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
244 raise ValueError |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
245 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
246 def _boardIsCurrent(self, currentChecksum): |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
247 try: |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
248 boardCksum = self._readBoardChecksum(len(currentChecksum)) |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
249 if boardCksum == currentChecksum: |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
250 log.info("board has current code (%s)" % currentChecksum) |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
251 return True |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
252 else: |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
253 log.info("board responds with incorrect code version") |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
254 except Exception as e: |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
255 log.info("can't get code version from board: %r" % e) |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
256 return False |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
257 |
164 | 258 def deployToArduino(self): |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
259 code, cksum = self.generateArduinoCode() |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
260 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
261 if self._boardIsCurrent(cksum): |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
262 return |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
263 |
164 | 264 try: |
265 if hasattr(self, 'ser'): | |
266 self.ser.close() | |
267 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy') | |
268 try: | |
269 self._arduinoMake(workDir, code) | |
270 finally: | |
271 shutil.rmtree(workDir) | |
272 finally: | |
273 self.open() | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
274 |
164 | 275 def _arduinoMake(self, workDir, code): |
276 with open(workDir + '/makefile', 'w') as makefile: | |
277 makefile.write(''' | |
278 BOARD_TAG = %(tag)s | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
279 USER_LIB_PATH := %(libs)s |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
280 ARDUINO_LIBS = %(arduinoLibs)s |
164 | 281 MONITOR_PORT = %(dev)s |
282 | |
283 include /usr/share/arduino/Arduino.mk | |
284 ''' % { | |
285 'dev': self.dev, | |
286 'tag': self.graph.value(self.uri, ROOM['boardTag']), | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
287 'libs': os.path.abspath('arduino-libraries'), |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
288 'arduinoLibs': ' '.join(sum((d.generateArduinoLibs() |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
289 for d in self._devs), [])), |
164 | 290 }) |
291 | |
292 with open(workDir + '/main.ino', 'w') as main: | |
293 main.write(code) | |
294 | |
295 subprocess.check_call(['make', 'upload'], cwd=workDir) | |
296 | |
297 | |
298 class GraphPage(cyclone.web.RequestHandler): | |
299 def get(self): | |
300 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host']) | |
301 | |
302 for b in self.settings.boards: | |
303 for stmt in b.currentGraph(): | |
304 g.add(stmt) | |
305 | |
306 if self.get_argument('config', 'no') == 'yes': | |
307 for stmt in self.settings.config.graph: | |
308 g.add(stmt) | |
309 | |
310 self.set_header('Content-type', 'application/x-trig') | |
311 self.write(g.asTrig()) | |
312 | |
313 class Dot(cyclone.web.RequestHandler): | |
314 def get(self): | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
315 configGraph = self.settings.config.graph |
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
316 dot = dotrender.render(configGraph, self.settings.boards) |
164 | 317 self.write(dot) |
318 | |
319 class ArduinoCode(cyclone.web.RequestHandler): | |
320 def get(self): | |
321 board = [b for b in self.settings.boards if | |
322 b.uri == URIRef(self.get_argument('board'))][0] | |
323 self.set_header('Content-type', 'text/plain') | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
324 code, cksum = board.generateArduinoCode() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
325 self.write(code) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
326 |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
327 def rdfGraphBody(body, headers): |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
328 g = Graph() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
329 g.parse(StringInputSource(body), format='nt') |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
330 return g |
164 | 331 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
332 class OutputPage(cyclone.web.RequestHandler): |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
333 def post(self): |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
334 stmts = list(rdfGraphBody(self.request.body, self.request.headers)) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
335 for b in self.settings.boards: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
336 b.outputStatements(stmts) |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
337 |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
338 class Boards(cyclone.web.RequestHandler): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
339 def get(self): |
164 | 340 |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
341 self.set_header('Content-type', 'application/json') |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
342 self.write(json.dumps({ |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
343 'boards': [b.description() for b in self.settings.boards] |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
344 }, indent=2)) |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
345 |
164 | 346 def currentSerialDevices(): |
347 log.info('find connected boards') | |
348 return glob.glob('/dev/serial/by-id/*') | |
349 | |
350 def main(): | |
351 config = Config() | |
352 current = currentSerialDevices() | |
353 | |
354 def onChange(): | |
355 # notify reasoning | |
356 pass | |
357 | |
358 boards = [] | |
359 for dev, board in config.serialDevices().items(): | |
360 if str(dev) not in current: | |
361 continue | |
362 log.info("we have board %s connected at %s" % (board, dev)) | |
363 b = Board(dev, config.graph, board, onChange) | |
364 boards.append(b) | |
365 | |
166
c0180bd2b33a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents:
164
diff
changeset
|
366 boards[0].deployToArduino() |
164 | 367 |
368 log.info('open boards') | |
369 for b in boards: | |
370 b.startPolling() | |
371 | |
372 from twisted.python import log as twlog | |
373 twlog.startLogging(sys.stdout) | |
374 | |
375 log.setLevel(logging.DEBUG) | |
376 reactor.listenTCP(9059, cyclone.web.Application([ | |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
377 (r"/()", cyclone.web.StaticFileHandler, { |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
378 "path": "static", "default_filename": "index.html"}), |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
379 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}), |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
380 (r'/boards', Boards), |
164 | 381 (r"/graph", GraphPage), |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
382 (r'/output', OutputPage), |
164 | 383 (r'/arduinoCode', ArduinoCode), |
384 (r'/dot', Dot), | |
385 ], config=config, boards=boards)) | |
386 reactor.run() | |
387 | |
388 main() |