annotate service/reasoning/actions.py @ 760:3c18b4b3b72c

more py3 fixes Ignore-this: f212b4a5edf8e599e9efd70bc65e7651
author drewp@bigasterisk.com
date Fri, 14 Feb 2020 00:33:31 -0800
parents f3f667769aef
children c8562ace4917
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
1 import json
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
2 import logging
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
3 import urllib
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
4
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
5 from rdflib import URIRef, Namespace, RDF, Literal
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
6 from twisted.internet import reactor
605
ad4c4d7c1fb9 reasoning output using treq, and keep writing to PUT calls forever (but not as fast as the reasoning loop runs)
drewp@bigasterisk.com
parents: 603
diff changeset
7 import treq
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
8
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
9 from httpputoutputs import HttpPutOutputs
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 720
diff changeset
10 from inputgraph import InputGraph
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
11
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
12 log = logging.getLogger('output')
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
13
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
14 ROOM = Namespace("http://projects.bigasterisk.com/room/")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
15 DEV = Namespace("http://projects.bigasterisk.com/device/")
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
16 REASONING = Namespace("http://projects.bigasterisk.com/ns/reasoning/")
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
17
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
18 def secsFromLiteral(v):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
19 if v[-1] != 's':
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
20 raise NotImplementedError(v)
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
21 return float(v[:-1])
605
ad4c4d7c1fb9 reasoning output using treq, and keep writing to PUT calls forever (but not as fast as the reasoning loop runs)
drewp@bigasterisk.com
parents: 603
diff changeset
22
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
23 def ntStatement(stmt):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
24 def compact(u):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
25 if isinstance(u, URIRef) and u.startswith(ROOM):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
26 return 'room:' + u[len(ROOM):]
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
27 return u.n3()
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
28 return '%s %s %s .' % (compact(stmt[0]), compact(stmt[1]), compact(stmt[2]))
605
ad4c4d7c1fb9 reasoning output using treq, and keep writing to PUT calls forever (but not as fast as the reasoning loop runs)
drewp@bigasterisk.com
parents: 603
diff changeset
29
ad4c4d7c1fb9 reasoning output using treq, and keep writing to PUT calls forever (but not as fast as the reasoning loop runs)
drewp@bigasterisk.com
parents: 603
diff changeset
30
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
31 class Actions(object):
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 720
diff changeset
32 def __init__(self, inputGraph: InputGraph, sendToLiveClients, mockOutput=False):
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
33 self.inputGraph = inputGraph
608
62171aa2bc4e mock output mode
drewp@bigasterisk.com
parents: 607
diff changeset
34 self.mockOutput = mockOutput
62171aa2bc4e mock output mode
drewp@bigasterisk.com
parents: 607
diff changeset
35 self.putOutputs = HttpPutOutputs(mockOutput=mockOutput)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
36 self.sendToLiveClients = sendToLiveClients
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
37
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
38 def putResults(self, inferred):
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
39 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
40 some conclusions in the inferred graph lead to PUT requests
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
41 getting made
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
42
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
43 if the graph contains (?d ?p ?o) and ?d and ?p are a device
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
44 and predicate we support PUTs for, then we look up
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
45 (?d :putUrl ?url) and (?o :putValue ?val) and call
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
46 PUT ?url <- ?val
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
47
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
48 If the graph doesn't contain any matches, we use (?d
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
49 :zeroValue ?val) for the value and PUT that.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
50 """
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
51 deviceGraph = self.inputGraph.getGraph()
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
52 activated = set() # (subj,pred) pairs for which we're currently putting some value
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
53 activated.update(self._putDevices(deviceGraph, inferred))
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
54 self._oneShotPostActions(deviceGraph, inferred)
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
55 self.putDefaults(deviceGraph, activated)
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
56
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
57 def _putDevices(self, deviceGraph, inferred):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
58 activated = set()
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
59 agentFor = {}
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
60 for stmt in inferred:
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
61 if stmt[1] == ROOM['putAgent']:
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
62 agentFor[stmt[0]] = stmt[2]
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
63 for stmt in inferred:
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
64 log.debug('inferred stmt we might PUT: %s', ntStatement(stmt))
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
65 putUrl = deviceGraph.value(stmt[0], ROOM['putUrl'])
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
66 putPred = deviceGraph.value(stmt[0], ROOM['putPredicate'])
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
67 matchPred = deviceGraph.value(stmt[0], ROOM['matchPredicate'],
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
68 default=putPred)
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
69 if putUrl and matchPred == stmt[1]:
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
70 log.debug('putDevices: stmt %s leads to putting at %s',
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
71 ntStatement(stmt), putUrl.n3())
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 720
diff changeset
72 self._put(putUrl + '?' + urllib.parse.urlencode([
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
73 ('s', str(stmt[0])),
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
74 ('p', str(putPred))]),
760
3c18b4b3b72c more py3 fixes
drewp@bigasterisk.com
parents: 756
diff changeset
75 payload=str(stmt[2].toPython()),
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
76 agent=agentFor.get(stmt[0], None),
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
77 refreshSecs=self._getRefreshSecs(stmt[0]))
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
78 activated.add((stmt[0],
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
79 # didn't test that this should be
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
80 # stmt[1] and not putPred
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
81 stmt[1]))
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
82 return activated
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
83
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
84 def _oneShotPostActions(self, deviceGraph, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
85 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
86 Inferred graph may contain some one-shot statements. We'll send
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
87 statement objects to anyone on web sockets, and also generate
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
88 POST requests as described in the graph.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
89
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
90 one-shot statement ?s ?p ?o
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
91 with this in the graph:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
92 ?osp a :OneShotPost
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
93 ?osp :subject ?s
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
94 ?osp :predicate ?p
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
95 this will cause a post to ?o
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
96 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
97 # nothing in this actually makes them one-shot yet. they'll
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
98 # just fire as often as we get in here, which is not desirable
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
99 log.debug("_oneShotPostActions")
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
100 def err(e):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
101 log.warn("post %s failed", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
102 for osp in deviceGraph.subjects(RDF.type, ROOM['OneShotPost']):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
103 s = deviceGraph.value(osp, ROOM['subject'])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
104 p = deviceGraph.value(osp, ROOM['predicate'])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
105 if s is None or p is None:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
106 continue
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
107 #log.info("checking for %s %s", s, p)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
108 for postTarget in inferred.objects(s, p):
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
109 log.debug("post target %r", postTarget)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
110 # this packet ought to have 'oneShot' in it somewhere
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
111 self.sendToLiveClients({"s":s, "p":p, "o":postTarget})
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
112
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
113 log.debug(" POST %s", postTarget)
608
62171aa2bc4e mock output mode
drewp@bigasterisk.com
parents: 607
diff changeset
114 if not self.mockOutput:
62171aa2bc4e mock output mode
drewp@bigasterisk.com
parents: 607
diff changeset
115 treq.post(postTarget, timeout=2).addErrback(err)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
116
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
117 def putDefaults(self, deviceGraph, activated):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
118 """
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
119 If inferring (:a :b :c) would cause a PUT, you can say
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
120
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
121 reasoning:defaultOutput reasoning:default [
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
122 :subject :a
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
123 :predicate :b
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
124 :defaultObject :c
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
125 ]
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
126
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
127 and we'll do that PUT if no rule has put anything else with
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
128 (:a :b *).
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
129 """
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
130
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
131 defaultStmts = set()
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
132 for defaultDesc in deviceGraph.objects(REASONING['defaultOutput'],
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
133 REASONING['default']):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
134 s = deviceGraph.value(defaultDesc, ROOM['subject'])
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
135 p = deviceGraph.value(defaultDesc, ROOM['predicate'])
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
136 if (s, p) not in activated:
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
137 obj = deviceGraph.value(defaultDesc, ROOM['defaultObject'])
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
138
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
139 defaultStmts.add((s, p, obj))
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
140 log.debug('defaultStmts %s', ntStatement((s, p, obj)))
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
141 self._putDevices(deviceGraph, defaultStmts)
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
142
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
143 def _getRefreshSecs(self, target):
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
144 # should be able to map(secsFromLiteral) in here somehow and
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
145 # remove the workaround in httpputoutputs.currentRefreshSecs
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
146 return self.inputGraph.rxValue(target, ROOM['refreshPutValue'],
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
147 default=Literal('30s'))#.map(secsFromLiteral)
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
148
760
3c18b4b3b72c more py3 fixes
drewp@bigasterisk.com
parents: 756
diff changeset
149 def _put(self, url, payload: str, refreshSecs, agent=None):
720
e157afd642b5 rewrite reasoning PutOutputs
drewp@bigasterisk.com
parents: 609
diff changeset
150 self.putOutputs.put(url, payload, agent, refreshSecs)
609
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
151
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
152 import cyclone.sse
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
153
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
154 class PutOutputsTable(cyclone.sse.SSEHandler):
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
155 def __init__(self, application, request):
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
156 cyclone.sse.SSEHandler.__init__(self, application, request)
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
157 self.actions = self.settings.reasoning.actions
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
158
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
159 def bind(self, *args, **kwargs):
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
160 self.bound = True
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
161 self.loop()
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
162
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
163 def unbind(self):
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
164 self.bound = False
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
165
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
166 def loop(self):
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
167 if not self.bound:
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
168 return
760
3c18b4b3b72c more py3 fixes
drewp@bigasterisk.com
parents: 756
diff changeset
169 puts = {
609
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
170 'puts': [row.report() for _, row in
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
171 sorted(self.actions.putOutputs.state.items())],
760
3c18b4b3b72c more py3 fixes
drewp@bigasterisk.com
parents: 756
diff changeset
172 }
3c18b4b3b72c more py3 fixes
drewp@bigasterisk.com
parents: 756
diff changeset
173 self.sendEvent(message=json.dumps(puts).encode('utf8'), event=b'update')
609
5290df01d911 reasoning web page uses rdf/browse/graphView for inputs and outputs now
drewp@bigasterisk.com
parents: 608
diff changeset
174 reactor.callLater(1, self.loop)