annotate service/arduinoNode/arduinoNode.py @ 972:5f1bbec24d45

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