annotate service/piNode/piNode.py @ 1442:759841548982

more piNode wip. treq etc Ignore-this: 153da9ea2f0e0cf56ec2ccb0c5dd5c5 darcs-hash:9620c5455a5c3e3ca98a525028205d295b1dee44
author drewp <drewp@bigasterisk.com>
date Mon, 12 Aug 2019 10:14:45 -0700
parents 233a07f068a8
children 23ab244aa649
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
1 import logging, socket, json, time, pkg_resources
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 import cyclone.web
1031
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
3 from rdflib import Namespace, URIRef, Literal, Graph, RDF, ConjunctiveGraph
989
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
4 from rdflib.parser import StringInputSource
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
5 from twisted.internet import reactor
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
6 from twisted.internet.defer import inlineCallbacks, maybeDeferred, returnValue
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
7 from twisted.internet.threads import deferToThread
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 from docopt import docopt
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
9 import etcd3
1269
73b6ed12bae6 stats handler, events output fix, build updaets
drewp <drewp@bigasterisk.com>
parents: 1211
diff changeset
10 from greplin import scales
73b6ed12bae6 stats handler, events output fix, build updaets
drewp <drewp@bigasterisk.com>
parents: 1211
diff changeset
11 from greplin.scales.cyclonehandler import StatsHandler
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
12 import pigpio
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
13 import treq
1136
1e43ec4a5f23 build and import updates for rdfdb, etc
drewp <drewp@bigasterisk.com>
parents: 1130
diff changeset
14
1031
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
15 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler
1392
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
16 from cycloneerr import PrettyErrorHandler
1143
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
17
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
18 from rdfdb.rdflibpatch import inContext
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
19 from rdfdb.patch import Patch
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
20
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
21 try:
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
22 import pigpio
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
23 except ImportError:
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
24 class pigpio(object):
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
25 @staticmethod
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
26 def pi():
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
27 return None
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 import devices
1098
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1076
diff changeset
30 from export_to_influxdb import InfluxExporter
1026
8e075449ba0a piNode support for temp sensors. proper hostname lookup
drewp <drewp@bigasterisk.com>
parents: 1025
diff changeset
31
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 log = logging.getLogger()
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 logging.getLogger('serial').setLevel(logging.WARN)
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
34
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 ROOM = Namespace('http://projects.bigasterisk.com/room/')
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 HOST = Namespace('http://bigasterisk.com/ruler/host/')
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 hostname = socket.gethostname()
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
39 CTX = ROOM['pi/%s' % hostname]
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
40
1269
73b6ed12bae6 stats handler, events output fix, build updaets
drewp <drewp@bigasterisk.com>
parents: 1211
diff changeset
41 STATS = scales.collection('/root',
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
42 scales.PmfStat('configReread'),
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
43 scales.IntStat('pollException'),
1428
56e07d3f0930 frontdoor configs; move device class timing to greplin stats
drewp <drewp@bigasterisk.com>
parents: 1427
diff changeset
44 scales.PmfStat('pollAll'),
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
45 scales.PmfStat('boardPoll'),
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
46 scales.PmfStat('sendOneshot'),
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
47 scales.PmfStat('outputStatements'),
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
48
1269
73b6ed12bae6 stats handler, events output fix, build updaets
drewp <drewp@bigasterisk.com>
parents: 1211
diff changeset
49 )
1063
295d20307b81 try a speedup for raspi
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
50
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
51 class Config(object):
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
52 def __init__(self, masterGraph, hubHost):
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
53 self.etcd = etcd3.client(host=hubHost, port=9022)
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
54
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
55 self.masterGraph = masterGraph
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
56 self.hubHost = hubHost
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
57 self.configGraph = ConjunctiveGraph()
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
58 self.boards = []
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
59 self.etcPrefix = 'pi/'
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
60 self.rereadLater = None
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
61
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
62 self.reread()
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
63
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
64 deferToThread(self.watchEtcd)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
65
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
66 def watchEtcd(self):
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
67 events, cancel = self.etcd.watch_prefix(self.etcPrefix)
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
68 reactor.addSystemEventTrigger('before', 'shutdown', cancel)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
69 for ev in events:
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
70 log.info('%s changed', ev.key)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
71 reactor.callFromThread(self.configChanged)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
72
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
73 def configChanged(self):
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
74 self.cancelRead()
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
75 self.rereadLater = reactor.callLater(.1, self.reread)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
76
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
77 def cancelRead(self):
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
78 if self.rereadLater:
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
79 self.rereadLater.cancel()
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
80 self.rereadLater = None
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
81
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
82 @STATS.configReread.time()
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
83 def reread(self):
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
84 self.rereadLater = None
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
85 log.info('read config')
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
86 self.configGraph = ConjunctiveGraph()
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
87 for v, md in self.etcd.get_prefix(self.etcPrefix):
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
88 log.info(' read file %r', md.key)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
89 self.configGraph.parse(StringInputSource(v), format='n3')
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
90 self.configGraph.bind('', ROOM)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
91 self.configGraph.bind('rdf', RDF)
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
92 # config graph is too noisy; maybe make it a separate resource
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
93 #masterGraph.patch(Patch(addGraph=self.configGraph))
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
94 self.setupBoards()
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
95
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
96 def setupBoards(self):
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
97 thisHost = Literal(hostname)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
98 for row in self.configGraph.query(
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
99 'SELECT ?board WHERE { ?board a :PiBoard; :hostname ?h }',
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
100 initBindings=dict(h=thisHost)):
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
101 thisBoard = row.board
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
102 break
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
103 else:
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
104 log.warn("config had no board for :hostname %s. Waiting for config update." %
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
105 thisHost)
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
106 self.boards = []
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
107 return
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
108
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
109 log.info("found config for board %r" % thisBoard)
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
110 self.boards = [Board(self.configGraph, self.masterGraph, thisBoard, self.hubHost)]
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
111
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
113 class DeviceRunner(object):
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
114 def __init__(self, dev):
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
115 self.dev = dev
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
116 self.period = getattr(self.dev, 'pollPeriod', .05)
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
117 #self._lastPollTime.get(i.uri, 0) + self.pollPeriod > now):
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
118
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
119 reactor.callLater(0, self.poll)
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
120
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
121 @inlineCallbacks
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
122 def poll(self):
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
123 now = time.time()
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
124 try:
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
125 with self.dev.stats.poll.time():
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
126 new = yield maybeDeferred(self.dev.poll)
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
127 finally:
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
128 reactor.callLater(max(0, time.time() - (now + self.period)), self.poll)
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
129 returnValue(new)
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
130
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
131 class Board(object):
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
132 """similar to arduinoNode.Board but without the communications stuff"""
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
133 def __init__(self, graph, masterGraph, uri, hubHost):
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 self.graph, self.uri = graph, uri
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
135 self.hubHost = hubHost
1031
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
136 self.masterGraph = masterGraph
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
137
1154
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
138 self.masterGraph.setToGraph(self.staticStmts())
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 self.pi = pigpio.pi()
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
140 self._devs = [DeviceRunner(d) for d in devices.makeDevices(graph, self.uri, self.pi)]
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
141 log.debug('found %s devices', len(self._devs))
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
142 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
143 self._lastPollTime = {} # input device uri: time()
1098
b5906f6fce3f save data to influxdb, not graphite
drewp <drewp@bigasterisk.com>
parents: 1076
diff changeset
144 self._influx = InfluxExporter(self.graph)
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
145 for d in self._devs:
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
146 self.syncMasterGraphToHostStatements(d.dev)
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
147
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
148 def startPolling(self):
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
149 task.LoopingCall(self._poll).start(.05)
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
150
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
151 @STATS.boardPoll.time() # not differentiating multiple boards here
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
152 def _poll(self):
1076
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
153 try:
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
154 self._pollMaybeError()
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
155 except Exception:
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
156 STATS.pollException += 1
1076
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
157 log.exception("During poll:")
1392
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
158
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
159
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
160 @inlineCallbacks
1428
56e07d3f0930 frontdoor configs; move device class timing to greplin stats
drewp <drewp@bigasterisk.com>
parents: 1427
diff changeset
161 def _pollOneDev(self, i):
1392
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
162 now = time.time()
1428
56e07d3f0930 frontdoor configs; move device class timing to greplin stats
drewp <drewp@bigasterisk.com>
parents: 1427
diff changeset
163
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
164 new = i.poll()
1392
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
165 if isinstance(new, dict): # new style
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
166 oneshot = new['oneshot']
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
167 new = new['latest']
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
168 else:
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
169 oneshot = None
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
170
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
171 self._updateMasterWithNewPollStatements(i.uri, new)
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
172
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
173 if oneshot:
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
174 self._sendOneshot(oneshot)
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
175 self._lastPollTime[i.uri] = now
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
176
1392
83043957c809 pinode devs can now poll in parallel (within one poll step). doesn't help much.
drewp <drewp@bigasterisk.com>
parents: 1356
diff changeset
177 @inlineCallbacks
1076
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
178 def _pollMaybeError(self):
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
179 pollTime = {} # uri: sec
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
180 yield gatherResults([self._pollOneDev(i.dev, pollTime)
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
181 for i in self._devs], consumeErrors=True)
1056
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
182
1143
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
183 pollResults = map(set, self._statementsFromInputs.values())
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
184 if pollResults:
d1bc88f67969 RgbPixelsAnimation and docker build updates
drewp <drewp@bigasterisk.com>
parents: 1136
diff changeset
185 self._influx.exportToInflux(set.union(*pollResults))
1026
8e075449ba0a piNode support for temp sensors. proper hostname lookup
drewp <drewp@bigasterisk.com>
parents: 1025
diff changeset
186
1154
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
187 def _updateMasterWithNewPollStatements(self, dev, new):
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
188 prev = self._statementsFromInputs.get(dev, set())
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
189
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
190 # it's important that quads from different devices
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
191 # don't clash, since that can lead to inconsistent
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
192 # patches (e.g.
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
193 # dev1 changes value from 1 to 2;
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
194 # dev2 changes value from 2 to 3;
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
195 # dev1 changes from 2 to 4 but this patch will
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
196 # fail since the '2' statement is gone)
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
197 self.masterGraph.patch(Patch.fromDiff(inContext(prev, dev),
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
198 inContext(new, dev)))
5da3a85b59e4 more robust about not accumulating old stmts
drewp <drewp@bigasterisk.com>
parents: 1152
diff changeset
199 self._statementsFromInputs[dev] = new
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
200
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
201 @STATS.sendOneshot.time()
1056
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
202 def _sendOneshot(self, oneshot):
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
203 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
204 for s,p,o in oneshot)).encode('utf8')
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
205 url = 'http://%s:9071/oneShot' % self.hubHost
1076
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
206 d = fetch(method='POST',
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
207 url=url,
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
208 headers={'Content-Type': ['text/n3']},
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
209 postdata=body,
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
210 timeout=5)
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
211 def err(e):
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
212 log.info('oneshot post to %r failed: %s',
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
213 url, e.getErrorMessage())
d0ca77a3faf4 rewrite oneShotPost to ease debugging. add try-block around polling
drewp <drewp@bigasterisk.com>
parents: 1074
diff changeset
214 d.addErrback(err)
1056
d2007482aec5 start sending oneshot events from some devices
drewp <drewp@bigasterisk.com>
parents: 1048
diff changeset
215
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
216 @STATS.outputStatements.time()
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
217 def outputStatements(self, stmts):
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
218 unused = set(stmts)
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
219 for devRunner in self._devs:
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
220 dev = devRunner.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
221 stmtsForDev = []
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
222 for pat in dev.outputPatterns():
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
223 if [term is None for term in pat] != [False, False, True]:
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
224 raise NotImplementedError
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
225 for stmt in stmts:
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
226 if stmt[:2] == pat[:2]:
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
227 stmtsForDev.append(stmt)
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
228 unused.discard(stmt)
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
229 if stmtsForDev:
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
230 log.info("output goes to action handler for %s" % dev.uri)
1428
56e07d3f0930 frontdoor configs; move device class timing to greplin stats
drewp <drewp@bigasterisk.com>
parents: 1427
diff changeset
231 with dev.stats.output.time():
56e07d3f0930 frontdoor configs; move device class timing to greplin stats
drewp <drewp@bigasterisk.com>
parents: 1427
diff changeset
232 dev.sendOutput(stmtsForDev)
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
233
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
234 # Dev *could* change hostStatements at any time, and
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
235 # we're not currently tracking that, but the usual is
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
236 # to change them in response to sendOutput so this
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
237 # should be good enough. The right answer is to give
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
238 # each dev the masterGraph for it to write to.
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
239 self.syncMasterGraphToHostStatements(dev)
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
240 log.info("output and masterGraph sync complete")
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
241 if unused:
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
242 log.info("Board %s doesn't care about these statements:", self.uri)
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
243 for s in unused:
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
244 log.warn("%r", s)
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
245
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
246 def syncMasterGraphToHostStatements(self, dev):
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
247 hostStmtCtx = URIRef(dev.uri + '/host')
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
248 newQuads = inContext(dev.hostStatements(), hostStmtCtx)
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
249 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
250 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
251
1031
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
252 def staticStmts(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
253 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
254
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
255 def description(self):
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
256 """for web page"""
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
257 return {
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
258 'uri': self.uri,
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
259 'devices': [d.dev.description() for d in self._devs],
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
260 'graph': 'http://sticker:9059/graph', #todo
1431
af2d0249a2cc wip for pytype support and separate device run loops on piNode
drewp <drewp@bigasterisk.com>
parents: 1430
diff changeset
261 }
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
262
989
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
263 def rdfGraphBody(body, headers):
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
264 g = Graph()
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
265 g.parse(StringInputSource(body), format='nt')
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
266 return g
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
267
1394
b27d0f9a00ef pinode use prettyerrorhandler
drewp <drewp@bigasterisk.com>
parents: 1392
diff changeset
268 class OutputPage(PrettyErrorHandler, cyclone.web.RequestHandler):
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
269 def put(self):
989
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
270 arg = self.request.arguments
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
271 if arg.get('s') and arg.get('p'):
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
272 subj = URIRef(arg['s'][-1])
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
273 pred = URIRef(arg['p'][-1])
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
274 turtleLiteral = self.request.body
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
275 try:
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
276 obj = Literal(float(turtleLiteral))
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
277 except ValueError:
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
278 obj = Literal(turtleLiteral)
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
279 stmt = (subj, pred, obj)
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
280 else:
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
281 g = rdfGraphBody(self.request.body, self.request.headers)
065fc9e07c10 piNode allow nt graphs as the body of a PUT /output
drewp <drewp@bigasterisk.com>
parents: 988
diff changeset
282 assert len(g) == 1, len(g)
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
283 stmt = next(g.triples((None, None, None)))
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
284
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
285 for b in self.settings.config.boards:
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
286 b.outputStatements({stmt})
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
287
1394
b27d0f9a00ef pinode use prettyerrorhandler
drewp <drewp@bigasterisk.com>
parents: 1392
diff changeset
288 class Boards(PrettyErrorHandler, cyclone.web.RequestHandler):
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
289 def get(self):
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
290 self.set_header('Content-type', 'application/json')
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
291 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
292 'host': hostname,
1152
6d2eba4d0dfd pi read config over etcd
drewp <drewp@bigasterisk.com>
parents: 1143
diff changeset
293 'boards': [b.description() for b in self.settings.config.boards]
988
63c172316239 get piNode working, for motionsensor at least
drewp <drewp@bigasterisk.com>
parents: 987
diff changeset
294 }, indent=2))
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
295
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
296 def main():
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
297 arg = docopt("""
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
298 Usage: piNode.py [options]
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
299
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
300 -v Verbose
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
301 --ow Just report onewire device URIs and readings, then exit.
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
302 --hub=HOST Hostname for etc3 and oneshot posts. [default: bang.vpn-home.bigasterisk.com]
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
303 """)
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
304 log.setLevel(logging.WARN)
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
305 if arg['-v']:
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
306 from twisted.python import log as twlog
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
307 twlog.startLogging(sys.stdout)
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
308
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
309 log.setLevel(logging.DEBUG)
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
310
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
311 if arg['--ow']:
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
312 log.setLevel(logging.INFO)
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
313 for stmt in devices.OneWire().poll():
1439
233a07f068a8 piNode to py3
drewp <drewp@bigasterisk.com>
parents: 1431
diff changeset
314 print(stmt)
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
315 return
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
316
1031
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
317 masterGraph = PatchableGraph()
1211
8512001ae0a1 cmdline flag on piNode to pick hub host
drewp <drewp@bigasterisk.com>
parents: 1187
diff changeset
318 config = Config(masterGraph, arg['--hub'])
1427
6bd36e5e109f whitespace
drewp <drewp@bigasterisk.com>
parents: 1394
diff changeset
319
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
320 static = pkg_resources.resource_filename('homeauto_anynode', 'static/')
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
321
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
322 reactor.listenTCP(9059, cyclone.web.Application([
1430
445e24e8c8bb lib upgrades; fix a static http server path
drewp <drewp@bigasterisk.com>
parents: 1428
diff changeset
323 (r"/(|output-widgets.html)", cyclone.web.StaticFileHandler, {
1355
f2159312b0e7 update build and deps to use invoke and to use new lib layout, plus more stats collection
drewp <drewp@bigasterisk.com>
parents: 1269
diff changeset
324 "path": static, "default_filename": "index.html"}),
1356
35090659d414 fix /static path dir
drewp <drewp@bigasterisk.com>
parents: 1355
diff changeset
325 (r'/static/(.*)', cyclone.web.StaticFileHandler, {"path": static}),
1269
73b6ed12bae6 stats handler, events output fix, build updaets
drewp <drewp@bigasterisk.com>
parents: 1211
diff changeset
326 (r'/stats/(.*)', StatsHandler, {'serverName': 'piNode'}),
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
327 (r'/boards', Boards),
1031
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
328 (r"/graph", CycloneGraphHandler, {'masterGraph': masterGraph}),
9328df09f679 piNode uses new graph SSE code
drewp <drewp@bigasterisk.com>
parents: 1030
diff changeset
329 (r"/graph/events", CycloneGraphEventsHandler, {'masterGraph': masterGraph}),
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
330 (r'/output', OutputPage),
1269
73b6ed12bae6 stats handler, events output fix, build updaets
drewp <drewp@bigasterisk.com>
parents: 1211
diff changeset
331 ], config=config, debug=arg['-v']), interface='::')
1063
295d20307b81 try a speedup for raspi
drewp <drewp@bigasterisk.com>
parents: 1056
diff changeset
332 log.warn('serving on 9059')
987
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
333 reactor.run()
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
334
cc79d092e136 start pinode
drewp <drewp@bigasterisk.com>
parents:
diff changeset
335 main()