comparison service/reasoning/reasoning.py @ 114:4cd065b97fa1

bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4 Ignore-this: 81dc30256f5d2658e53fce60abea66db
author drewp@bigasterisk.com
date Tue, 10 Sep 2013 00:38:52 -0700
parents bab30d0fb240
children 7179284779fd
comparison
equal deleted inserted replaced
113:66f8897b74ea 114:4cd065b97fa1
28 from inference import addTrig, infer 28 from inference import addTrig, infer
29 from graphop import graphEqual 29 from graphop import graphEqual
30 30
31 sys.path.append("../../lib") 31 sys.path.append("../../lib")
32 from logsetup import log 32 from logsetup import log
33 log.setLevel(logging.DEBUG) 33 log.setLevel(logging.INFO)
34 outlog = logging.getLogger('output') 34 outlog = logging.getLogger('output')
35 outlog.setLevel(logging.DEBUG) 35 outlog.setLevel(logging.DEBUG)
36 36
37 ROOM = Namespace("http://projects.bigasterisk.com/room/") 37 ROOM = Namespace("http://projects.bigasterisk.com/room/")
38 DEV = Namespace("http://projects.bigasterisk.com/device/") 38 DEV = Namespace("http://projects.bigasterisk.com/device/")
236 log.error(e) 236 log.error(e)
237 237
238 fetch("http://bang:8014/reasoningChange", 238 fetch("http://bang:8014/reasoningChange",
239 method="POST", 239 method="POST",
240 timeout=2, 240 timeout=2,
241 payload=body, 241 postdata=body,
242 headers={"content-type" : ["application/json"]}).addErrback(err) 242 headers={"content-type" : ["application/json"]}).addErrback(err)
243 243
244 def _put(self, url, payload): 244 def _put(self, url, payload):
245 def err(e): 245 def err(e):
246 outlog.warn("put %s falied", url) 246 outlog.warn("put %s failed", url)
247 outlog.info("PUT %s payload=%r", url, payload) 247 outlog.info("PUT %s payload=%r", url, payload)
248 fetch(url, method="PUT", payload=payload, timeout=2).addErrback(err) 248 fetch(url, method="PUT", postdata=payload, timeout=2).addErrback(err)
249 249
250 def putResults(self, inferred): 250 def putResults(self, inferred):
251 """ 251 """
252 some conclusions in the inferred graph lead to PUT requests 252 some conclusions in the inferred graph lead to PUT requests
253 getting made 253 getting made
315 def putZero(self, deviceGraph, dev, pred, putUrl): 315 def putZero(self, deviceGraph, dev, pred, putUrl):
316 # zerovalue should be a function of pred as well. 316 # zerovalue should be a function of pred as well.
317 value = deviceGraph.value(dev, ROOM.zeroValue) 317 value = deviceGraph.value(dev, ROOM.zeroValue)
318 if value is not None: 318 if value is not None:
319 outlog.info("put zero (%r) to %s", value, putUrl) 319 outlog.info("put zero (%r) to %s", value, putUrl)
320 self._put(putUrl, payload=value) 320 self._put(putUrl, payload=str(value))
321 # this should be written back into the inferred graph 321 # this should be written back into the inferred graph
322 # for feedback 322 # for feedback
323 323
324 def putInferred(self, deviceGraph, dev, pred, putUrl, obj): 324 def putInferred(self, deviceGraph, dev, pred, putUrl, obj):
325 value = deviceGraph.value(obj, ROOM.putValue) 325 value = deviceGraph.value(obj, ROOM.putValue)
326 if value is not None: 326 if value is not None:
327 outlog.info("put %s to %s", value, putUrl) 327 outlog.info("put %s to %s", value, putUrl)
328 self._put(putUrl, payload=value) 328 self._put(putUrl, payload=str(value))
329 else: 329 else:
330 outlog.warn("%s %s %s has no :putValue" % 330 outlog.warn("%s %s %s has no :putValue" %
331 (dev, pred, obj)) 331 (dev, pred, obj))
332 332
333 def frontDoorPuts(self, deviceGraph, inferred): 333 def frontDoorPuts(self, deviceGraph, inferred):
335 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness) 335 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness)
336 if brt is None: 336 if brt is None:
337 return 337 return
338 url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl) 338 url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl)
339 outlog.info("put lcd %s brightness %s", url, brt) 339 outlog.info("put lcd %s brightness %s", url, brt)
340 self._put(str(url) + "?brightness=%s" % str(brt)) 340 self._put(str(url) + "?brightness=%s" % str(brt), payload='')
341 341
342 msg = "open %s motion %s" % ( 342 msg = "open %s motion %s" % (
343 inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text), 343 inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text),
344 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text)) 344 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text))
345 # this was meant to be 2 chars in the bottom row, but the 345 # this was meant to be 2 chars in the bottom row, but the