Mercurial > code > home > repos > homeauto
annotate service/arduinoNode/arduinoNode.py @ 1685:6b80a6c58907
minor changes to several services
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 23:12:43 -0700 |
parents | 686079900c20 |
children |
rev | line source |
---|---|
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
1 from __future__ import division |
620 | 2 import glob, sys, logging, subprocess, socket, hashlib, time, tempfile, pkg_resources |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
3 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
|
4 import serial |
164 | 5 import cyclone.web |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
6 from cyclone.httpclient import fetch |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
7 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
|
8 from rdflib.parser import StringInputSource |
164 | 9 from twisted.internet import reactor, task |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
10 from twisted.internet.defer import inlineCallbacks |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
11 from twisted.internet.threads import deferToThread |
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
|
12 from docopt import docopt |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
13 import etcd3 |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
14 from greplin import scales |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
15 from greplin.scales.cyclonehandler import StatsHandler |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
16 |
164 | 17 import devices |
229 | 18 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
|
19 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
|
20 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
|
21 rdflib_patch.fixQnameOfUriWithTrailingSlash() |
164 | 22 |
23 logging.basicConfig(level=logging.DEBUG) | |
24 | |
25 from loggingserial import LoggingSerial | |
26 | |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
27 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler |
331
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
305
diff
changeset
|
28 from export_to_influxdb import InfluxExporter |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
29 |
331
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
305
diff
changeset
|
30 from rdfdb.patch import Patch |
a94f2a522d41
build and import updates for rdfdb, etc
drewp@bigasterisk.com
parents:
305
diff
changeset
|
31 from rdfdb.rdflibpatch import inContext |
164 | 32 |
213
9c8674329a13
write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents:
192
diff
changeset
|
33 |
164 | 34 log = logging.getLogger() |
35 logging.getLogger('serial').setLevel(logging.WARN) | |
36 | |
37 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
|
38 HOST = Namespace('http://bigasterisk.com/ruler/host/') |
164 | 39 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
40 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
|
41 |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
42 hostname = socket.gethostname() |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
43 CTX = ROOM['arduinosOn%s' % hostname] |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
44 |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
45 STATS = scales.collection('/root', |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
46 ) |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
47 |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
48 |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
49 etcd = etcd3.client(host='bang6', port=9022) |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
50 |
164 | 51 class Config(object): |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
52 def __init__(self, masterGraph, slowMode=False): |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
53 self.masterGraph = masterGraph |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
54 self.slowMode = slowMode |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
55 self.configGraph = ConjunctiveGraph() |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
56 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
57 self.etcPrefix = 'arduino/' |
620 | 58 |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
59 self.boards = [] |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
60 self.reread() |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
61 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
62 deferToThread(self.watchEtcd) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
63 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
64 def watchEtcd(self): |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
65 events, cancel = etcd.watch_prefix(self.etcPrefix) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
66 reactor.addSystemEventTrigger('before', 'shutdown', cancel) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
67 for ev in events: |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
68 log.info('%s changed', ev.key) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
69 reactor.callFromThread(self.configChanged) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
70 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
71 def configChanged(self): |
343 | 72 self.cancelRead() |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
73 self.rereadLater = reactor.callLater(.1, self.reread) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
74 |
343 | 75 def cancelRead(self): |
76 if getattr(self, 'rereadLater', None): | |
77 self.rereadLater.cancel() | |
78 self.rereadLater = None | |
620 | 79 |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
80 def reread(self): |
343 | 81 self.cancelRead() |
164 | 82 log.info('read config') |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
83 self.configGraph = ConjunctiveGraph() |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
84 for v, md in etcd.get_prefix(self.etcPrefix): |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
85 log.info(' read file %r', md.key) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
86 self.configGraph.parse(StringInputSource(v), format='n3') |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
87 self.configGraph.bind('', ROOM) # not working |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
88 self.configGraph.bind('rdf', RDF) |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
89 # config graph is too noisy; maybe make it a separate resource |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
90 #masterGraph.patch(Patch(addGraph=self.configGraph)) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
91 self.setupBoards() |
164 | 92 |
93 def serialDevices(self): | |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
94 return dict([(row.dev, row.board) for row in self.configGraph.query( |
164 | 95 """SELECT ?board ?dev WHERE { |
96 ?board :device ?dev; | |
97 a :ArduinoBoard . | |
98 }""", initNs={'': ROOM})]) | |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
99 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
100 def setupBoards(self): |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
101 current = currentSerialDevices() |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
102 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
103 self.boards = [] |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
104 for dev, board in self.serialDevices().items(): |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
105 if str(dev) not in current: |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
106 continue |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
107 log.info("we have board %s connected at %s" % (board, dev)) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
108 b = Board(dev, self.configGraph, self.masterGraph, board) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
109 self.boards.append(b) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
110 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
111 for b in self.boards: |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
112 b.deployToArduino() |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
113 |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
114 log.info('open boards') |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
115 for b in self.boards: |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
116 b.startPolling(period=.1 if not self.slowMode else 10) |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
117 |
620 | 118 |
164 | 119 class Board(object): |
120 """an arduino connected to this computer""" | |
121 baudrate = 115200 | |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
122 def __init__(self, dev, configGraph, masterGraph, uri): |
164 | 123 """ |
124 each connected thing has some pins. | |
125 """ | |
126 self.uri = uri | |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
127 self.configGraph = configGraph |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
128 self.masterGraph = masterGraph |
164 | 129 self.dev = dev |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
130 |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
131 self.masterGraph.setToGraph(self.staticStmts()) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
132 |
164 | 133 # The order of this list needs to be consistent between the |
134 # deployToArduino call and the poll call. | |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 self._polledDevs = [d for d in self._devs if d.generatePollCode()] |
620 | 139 |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
140 self._statementsFromInputs = {} # input device uri: latest statements |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
141 self._lastPollTime = {} # input device uri: time() |
293 | 142 self._influx = InfluxExporter(self.configGraph) |
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
|
143 self.open() |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
144 for d in self._devs: |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
145 self.syncMasterGraphToHostStatements(d) |
164 | 146 |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
147 def description(self): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
148 """for web page""" |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
149 return { |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
150 'uri': self.uri, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
151 'dev': self.dev, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
152 'baudrate': self.baudrate, |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
153 'devices': [d.description() for d in self._devs], |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
154 } |
620 | 155 |
164 | 156 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
|
157 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
|
158 timeout=2) |
620 | 159 |
256 | 160 def startPolling(self, period=.5): |
161 task.LoopingCall(self._poll).start(period) | |
620 | 162 |
164 | 163 def _poll(self): |
164 """ | |
165 even boards with no inputs need some polling to see if they're | |
166 still ok | |
167 """ | |
168 try: | |
169 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
|
170 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
|
171 reactor.crash() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
172 raise |
164 | 173 except Exception as e: |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
174 import traceback; traceback.print_exc() |
164 | 175 log.warn("poll: %r" % e) |
620 | 176 |
164 | 177 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
|
178 t1 = time.time() |
243
141079644c45
piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp@bigasterisk.com
parents:
233
diff
changeset
|
179 self.ser.write("\x60\x00") # "poll everything" |
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
|
180 for i in self._polledDevs: |
1685 | 181 with i.stats.poll.time(): |
621
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
182 try: |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
183 now = time.time() |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
184 new = i.readFromPoll(self.ser.read) |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
185 if isinstance(new, dict): # new style |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
186 oneshot = new['oneshot'] |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
187 new = new['latest'] |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
188 else: |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
189 oneshot = None |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
190 |
621
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
191 self._updateMasterWithNewPollStatements(i.uri, new) |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
192 |
621
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
193 if oneshot: |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
194 self._sendOneshot(oneshot) |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
195 self._lastPollTime[i.uri] = now |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
196 except: |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
197 log.warn('while polling %r:', i.uri) |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
198 raise |
164 | 199 #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
|
200 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
|
201 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
|
202 raise ValueError("after poll, got %x instead of 'x'" % byte) |
335
3010238b94a0
rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents:
334
diff
changeset
|
203 for i in self._devs: |
3010238b94a0
rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents:
334
diff
changeset
|
204 if i.wantIdleOutput(): |
3010238b94a0
rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents:
334
diff
changeset
|
205 self.ser.write("\x60" + chr(self._devCommandNum[i.uri])) |
3010238b94a0
rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents:
334
diff
changeset
|
206 i.outputIdle(self.ser.write) |
3010238b94a0
rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents:
334
diff
changeset
|
207 if self.ser.read(1) != 'k': |
3010238b94a0
rgb strip animation support in arduinoNode
drewp@bigasterisk.com
parents:
334
diff
changeset
|
208 raise ValueError('no ack after outputIdle') |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
209 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
|
210 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
|
211 log.warn('poll took %.1f seconds' % elapsed) |
293 | 212 |
305
6614416dd2c3
influx output for arduino, update web lib path
drewp@bigasterisk.com
parents:
293
diff
changeset
|
213 stmts = set() |
6614416dd2c3
influx output for arduino, update web lib path
drewp@bigasterisk.com
parents:
293
diff
changeset
|
214 for v in self._statementsFromInputs.values(): |
6614416dd2c3
influx output for arduino, update web lib path
drewp@bigasterisk.com
parents:
293
diff
changeset
|
215 stmts.update(v) |
6614416dd2c3
influx output for arduino, update web lib path
drewp@bigasterisk.com
parents:
293
diff
changeset
|
216 self._influx.exportToInflux(stmts) |
213
9c8674329a13
write some values to carbon/graphite upon polling
drewp@bigasterisk.com
parents:
192
diff
changeset
|
217 |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
218 def _updateMasterWithNewPollStatements(self, dev, new): |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
219 prev = self._statementsFromInputs.get(dev, set()) |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
220 |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
221 # it's important that quads from different devices |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
222 # don't clash, since that can lead to inconsistent |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
223 # patches (e.g. |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
224 # dev1 changes value from 1 to 2; |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
225 # dev2 changes value from 2 to 3; |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
226 # dev1 changes from 2 to 4 but this patch will |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
227 # fail since the '2' statement is gone) |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
228 self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev), |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
229 inContext(new, dev))) |
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
230 self._statementsFromInputs[dev] = new |
620 | 231 |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
232 def _sendOneshot(self, oneshot): |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
233 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
234 for s,p,o in oneshot)).encode('utf8') |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
235 fetch(method='POST', |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
236 url='http://bang6:9071/oneShot', |
251
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
237 headers={'Content-Type': ['text/n3']}, postdata=body, |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
238 timeout=5) |
254df9f881a6
start sending oneshot events from some devices
drewp@bigasterisk.com
parents:
243
diff
changeset
|
239 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
240 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
|
241 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
|
242 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
|
243 stmtsForDev = [] |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
244 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
|
245 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
|
246 raise NotImplementedError |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
247 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
|
248 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
|
249 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
|
250 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
|
251 if stmtsForDev: |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
252 log.info("output goes to action handler for %s" % dev.uri) |
1685 | 253 with dev.stats.output.time(): |
621
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
254 self.ser.write("\x60" + chr(self._devCommandNum[dev.uri])) |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
255 dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read) |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
256 if self.ser.read(1) != 'k': |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
257 raise ValueError( |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
258 "%s sendOutput/generateActionCode didn't use " |
686079900c20
arduinonode: per-device-type timing stats
drewp@bigasterisk.com
parents:
620
diff
changeset
|
259 "matching output bytes" % dev.__class__) |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
260 # Dev *could* change hostStatements at any time, and |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
261 # 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
|
262 # to change them in response to sendOutput so this |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
263 # 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
|
264 # each dev the masterGraph for it to write to. |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
265 self.syncMasterGraphToHostStatements(dev) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
266 log.info("output and masterGraph sync complete") |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
267 if unused: |
218
f8ffb9d8d982
multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents:
214
diff
changeset
|
268 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
|
269 for s in unused: |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
270 log.info("%r", s) |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
271 |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
272 def syncMasterGraphToHostStatements(self, dev): |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
273 hostStmtCtx = URIRef(dev.uri + '/host') |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
274 newQuads = inContext(dev.hostStatements(), hostStmtCtx) |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
275 p = self.masterGraph.patchSubgraph(hostStmtCtx, newQuads) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
276 log.debug("patch master with these host stmts %s", p) |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
277 |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
278 def staticStmts(self): |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
279 return [(HOST[hostname], ROOM['connectedTo'], self.uri, CTX)] |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
280 |
164 | 281 def generateArduinoCode(self): |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
282 code = write_arduino_code.writeCode(self.baudrate, self._devs, |
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
283 self._devCommandNum) |
229 | 284 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
|
285 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
|
286 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
|
287 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
|
288 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
289 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
|
290 # 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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
301 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
|
302 try: |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 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
|
311 return False |
620 | 312 |
164 | 313 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
|
314 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
|
315 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
316 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
|
317 return |
620 | 318 |
164 | 319 try: |
320 if hasattr(self, 'ser'): | |
321 self.ser.close() | |
322 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy') | |
323 try: | |
324 self._arduinoMake(workDir, code) | |
325 finally: | |
326 shutil.rmtree(workDir) | |
327 finally: | |
328 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
|
329 |
164 | 330 def _arduinoMake(self, workDir, code): |
331 with open(workDir + '/makefile', 'w') as makefile: | |
229 | 332 makefile.write(write_arduino_code.writeMakefile( |
333 dev=self.dev, | |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
334 tag=self.configGraph.value(self.uri, ROOM['boardTag']), |
229 | 335 allLibs=sum((d.generateArduinoLibs() for d in self._devs), []))) |
164 | 336 |
337 with open(workDir + '/main.ino', 'w') as main: | |
338 main.write(code) | |
339 | |
340 subprocess.check_call(['make', 'upload'], cwd=workDir) | |
620 | 341 |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
342 |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
343 def currentGraph(self): |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
344 g = Graph() |
620 | 345 |
164 | 346 |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
347 for dev in self._devs: |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
348 for stmt in dev.hostStatements(): |
164 | 349 g.add(stmt) |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
350 return g |
164 | 351 |
352 class Dot(cyclone.web.RequestHandler): | |
353 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
|
354 configGraph = self.settings.config.graph |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
355 dot = dotrender.render(configGraph, self.settings.config.boards) |
164 | 356 self.write(dot) |
620 | 357 |
164 | 358 class ArduinoCode(cyclone.web.RequestHandler): |
359 def get(self): | |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
360 board = [b for b in self.settings.config.boards if |
164 | 361 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
|
362 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
|
363 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
|
364 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
|
365 |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
366 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
|
367 g = Graph() |
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
368 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
|
369 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
|
370 |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
371 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
|
372 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
|
373 # 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
|
374 stmts = list(rdfGraphBody(self.request.body, self.request.headers)) |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
375 for b in self.settings.config.boards: |
169
d228105749ac
new /output to post statements which devices can handle. led and lcd output working
drewp@bigasterisk.com
parents:
166
diff
changeset
|
376 b.outputStatements(stmts) |
620 | 377 |
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
|
378 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
|
379 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
|
380 pred = URIRef(self.get_argument('p')) |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
381 |
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
|
382 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
|
383 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
|
384 obj = Literal(float(turtleLiteral)) |
185
2161c71c7b02
support for device code in the idle loop
drewp@bigasterisk.com
parents:
176
diff
changeset
|
385 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
|
386 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
|
387 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
388 stmt = (subj, pred, obj) |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
389 for b in self.settings.config.boards: |
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
|
390 b.outputStatements([stmt]) |
620 | 391 |
392 | |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
393 class Boards(cyclone.web.RequestHandler): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
394 def get(self): |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
395 self.set_header('Content-type', 'application/json') |
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
396 self.write(json.dumps({ |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
397 'host': hostname, |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
398 'boards': [b.description() for b in self.settings.config.boards] |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
399 }, indent=2)) |
620 | 400 |
164 | 401 def currentSerialDevices(): |
402 log.info('find connected boards') | |
403 return glob.glob('/dev/serial/by-id/*') | |
620 | 404 |
164 | 405 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
|
406 arg = docopt(""" |
218
f8ffb9d8d982
multi-boards on one service, new devices, devices return their current
drewp@bigasterisk.com
parents:
214
diff
changeset
|
407 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
|
408 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
409 -v Verbose |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
410 -s serial logging |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
411 -l slow polling |
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
|
412 """) |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
413 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
|
414 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
|
415 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
|
416 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
|
417 |
f81c4d3d774b
arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp@bigasterisk.com
parents:
170
diff
changeset
|
418 log.setLevel(logging.DEBUG) |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
419 if arg['-s']: |
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
420 logging.getLogger('serial').setLevel(logging.INFO) |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
421 |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
422 masterGraph = PatchableGraph() |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
423 config = Config(masterGraph, slowMode=arg['-l']) |
620 | 424 static = pkg_resources.resource_filename('homeauto_anynode', 'static/') |
190
ad20e5cb4feb
serve ipv6 and use localhost's ipv6 name for getting updates
drewp@bigasterisk.com
parents:
185
diff
changeset
|
425 |
233
4ebb5cc30002
server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp@bigasterisk.com
parents:
230
diff
changeset
|
426 reactor.listenTCP(9059, cyclone.web.Application([ |
620 | 427 (r"/(|output-widgets.html)", cyclone.web.StaticFileHandler, { |
428 "path": static, "default_filename": "index.html"}), | |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
429 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}), |
456
12f9f1838fb5
arduinoNode: build updates, stats support, etc
drewp@bigasterisk.com
parents:
343
diff
changeset
|
430 (r'/stats/(.*)', StatsHandler, {'serverName': 'arduinoNode'}), |
170
376599552a4c
polymer board debug page with working output widgets
drewp@bigasterisk.com
parents:
169
diff
changeset
|
431 (r'/boards', Boards), |
230
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
432 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}), |
0aa54404df19
update arduinoNode to support streamed graph output
drewp@bigasterisk.com
parents:
229
diff
changeset
|
433 (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
|
434 (r'/output', OutputPage), |
164 | 435 (r'/arduinoCode', ArduinoCode), |
436 (r'/dot', Dot), | |
334
bb80182195c0
arduinonode reads config from etcd. use pushConfig.py to inform all nodes
drewp@bigasterisk.com
parents:
331
diff
changeset
|
437 ], config=config), interface='::') |
164 | 438 reactor.run() |
439 | |
440 main() |