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