annotate service/arduinoNode/arduinoNode.py @ 974:f707210c13bd

new /output to post statements which devices can handle. led and lcd output working Ignore-this: afa16b081869a52380b04271a35c53c7 darcs-hash:20150412104414-312f9-f0e67d9f939025fd0a67e78463e6902ddcf0e6d9
author drewp <drewp@bigasterisk.com>
date Sun, 12 Apr 2015 03:44:14 -0700
parents fbe72d44f15a
children 376599552a4c
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 """
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
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 import shutil
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
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 import cyclone.web
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
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
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 from twisted.internet import reactor, task
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
14
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 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
16 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
17 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
18 rdflib_patch.fixQnameOfUriWithTrailingSlash()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 logging.basicConfig(level=logging.DEBUG)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 from loggingserial import LoggingSerial
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 sys.path.append("/my/site/magma")
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 from stategraph import StateGraph
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 log = logging.getLogger()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 logging.getLogger('serial').setLevel(logging.WARN)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 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
31 HOST = Namespace('http://bigasterisk.com/ruler/host/')
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
33 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
34
969
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)
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
67 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
68 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
69 self._polledDevs = [d for d in self._devs if d.generatePollCode()]
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 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
72
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.open()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 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
76 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
77 timeout=2)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 def startPolling(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 task.LoopingCall(self._poll).start(.5)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 def _poll(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 """
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 even boards with no inputs need some polling to see if they're
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 still ok
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 """
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 try:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 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
89 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
90 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
91 raise
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 except Exception as e:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 log.warn("poll: %r" % e)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 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
96 t1 = time.time()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 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
98 for i in self._polledDevs:
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 self._statementsFromInputs[i.uri] = i.readFromPoll(self.ser.read)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 #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
101 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
102 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
103 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
104 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
105 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
106 log.warn('poll took %.1f seconds' % elapsed)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
107
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 def currentGraph(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 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
111
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 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
113
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 for si in self._statementsFromInputs.values():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 for s in si:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 g.add(s)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117 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
118
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
119 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
120 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
121 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
122 stmtsForDev = []
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
123 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
124 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
125 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
126 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
127 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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 "%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
137 "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
138 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
139 if unused:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
140 log.warn("No devices cared about these statements:")
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
141 for s in unused:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
142 log.warn(repr(s))
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
143
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
144 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
145 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
146 '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
147 '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
148 '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
149 'setups': '',
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
150 'polls': '',
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
151 'actions': '',
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
152 }
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
153 for attr in ['includes', 'global', 'setups', 'polls', 'actions']:
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 dev in 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
155 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
156 gen = '\n'.join('#include "%s"\n' % inc
974
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 inc in dev.generateIncludes())
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
158 elif attr == 'global': gen = dev.generateGlobalCode()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
159 elif attr == 'setups': gen = dev.generateSetupCode()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
160 elif attr == 'polls': gen = dev.generatePollCode()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
161 elif attr == 'actions':
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
162 code = dev.generateActionCode()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
163 if code:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
164 gen = '''else if (cmd == %(cmdNum)s) {
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
165 %(code)s
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
166 Serial.write('k');
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
167 }
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
168 ''' % dict(cmdNum=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
169 code=code)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
170 else:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
171 gen = ''
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
172 else:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
173 raise NotImplementedError
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
174
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 if gen:
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
176 generated[attr] += '// for %s\n%s\n' % (dev.uri, gen)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
177
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 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
179 %(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
180
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 %(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
182
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
183 void setup() {
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
184 Serial.begin(%(baudrate)d);
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
185 Serial.flush();
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
186 %(setups)s
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
187 }
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
188
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
189 void loop() {
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
190 byte head, cmd;
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
191 if (Serial.available() >= 2) {
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
192 head = Serial.read();
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
193 if (head != 0x60) {
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
194 Serial.flush();
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
195 return;
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
196 }
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
197 cmd = Serial.read();
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
198 if (cmd == 0x00) { // poll
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
199 %(polls)s
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
200 Serial.write('x');
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
201 } else if (cmd == 0x01) { // get code checksum
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
202 Serial.write("CODE_CHECKSUM");
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
203 }
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
204 %(actions)s
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
205 }
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
206 }
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
207 ''' % generated
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
208 try:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
209 with tempfile.SpooledTemporaryFile() as codeFile:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
210 codeFile.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
211 codeFile.seek(0)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
212 code = subprocess.check_output([
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
213 'indent',
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
214 '-linux',
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
215 '-fc1', # ok to indent comments
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
216 '-i4', # 4-space indent
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
217 '-sob' # swallow blanks (not working)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
218 ], stdin=codeFile)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
219 except OSError as e:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
220 log.warn("indent failed (%r)", e)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
221 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
222 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
223 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
224
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
225 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
226 # 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
249 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
250 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
251
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
252 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
253 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
254
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
255 try:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
256 if hasattr(self, 'ser'):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
257 self.ser.close()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
258 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
259 try:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
260 self._arduinoMake(workDir, code)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
261 finally:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
262 shutil.rmtree(workDir)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
263 finally:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
264 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
265
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
266 def _arduinoMake(self, workDir, code):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
267 with open(workDir + '/makefile', 'w') as makefile:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
268 makefile.write('''
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
269 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
270 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
271 ARDUINO_LIBS = %(arduinoLibs)s
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
272 MONITOR_PORT = %(dev)s
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
273
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
274 include /usr/share/arduino/Arduino.mk
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
275 ''' % {
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
276 'dev': self.dev,
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
277 '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
278 '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
279 '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
280 for d in self._devs), [])),
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
281 })
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
282
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
283 with open(workDir + '/main.ino', 'w') as main:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
284 main.write(code)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
285
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
286 subprocess.check_call(['make', 'upload'], cwd=workDir)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
287
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
288
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
289 class Index(cyclone.web.RequestHandler):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
290 def get(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
291 self.set_header("Content-Type", "text/html")
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
292 self.write(open("index.html").read())
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
293
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
294 class GraphPage(cyclone.web.RequestHandler):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
295 def get(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
296 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host'])
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
297
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
298 for b in self.settings.boards:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
299 for stmt in b.currentGraph():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
300 g.add(stmt)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
301
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
302 if self.get_argument('config', 'no') == 'yes':
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
303 for stmt in self.settings.config.graph:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
304 g.add(stmt)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
305
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
306 self.set_header('Content-type', 'application/x-trig')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
307 self.write(g.asTrig())
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
308
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
309 class Dot(cyclone.web.RequestHandler):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
310 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
311 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
312 dot = dotrender.render(configGraph, self.settings.boards)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
313 self.write(dot)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
314
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
315 class ArduinoCode(cyclone.web.RequestHandler):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
316 def get(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
317 board = [b for b in self.settings.boards if
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
318 b.uri == URIRef(self.get_argument('board'))][0]
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
319 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
320 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
321 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
322
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
323 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
324 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
325 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
326 return g
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
327
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
328 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
329 def post(self):
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
330 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
331 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
332 b.outputStatements(stmts)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
333
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
334 def currentSerialDevices():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
335 log.info('find connected boards')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
336 return glob.glob('/dev/serial/by-id/*')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
337
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
338 def main():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
339 config = Config()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
340 current = currentSerialDevices()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
341
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
342 def onChange():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
343 # notify reasoning
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
344 pass
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
345
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
346 boards = []
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
347 for dev, board in config.serialDevices().items():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
348 if str(dev) not in current:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
349 continue
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
350 log.info("we have board %s connected at %s" % (board, dev))
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
351 b = Board(dev, config.graph, board, onChange)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
352 boards.append(b)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
353
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
354 boards[0].deployToArduino()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
355
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
356 log.info('open boards')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
357 for b in boards:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
358 b.startPolling()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
359
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
360 from twisted.python import log as twlog
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
361 twlog.startLogging(sys.stdout)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
362
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
363 log.setLevel(logging.DEBUG)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
364 reactor.listenTCP(9059, cyclone.web.Application([
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
365 (r"/", Index),
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
366 (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
367 (r'/output', OutputPage),
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
368 (r'/arduinoCode', ArduinoCode),
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
369 (r'/dot', Dot),
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
370 ], config=config, boards=boards))
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
371 reactor.run()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
372
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
373 main()