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