Mercurial > code > home > repos > homeauto
annotate service/arduinoNode/arduinoNode.py @ 185:2161c71c7b02
support for device code in the idle loop
Ignore-this: 7454ba4c9bab3c5393707af7ac91547
author | drewp@bigasterisk.com |
---|---|
date | Wed, 08 Jul 2015 01:19:21 -0700 |
parents | f81c4d3d774b |
children | ad20e5cb4feb |
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 |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
14 from docopt import docopt |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
15 |
164 | 16 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
|
17 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
|
18 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
|
19 rdflib_patch.fixQnameOfUriWithTrailingSlash() |
164 | 20 |
21 logging.basicConfig(level=logging.DEBUG) | |
22 | |
23 from loggingserial import LoggingSerial | |
24 | |
25 sys.path.append("/my/site/magma") | |
26 from stategraph import StateGraph | |
27 | |
28 log = logging.getLogger() | |
29 logging.getLogger('serial').setLevel(logging.WARN) | |
30 | |
31 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
|
32 HOST = Namespace('http://bigasterisk.com/ruler/host/') |
164 | 33 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
34 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
|
35 |
164 | 36 class Config(object): |
37 def __init__(self): | |
38 self.graph = Graph() | |
39 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
|
40 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
|
41 self.graph.bind('', ROOM) # not working |
164 | 42 self.graph.bind('rdf', RDF) |
43 | |
44 def serialDevices(self): | |
45 return dict([(row.dev, row.board) for row in self.graph.query( | |
46 """SELECT ?board ?dev WHERE { | |
47 ?board :device ?dev; | |
48 a :ArduinoBoard . | |
49 }""", 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
|
50 |
164 | 51 class Board(object): |
52 """an arduino connected to this computer""" | |
53 baudrate = 115200 | |
54 def __init__(self, dev, graph, uri, onChange): | |
55 """ | |
56 each connected thing has some pins. | |
57 | |
58 We'll call onChange when we know the currentGraph() has | |
59 changed (and not just in creation time). | |
60 """ | |
61 self.uri = uri | |
62 self.graph = graph | |
63 self.dev = dev | |
64 | |
65 # The order of this list needs to be consistent between the | |
66 # 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
|
67 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
|
68 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
|
69 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
|
70 self._polledDevs = [d for d in self._devs if d.generatePollCode()] |
164 | 71 |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
72 self._statementsFromInputs = {} # input device 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
|
73 |
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
|
74 self.open() |
164 | 75 |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
76 def description(self): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
77 """for web page""" |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
78 return { |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
79 'uri': self.uri, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
80 'dev': self.dev, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
81 'baudrate': self.baudrate, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
82 'devices': [d.description() for d in self._devs], |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
83 } |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
84 |
164 | 85 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
|
86 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
|
87 timeout=2) |
164 | 88 |
89 def startPolling(self): | |
90 task.LoopingCall(self._poll).start(.5) | |
91 | |
92 def _poll(self): | |
93 """ | |
94 even boards with no inputs need some polling to see if they're | |
95 still ok | |
96 """ | |
97 try: | |
98 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
|
99 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
|
100 reactor.crash() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
101 raise |
164 | 102 except Exception as e: |
103 log.warn("poll: %r" % e) | |
104 | |
105 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
|
106 t1 = time.time() |
164 | 107 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
|
108 for i in self._polledDevs: |
164 | 109 self._statementsFromInputs[i.uri] = i.readFromPoll(self.ser.read) |
110 #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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 |
164 | 118 |
119 def currentGraph(self): | |
120 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
|
121 |
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 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
|
123 |
164 | 124 for si in self._statementsFromInputs.values(): |
125 for s in si: | |
126 g.add(s) | |
127 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
|
128 |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 stmtsForDev = [] |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
133 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
|
134 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
|
135 raise NotImplementedError |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 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
|
140 if stmtsForDev: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 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
|
145 raise ValueError( |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
146 "%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
|
147 "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
|
148 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
|
149 if unused: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 |
164 | 154 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
|
155 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
|
156 '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
|
157 '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
|
158 '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
|
159 'setups': '', |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
160 'polls': '', |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
161 'idles': '', |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
162 '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
|
163 } |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
164 for attr in ['includes', 'global', 'setups', 'polls', 'idles', |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
165 'actions']: |
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 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 elif attr == 'polls': gen = dev.generatePollCode() |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
173 elif attr == 'idles': gen = dev.generateIdleCode() |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
174 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
|
175 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
|
176 if code: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
177 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
|
178 %(code)s |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
179 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
|
180 } |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
181 ''' % 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
|
182 code=code) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
183 else: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
184 gen = '' |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
185 else: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
186 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
|
187 |
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 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
|
189 generated[attr] += '// for %s\n%s\n' % (dev.uri, gen) |
164 | 190 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
191 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
|
192 %(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
|
193 |
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
|
194 %(global)s |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
195 byte frame=0; |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
196 unsigned long lastFrame=0; |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
197 |
164 | 198 void setup() { |
199 Serial.begin(%(baudrate)d); | |
200 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
|
201 %(setups)s |
164 | 202 } |
203 | |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
204 void idle() { |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
205 // this slowdown is to spend somewhat less time PWMing, to reduce |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
206 // leaking from on channels to off ones (my shift register has no |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
207 // latching) |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
208 if (micros() < lastFrame + 128) { |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
209 return; |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
210 } |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
211 lastFrame = micros(); |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
212 frame++; |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
213 %(idles)s |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
214 } |
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
215 |
164 | 216 void loop() { |
217 byte head, cmd; | |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
218 idle(); |
164 | 219 if (Serial.available() >= 2) { |
220 head = Serial.read(); | |
221 if (head != 0x60) { | |
222 Serial.flush(); | |
223 return; | |
224 } | |
225 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
|
226 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
|
227 %(polls)s |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
228 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
|
229 } 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
|
230 Serial.write("CODE_CHECKSUM"); |
164 | 231 } |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
232 %(actions)s |
164 | 233 } |
234 } | |
235 ''' % generated | |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
236 try: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 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
|
241 'indent', |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
242 '-linux', |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
243 '-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
|
244 '-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
|
245 '-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
|
246 ], stdin=codeFile) |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
253 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
|
254 # 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
265 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
|
266 try: |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
267 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
|
268 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 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
|
276 |
164 | 277 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
|
278 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
|
279 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
280 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
|
281 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
|
282 |
164 | 283 try: |
284 if hasattr(self, 'ser'): | |
285 self.ser.close() | |
286 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy') | |
287 try: | |
288 self._arduinoMake(workDir, code) | |
289 finally: | |
290 shutil.rmtree(workDir) | |
291 finally: | |
292 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
|
293 |
164 | 294 def _arduinoMake(self, workDir, code): |
295 with open(workDir + '/makefile', 'w') as makefile: | |
296 makefile.write(''' | |
297 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
|
298 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
|
299 ARDUINO_LIBS = %(arduinoLibs)s |
164 | 300 MONITOR_PORT = %(dev)s |
301 | |
302 include /usr/share/arduino/Arduino.mk | |
303 ''' % { | |
304 'dev': self.dev, | |
305 '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
|
306 '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
|
307 '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
|
308 for d in self._devs), [])), |
164 | 309 }) |
310 | |
311 with open(workDir + '/main.ino', 'w') as main: | |
312 main.write(code) | |
313 | |
314 subprocess.check_call(['make', 'upload'], cwd=workDir) | |
315 | |
316 | |
317 class GraphPage(cyclone.web.RequestHandler): | |
318 def get(self): | |
319 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host']) | |
320 | |
321 for b in self.settings.boards: | |
322 for stmt in b.currentGraph(): | |
323 g.add(stmt) | |
324 | |
325 if self.get_argument('config', 'no') == 'yes': | |
326 for stmt in self.settings.config.graph: | |
327 g.add(stmt) | |
328 | |
329 self.set_header('Content-type', 'application/x-trig') | |
330 self.write(g.asTrig()) | |
331 | |
332 class Dot(cyclone.web.RequestHandler): | |
333 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
|
334 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
|
335 dot = dotrender.render(configGraph, self.settings.boards) |
164 | 336 self.write(dot) |
337 | |
338 class ArduinoCode(cyclone.web.RequestHandler): | |
339 def get(self): | |
340 board = [b for b in self.settings.boards if | |
341 b.uri == URIRef(self.get_argument('board'))][0] | |
342 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
|
343 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
|
344 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
|
345 |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
346 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
|
347 g = Graph() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
348 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
|
349 return g |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
350 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
351 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
|
352 def post(self): |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
353 # for old ui; use PUT instead |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
354 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
|
355 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
|
356 b.outputStatements(stmts) |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
357 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
358 def put(self): |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
359 subj = URIRef(self.get_argument('s')) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
360 pred = URIRef(self.get_argument('p')) |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
361 |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
362 turtleLiteral = self.request.body |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
363 try: |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
364 obj = Literal(float(turtleLiteral)) |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
365 except ValueError: |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
366 obj = Literal(turtleLiteral) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
367 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
368 stmt = (subj, pred, obj) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
369 for b in self.settings.boards: |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
370 b.outputStatements([stmt]) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
371 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
372 |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
373 class Boards(cyclone.web.RequestHandler): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
374 def get(self): |
164 | 375 |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
376 self.set_header('Content-type', 'application/json') |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
377 self.write(json.dumps({ |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
378 '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
|
379 }, indent=2)) |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
380 |
164 | 381 def currentSerialDevices(): |
382 log.info('find connected boards') | |
383 return glob.glob('/dev/serial/by-id/*') | |
384 | |
385 def main(): | |
176
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
386 arg = docopt(""" |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
387 Usage: reasoning.py [options] |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
388 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
389 -v Verbose |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
390 """) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
391 log.setLevel(logging.WARN) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
392 if arg['-v']: |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
393 from twisted.python import log as twlog |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
394 twlog.startLogging(sys.stdout) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
395 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
396 log.setLevel(logging.DEBUG) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
397 |
164 | 398 config = Config() |
399 current = currentSerialDevices() | |
400 | |
401 def onChange(): | |
402 # notify reasoning | |
403 pass | |
404 | |
405 boards = [] | |
406 for dev, board in config.serialDevices().items(): | |
407 if str(dev) not in current: | |
408 continue | |
409 log.info("we have board %s connected at %s" % (board, dev)) | |
410 b = Board(dev, config.graph, board, onChange) | |
411 boards.append(b) | |
412 | |
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
|
413 boards[0].deployToArduino() |
164 | 414 |
415 log.info('open boards') | |
416 for b in boards: | |
417 b.startPolling() | |
418 | |
419 reactor.listenTCP(9059, cyclone.web.Application([ | |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
420 (r"/()", cyclone.web.StaticFileHandler, { |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
421 "path": "static", "default_filename": "index.html"}), |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
422 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}), |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
423 (r'/boards', Boards), |
164 | 424 (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
|
425 (r'/output', OutputPage), |
164 | 426 (r'/arduinoCode', ArduinoCode), |
427 (r'/dot', Dot), | |
428 ], config=config, boards=boards)) | |
429 reactor.run() | |
430 | |
431 main() |