annotate service/reasoning/actions.py @ 1045:a328cc370b22

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