Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 20:3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
Ignore-this: 5c5551d566324f5a6e87f6f7623f3c3
author | drewp@bigasterisk.com |
---|---|
date | Tue, 07 Feb 2012 02:45:22 -0800 |
parents | |
children | c3c203100e06 |
rev | line source |
---|---|
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
1 #!bin/python |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
2 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
3 gather subgraphs from various services, run them through a rules |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
4 engine, and make http requests with the conclusions. |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
5 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
6 E.g. 'when drew's phone is near the house, and someone is awake, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
7 unlock the door when the door's motion sensor is activated' |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
8 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
9 When do we gather? The services should be able to trigger us, perhaps |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
10 with PSHB, that their graph has changed. |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
11 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
12 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
13 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
14 from twisted.internet import reactor, task |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from twisted.web.client import getPage |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
16 import time, traceback, sys, json |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
17 from rdflib.Graph import Graph, ConjunctiveGraph |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
18 from rdflib import Namespace, URIRef, Literal |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
19 import restkit |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
20 from FuXi.Rete.RuleStore import N3RuleStore |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
21 import cyclone.web |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
22 from inference import addTrig, infer |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
23 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
24 sys.path.append("../../lib") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
25 from logsetup import log |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
26 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
27 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
28 DEV = Namespace("http://projects.bigasterisk.com/device/") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
29 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
30 def gatherGraph(): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
31 g = ConjunctiveGraph() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
32 for source in ["http://bang:9069/graph", # arduino watchpins |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
33 "http://bang:9070/graph", # wifi usage |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
34 "http://bang:9075/graph", # env |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
35 "http://slash:9050/graph", # garageArduino for front motion |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
36 ]: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
37 try: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
38 addTrig(g, source) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
39 except: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
40 log.error("adding source %s", source) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
41 raise |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
42 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
43 return g |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
44 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
45 def graphWithoutMetadata(g, ignorePredicates=[]): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
46 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
47 graph filter that removes any statements whose subjects are |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
48 contexts in the graph and also any statements with the given |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
49 predicates |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
50 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
51 ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
52 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
53 out = ConjunctiveGraph() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
54 for stmt in g.quads((None, None, None)): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
55 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
56 out.addN([stmt]) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
57 return out |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
58 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
59 def graphEqual(a, b, ignorePredicates=[]): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
60 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
61 compare graphs, omitting any metadata statements about contexts |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
62 (especially modification times) and also any statements using the |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
63 given predicates |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
64 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
65 stmtsA = graphWithoutMetadata(a, ignorePredicates) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
66 stmtsB = graphWithoutMetadata(b, ignorePredicates) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
67 return set(stmtsA) == set(stmtsB) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
68 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
69 class Reasoning(object): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
70 def __init__(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
71 self.prevGraph = None |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
72 self.lastPollTime = 0 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
73 self.lastError = "" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
74 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
75 self.deviceGraph = Graph() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
76 self.deviceGraph.parse("/my/proj/room/devices.n3", format="n3") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
77 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
78 self.rulesN3 = "(not read yet)" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
79 self.inferred = Graph() # gets replaced in each graphChanged call |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
80 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
81 def readRules(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
82 self.rulesN3 = open('rules.n3').read() # for web display |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
83 self.ruleStore = N3RuleStore() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
84 self.ruleGraph = Graph(self.ruleStore) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
85 self.ruleGraph.parse('rules.n3', format='n3') # for inference |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
86 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
87 def poll(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
88 try: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
89 self._poll() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
90 self.lastPollTime = time.time() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
91 except Exception, e: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
92 log.error(traceback.format_exc()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
93 self.lastError = str(e) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
94 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
95 def _poll(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
96 g = gatherGraph() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
97 if (self.prevGraph is None or |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
98 not graphEqual(g, self.prevGraph, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
99 ignorePredicates=[ROOM.signalStrength])): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
100 self.graphChanged(g) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
101 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
102 self.prevGraph = g |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
103 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
104 def graphChanged(self, g): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
105 # i guess these are getting consumed each inference |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
106 try: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
107 t1 = time.time() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
108 self.readRules() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
109 ruleParseTime = time.time() - t1 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
110 except ValueError, e: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
111 # this is so if you're just watching the inferred output, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
112 # you'll see the error too |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
113 self.inferred = Graph() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
114 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
115 Literal(traceback.format_exc()))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
116 raise |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
117 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
118 t1 = time.time() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
119 self.inferred = infer(g, self.ruleStore) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
120 inferenceTime = time.time() - t1 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
121 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
122 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseTime'], |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
123 Literal(ruleParseTime))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
124 self.inferred.add((ROOM['reasoner'], ROOM['inferenceTime'], |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
125 Literal(inferenceTime))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
126 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
127 for dev in [DEV.theaterDoorLock]: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
128 url = self.deviceGraph.value(dev, ROOM.putUrl) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
129 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
130 putValue = self.deviceGraph.value(ROOM.unlocked, ROOM.putValue) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
131 zeroValue = self.deviceGraph.value(dev, ROOM.zeroValue) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
132 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
133 value = putValue if (dev, ROOM.state, ROOM.unlocked) in self.inferred else zeroValue |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
134 log.info("put %s to %s", value, url) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
135 restkit.request(url=url+"/mode", method="PUT", body="output") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
136 restkit.request(url=url, method="PUT", body=value) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
137 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
138 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
139 # todo: shouldn't have to be a special case |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
140 brt = self.inferred.value(DEV.frontDoorLcd, ROOM.brightness) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
141 url = self.deviceGraph.value(DEV.frontDoorLcdBrightness, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
142 ROOM.putUrl) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
143 log.info("put lcd %s brightness %s", url, brt) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
144 getPage(str(url) + "?brightness=%s" % str(brt), method="PUT") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
145 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
146 msg = "open %s motion %s" % (self.inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
147 self.inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
148 # this was meant to be 2 chars in the bottom row, but the |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
149 # easier test was to replace the whole top msg |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
150 #restkit.Resource("http://slash:9080/").put("lcd", message=msg) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
151 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
152 try: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
153 inputGraphNt = g.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
154 inferredNt = self.inferred.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
155 body = json.dumps({"input": inputGraphNt, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
156 "inferred": inferredNt}) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
157 restkit.Resource("http://bang:8014/").post( |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
158 "reasoningChange", payload=body, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
159 headers={"content-type" : "application/json"}) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
160 except Exception, e: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
161 traceback.print_exc() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
162 log.error("while sending changes to magma:") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
163 log.error(e) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
164 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
165 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
166 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
167 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
168 class Index(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
169 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
170 # make sure GET / fails if our poll loop died |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
171 ago = time.time() - self.settings.reasoning.lastPollTime |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
172 if ago > 2: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
173 self.set_status(500) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
174 self.finish("last poll was %s sec ago. last error: %s" % |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
175 (ago, self.settings.reasoning.lastError)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
176 return |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
177 self.set_header("Content-Type", "application/xhtml+xml") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
178 self.write(open('index.html').read()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
179 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
180 # for reuse |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
181 class GraphResource(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
182 def get(self, which): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
183 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
184 r = self.settings.reasoning |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
185 g = {'lastInput': r.prevGraph, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
186 'lastOutput': r.inferred, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
187 }[which] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
188 self.write(self.jsonRdf(g)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
189 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
190 def jsonRdf(self, g): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
191 return json.dumps(sorted(list(g))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
192 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
193 class NtGraphs(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
194 """same as what gets posted above""" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
195 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
196 r = self.settings.reasoning |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
197 inputGraphNt = r.prevGraph.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
198 inferredNt = r.inferred.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
199 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
200 self.write(json.dumps({"input": inputGraphNt, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
201 "inferred": inferredNt})) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
202 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
203 class Rules(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
204 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
205 self.set_header("Content-Type", "text/plain") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
206 self.write(self.settings.reasoning.rulesN3) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
207 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
208 class Static(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
209 def get(self, p): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
210 self.write(open(p).read()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
211 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
212 class Application(cyclone.web.Application): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
213 def __init__(self, reasoning): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
214 handlers = [ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
215 (r"/", Index), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
216 (r'/(jquery.min.js)', Static), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
217 (r'/(lastInput|lastOutput)Graph', GraphResource), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
218 (r'/ntGraphs', NtGraphs), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
219 (r'/rules', Rules), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
220 ] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
221 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
222 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
223 if __name__ == '__main__': |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
224 r = Reasoning() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
225 #import twisted.python.log |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
226 #twisted.python.log.startLogging(sys.stdout) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
227 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
228 task.LoopingCall(r.poll).start(1.0) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
229 reactor.listenTCP(9071, Application(r)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
230 reactor.run() |