Mercurial > code > home > repos > homeauto
comparison service/reasoning/actions.py @ 1410:dc70ee50c981
redo reasoning actions log levels
Ignore-this: ff37dd55cf590b440f90ba36f54f8191
darcs-hash:20c2bca7a9c232959640dc2f873011e622505bee
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Tue, 23 Jul 2019 17:40:55 -0700 |
parents | e78b8806ad05 |
children | 62171aa2bc4e |
comparison
equal
deleted
inserted
replaced
1409:e78b8806ad05 | 1410:dc70ee50c981 |
---|---|
25 self.foafAgent = foafAgent | 25 self.foafAgent = foafAgent |
26 self.makeRequest() | 26 self.makeRequest() |
27 | 27 |
28 def makeRequest(self): | 28 def makeRequest(self): |
29 if self.payload is None: | 29 if self.payload is None: |
30 log.info("PUT None to %s - waiting", self.url) | 30 log.debug("PUT None to %s - waiting", self.url) |
31 return | 31 return |
32 h = {} | 32 h = {} |
33 if self.foafAgent: | 33 if self.foafAgent: |
34 h['x-foaf-agent'] = self.foafAgent | 34 h['x-foaf-agent'] = self.foafAgent |
35 if self.nextCall and self.nextCall.active(): | 35 if self.nextCall and self.nextCall.active(): |
36 self.nextCall.cancel() | 36 self.nextCall.cancel() |
37 self.nextCall = None | 37 self.nextCall = None |
38 self.lastErr = None | 38 self.lastErr = None |
39 log.info("PUT %s payload=%s agent=%s", self.url, self.payload, self.foafAgent) | 39 log.debug("PUT %s payload=%s agent=%s", self.url, self.payload, self.foafAgent) |
40 self.currentRequest = treq.put(self.url, data=self.payload, headers=h, timeout=3) | 40 self.currentRequest = treq.put(self.url, data=self.payload, headers=h, timeout=3) |
41 self.currentRequest.addCallback(self.onResponse).addErrback(self.onError) | 41 self.currentRequest.addCallback(self.onResponse).addErrback(self.onError) |
42 self.numRequests += 1 | 42 self.numRequests += 1 |
43 | 43 |
44 def onResponse(self, resp): | 44 def onResponse(self, resp): |
45 log.info(" PUT %s ok", self.url) | 45 log.debug(" PUT %s ok", self.url) |
46 self.lastErr = None | 46 self.lastErr = None |
47 self.currentRequest = None | 47 self.currentRequest = None |
48 self.nextCall = reactor.callLater(3, self.makeRequest) | 48 self.nextCall = reactor.callLater(3, self.makeRequest) |
49 | 49 |
50 def onError(self, err): | 50 def onError(self, err): |
51 self.lastErr = err | 51 self.lastErr = err |
52 log.info(' PUT %s failed: %s', self.url, err) | 52 log.debug(' PUT %s failed: %s', self.url, err) |
53 self.currentRequest = None | 53 self.currentRequest = None |
54 self.nextCall = reactor.callLater(5, self.makeRequest) | 54 self.nextCall = reactor.callLater(5, self.makeRequest) |
55 | 55 |
56 class HttpPutOutputs(object): | 56 class HttpPutOutputs(object): |
57 """these grow forever""" | 57 """these grow forever""" |
60 | 60 |
61 def put(self, url, payload, foafAgent): | 61 def put(self, url, payload, foafAgent): |
62 if url not in self.state: | 62 if url not in self.state: |
63 self.state[url] = HttpPutOutput(url) | 63 self.state[url] = HttpPutOutput(url) |
64 self.state[url].setPayload(payload, foafAgent) | 64 self.state[url].setPayload(payload, foafAgent) |
65 log.info('PutOutputs has %s urls', len(self.state)) | |
66 | 65 |
67 class Actions(object): | 66 class Actions(object): |
68 def __init__(self, sendToLiveClients): | 67 def __init__(self, sendToLiveClients): |
69 self.putOutputs = HttpPutOutputs() | 68 self.putOutputs = HttpPutOutputs() |
70 self.sendToLiveClients = sendToLiveClients | 69 self.sendToLiveClients = sendToLiveClients |
162 ?osp :predicate ?p | 161 ?osp :predicate ?p |
163 this will cause a post to ?o | 162 this will cause a post to ?o |
164 """ | 163 """ |
165 # nothing in this actually makes them one-shot yet. they'll | 164 # nothing in this actually makes them one-shot yet. they'll |
166 # just fire as often as we get in here, which is not desirable | 165 # just fire as often as we get in here, which is not desirable |
167 log.info("_oneShotPostActions") | 166 log.debug("_oneShotPostActions") |
168 def err(e): | 167 def err(e): |
169 log.warn("post %s failed", postTarget) | 168 log.warn("post %s failed", postTarget) |
170 for osp in deviceGraph.subjects(RDF.type, ROOM['OneShotPost']): | 169 for osp in deviceGraph.subjects(RDF.type, ROOM['OneShotPost']): |
171 s = deviceGraph.value(osp, ROOM['subject']) | 170 s = deviceGraph.value(osp, ROOM['subject']) |
172 p = deviceGraph.value(osp, ROOM['predicate']) | 171 p = deviceGraph.value(osp, ROOM['predicate']) |
173 if s is None or p is None: | 172 if s is None or p is None: |
174 continue | 173 continue |
175 #log.info("checking for %s %s", s, p) | 174 #log.info("checking for %s %s", s, p) |
176 for postTarget in inferred.objects(s, p): | 175 for postTarget in inferred.objects(s, p): |
177 log.info("post target %r", postTarget) | 176 log.debug("post target %r", postTarget) |
178 # this packet ought to have 'oneShot' in it somewhere | 177 # this packet ought to have 'oneShot' in it somewhere |
179 self.sendToLiveClients({"s":s, "p":p, "o":postTarget}) | 178 self.sendToLiveClients({"s":s, "p":p, "o":postTarget}) |
180 | 179 |
181 log.info(" POST %s", postTarget) | 180 log.debug(" POST %s", postTarget) |
182 treq.post(postTarget, timeout=2).addErrback(err) | 181 treq.post(postTarget, timeout=2).addErrback(err) |
183 | 182 |
184 def _putDevices(self, deviceGraph, inferred): | 183 def _putDevices(self, deviceGraph, inferred): |
185 activated = set() | 184 activated = set() |
186 agentFor = {} | 185 agentFor = {} |
187 for stmt in inferred: | 186 for stmt in inferred: |
188 if stmt[1] == ROOM['putAgent']: | 187 if stmt[1] == ROOM['putAgent']: |
189 agentFor[stmt[0]] = stmt[2] | 188 agentFor[stmt[0]] = stmt[2] |
190 for stmt in inferred: | 189 for stmt in inferred: |
191 log.info('inferred stmt we might PUT: %s', stmt) | 190 log.debug('inferred stmt we might PUT: %s', stmt) |
192 putUrl = deviceGraph.value(stmt[0], ROOM['putUrl']) | 191 putUrl = deviceGraph.value(stmt[0], ROOM['putUrl']) |
193 putPred = deviceGraph.value(stmt[0], ROOM['putPredicate']) | 192 putPred = deviceGraph.value(stmt[0], ROOM['putPredicate']) |
194 matchPred = deviceGraph.value(stmt[0], ROOM['matchPredicate'], | 193 matchPred = deviceGraph.value(stmt[0], ROOM['matchPredicate'], |
195 default=putPred) | 194 default=putPred) |
196 if putUrl and matchPred == stmt[1]: | 195 if putUrl and matchPred == stmt[1]: |
197 log.info('putDevices: stmt %r %r %r leds to putting at %r', | 196 log.debug('putDevices: stmt %r %r %r leds to putting at %r', |
198 stmt[0], stmt[1], stmt[2], putUrl) | 197 stmt[0], stmt[1], stmt[2], putUrl) |
199 self._put(putUrl + '?' + urllib.urlencode([ | 198 self._put(putUrl + '?' + urllib.urlencode([ |
200 ('s', str(stmt[0])), | 199 ('s', str(stmt[0])), |
201 ('p', str(putPred))]), | 200 ('p', str(putPred))]), |
202 str(stmt[2].toPython()), | 201 str(stmt[2].toPython()), |
223 | 222 |
224 def _putZero(self, deviceGraph, dev, pred, putUrl): | 223 def _putZero(self, deviceGraph, dev, pred, putUrl): |
225 # zerovalue should be a function of pred as well. | 224 # zerovalue should be a function of pred as well. |
226 value = deviceGraph.value(dev, ROOM.zeroValue) | 225 value = deviceGraph.value(dev, ROOM.zeroValue) |
227 if value is not None: | 226 if value is not None: |
228 log.info(" put zero (%r) to %s", value.toPython(), putUrl) | 227 log.debug(" put zero (%r) to %s", value.toPython(), putUrl) |
229 self._put(putUrl, payload=str(value)) | 228 self._put(putUrl, payload=str(value)) |
230 # this should be written back into the inferred graph | 229 # this should be written back into the inferred graph |
231 # for feedback | 230 # for feedback |
232 | 231 |
233 def _put(self, url, payload, agent=None): | 232 def _put(self, url, payload, agent=None): |