annotate service/arduinoNode/arduinoNode.py @ 1098:b5906f6fce3f

save data to influxdb, not graphite Ignore-this: ebf07e54d1949bdf3c2e9a81c5fc7292 darcs-hash:d65ec17d9811cc51bbbbdaef0d1730767bf768df
author drewp <drewp@bigasterisk.com>
date Mon, 01 Aug 2016 02:26:38 -0700
parents ef635e9b9dce
children 6614416dd2c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
1 """
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
2 depends on packages:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
3 arduino-mk
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
4 indent
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
5 """
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
6 from __future__ import division
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
7 import glob, sys, logging, subprocess, socket, os, hashlib, time, tempfile
975
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
8 import shutil, json
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
9 import serial
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 import cyclone.web
1056
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
11 from cyclone.httpclient import fetch
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
12 from rdflib import Graph, Namespace, URIRef, Literal, RDF, ConjunctiveGraph
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
13 from rdflib.parser import StringInputSource
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 from twisted.internet import reactor, task
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
15 from docopt import docopt
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
16
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 import devices
1034
43a2170bbdb8 refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents: 1027
diff changeset
18 import write_arduino_code
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
19 import dotrender
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
20 import rdflib_patch
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
21 rdflib_patch.fixQnameOfUriWithTrailingSlash()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 logging.basicConfig(level=logging.DEBUG)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 from loggingserial import LoggingSerial
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
27 sys.path.append("../../lib")
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
28 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
29
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
30 sys.path.append("/my/proj/light9")
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
31 from light9.rdfdb.patch import Patch
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
32 from light9.rdfdb.rdflibpatch import inContext
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33
1098
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1061
diff changeset
34 sys.path.append("../piNode")
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1061
diff changeset
35 from export_to_influxdb import InfluxExporter
1018
651e5c5b1f3d write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents: 997
diff changeset
36
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 log = logging.getLogger()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 logging.getLogger('serial').setLevel(logging.WARN)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 ROOM = Namespace('http://projects.bigasterisk.com/room/')
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
41 HOST = Namespace('http://bigasterisk.com/ruler/host/')
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
43 ACTION_BASE = 10 # higher than any of the fixed command numbers
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
44
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
45 hostname = socket.gethostname()
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
46 CTX = ROOM['arduinosOn%s' % hostname]
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
47
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48 class Config(object):
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
49 def __init__(self, masterGraph):
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
50 self.graph = ConjunctiveGraph()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 log.info('read config')
1019
93cc6238f5ce support a dir of config files
drewp <drewp@bigasterisk.com>
parents: 1018
diff changeset
52 for f in os.listdir('config'):
93cc6238f5ce support a dir of config files
drewp <drewp@bigasterisk.com>
parents: 1018
diff changeset
53 if f.startswith('.'): continue
93cc6238f5ce support a dir of config files
drewp <drewp@bigasterisk.com>
parents: 1018
diff changeset
54 self.graph.parse('config/%s' % f, format='n3')
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
55 self.graph.bind('', ROOM) # not working
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 self.graph.bind('rdf', RDF)
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
57 # config graph is too noisy; maybe make it a separate resource
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
58 #masterGraph.patch(Patch(addGraph=self.graph))
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 def serialDevices(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 return dict([(row.dev, row.board) for row in self.graph.query(
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 """SELECT ?board ?dev WHERE {
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 ?board :device ?dev;
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 a :ArduinoBoard .
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 }""", initNs={'': ROOM})])
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
66
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 class Board(object):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 """an arduino connected to this computer"""
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 baudrate = 115200
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
70 def __init__(self, dev, configGraph, masterGraph, uri):
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 """
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 each connected thing has some pins.
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 """
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 self.uri = uri
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
75 self.configGraph = configGraph
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
76 self.masterGraph = masterGraph
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 self.dev = dev
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
78
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
79 self.masterGraph.patch(Patch(addQuads=self.staticStmts()))
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
80
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 # The order of this list needs to be consistent between the
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 # deployToArduino call and the poll call.
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
83 self._devs = devices.makeDevices(configGraph, self.uri)
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
84 self._devCommandNum = dict((dev.uri, ACTION_BASE + devIndex)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
85 for devIndex, dev in enumerate(self._devs))
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
86 self._polledDevs = [d for d in self._devs if d.generatePollCode()]
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87
990
11fff6301027 support for device code in the idle loop
drewp <drewp@bigasterisk.com>
parents: 981
diff changeset
88 self._statementsFromInputs = {} # input device uri: latest statements
1048
f3c7f617c335 piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp <drewp@bigasterisk.com>
parents: 1038
diff changeset
89 self._lastPollTime = {} # input device uri: time()
1098
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1061
diff changeset
90 self._influx = InfluxExporter(self.configGraph)
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
91 self.open()
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
92 for d in self._devs:
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
93 self.syncMasterGraphToHostStatements(d)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94
975
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
95 def description(self):
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
96 """for web page"""
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
97 return {
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
98 'uri': self.uri,
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
99 'dev': self.dev,
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
100 'baudrate': self.baudrate,
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
101 'devices': [d.description() for d in self._devs],
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
102 }
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
103
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 def open(self):
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
105 self.ser = LoggingSerial(port=self.dev, baudrate=self.baudrate,
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
106 timeout=2)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107
1061
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
108 def startPolling(self, period=.5):
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
109 task.LoopingCall(self._poll).start(period)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 def _poll(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112 """
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 even boards with no inputs need some polling to see if they're
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 still ok
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 """
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 try:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117 self._pollWork()
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
118 except serial.SerialException:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
119 reactor.crash()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
120 raise
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 except Exception as e:
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
122 import traceback; traceback.print_exc()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 log.warn("poll: %r" % e)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125 def _pollWork(self):
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
126 t1 = time.time()
1048
f3c7f617c335 piNode poll switches much faster. mirror the logic in arduinoNode though vari-rate poll is not supported yet
drewp <drewp@bigasterisk.com>
parents: 1038
diff changeset
127 self.ser.write("\x60\x00") # "poll everything"
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
128 for i in self._polledDevs:
1061
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
129 try:
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
130 now = time.time()
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
131 new = i.readFromPoll(self.ser.read)
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
132 if isinstance(new, dict): # new style
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
133 oneshot = new['oneshot']
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
134 new = new['latest']
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
135 else:
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
136 oneshot = None
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
137 prev = self._statementsFromInputs.get(i.uri, [])
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
138 if new or prev:
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
139 self._statementsFromInputs[i.uri] = new
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
140 # it's important that quads from different devices
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
141 # don't clash, since that can lead to inconsistent
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
142 # patches (e.g.
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
143 # dev1 changes value from 1 to 2;
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
144 # dev2 changes value from 2 to 3;
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
145 # dev1 changes from 2 to 4 but this patch will
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
146 # fail since the '2' statement is gone)
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
147 self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri),
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
148 inContext(new, i.uri)))
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
149 if oneshot:
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
150 self._sendOneshot(oneshot)
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
151 self._lastPollTime[i.uri] = now
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
152 except:
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
153 log.warn('while polling %r:', i.uri)
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
154 raise
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
155 #plus statements about succeeding or erroring on the last poll
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
156 byte = self.ser.read(1)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
157 if byte != 'x':
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
158 raise ValueError("after poll, got %x instead of 'x'" % byte)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
159 elapsed = time.time() - t1
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
160 if elapsed > 1.0:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
161 log.warn('poll took %.1f seconds' % elapsed)
1098
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1061
diff changeset
162
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1061
diff changeset
163 self._influx.exportToInflux(
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1061
diff changeset
164 set.union([set(v) for v in self._statementsFromInputs.values()]))
1018
651e5c5b1f3d write some values to carbon/graphite upon polling
drewp <drewp@bigasterisk.com>
parents: 997
diff changeset
165
1056
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
166 def _sendOneshot(self, oneshot):
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
167 body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3())
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
168 for s,p,o in oneshot)).encode('utf8')
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
169 bang6 = 'fcb8:4119:fb46:96f8:8b07:1260:0f50:fcfa'
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
170 fetch(method='POST',
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
171 url='http://[%s]:9071/oneShot' % bang6,
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
172 headers={'Content-Type': ['text/n3']}, postdata=body,
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
173 timeout=5)
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
174
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
175 def outputStatements(self, stmts):
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
176 unused = set(stmts)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
177 for dev in self._devs:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
178 stmtsForDev = []
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
179 for pat in dev.outputPatterns():
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
180 if [term is None for term in pat] != [False, False, True]:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
181 raise NotImplementedError
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
182 for stmt in stmts:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
183 if stmt[:2] == pat[:2]:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
184 stmtsForDev.append(stmt)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
185 unused.discard(stmt)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
186 if stmtsForDev:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
187 log.info("output goes to action handler for %s" % dev.uri)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
188 self.ser.write("\x60" + chr(self._devCommandNum[dev.uri]))
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
189 dev.sendOutput(stmtsForDev, self.ser.write, self.ser.read)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
190 if self.ser.read(1) != 'k':
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
191 raise ValueError(
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
192 "%s sendOutput/generateActionCode didn't use "
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
193 "matching output bytes" % dev.__class__)
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
194 # Dev *could* change hostStatements at any time, and
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
195 # we're not currently tracking that, but the usual is
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
196 # to change them in response to sendOutput so this
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
197 # should be good enough. The right answer is to give
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
198 # each dev the masterGraph for it to write to.
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
199 self.syncMasterGraphToHostStatements(dev)
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
200 log.info("output and masterGraph sync complete")
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
201 if unused:
1023
3e6fac8569cd multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents: 1019
diff changeset
202 log.info("Board %s doesn't care about these statements:", self.uri)
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
203 for s in unused:
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
204 log.warn("%r", s)
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
205
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
206 def syncMasterGraphToHostStatements(self, dev):
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
207 hostStmtCtx = URIRef(dev.uri + '/host')
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
208 newQuads = inContext(dev.hostStatements(), hostStmtCtx)
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
209 p = self.masterGraph.patchSubgraph(hostStmtCtx, newQuads)
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
210 log.debug("patch master with these host stmts %s", p)
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
211
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
212 def staticStmts(self):
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
213 return [(HOST[hostname], ROOM['connectedTo'], self.uri, CTX)]
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
214
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
215 def generateArduinoCode(self):
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
216 code = write_arduino_code.writeCode(self.baudrate, self._devs,
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
217 self._devCommandNum)
1034
43a2170bbdb8 refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents: 1027
diff changeset
218 code = write_arduino_code.indent(code)
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
219 cksum = hashlib.sha1(code).hexdigest()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
220 code = code.replace('CODE_CHECKSUM', cksum)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
221 return code, cksum
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
222
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
223 def _readBoardChecksum(self, length):
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
224 # this is likely right after reset, so it might take 2 seconds
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
225 for tries in range(6):
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
226 self.ser.write("\x60\x01")
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
227 try:
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
228 return self.ser.read(length)
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
229 except ValueError:
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
230 if tries == 5:
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
231 raise
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
232 time.sleep(.5)
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
233 raise ValueError
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
234
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
235 def _boardIsCurrent(self, currentChecksum):
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
236 try:
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
237 boardCksum = self._readBoardChecksum(len(currentChecksum))
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
238 if boardCksum == currentChecksum:
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
239 log.info("board has current code (%s)" % currentChecksum)
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
240 return True
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
241 else:
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
242 log.info("board responds with incorrect code version")
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
243 except Exception as e:
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
244 log.info("can't get code version from board: %r" % e)
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
245 return False
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
246
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
247 def deployToArduino(self):
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
248 code, cksum = self.generateArduinoCode()
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
249
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
250 if self._boardIsCurrent(cksum):
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
251 return
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
252
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
253 try:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
254 if hasattr(self, 'ser'):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
255 self.ser.close()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
256 workDir = tempfile.mkdtemp(prefix='arduinoNode_board_deploy')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
257 try:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
258 self._arduinoMake(workDir, code)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
259 finally:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
260 shutil.rmtree(workDir)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
261 finally:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
262 self.open()
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
263
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
264 def _arduinoMake(self, workDir, code):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
265 with open(workDir + '/makefile', 'w') as makefile:
1034
43a2170bbdb8 refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents: 1027
diff changeset
266 makefile.write(write_arduino_code.writeMakefile(
43a2170bbdb8 refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents: 1027
diff changeset
267 dev=self.dev,
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
268 tag=self.configGraph.value(self.uri, ROOM['boardTag']),
1034
43a2170bbdb8 refactor to write_arduino_node
drewp <drewp@bigasterisk.com>
parents: 1027
diff changeset
269 allLibs=sum((d.generateArduinoLibs() for d in self._devs), [])))
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
270
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
271 with open(workDir + '/main.ino', 'w') as main:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
272 main.write(code)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
273
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
274 subprocess.check_call(['make', 'upload'], cwd=workDir)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
275
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
276
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
277 def currentGraph(self):
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
278 g = Graph()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
279
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
280
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
281 for dev in self._devs:
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
282 for stmt in dev.hostStatements():
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
283 g.add(stmt)
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
284 return g
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
285
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
286 class Dot(cyclone.web.RequestHandler):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
287 def get(self):
971
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
288 configGraph = self.settings.config.graph
fbe72d44f15a only recompile if the C code is new. redo Device class api. single temperature sensor is working
drewp <drewp@bigasterisk.com>
parents: 969
diff changeset
289 dot = dotrender.render(configGraph, self.settings.boards)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
290 self.write(dot)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
291
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
292 class ArduinoCode(cyclone.web.RequestHandler):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
293 def get(self):
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
294 board = [b for b in self.settings.boards if
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
295 b.uri == URIRef(self.get_argument('board'))][0]
1023
3e6fac8569cd multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents: 1019
diff changeset
296 self.set_header('Content-Type', 'text/plain')
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
297 code, cksum = board.generateArduinoCode()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
298 self.write(code)
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
299
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
300 def rdfGraphBody(body, headers):
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
301 g = Graph()
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
302 g.parse(StringInputSource(body), format='nt')
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
303 return g
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
304
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
305 class OutputPage(cyclone.web.RequestHandler):
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
306 def post(self):
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
307 # for old ui; use PUT instead
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
308 stmts = list(rdfGraphBody(self.request.body, self.request.headers))
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
309 for b in self.settings.boards:
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
310 b.outputStatements(stmts)
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
311
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
312 def put(self):
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
313 subj = URIRef(self.get_argument('s'))
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
314 pred = URIRef(self.get_argument('p'))
975
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
315
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
316 turtleLiteral = self.request.body
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
317 try:
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
318 obj = Literal(float(turtleLiteral))
990
11fff6301027 support for device code in the idle loop
drewp <drewp@bigasterisk.com>
parents: 981
diff changeset
319 except ValueError:
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
320 obj = Literal(turtleLiteral)
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
321
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
322 stmt = (subj, pred, obj)
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
323 for b in self.settings.boards:
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
324 b.outputStatements([stmt])
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
325
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
326
975
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
327 class Boards(cyclone.web.RequestHandler):
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
328 def get(self):
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
329 self.set_header('Content-type', 'application/json')
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
330 self.write(json.dumps({
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
331 'host': hostname,
975
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
332 'boards': [b.description() for b in self.settings.boards]
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
333 }, indent=2))
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
334
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
335 def currentSerialDevices():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
336 log.info('find connected boards')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
337 return glob.glob('/dev/serial/by-id/*')
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
338
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
339 def main():
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
340 arg = docopt("""
1023
3e6fac8569cd multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents: 1019
diff changeset
341 Usage: arduinoNode.py [options]
981
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
342
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
343 -v Verbose
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
344 """)
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
345 log.setLevel(logging.WARN)
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
346 if arg['-v']:
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
347 from twisted.python import log as twlog
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
348 twlog.startLogging(sys.stdout)
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
349
d9bbbd8d86f6 arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body
drewp <drewp@bigasterisk.com>
parents: 975
diff changeset
350 log.setLevel(logging.DEBUG)
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
351
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
352 masterGraph = PatchableGraph()
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
353 config = Config(masterGraph)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
354 current = currentSerialDevices()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
355
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
356 boards = []
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
357 for dev, board in config.serialDevices().items():
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
358 if str(dev) not in current:
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
359 continue
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
360 log.info("we have board %s connected at %s" % (board, dev))
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
361 b = Board(dev, config.graph, masterGraph, board)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
362 boards.append(b)
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
363
1023
3e6fac8569cd multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents: 1019
diff changeset
364 for b in boards:
3e6fac8569cd multi-boards on one service, new devices, devices return their current
drewp <drewp@bigasterisk.com>
parents: 1019
diff changeset
365 b.deployToArduino()
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
366
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
367 log.info('open boards')
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
368 for b in boards:
1061
ef635e9b9dce polling timing and error catching
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
369 b.startPolling(period=.1 if not arg['-v'] else 10)
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
370
995
f72068e9d9d8 serve ipv6 and use localhost's ipv6 name for getting updates
drewp <drewp@bigasterisk.com>
parents: 990
diff changeset
371
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
372 reactor.listenTCP(9059, cyclone.web.Application([
975
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
373 (r"/()", cyclone.web.StaticFileHandler, {
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
374 "path": "static", "default_filename": "index.html"}),
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
375 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": "static"}),
f3023410d875 polymer board debug page with working output widgets
drewp <drewp@bigasterisk.com>
parents: 974
diff changeset
376 (r'/boards', Boards),
1035
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
377 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}),
f01d9892ed79 update arduinoNode to support streamed graph output
drewp <drewp@bigasterisk.com>
parents: 1034
diff changeset
378 (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}),
974
f707210c13bd new /output to post statements which devices can handle. led and lcd output working
drewp <drewp@bigasterisk.com>
parents: 971
diff changeset
379 (r'/output', OutputPage),
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
380 (r'/arduinoCode', ArduinoCode),
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
381 (r'/dot', Dot),
1038
ffe6a00c6cef server/browser graph sync. cut dependency on the WS version. merge some changes between arduino/pi code.
drewp <drewp@bigasterisk.com>
parents: 1035
diff changeset
382 ], config=config, boards=boards), interface='::')
969
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
383 reactor.run()
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
384
70a5392b24d3 start arduinonode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
385 main()