annotate service/reasoning/actions.py @ 240:0c306e76d8c5

ipv6 fetch support. refactor Actions to new class and file Ignore-this: 200d7093919cf001706ad9c02347fabb
author drewp@bigasterisk.com
date Mon, 01 Feb 2016 03:28:17 -0800
parents
children 3f936fb61e4e
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
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
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
5 from cyclone.httpclient import fetch
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
6 log = logging.getLogger('output')
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
7 log.setLevel(logging.WARN)
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/")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
11
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
12 class Actions(object):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
13 def __init__(self, sendToLiveClients):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
14 self.sendToLiveClients = sendToLiveClients
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
15
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
16 def putResults(self, deviceGraph, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
17 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
18 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
19 getting made
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
20
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
21 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
22 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
23 (?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
24 PUT ?url <- ?val
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
25
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
26 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
27 :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
28 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
29
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
30 self._oneShotPostActions(deviceGraph, inferred)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
31 for dev, pred in [
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
32 # the config of each putUrl should actually be in the
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
33 # context of a dev and predicate pair, and then that would
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
34 # be the source of this list
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
35 #(DEV.theaterDoorLock, ROOM.state),
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
36 #(URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState),
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
37 (URIRef('http://bigasterisk.com/host/dash/monitor'), ROOM.powerState),
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
38 (URIRef('http://projects.bigasterisk.com/room/storageCeilingLedLong'), ROOM.brightness),
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
39 (URIRef('http://projects.bigasterisk.com/room/storageCeilingLedCross'), ROOM.brightness),
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
40 ]:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
41 url = deviceGraph.value(dev, ROOM.putUrl)
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 url and dev == DEV.theaterDoorLock: # ew
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
44 self._put(url+"/mode", payload="output")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
45
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
46 inferredObjects = list(inferred.objects(dev, pred))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
47 if len(inferredObjects) == 0:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
48 self._putZero(deviceGraph, dev, pred, url)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
49 elif len(inferredObjects) == 1:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
50 self._putInferred(deviceGraph, url, inferredObjects[0])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
51 elif len(inferredObjects) > 1:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
52 log.info("conflict, ignoring: %s has %s of %s" %
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
53 (dev, pred, inferredObjects))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
54 # write about it to the inferred graph?
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
55
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
56 #self._frontDoorPuts(deviceGraph, inferred)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
57
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
58
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
59 def _put(self, url, payload):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
60 def err(e):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
61 log.warn(" put %s failed (%r)", url, e)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
62 log.info(" PUT %s payload=%r", url, payload)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
63 fetch(url, method="PUT", postdata=payload, timeout=2).addErrback(err)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
64
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
65 def _oneShotPostActions(self, deviceGraph, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
66 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
67 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
68 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
69 POST requests as described in the graph.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
70
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
71 one-shot statement ?s ?p ?o
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
72 with this in the graph:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
73 ?osp a :OneShotPost
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
74 ?osp :subject ?s
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
75 ?osp :predicate ?p
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
76 this will cause a post to ?o
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
77 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
78 # 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
79 # just fire as often as we get in here, which is not desirable
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
80 log.info("_oneShotPostActions")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
81 def err(e):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
82 log.warn("post %s failed", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
83 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
84 s = deviceGraph.value(osp, ROOM['subject'])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
85 p = deviceGraph.value(osp, ROOM['predicate'])
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
86 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
87 continue
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
88 for postTarget in inferred.objects(s, p):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
89 log.info("post target %r", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
90 # 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
91 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
92
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
93 log.info(" POST %s", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
94 fetch(postTarget, method="POST", timeout=2).addErrback(err)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
95 self._postMpdCommands(inferred)
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 def _postMpdCommands(self, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
98 """special case to be eliminated. mpd play urls are made of an
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
99 mpd service and a song/album/playlist uri to be played.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
100 Ideally the graph rules would assemble these like
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
101 http://{mpd}/addAndPlay?uri={toPlay} or maybe toPlay as the payload
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
102 which would be fairly general but still allow toPlay uris to
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
103 be matched with any player."""
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
104 def post(postTarget):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
105 log.info("special mpd POST %s", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
106 def err(e):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
107 log.warn("post %s failed", postTarget)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
108 fetch(postTarget, method="POST", timeout=2).addErrback(err)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
109
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
110 rootSkippingAuth = "http://brace:9009/"
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
111 for mpd in [URIRef("http://bigasterisk.com/host/brace/mpd")]:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
112
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
113
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
114 for song in inferred.objects(mpd, ROOM['startMusic']):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
115 log.info("mpd statement: %r" % song)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
116 assert song.startswith('http://bigasterisk.com/music/')
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
117 post(rootSkippingAuth + "addAndPlay" + urllib.quote(song[len("http://bigasterisk.com/music"):]))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
118
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
119 for state in inferred.objects(mpd, ROOM['playState']):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
120 log.info('hello playstate %s', state)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
121 if state == ROOM['pause']:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
122 log.info("mpd %s %s", mpd, state)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
123 post(rootSkippingAuth + "mpd/pause")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
124 for vol in inferred.objects(mpd, ROOM['audioState']):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
125 if vol == ROOM['volumeStepUp']:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
126 post(rootSkippingAuth + "volumeAdjust?amount=6&max=70")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
127 if vol == ROOM['volumeStepDown']:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
128 post(rootSkippingAuth + "volumeAdjust?amount=-6&min=10")
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
129
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
130 def _putZero(self, deviceGraph, dev, pred, putUrl):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
131 # zerovalue should be a function of pred as well.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
132 value = deviceGraph.value(dev, ROOM.zeroValue)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
133 if value is not None:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
134 log.info("put zero (%r) to %s", value, putUrl)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
135 self._put(putUrl, payload=str(value))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
136 # this should be written back into the inferred graph
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
137 # for feedback
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
138
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
139 def _putInferred(self, deviceGraph, putUrl, obj):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
140 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
141 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
142 or obj itself.
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
143 """
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
144 value = deviceGraph.value(obj, ROOM.putValue)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
145 if value is not None:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
146 log.info("put %s to %s", value, putUrl)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
147 self._put(putUrl, payload=str(value))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
148 elif isinstance(obj, Literal):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
149 log.info("put %s to %s", obj, putUrl)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
150 self._put(putUrl, payload=str(obj))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
151 else:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
152 log.warn("don't know what payload to put for %s. obj=%r",
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
153 putUrl, obj)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
154
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
155 def _frontDoorPuts(self, deviceGraph, inferred):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
156 # todo: shouldn't have to be a special case
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
157 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
158 if brt is None:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
159 return
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
160 url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
161 log.info("put lcd %s brightness %s", url, brt)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
162 self._put(str(url) + "?brightness=%s" % str(brt), payload='')
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 msg = "open %s motion %s" % (
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
165 inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text),
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
166 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
167 # this was meant to be 2 chars in the bottom row, but the
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
168 # easier test was to replace the whole top msg
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
169 #restkit.Resource("http://slash:9080/").put("lcd", message=msg)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
170
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
diff changeset
171