annotate service/reasoning/actions.py @ 607:7f5451a76a80

redo reasoning actions log levels Ignore-this: ff37dd55cf590b440f90ba36f54f8191
author drewp@bigasterisk.com
date Tue, 23 Jul 2019 17:40:55 -0700
parents 83ebabe81b9d
children 62171aa2bc4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
1 from rdflib import URIRef, Namespace, RDF, Literal
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
2 from twisted.internet import reactor
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
3 import logging
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
4 import urllib
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
5
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
6 import treq
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
7 log = logging.getLogger('output')
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
8
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
9 ROOM = Namespace("http://projects.bigasterisk.com/room/")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
10 DEV = Namespace("http://projects.bigasterisk.com/device/")
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
11 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
12
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
13 class HttpPutOutput(object):
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
14 def __init__(self, url):
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
15 self.url = url
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
16 self.payload = None
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
17 self.foafAgent = None
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
18 self.nextCall = None
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
19 self.numRequests = 0
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
20
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
21 def setPayload(self, payload, foafAgent):
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
22 if self.numRequests > 0 and (self.payload == payload or self.foafAgent == foafAgent):
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
23 return
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
24 self.payload = payload
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
25 self.foafAgent = foafAgent
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
26 self.makeRequest()
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
27
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
28 def makeRequest(self):
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
29 if self.payload is None:
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
30 log.debug("PUT None to %s - waiting", self.url)
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
31 return
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
32 h = {}
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
33 if self.foafAgent:
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
34 h['x-foaf-agent'] = self.foafAgent
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
35 if self.nextCall and self.nextCall.active():
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
36 self.nextCall.cancel()
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
37 self.nextCall = None
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
38 self.lastErr = None
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
39 log.debug("PUT %s payload=%s agent=%s", self.url, self.payload, self.foafAgent)
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
40 self.currentRequest = treq.put(self.url, data=self.payload, headers=h, timeout=3)
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
41 self.currentRequest.addCallback(self.onResponse).addErrback(self.onError)
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
42 self.numRequests += 1
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
43
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
44 def onResponse(self, resp):
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
45 log.debug(" PUT %s ok", self.url)
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
46 self.lastErr = None
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
47 self.currentRequest = None
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
48 self.nextCall = reactor.callLater(3, self.makeRequest)
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
49
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
50 def onError(self, err):
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
51 self.lastErr = err
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
52 log.debug(' PUT %s failed: %s', self.url, err)
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
53 self.currentRequest = None
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
54 self.nextCall = reactor.callLater(5, self.makeRequest)
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
55
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
56 class HttpPutOutputs(object):
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
57 """these grow forever"""
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
58 def __init__(self):
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
59 self.state = {} # url: HttpPutOutput
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
60
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
61 def put(self, url, payload, foafAgent):
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
62 if url not in self.state:
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
63 self.state[url] = HttpPutOutput(url)
606
83ebabe81b9d actually set put payload this time. add treq dep
drewp@bigasterisk.com
parents: 605
diff changeset
64 self.state[url].setPayload(payload, foafAgent)
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
65
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
66 class Actions(object):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
67 def __init__(self, sendToLiveClients):
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
68 self.putOutputs = HttpPutOutputs()
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
69 self.sendToLiveClients = sendToLiveClients
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
70
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
71 def putResults(self, deviceGraph, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
72 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
73 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
74 getting made
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
75
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
76 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
77 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
78 (?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
79 PUT ?url <- ?val
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
80
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
81 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
82 :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
83 """
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
84 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
85 activated.update(self._putDevices(deviceGraph, inferred))
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
86 self._oneShotPostActions(deviceGraph, inferred)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
87 for dev, pred in [
247
b0089d1ca4f6 add bed buttons
drewp@bigasterisk.com
parents: 242
diff changeset
88 #(URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState),
b0089d1ca4f6 add bed buttons
drewp@bigasterisk.com
parents: 242
diff changeset
89 (URIRef('http://bigasterisk.com/host/dash/monitor'), ROOM.powerState),
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
90 (URIRef('http://bigasterisk.com/host/frontdoor/monitor'), ROOM.powerState),
264
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
91 (ROOM['storageCeilingLedLong'], ROOM.brightness),
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
92 (ROOM['storageCeilingLedCross'], ROOM.brightness),
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
93 (ROOM['garageOverhead'], ROOM.brightness),
264
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
94 (ROOM['headboardWhite'], ROOM.brightness),
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
95 (ROOM['changingWhite'], ROOM.brightness),
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
96 (ROOM['starTrekLight'], ROOM.brightness),
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
97 (ROOM['kitchenLight'], ROOM.brightness),
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
98 (ROOM['kitchenCounterLight'], ROOM.brightness),
264
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
99 (ROOM['livingRoomLamp1'], ROOM.brightness),
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
100 (ROOM['livingRoomLamp2'], ROOM.brightness),
603
63a3d8eeb46f loftdesk config
drewp@bigasterisk.com
parents: 602
diff changeset
101 (ROOM['loftDeskStrip'], ROOM.x),
264
570b0e73d2bc rules updates
drewp@bigasterisk.com
parents: 259
diff changeset
102 (ROOM['bedLedStrip'], ROOM.color),
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
103 ]:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
104 url = deviceGraph.value(dev, ROOM.putUrl)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
105
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
106 log.debug('inferredObjects of dev=%s pred=%s',
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
107 deviceGraph.qname(dev),
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
108 deviceGraph.qname(pred))
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
109 inferredObjects = list(inferred.objects(dev, pred))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
110 if len(inferredObjects) == 0:
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
111 # rm this- use activated instead
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
112 self._putZero(deviceGraph, dev, pred, url)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
113 elif len(inferredObjects) == 1:
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
114 log.debug(' inferredObject: %s %s %r',
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 264
diff changeset
115 deviceGraph.qname(dev),
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 264
diff changeset
116 deviceGraph.qname(pred),
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
117 inferredObjects[0].toPython())
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
118 activated.add((dev, pred))
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
119 self._putInferred(deviceGraph, url, inferredObjects[0])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
120 elif len(inferredObjects) > 1:
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
121 log.info(" conflict, ignoring: %s has %s of %s" %
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
122 (dev, pred, inferredObjects))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
123 # write about it to the inferred graph?
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
124 self.putDefaults(deviceGraph, activated)
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
125
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
126 def putDefaults(self, deviceGraph, activated):
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
127 """
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
128 If inferring (:a :b :c) would cause a PUT, you can say
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
129
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
130 reasoning:defaultOutput reasoning:default [
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
131 :subject :a
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
132 :predicate :b
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
133 :defaultObject :c
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
134 ]
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
135
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
136 and we'll do that PUT if no rule has put anything else with
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
137 (:a :b *).
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
138 """
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
139
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
140 defaultStmts = set()
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
141 for defaultDesc in deviceGraph.objects(REASONING['defaultOutput'],
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
142 REASONING['default']):
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
143 s = deviceGraph.value(defaultDesc, ROOM['subject'])
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
144 p = deviceGraph.value(defaultDesc, ROOM['predicate'])
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
145 if (s, p) not in activated:
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
146 obj = deviceGraph.value(defaultDesc, ROOM['defaultObject'])
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
147
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
148 defaultStmts.add((s, p, obj))
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
149 self._putDevices(deviceGraph, defaultStmts)
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
150
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
151 def _oneShotPostActions(self, deviceGraph, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
152 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
153 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
154 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
155 POST requests as described in the graph.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
156
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
157 one-shot statement ?s ?p ?o
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
158 with this in the graph:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
159 ?osp a :OneShotPost
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
160 ?osp :subject ?s
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
161 ?osp :predicate ?p
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
162 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
163 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
164 # 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
165 # 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
166 log.debug("_oneShotPostActions")
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
167 def err(e):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
168 log.warn("post %s failed", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
169 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
170 s = deviceGraph.value(osp, ROOM['subject'])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
171 p = deviceGraph.value(osp, ROOM['predicate'])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
172 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
173 continue
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
174 #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
175 for postTarget in inferred.objects(s, p):
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
176 log.debug("post target %r", postTarget)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
177 # 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
178 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
179
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
180 log.debug(" POST %s", postTarget)
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
181 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
182
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
183 def _putDevices(self, deviceGraph, inferred):
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
184 activated = set()
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
185 agentFor = {}
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
186 for stmt in inferred:
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
187 if stmt[1] == ROOM['putAgent']:
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
188 agentFor[stmt[0]] = stmt[2]
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
189 for stmt in inferred:
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
190 log.debug('inferred stmt we might PUT: %s', stmt)
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
191 putUrl = deviceGraph.value(stmt[0], ROOM['putUrl'])
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
192 putPred = deviceGraph.value(stmt[0], ROOM['putPredicate'])
463
1ceb26846eca add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents: 392
diff changeset
193 matchPred = deviceGraph.value(stmt[0], ROOM['matchPredicate'],
1ceb26846eca add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents: 392
diff changeset
194 default=putPred)
1ceb26846eca add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents: 392
diff changeset
195 if putUrl and matchPred == stmt[1]:
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
196 log.debug('putDevices: stmt %r %r %r leds to putting at %r',
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
197 stmt[0], stmt[1], stmt[2], putUrl)
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
198 self._put(putUrl + '?' + urllib.urlencode([
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
199 ('s', str(stmt[0])),
463
1ceb26846eca add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents: 392
diff changeset
200 ('p', str(putPred))]),
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
201 str(stmt[2].toPython()),
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
202 agent=agentFor.get(stmt[0], None))
602
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
203 activated.add((stmt[0],
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
204 # didn't test that this should be
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
205 # stmt[1] and not putPred
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
206 stmt[1]))
0b1249c3137d support for default values for http PUT outputs
drewp@bigasterisk.com
parents: 600
diff changeset
207 return activated
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
208
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
209 def _putInferred(self, deviceGraph, putUrl, obj):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
210 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
211 HTTP PUT to putUrl, with a payload that's either obj's :putValue
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
212 or obj itself.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
213 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
214 value = deviceGraph.value(obj, ROOM.putValue)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
215 if value is not None:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
216 self._put(putUrl, payload=str(value))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
217 elif isinstance(obj, Literal):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
218 self._put(putUrl, payload=str(obj))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
219 else:
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
220 log.warn(" don't know what payload to put for %s. obj=%r",
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
221 putUrl, obj)
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
222
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
223 def _putZero(self, deviceGraph, dev, pred, putUrl):
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
224 # zerovalue should be a function of pred as well.
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
225 value = deviceGraph.value(dev, ROOM.zeroValue)
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
226 if value is not None:
607
7f5451a76a80 redo reasoning actions log levels
drewp@bigasterisk.com
parents: 606
diff changeset
227 log.debug(" put zero (%r) to %s", value.toPython(), putUrl)
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
228 self._put(putUrl, payload=str(value))
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
229 # this should be written back into the inferred graph
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
230 # for feedback
600
a34dee00a989 comments and logging
drewp@bigasterisk.com
parents: 463
diff changeset
231
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
232 def _put(self, url, payload, agent=None):
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 328
diff changeset
233 assert isinstance(payload, bytes)
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
234 self.putOutputs.put(url, payload, agent)