annotate service/arduinoNode/arduinoNode.py @ 230:0aa54404df19

update arduinoNode to support streamed graph output Ignore-this: fa35d1fae5b0e411b167650550c3e77d
author drewp@bigasterisk.com
date Thu, 28 Jan 2016 02:24:32 -0800
parents 07ee72a042db
children 4ebb5cc30002
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
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
11 from rdflib import Graph, Namespace, URIRef, Literal, RDF, ConjunctiveGraph
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
229
07ee72a042db refactor to write_arduino_node
drewp@bigasterisk.com
parents: 222
diff changeset
17 import write_arduino_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
18 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
19 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
20 rdflib_patch.fixQnameOfUriWithTrailingSlash()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
21
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
22 logging.basicConfig(level=logging.DEBUG)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
23
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
24 from loggingserial import LoggingSerial
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
25
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
26 sys.path.append("../../lib")
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
27 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
28
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
29 sys.path.append("/my/proj/light9")
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
30 from light9.rdfdb.patch import Patch
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
31 from light9.rdfdb.rdflibpatch import inContext
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
32
213
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
33 sys.path.append("/my/proj/room")
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
34 from carbondata import CarbonClient
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
35
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
36 log = logging.getLogger()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
37 logging.getLogger('serial').setLevel(logging.WARN)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
38
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
39 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
40 HOST = Namespace('http://bigasterisk.com/ruler/host/')
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
41
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
42 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
43
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
44 CTX = ROOM['arduinosOn%s' % socket.gethostname()]
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
45
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
46 class Config(object):
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
47 def __init__(self, masterGraph):
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
48 self.graph = ConjunctiveGraph()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
49 log.info('read config')
214
254d656af72e support a dir of config files
drewp@bigasterisk.com
parents: 213
diff changeset
50 for f in os.listdir('config'):
254d656af72e support a dir of config files
drewp@bigasterisk.com
parents: 213
diff changeset
51 if f.startswith('.'): continue
254d656af72e support a dir of config files
drewp@bigasterisk.com
parents: 213
diff changeset
52 self.graph.parse('config/%s' % f, format='n3')
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
53 self.graph.bind('', ROOM) # not working
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
54 self.graph.bind('rdf', RDF)
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
55 # config graph is too noisy; maybe make it a separate resource
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
56 #masterGraph.patch(Patch(addGraph=self.graph))
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
57
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
58 def serialDevices(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
59 return dict([(row.dev, row.board) for row in self.graph.query(
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
60 """SELECT ?board ?dev WHERE {
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
61 ?board :device ?dev;
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
62 a :ArduinoBoard .
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
63 }""", 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
64
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
65 class Board(object):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
66 """an arduino connected to this computer"""
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
67 baudrate = 115200
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
68 def __init__(self, dev, configGraph, masterGraph, uri):
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
69 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
70 each connected thing has some pins.
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
71 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
72 self.uri = uri
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
73 self.configGraph = configGraph
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
74 self.masterGraph = masterGraph
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
75 self.dev = dev
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
76
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
77 self.masterGraph.patch(Patch(addQuads=[
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
78 (HOST[socket.gethostname()], ROOM['connectedTo'], self.uri, CTX),
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
79 ]))
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
80
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
81 # The order of this list needs to be consistent between the
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
82 # deployToArduino call and the poll call.
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
83 self._devs = devices.makeDevices(configGraph, 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
84 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
85 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
86 self._polledDevs = [d for d in self._devs if d.generatePollCode()]
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
87
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
88 self._statementsFromInputs = {} # input device uri: latest statements
213
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
89 self._carbon = CarbonClient(serverHost='bang')
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
90 self.open()
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
91 for d in self._devs:
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
92 self.syncMasterGraphToHostStatements(d)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
93
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
94 def description(self):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
95 """for web page"""
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
96 return {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
97 'uri': self.uri,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
98 'dev': self.dev,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
99 'baudrate': self.baudrate,
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
100 'devices': [d.description() for d in self._devs],
190
ad20e5cb4feb serve ipv6 and use localhost's ipv6 name for getting updates
drewp@bigasterisk.com
parents: 185
diff changeset
101 'graph': 'http://%s6:9059/graph' % socket.gethostname(), #todo
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
102 }
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
103
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
104 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
105 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
106 timeout=2)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
107
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
108 def startPolling(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
109 task.LoopingCall(self._poll).start(.5)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
110
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
111 def _poll(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
112 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
113 even boards with no inputs need some polling to see if they're
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
114 still ok
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
115 """
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
116 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
117 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
118 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
119 reactor.crash()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
120 raise
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
121 except Exception as e:
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
122 import traceback; traceback.print_exc()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
123 log.warn("poll: %r" % e)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
124
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
125 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
126 t1 = time.time()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
127 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
128 for i in self._polledDevs:
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
129 prev = self._statementsFromInputs.get(i.uri, [])
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
130 new = self._statementsFromInputs[i.uri] = inContext(
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
131 i.readFromPoll(self.ser.read), i.uri)
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
132 self.masterGraph.patch(Patch.fromDiff(prev, new))
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
133
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
134 #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
135 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
136 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
137 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
138 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
139 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
140 log.warn('poll took %.1f seconds' % elapsed)
213
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
141 self._exportToGraphite()
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
142
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
143 def _exportToGraphite(self):
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
144 # note this is writing way too often- graphite is storing at a lower res
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
145 now = time.time()
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
146 # objects of these statements are suitable as graphite values.
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
147 graphitePredicates = {ROOM['temperatureF']}
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
148 for s, graphiteName in self.configGraph.subject_objects(ROOM['graphiteName']):
213
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
149 for group in self._statementsFromInputs.values():
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
150 for stmt in group:
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
151 if stmt[0] == s and stmt[1] in graphitePredicates:
9c8674329a13 write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents: 192
diff changeset
152 self._carbon.send(graphiteName, stmt[2].toPython(), now)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
153
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
154 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
155 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
156 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
157 stmtsForDev = []
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
158 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
159 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
160 raise NotImplementedError
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
161 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
162 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
163 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
164 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
165 if stmtsForDev:
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
166 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
167 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
168 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
169 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
170 raise ValueError(
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
171 "%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
172 "matching output bytes" % dev.__class__)
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
173 # Dev *could* change hostStatements at any time, and
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
174 # we're not currently tracking that, but the usual is
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
175 # to change them in response to sendOutput so this
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
176 # should be good enough. The right answer is to give
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
177 # each dev the masterGraph for it to write to.
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
178 self.syncMasterGraphToHostStatements(dev)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
179 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
180 if unused:
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 214
diff changeset
181 log.info("Board %s doesn't care about these statements:", 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
182 for s in unused:
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 214
diff changeset
183 log.info("%r", s)
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
184
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
185 def syncMasterGraphToHostStatements(self, dev):
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
186 hostStmtCtx = URIRef(dev.uri + '/host')
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
187 newQuads = inContext(dev.hostStatements(), hostStmtCtx)
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
188 self.masterGraph.patchSubgraph(hostStmtCtx, newQuads)
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
189
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
190 def generateArduinoCode(self):
229
07ee72a042db refactor to write_arduino_node
drewp@bigasterisk.com
parents: 222
diff changeset
191 code = write_arduino_code.writeCode(self.baudrate, self._devs, self._devCommandNum)
07ee72a042db refactor to write_arduino_node
drewp@bigasterisk.com
parents: 222
diff changeset
192 code = write_arduino_code.indent(code)
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
193 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
194 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
195 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
196
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
197 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
198 # 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
199 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
200 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
201 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
202 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
203 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
204 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
205 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
206 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
207 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
208
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
209 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
210 try:
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
221 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
222 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
223
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
224 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
225 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
226
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
227 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
228 if hasattr(self, 'ser'):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
229 self.ser.close()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
230 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
231 try:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
232 self._arduinoMake(workDir, code)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
233 finally:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
234 shutil.rmtree(workDir)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
235 finally:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
236 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
237
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
238 def _arduinoMake(self, workDir, code):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
239 with open(workDir + '/makefile', 'w') as makefile:
229
07ee72a042db refactor to write_arduino_node
drewp@bigasterisk.com
parents: 222
diff changeset
240 makefile.write(write_arduino_code.writeMakefile(
07ee72a042db refactor to write_arduino_node
drewp@bigasterisk.com
parents: 222
diff changeset
241 dev=self.dev,
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
242 tag=self.configGraph.value(self.uri, ROOM['boardTag']),
229
07ee72a042db refactor to write_arduino_node
drewp@bigasterisk.com
parents: 222
diff changeset
243 allLibs=sum((d.generateArduinoLibs() for d in self._devs), [])))
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
244
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
245 with open(workDir + '/main.ino', 'w') as main:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
246 main.write(code)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
247
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
248 subprocess.check_call(['make', 'upload'], cwd=workDir)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
249
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
250
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
251 def currentGraph(self):
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
252 g = Graph()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
253
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
254
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
255 for dev in self._devs:
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
256 for stmt in dev.hostStatements():
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
257 g.add(stmt)
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
258 return g
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
259
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
260 class Dot(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
261 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
262 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
263 dot = dotrender.render(configGraph, self.settings.boards)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
264 self.write(dot)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
265
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
266 class ArduinoCode(cyclone.web.RequestHandler):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
267 def get(self):
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
268 board = [b for b in self.settings.boards if
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
269 b.uri == URIRef(self.get_argument('board'))][0]
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 214
diff changeset
270 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
271 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
272 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
273
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
274 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
275 g = Graph()
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
276 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
277 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
278
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
279 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
280 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
281 # 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
282 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
283 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
284 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
285
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
286 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
287 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
288 pred = URIRef(self.get_argument('p'))
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
289
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
290 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
291 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
292 obj = Literal(float(turtleLiteral))
185
2161c71c7b02 support for device code in the idle loop
drewp@bigasterisk.com
parents: 176
diff changeset
293 except ValueError:
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
294 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
295
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
296 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
297 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
298 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
299
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
300
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
301 class Boards(cyclone.web.RequestHandler):
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
302 def get(self):
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
303
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
304 self.set_header('Content-type', 'application/json')
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
305 self.write(json.dumps({
222
e606f1d89d89 hostname in page title
drewp@bigasterisk.com
parents: 218
diff changeset
306 'host': socket.gethostname(),
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
307 '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
308 }, indent=2))
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
309
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
310 def currentSerialDevices():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
311 log.info('find connected boards')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
312 return glob.glob('/dev/serial/by-id/*')
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
313
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
314 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
315 arg = docopt("""
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 214
diff changeset
316 Usage: arduinoNode.py [options]
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
317
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
318 -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
319 """)
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
320 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
321 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
322 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
323 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
324
f81c4d3d774b arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents: 170
diff changeset
325 log.setLevel(logging.DEBUG)
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
326
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
327 masterGraph = PatchableGraph()
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
328 config = Config(masterGraph)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
329 current = currentSerialDevices()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
330
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
331 boards = []
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
332 for dev, board in config.serialDevices().items():
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
333 if str(dev) not in current:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
334 continue
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
335 log.info("we have board %s connected at %s" % (board, dev))
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
336 b = Board(dev, config.graph, masterGraph, board)
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
337 boards.append(b)
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
338
218
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 214
diff changeset
339 for b in boards:
f8ffb9d8d982 multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents: 214
diff changeset
340 b.deployToArduino()
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
341
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
342 log.info('open boards')
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
343 for b in boards:
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
344 b.startPolling()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
345
190
ad20e5cb4feb serve ipv6 and use localhost's ipv6 name for getting updates
drewp@bigasterisk.com
parents: 185
diff changeset
346
ad20e5cb4feb serve ipv6 and use localhost's ipv6 name for getting updates
drewp@bigasterisk.com
parents: 185
diff changeset
347 app = cyclone.web.Application([
170
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
348 (r"/()", cyclone.web.StaticFileHandler, {
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
349 "path": "static", "default_filename": "index.html"}),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
350 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}),
376599552a4c polymer board debug page with working output widgets
drewp@bigasterisk.com
parents: 169
diff changeset
351 (r'/boards', Boards),
230
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
352 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}),
0aa54404df19 update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents: 229
diff changeset
353 (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}),
169
d228105749ac new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents: 166
diff changeset
354 (r'/output', OutputPage),
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
355 (r'/arduinoCode', ArduinoCode),
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
356 (r'/dot', Dot),
190
ad20e5cb4feb serve ipv6 and use localhost's ipv6 name for getting updates
drewp@bigasterisk.com
parents: 185
diff changeset
357 ], config=config, boards=boards)
ad20e5cb4feb serve ipv6 and use localhost's ipv6 name for getting updates
drewp@bigasterisk.com
parents: 185
diff changeset
358 reactor.listenTCP(9059, app, interface='::')
164
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
359 reactor.run()
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
360
49c1756b2edb start arduinonode
drewp@bigasterisk.com
parents:
diff changeset
361 main()