Mercurial > code > home > repos > homeauto
annotate service/arduinoNode/arduinoNode.py @ 1034:43a2170bbdb8
refactor to write_arduino_node
Ignore-this: 7b613fe7aa1cef2c88f190db1fcc1c4a
darcs-hash:c3e034630c13dbaf7a73397f82ffc0da68be32fb
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Thu, 28 Jan 2016 00:21:31 -0800 |
parents | 208b960fde31 |
children | 0aa54404df19 |
rev | line source |
---|---|
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
1 """ |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
2 depends on packages: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
3 arduino-mk |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
4 indent |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
5 """ |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
6 from __future__ import division |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
7 import glob, sys, logging, subprocess, socket, os, hashlib, time, tempfile |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
8 import shutil, json |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
9 import serial |
969 | 10 import cyclone.web |
11 from rdflib import Graph, Namespace, URIRef, Literal, RDF | |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
12 from rdflib.parser import StringInputSource |
969 | 13 from twisted.internet import reactor, task |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
14 from docopt import docopt |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
15 |
969 | 16 import devices |
1034
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
17 import write_arduino_code |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
18 import dotrender |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
19 import rdflib_patch |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
20 rdflib_patch.fixQnameOfUriWithTrailingSlash() |
969 | 21 |
22 logging.basicConfig(level=logging.DEBUG) | |
23 | |
24 from loggingserial import LoggingSerial | |
25 | |
26 sys.path.append("/my/site/magma") | |
27 from stategraph import StateGraph | |
28 | |
1018
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
29 sys.path.append("/my/proj/room") |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
30 from carbondata import CarbonClient |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
31 |
969 | 32 log = logging.getLogger() |
33 logging.getLogger('serial').setLevel(logging.WARN) | |
34 | |
35 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
36 HOST = Namespace('http://bigasterisk.com/ruler/host/') |
969 | 37 |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
38 ACTION_BASE = 10 # higher than any of the fixed command numbers |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
39 |
969 | 40 class Config(object): |
41 def __init__(self): | |
42 self.graph = Graph() | |
43 log.info('read config') | |
1019
93cc6238f5ce
support a dir of config files
drewp <drewp@bigasterisk.com>
parents:
1018
diff
changeset
|
44 for f in os.listdir('config'): |
93cc6238f5ce
support a dir of config files
drewp <drewp@bigasterisk.com>
parents:
1018
diff
changeset
|
45 if f.startswith('.'): continue |
93cc6238f5ce
support a dir of config files
drewp <drewp@bigasterisk.com>
parents:
1018
diff
changeset
|
46 self.graph.parse('config/%s' % f, format='n3') |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
47 self.graph.bind('', ROOM) # not working |
969 | 48 self.graph.bind('rdf', RDF) |
49 | |
50 def serialDevices(self): | |
51 return dict([(row.dev, row.board) for row in self.graph.query( | |
52 """SELECT ?board ?dev WHERE { | |
53 ?board :device ?dev; | |
54 a :ArduinoBoard . | |
55 }""", initNs={'': ROOM})]) | |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
56 |
969 | 57 class Board(object): |
58 """an arduino connected to this computer""" | |
59 baudrate = 115200 | |
60 def __init__(self, dev, graph, uri, onChange): | |
61 """ | |
62 each connected thing has some pins. | |
63 | |
64 We'll call onChange when we know the currentGraph() has | |
65 changed (and not just in creation time). | |
66 """ | |
67 self.uri = uri | |
68 self.graph = graph | |
69 self.dev = dev | |
70 | |
71 # The order of this list needs to be consistent between the | |
72 # deployToArduino call and the poll call. | |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
73 self._devs = devices.makeDevices(graph, self.uri) |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
74 self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
75 for devIndex, dev in enumerate(self._devs)) |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
76 self._polledDevs = [d for d in self._devs if d.generatePollCode()] |
969 | 77 |
990
11fff6301027
support for device code in the idle loop
drewp <drewp@bigasterisk.com>
parents:
981
diff
changeset
|
78 self._statementsFromInputs = {} # input device uri: latest statements |
1018
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
79 self._carbon = CarbonClient(serverHost='bang') |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
80 self.open() |
969 | 81 |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
82 def description(self): |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
83 """for web page""" |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
84 return { |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
85 'uri': self.uri, |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
86 'dev': self.dev, |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
87 'baudrate': self.baudrate, |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
88 'devices': [d.description() for d in self._devs], |
995
f72068e9d9d8
serve ipv6 and use localhost's ipv6 name for getting updates
drewp <drewp@bigasterisk.com>
parents:
990
diff
changeset
|
89 'graph': 'http://%s6:9059/graph' % socket.gethostname(), #todo |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
90 } |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
91 |
969 | 92 def open(self): |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
93 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate, |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
94 timeout=2) |
969 | 95 |
96 def startPolling(self): | |
97 task.LoopingCall(self._poll).start(.5) | |
98 | |
99 def _poll(self): | |
100 """ | |
101 even boards with no inputs need some polling to see if they're | |
102 still ok | |
103 """ | |
104 try: | |
105 self._pollWork() | |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
106 except serial.SerialException: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
107 reactor.crash() |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
108 raise |
969 | 109 except Exception as e: |
110 log.warn("poll: %r" % e) | |
111 | |
112 def _pollWork(self): | |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
113 t1 = time.time() |
969 | 114 self.ser.write("\x60\x00") |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
115 for i in self._polledDevs: |
969 | 116 self._statementsFromInputs[i.uri] = i.readFromPoll(self.ser.read) |
117 #plus statements about succeeding or erroring on the last poll | |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
118 byte = self.ser.read(1) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
119 if byte != 'x': |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
120 raise ValueError("after poll, got %x instead of 'x'" % byte) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
121 elapsed = time.time() - t1 |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
122 if elapsed > 1.0: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
123 log.warn('poll took %.1f seconds' % elapsed) |
1018
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
124 self._exportToGraphite() |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
125 |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
126 def _exportToGraphite(self): |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
127 # note this is writing way too often- graphite is storing at a lower res |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
128 now = time.time() |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
129 # objects of these statements are suitable as graphite values. |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
130 graphitePredicates = {ROOM['temperatureF']} |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
131 for s, graphiteName in self.graph.subject_objects(ROOM['graphiteName']): |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
132 for group in self._statementsFromInputs.values(): |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
133 for stmt in group: |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
134 if stmt[0] == s and stmt[1] in graphitePredicates: |
651e5c5b1f3d
write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents:
997
diff
changeset
|
135 self._carbon.send(graphiteName, stmt[2].toPython(), now) |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
136 |
969 | 137 def currentGraph(self): |
138 g = Graph() | |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
139 |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
140 g.add((HOST[socket.gethostname()], ROOM['connectedTo'], self.uri)) |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
141 |
969 | 142 for si in self._statementsFromInputs.values(): |
143 for s in si: | |
144 g.add(s) | |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
145 for dev in self._devs: |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
146 for stmt in dev.hostStatements(): |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
147 g.add(stmt) |
969 | 148 return g |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
149 |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
150 def outputStatements(self, stmts): |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
151 unused = set(stmts) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
152 for dev in self._devs: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
153 stmtsForDev = [] |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
154 for pat in dev.outputPatterns(): |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
155 if [term is None for term in pat] != [False, False, True]: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
156 raise NotImplementedError |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
157 for stmt in stmts: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
158 if stmt[:2] == pat[:2]: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
159 stmtsForDev.append(stmt) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
160 unused.discard(stmt) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
161 if stmtsForDev: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
162 log.info("output goes to action handler for %s" % dev.uri) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
163 self.ser.write("\x60" + chr(self._devCommandNum[dev.uri])) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
164 dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
165 if self.ser.read(1) != 'k': |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
166 raise ValueError( |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
167 "%s sendOutput/generateActionCode didn't use " |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
168 "matching output bytes" % dev.__class__) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
169 log.info("success") |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
170 if unused: |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
171 log.info("Board %s doesn't care about these statements:", self.uri) |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
172 for s in unused: |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
173 log.info("%r", s) |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
174 |
969 | 175 def generateArduinoCode(self): |
1034
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
176 code = write_arduino_code.writeCode(self.baudrate, self._devs, self._devCommandNum) |
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
177 code = write_arduino_code.indent(code) |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
178 cksum = hashlib.sha1(code).hexdigest() |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
179 code = code.replace('CODE_CHECKSUM', cksum) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
180 return code, cksum |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
181 |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
182 def _readBoardChecksum(self, length): |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
183 # this is likely right after reset, so it might take 2 seconds |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
184 for tries in range(6): |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
185 self.ser.write("\x60\x01") |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
186 try: |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
187 return self.ser.read(length) |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
188 except ValueError: |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
189 if tries == 5: |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
190 raise |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
191 time.sleep(.5) |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
192 raise ValueError |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
193 |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
194 def _boardIsCurrent(self, currentChecksum): |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
195 try: |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
196 boardCksum = self._readBoardChecksum(len(currentChecksum)) |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
197 if boardCksum == currentChecksum: |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
198 log.info("board has current code (%s)" % currentChecksum) |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
199 return True |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
200 else: |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
201 log.info("board responds with incorrect code version") |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
202 except Exception as e: |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
203 log.info("can't get code version from board: %r" % e) |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
204 return False |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
205 |
969 | 206 def deployToArduino(self): |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
207 code, cksum = self.generateArduinoCode() |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
208 |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
209 if self._boardIsCurrent(cksum): |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
210 return |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
211 |
969 | 212 try: |
213 if hasattr(self, 'ser'): | |
214 self.ser.close() | |
215 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy') | |
216 try: | |
217 self._arduinoMake(workDir, code) | |
218 finally: | |
219 shutil.rmtree(workDir) | |
220 finally: | |
221 self.open() | |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
222 |
969 | 223 def _arduinoMake(self, workDir, code): |
224 with open(workDir + '/makefile', 'w') as makefile: | |
1034
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
225 makefile.write(write_arduino_code.writeMakefile( |
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
226 dev=self.dev, |
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
227 tag=self.graph.value(self.uri, ROOM['boardTag']), |
43a2170bbdb8
refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents:
1027
diff
changeset
|
228 allLibs=sum((d.generateArduinoLibs() for d in self._devs), []))) |
969 | 229 |
230 with open(workDir + '/main.ino', 'w') as main: | |
231 main.write(code) | |
232 | |
233 subprocess.check_call(['make', 'upload'], cwd=workDir) | |
234 | |
235 | |
236 class GraphPage(cyclone.web.RequestHandler): | |
237 def get(self): | |
238 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host']) | |
239 | |
240 for b in self.settings.boards: | |
241 for stmt in b.currentGraph(): | |
242 g.add(stmt) | |
243 | |
244 if self.get_argument('config', 'no') == 'yes': | |
245 for stmt in self.settings.config.graph: | |
246 g.add(stmt) | |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
247 |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
248 if self.request.headers.get('accept') == 'application/ld+json': |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
249 self.set_header('Content-type', 'application/ld+json') |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
250 self.write(g.asJsonLd()) |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
251 return |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
252 |
969 | 253 self.set_header('Content-type', 'application/x-trig') |
254 self.write(g.asTrig()) | |
255 | |
256 class Dot(cyclone.web.RequestHandler): | |
257 def get(self): | |
971
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
258 configGraph = self.settings.config.graph |
fbe72d44f15a
only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents:
969
diff
changeset
|
259 dot = dotrender.render(configGraph, self.settings.boards) |
969 | 260 self.write(dot) |
261 | |
262 class ArduinoCode(cyclone.web.RequestHandler): | |
263 def get(self): | |
264 board = [b for b in self.settings.boards if | |
265 b.uri == URIRef(self.get_argument('board'))][0] | |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
266 self.set_header('Content-Type', 'text/plain') |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
267 code, cksum = board.generateArduinoCode() |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
268 self.write(code) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
269 |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
270 def rdfGraphBody(body, headers): |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
271 g = Graph() |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
272 g.parse(StringInputSource(body), format='nt') |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
273 return g |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
274 |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
275 class OutputPage(cyclone.web.RequestHandler): |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
276 def post(self): |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
277 # for old ui; use PUT instead |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
278 stmts = list(rdfGraphBody(self.request.body, self.request.headers)) |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
279 for b in self.settings.boards: |
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
280 b.outputStatements(stmts) |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
281 |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
282 def put(self): |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
283 subj = URIRef(self.get_argument('s')) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
284 pred = URIRef(self.get_argument('p')) |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
285 |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
286 turtleLiteral = self.request.body |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
287 try: |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
288 obj = Literal(float(turtleLiteral)) |
990
11fff6301027
support for device code in the idle loop
drewp <drewp@bigasterisk.com>
parents:
981
diff
changeset
|
289 except ValueError: |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
290 obj = Literal(turtleLiteral) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
291 |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
292 stmt = (subj, pred, obj) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
293 for b in self.settings.boards: |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
294 b.outputStatements([stmt]) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
295 |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
296 |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
297 class Boards(cyclone.web.RequestHandler): |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
298 def get(self): |
969 | 299 |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
300 self.set_header('Content-type', 'application/json') |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
301 self.write(json.dumps({ |
1027 | 302 'host': socket.gethostname(), |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
303 'boards': [b.description() for b in self.settings.boards] |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
304 }, indent=2)) |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
305 |
969 | 306 def currentSerialDevices(): |
307 log.info('find connected boards') | |
308 return glob.glob('/dev/serial/by-id/*') | |
309 | |
310 def main(): | |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
311 arg = docopt(""" |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
312 Usage: arduinoNode.py [options] |
981
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
313 |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
314 -v Verbose |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
315 """) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
316 log.setLevel(logging.WARN) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
317 if arg['-v']: |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
318 from twisted.python import log as twlog |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
319 twlog.startLogging(sys.stdout) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
320 |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
321 log.setLevel(logging.DEBUG) |
d9bbbd8d86f6
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents:
975
diff
changeset
|
322 |
969 | 323 config = Config() |
324 current = currentSerialDevices() | |
325 | |
326 def onChange(): | |
327 # notify reasoning | |
328 pass | |
329 | |
330 boards = [] | |
331 for dev, board in config.serialDevices().items(): | |
332 if str(dev) not in current: | |
333 continue | |
334 log.info("we have board %s connected at %s" % (board, dev)) | |
335 b = Board(dev, config.graph, board, onChange) | |
336 boards.append(b) | |
337 | |
1023
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
338 for b in boards: |
3e6fac8569cd
multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents:
1019
diff
changeset
|
339 b.deployToArduino() |
969 | 340 |
341 log.info('open boards') | |
342 for b in boards: | |
343 b.startPolling() | |
344 | |
995
f72068e9d9d8
serve ipv6 and use localhost's ipv6 name for getting updates
drewp <drewp@bigasterisk.com>
parents:
990
diff
changeset
|
345 |
f72068e9d9d8
serve ipv6 and use localhost's ipv6 name for getting updates
drewp <drewp@bigasterisk.com>
parents:
990
diff
changeset
|
346 app = cyclone.web.Application([ |
975
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
347 (r"/()", cyclone.web.StaticFileHandler, { |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
348 "path": "static", "default_filename": "index.html"}), |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
349 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}), |
f3023410d875
polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents:
974
diff
changeset
|
350 (r'/boards', Boards), |
969 | 351 (r"/graph", GraphPage), |
974
f707210c13bd
new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents:
971
diff
changeset
|
352 (r'/output', OutputPage), |
969 | 353 (r'/arduinoCode', ArduinoCode), |
354 (r'/dot', Dot), | |
995
f72068e9d9d8
serve ipv6 and use localhost's ipv6 name for getting updates
drewp <drewp@bigasterisk.com>
parents:
990
diff
changeset
|
355 ], config=config, boards=boards) |
f72068e9d9d8
serve ipv6 and use localhost's ipv6 name for getting updates
drewp <drewp@bigasterisk.com>
parents:
990
diff
changeset
|
356 reactor.listenTCP(9059, app, interface='::') |
969 | 357 reactor.run() |
358 | |
359 main() |