annotate service/arduinoNode/arduinoNode.py @ 167:3dd84ac050d0

config: new arduino board with temp and lcd Ignore-this: 7b3143060e68e46a42f3ed56839c5a46
author drewp@bigasterisk.com
date Sat, 11 Apr 2015 01:45:51 -0700
parents c0180bd2b33a
children d228105749ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 """
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
2 depends on arduino-mk
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
3 """
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
4 import shutil
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
5 import tempfile
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
6 import glob, sys, logging, subprocess, socket, os, hashlib, time
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
7 import cyclone.web
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
8 from rdflib import Graph, Namespace, URIRef, Literal, RDF
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
9 from twisted.internet import reactor, task
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
10 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
11 import dotrender
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
12
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
13 logging.basicConfig(level=logging.DEBUG)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
14
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
15 from loggingserial import LoggingSerial
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
16
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
17 sys.path.append("/my/site/magma")
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
18 from stategraph import StateGraph
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
19
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
20 log = logging.getLogger()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
21 logging.getLogger('serial').setLevel(logging.WARN)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
22
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
23 import rdflib.namespace
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
24 old_split = rdflib.namespace.split_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
25 def new_split(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
26 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
27 return old_split(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
28 except Exception:
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
29 return 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
30 rdflib.namespace.split_uri = new_split
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
31
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
32 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
33 HOST = Namespace('http://bigasterisk.com/ruler/host/')
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
34
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
35 class Config(object):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
36 def __init__(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
37 self.graph = Graph()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
38 log.info('read config')
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
39 self.graph.parse('config.n3', format='n3')
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
40 self.graph.bind('', ROOM) # not working
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
41 self.graph.bind('rdf', RDF)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
42
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
43 def serialDevices(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
44 return dict([(row.dev, row.board) for row in self.graph.query(
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
45 """SELECT ?board ?dev WHERE {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
46 ?board :device ?dev;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
47 a :ArduinoBoard .
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
48 }""", initNs={'': ROOM})])
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
49
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
50 class Board(object):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
51 """an arduino connected to this computer"""
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
52 baudrate = 115200
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
53 def __init__(self, dev, graph, uri, onChange):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
54 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
55 each connected thing has some pins.
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
56
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
57 We'll call onChange when we know the currentGraph() has
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
58 changed (and not just in creation time).
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
59 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
60 self.uri = uri
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
61 self.graph = graph
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
62 self.dev = dev
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
63
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
64 # The order of this list needs to be consistent between the
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
65 # deployToArduino call and the poll call.
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
66 self._devs = devices.makeDevices(graph, self.uri)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
67 self._polledDevs = [d for d in self._devs if d.generatePollCode()]
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
68
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
69 self._statementsFromInputs = {} # input uri: latest statements
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
70
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
71 self.open()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
72
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
73 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
74 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
75 timeout=2)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
76
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
77 def startPolling(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
78 task.LoopingCall(self._poll).start(.5)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
79
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
80 def _poll(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
81 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
82 even boards with no inputs need some polling to see if they're
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
83 still ok
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
84 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
85 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
86 self._pollWork()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
87 except Exception as e:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
88 log.warn("poll: %r" % e)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
89
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
90 def _pollWork(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
91 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
92 for i in self._polledDevs:
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
93 self._statementsFromInputs[i.uri] = i.readFromPoll(self.ser.read)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
94 #plus statements about succeeding or erroring on the last poll
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
95
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
96 def currentGraph(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
97 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
98
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
99 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
100
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
101 for si in self._statementsFromInputs.values():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
102 for s in si:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
103 g.add(s)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
104 return g
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
105
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
106 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
107 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
108 '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
109 '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
110 '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
111 'setups': '',
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
112 'polls': ''
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
113 }
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 attr in ['includes', 'global', 'setups', 'polls']:
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
115 for i in self._devs:
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
116 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
117 gen = '\n'.join('#include "%s"\n' % inc
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
118 for inc in i.generateIncludes())
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
119 elif attr == 'global': gen = i.generateGlobalCode()
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
120 elif attr == 'setups': gen = i.generateSetupCode()
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
121 elif attr == 'polls': gen = i.generatePollCode()
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
122 else: raise NotImplementedError
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
123
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
124 if gen:
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
125 generated[attr] += '// for %s\n%s\n' % (i.uri, gen)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
126
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
127 return '''
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
128 %(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
129
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
130 %(global)s
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
131
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
132 void setup() {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
133 Serial.begin(%(baudrate)d);
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
134 Serial.flush();
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
135 %(setups)s
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
136 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
137
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
138 void loop() {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
139 byte head, cmd;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
140 if (Serial.available() >= 2) {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
141 head = Serial.read();
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
142 if (head != 0x60) {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
143 Serial.flush();
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
144 return;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
145 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
146 cmd = Serial.read();
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
147 if (cmd == 0x00) {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
148 %(polls)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
149 } else if (cmd == 0x01) {
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
150 Serial.write("CODE_CHECKSUM");
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
151 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
152 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
153 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
154 ''' % generated
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
155
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
156
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
157 def codeChecksum(self, code):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
158 # this is run on the code without CODE_CHECKSUM replaced yet
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
159 return hashlib.sha1(code).hexdigest()
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
160
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
161 def readBoardChecksum(self, 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
162 # 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
163 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
164 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
165 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
166 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
167 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
168 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
169 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
170 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
171 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
172
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
173 def boardIsCurrent(self, 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
174 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
175 boardCksum = self.readBoardChecksum(len(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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
185 def deployToArduino(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
186 code = 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
187 cksum = self.codeChecksum(code)
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 code = code.replace('CODE_CHECKSUM', cksum)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
189
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
190 if self.boardIsCurrent(cksum):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
191 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
192
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
193 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
194 if hasattr(self, 'ser'):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
195 self.ser.close()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
196 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
197 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
198 self._arduinoMake(workDir, code)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
199 finally:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
200 shutil.rmtree(workDir)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
201 finally:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
202 self.open()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
203
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
204 def _arduinoMake(self, workDir, code):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
205 with open(workDir + '/makefile', 'w') as makefile:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
206 makefile.write('''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
207 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
208 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
209 ARDUINO_LIBS = %(arduinoLibs)s
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
210 MONITOR_PORT = %(dev)s
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
211
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
212 include /usr/share/arduino/Arduino.mk
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
213 ''' % {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
214 'dev': self.dev,
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
215 '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
216 '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
217 '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
218 for d in self._devs), [])),
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
219 })
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
220
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
221 with open(workDir + '/main.ino', 'w') as main:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
222 main.write(code)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
223
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
224 subprocess.check_call(['make', 'upload'], cwd=workDir)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
225
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
226
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
227 class Index(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
228 def get(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
229 self.set_header("Content-Type", "text/html")
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
230 self.write(open("index.html").read())
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
231
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
232 class GraphPage(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
233 def get(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
234 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host'])
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
235
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
236 for b in self.settings.boards:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
237 for stmt in b.currentGraph():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
238 g.add(stmt)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
239
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
240 if self.get_argument('config', 'no') == 'yes':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
241 for stmt in self.settings.config.graph:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
242 g.add(stmt)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
243
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
244 self.set_header('Content-type', 'application/x-trig')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
245 self.write(g.asTrig())
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
246
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
247 class Dot(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
248 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
249 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
250 dot = dotrender.render(configGraph, self.settings.boards)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
251 self.write(dot)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
252
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
253 class ArduinoCode(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
254 def get(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
255 board = [b for b in self.settings.boards if
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
256 b.uri == URIRef(self.get_argument('board'))][0]
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
257 self.set_header('Content-type', 'text/plain')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
258 self.write(board.generateArduinoCode())
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
259
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
260
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
261 def currentSerialDevices():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
262 log.info('find connected boards')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
263 return glob.glob('/dev/serial/by-id/*')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
264
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
265 def main():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
266 config = Config()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
267 current = currentSerialDevices()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
268
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
269 def onChange():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
270 # notify reasoning
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
271 pass
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
272
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
273 boards = []
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
274 for dev, board in config.serialDevices().items():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
275 if str(dev) not in current:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
276 continue
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
277 log.info("we have board %s connected at %s" % (board, dev))
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
278 b = Board(dev, config.graph, board, onChange)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
279 boards.append(b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
280
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
281 boards[0].deployToArduino()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
282
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
283 log.info('open boards')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
284 for b in boards:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
285 b.startPolling()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
286
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
287 from twisted.python import log as twlog
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
288 twlog.startLogging(sys.stdout)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
289
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
290 log.setLevel(logging.DEBUG)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
291 reactor.listenTCP(9059, cyclone.web.Application([
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
292 (r"/", Index),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
293 (r"/graph", GraphPage),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
294 (r'/arduinoCode', ArduinoCode),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
295 (r'/dot', Dot),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
296 ], config=config, boards=boards))
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
297 reactor.run()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
298
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
299 main()