annotate service/arduinoNode/arduinoNode.py @ 177:5305fe90c09b

bang arduino board config update Ignore-this: 27f7b4313925eae037e5bd977fa2c398
author drewp@bigasterisk.com
date Thu, 14 May 2015 01:27:25 -0700
parents f81c4d3d774b
children 2161c71c7b02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
1 """
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
2 depends on packages:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
3 arduino-mk
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
4 indent
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
5 """
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
6 from __future__ import division
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
7 import glob, sys, logging, subprocess, socket, os, hashlib, time, tempfile
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
8 import shutil, json
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
9 import serial
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
10 import cyclone.web
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
11 from rdflib import Graph, Namespace, URIRef, Literal, RDF
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
12 from rdflib.parser import StringInputSource
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
13 from twisted.internet import reactor, task
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
14 from docopt import docopt
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
15
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
16 import devices
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
17 import dotrender
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
18 import rdflib_patch
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
19 rdflib_patch.fixQnameOfUriWithTrailingSlash()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
20
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
21 logging.basicConfig(level=logging.DEBUG)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
22
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
23 from loggingserial import LoggingSerial
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
24
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
25 sys.path.append("/my/site/magma")
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
26 from stategraph import StateGraph
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
27
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
28 log = logging.getLogger()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
29 logging.getLogger('serial').setLevel(logging.WARN)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
30
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
31 ROOM = Namespace('http://projects.bigasterisk.com/room/')
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
32 HOST = Namespace('http://bigasterisk.com/ruler/host/')
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
33
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
34 ACTION_BASE = 10 # higher than any of the fixed command numbers
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
35
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
36 class Config(object):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
37 def __init__(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
38 self.graph = Graph()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
39 log.info('read config')
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
40 self.graph.parse('config.n3', format='n3')
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
41 self.graph.bind('', ROOM) # not working
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
42 self.graph.bind('rdf', RDF)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
43
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
44 def serialDevices(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
45 return dict([(row.dev, row.board) for row in self.graph.query(
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
46 """SELECT ?board ?dev WHERE {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
47 ?board :device ?dev;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
48 a :ArduinoBoard .
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
49 }""", initNs={'': ROOM})])
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
50
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
51 class Board(object):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
52 """an arduino connected to this computer"""
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
53 baudrate = 115200
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
54 def __init__(self, dev, graph, uri, onChange):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
55 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
56 each connected thing has some pins.
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
57
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
58 We'll call onChange when we know the currentGraph() has
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
59 changed (and not just in creation time).
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
60 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
61 self.uri = uri
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
62 self.graph = graph
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
63 self.dev = dev
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
64
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
65 # The order of this list needs to be consistent between the
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
66 # deployToArduino call and the poll call.
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
67 self._devs = devices.makeDevices(graph, self.uri)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
68 self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
69 for devIndex, dev in enumerate(self._devs))
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
70 self._polledDevs = [d for d in self._devs if d.generatePollCode()]
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
71
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
72 self._statementsFromInputs = {} # input uri: latest statements
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
73
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
74 self.open()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
75
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
76 def description(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
77 """for web page"""
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
78 return {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
79 'uri': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
80 'dev': self.dev,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
81 'baudrate': self.baudrate,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
82 'devices': [d.description() for d in self._devs],
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
83 }
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
84
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
85 def open(self):
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
86 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate,
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
87 timeout=2)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
88
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
89 def startPolling(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
90 task.LoopingCall(self._poll).start(.5)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
91
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
92 def _poll(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
93 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
94 even boards with no inputs need some polling to see if they're
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
95 still ok
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
96 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
97 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
98 self._pollWork()
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
99 except serial.SerialException:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
100 reactor.crash()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
101 raise
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
102 except Exception as e:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
103 log.warn("poll: %r" % e)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
104
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
105 def _pollWork(self):
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
106 t1 = time.time()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
107 self.ser.write("\x60\x00")
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
108 for i in self._polledDevs:
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
109 self._statementsFromInputs[i.uri] = i.readFromPoll(self.ser.read)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
110 #plus statements about succeeding or erroring on the last poll
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
111 byte = self.ser.read(1)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
112 if byte != 'x':
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
113 raise ValueError("after poll, got %x instead of 'x'" % byte)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
114 elapsed = time.time() - t1
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
115 if elapsed > 1.0:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
116 log.warn('poll took %.1f seconds' % elapsed)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
117
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
118
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
119 def currentGraph(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
120 g = Graph()
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
121
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
122 g.add((HOST[socket.gethostname()], ROOM['connectedTo'], self.uri))
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
123
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
124 for si in self._statementsFromInputs.values():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
125 for s in si:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
126 g.add(s)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
127 return g
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
128
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
129 def outputStatements(self, stmts):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
130 unused = set(stmts)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
131 for dev in self._devs:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
132 stmtsForDev = []
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
133 for pat in dev.outputPatterns():
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
134 if [term is None for term in pat] != [False, False, True]:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
135 raise NotImplementedError
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
136 for stmt in stmts:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
137 if stmt[:2] == pat[:2]:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
138 stmtsForDev.append(stmt)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
139 unused.discard(stmt)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
140 if stmtsForDev:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
141 log.info("output goes to action handler for %s" % dev.uri)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
142 self.ser.write("\x60" + chr(self._devCommandNum[dev.uri]))
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
143 dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
144 if self.ser.read(1) != 'k':
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
145 raise ValueError(
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
146 "%s sendOutput/generateActionCode didn't use "
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
147 "matching output bytes" % dev.__class__)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
148 log.info("success")
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
149 if unused:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
150 log.warn("No devices cared about these statements:")
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
151 for s in unused:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
152 log.warn(repr(s))
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
153
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
154 def generateArduinoCode(self):
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
155 generated = {
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
156 'baudrate': self.baudrate,
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
157 'includes': '',
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
158 'global': '',
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
159 'setups': '',
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
160 'polls': '',
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
161 'actions': '',
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
162 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
163 for attr in ['includes', 'global', 'setups', 'polls', 'actions']:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
164 for dev in self._devs:
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
165 if attr == 'includes':
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
166 gen = '\n'.join('#include "%s"\n' % inc
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
167 for inc in dev.generateIncludes())
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
168 elif attr == 'global': gen = dev.generateGlobalCode()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
169 elif attr == 'setups': gen = dev.generateSetupCode()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
170 elif attr == 'polls': gen = dev.generatePollCode()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
171 elif attr == 'actions':
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
172 code = dev.generateActionCode()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
173 if code:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
174 gen = '''else if (cmd == %(cmdNum)s) {
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
175 %(code)s
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
176 Serial.write('k');
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
177 }
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
178 ''' % dict(cmdNum=self._devCommandNum[dev.uri],
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
179 code=code)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
180 else:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
181 gen = ''
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
182 else:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
183 raise NotImplementedError
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
184
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
185 if gen:
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
186 generated[attr] += '// for %s\n%s\n' % (dev.uri, gen)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
187
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
188 code = '''
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
189 %(includes)s
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
190
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
191 %(global)s
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
192
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
193 void setup() {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
194 Serial.begin(%(baudrate)d);
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
195 Serial.flush();
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
196 %(setups)s
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
197 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
198
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
199 void loop() {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
200 byte head, cmd;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
201 if (Serial.available() >= 2) {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
202 head = Serial.read();
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
203 if (head != 0x60) {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
204 Serial.flush();
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
205 return;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
206 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
207 cmd = Serial.read();
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
208 if (cmd == 0x00) { // poll
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
209 %(polls)s
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
210 Serial.write('x');
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
211 } else if (cmd == 0x01) { // get code checksum
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
212 Serial.write("CODE_CHECKSUM");
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
213 }
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
214 %(actions)s
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
215 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
216 }
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
217 ''' % generated
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
218 try:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
219 with tempfile.SpooledTemporaryFile() as codeFile:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
220 codeFile.write(code)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
221 codeFile.seek(0)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
222 code = subprocess.check_output([
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
223 'indent',
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
224 '-linux',
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
225 '-fc1', # ok to indent comments
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
226 '-i4', # 4-space indent
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
227 '-sob' # swallow blanks (not working)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
228 ], stdin=codeFile)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
229 except OSError as e:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
230 log.warn("indent failed (%r)", e)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
231 cksum = hashlib.sha1(code).hexdigest()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
232 code = code.replace('CODE_CHECKSUM', cksum)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
233 return code, cksum
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
234
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
235 def _readBoardChecksum(self, length):
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
236 # this is likely right after reset, so it might take 2 seconds
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
237 for tries in range(6):
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
238 self.ser.write("\x60\x01")
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
239 try:
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
240 return self.ser.read(length)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
241 except ValueError:
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
242 if tries == 5:
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
243 raise
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
244 time.sleep(.5)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
245 raise ValueError
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
246
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
247 def _boardIsCurrent(self, currentChecksum):
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
248 try:
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
249 boardCksum = self._readBoardChecksum(len(currentChecksum))
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
250 if boardCksum == currentChecksum:
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
251 log.info("board has current code (%s)" % currentChecksum)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
252 return True
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
253 else:
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
254 log.info("board responds with incorrect code version")
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
255 except Exception as e:
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
256 log.info("can't get code version from board: %r" % e)
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
257 return False
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
258
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
259 def deployToArduino(self):
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
260 code, cksum = self.generateArduinoCode()
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
261
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
262 if self._boardIsCurrent(cksum):
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
263 return
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
264
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
265 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
266 if hasattr(self, 'ser'):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
267 self.ser.close()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
268 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
269 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
270 self._arduinoMake(workDir, code)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
271 finally:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
272 shutil.rmtree(workDir)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
273 finally:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
274 self.open()
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
275
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
276 def _arduinoMake(self, workDir, code):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
277 with open(workDir + '/makefile', 'w') as makefile:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
278 makefile.write('''
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
279 BOARD_TAG = %(tag)s
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
280 USER_LIB_PATH := %(libs)s
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
281 ARDUINO_LIBS = %(arduinoLibs)s
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
282 MONITOR_PORT = %(dev)s
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
283
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
284 include /usr/share/arduino/Arduino.mk
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
285 ''' % {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
286 'dev': self.dev,
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
287 'tag': self.graph.value(self.uri, ROOM['boardTag']),
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
288 'libs': os.path.abspath('arduino-libraries'),
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
289 'arduinoLibs': ' '.join(sum((d.generateArduinoLibs()
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
290 for d in self._devs), [])),
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
291 })
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
292
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
293 with open(workDir + '/main.ino', 'w') as main:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
294 main.write(code)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
295
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
296 subprocess.check_call(['make', 'upload'], cwd=workDir)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
297
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
298
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
299 class GraphPage(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
300 def get(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
301 g = StateGraph(ctx=ROOM['arduinosOn%s' % 'host'])
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
302
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
303 for b in self.settings.boards:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
304 for stmt in b.currentGraph():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
305 g.add(stmt)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
306
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
307 if self.get_argument('config', 'no') == 'yes':
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
308 for stmt in self.settings.config.graph:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
309 g.add(stmt)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
310
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
311 self.set_header('Content-type', 'application/x-trig')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
312 self.write(g.asTrig())
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
313
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
314 class Dot(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
315 def get(self):
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
316 configGraph = self.settings.config.graph
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
317 dot = dotrender.render(configGraph, self.settings.boards)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
318 self.write(dot)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
319
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
320 class ArduinoCode(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
321 def get(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
322 board = [b for b in self.settings.boards if
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
323 b.uri == URIRef(self.get_argument('board'))][0]
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
324 self.set_header('Content-type', 'text/plain')
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
325 code, cksum = board.generateArduinoCode()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
326 self.write(code)
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
327
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
328 def rdfGraphBody(body, headers):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
329 g = Graph()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
330 g.parse(StringInputSource(body), format='nt')
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
331 return g
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
332
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
333 class OutputPage(cyclone.web.RequestHandler):
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
334 def post(self):
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
335 # for old ui; use PUT instead
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
336 stmts = list(rdfGraphBody(self.request.body, self.request.headers))
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
337 for b in self.settings.boards:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
338 b.outputStatements(stmts)
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
339
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
340 def put(self):
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
341 subj = URIRef(self.get_argument('s'))
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
342 pred = URIRef(self.get_argument('p'))
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
343
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
344 turtleLiteral = self.request.body
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
345 try:
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
346 obj = Literal(float(turtleLiteral))
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
347 except TypeError:
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
348 obj = Literal(turtleLiteral)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
349
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
350 stmt = (subj, pred, obj)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
351 for b in self.settings.boards:
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
352 b.outputStatements([stmt])
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
353
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
354
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
355 class Boards(cyclone.web.RequestHandler):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
356 def get(self):
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
357
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
358 self.set_header('Content-type', 'application/json')
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
359 self.write(json.dumps({
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
360 'boards': [b.description() for b in self.settings.boards]
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
361 }, indent=2))
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
362
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
363 def currentSerialDevices():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
364 log.info('find connected boards')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
365 return glob.glob('/dev/serial/by-id/*')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
366
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
367 def main():
176
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
368 arg = docopt("""
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
369 Usage: reasoning.py [options]
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
370
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
371 -v Verbose
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
372 """)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
373 log.setLevel(logging.WARN)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
374 if arg['-v']:
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
375 from twisted.python import log as twlog
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
376 twlog.startLogging(sys.stdout)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
377
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
378 log.setLevel(logging.DEBUG)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
379
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
380 config = Config()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
381 current = currentSerialDevices()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
382
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
383 def onChange():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
384 # notify reasoning
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
385 pass
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
386
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
387 boards = []
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
388 for dev, board in config.serialDevices().items():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
389 if str(dev) not in current:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
390 continue
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
391 log.info("we have board %s connected at %s" % (board, dev))
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
392 b = Board(dev, config.graph, board, onChange)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
393 boards.append(b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
394
166
c0180bd2b33a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp@bigasterisk.com
parents: 164
diff changeset
395 boards[0].deployToArduino()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
396
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
397 log.info('open boards')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
398 for b in boards:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
399 b.startPolling()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
400
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
401 reactor.listenTCP(9059, cyclone.web.Application([
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
402 (r"/()", cyclone.web.StaticFileHandler, {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
403 "path": "static", "default_filename": "index.html"}),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
404 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
405 (r'/boards', Boards),
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
406 (r"/graph", GraphPage),
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
407 (r'/output', OutputPage),
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
408 (r'/arduinoCode', ArduinoCode),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
409 (r'/dot', Dot),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
410 ], config=config, boards=boards))
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
411 reactor.run()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
412
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
413 main()