changeset 176:f81c4d3d774b

arduinoNode: use -v for logging; support a PUT with subj+pred in query, obj in body Ignore-this: 744c3c7d95655430b8ec547e56f6b4bc
author drewp@bigasterisk.com
date Thu, 14 May 2015 01:26:12 -0700
parents c81a451f9b26
children 5305fe90c09b
files service/arduinoNode/arduinoNode.py service/arduinoNode/devices.py
diffstat 2 files changed, 51 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/service/arduinoNode/arduinoNode.py	Sun May 03 17:21:20 2015 -0700
+++ b/service/arduinoNode/arduinoNode.py	Thu May 14 01:26:12 2015 -0700
@@ -11,6 +11,7 @@
 from rdflib import Graph, Namespace, URIRef, Literal, RDF
 from rdflib.parser import StringInputSource
 from twisted.internet import reactor, task
+from docopt import docopt
 
 import devices
 import dotrender
@@ -328,13 +329,29 @@
     g = Graph()
     g.parse(StringInputSource(body), format='nt')
     return g
-        
+
 class OutputPage(cyclone.web.RequestHandler):
     def post(self):
+        # for old ui; use PUT instead
         stmts = list(rdfGraphBody(self.request.body, self.request.headers))
         for b in self.settings.boards:
             b.outputStatements(stmts)
+            
+    def put(self):
+        subj = URIRef(self.get_argument('s'))
+        pred = URIRef(self.get_argument('p'))
 
+        turtleLiteral = self.request.body
+        try:
+            obj = Literal(float(turtleLiteral))
+        except TypeError:
+            obj = Literal(turtleLiteral)
+
+        stmt = (subj, pred, obj)
+        for b in self.settings.boards:
+            b.outputStatements([stmt])
+        
+        
 class Boards(cyclone.web.RequestHandler):
     def get(self):
         
@@ -348,6 +365,18 @@
     return glob.glob('/dev/serial/by-id/*')
 
 def main():
+    arg = docopt("""
+    Usage: reasoning.py [options]
+
+    -v   Verbose
+    """)
+    log.setLevel(logging.WARN)
+    if arg['-v']:
+        from twisted.python import log as twlog
+        twlog.startLogging(sys.stdout)
+
+        log.setLevel(logging.DEBUG)
+    
     config = Config()
     current = currentSerialDevices()
 
@@ -368,11 +397,7 @@
     log.info('open boards')
     for b in boards:
         b.startPolling()
-        
-    from twisted.python import log as twlog
-    twlog.startLogging(sys.stdout)
 
-    log.setLevel(logging.DEBUG)
     reactor.listenTCP(9059, cyclone.web.Application([
         (r"/()", cyclone.web.StaticFileHandler, {
             "path": "static", "default_filename": "index.html"}),
--- a/service/arduinoNode/devices.py	Sun May 03 17:21:20 2015 -0700
+++ b/service/arduinoNode/devices.py	Thu May 14 01:26:12 2015 -0700
@@ -1,6 +1,7 @@
 from __future__ import division
 import itertools
 from rdflib import Namespace, RDF, URIRef, Literal
+import time
 
 ROOM = Namespace('http://projects.bigasterisk.com/room/')
 XSD = Namespace('http://www.w3.org/2001/XMLSchema#')
@@ -164,11 +165,28 @@
         if b not in 'yn':
             raise ValueError('unexpected response %r' % b)
         motion = b == 'y'
-        return [(self.uri, ROOM['sees'],
-                 ROOM['motion'] if motion else ROOM['noMotion'])]
+                
+        return [
+            (self.uri, ROOM['sees'],
+             ROOM['motion'] if motion else ROOM['noMotion']),
+            self.recentMotionStatement(motion),
+        ]
 
+    def recentMotionStatement(self, motion):
+        if not hasattr(self, 'lastMotionTime'):
+            self.lastMotionTime = 0
+        now = time.time()
+        if motion:
+            self.lastMotionTime = now
+        recentMotion = now - self.lastMotionTime < 60 * 10
+        return (self.uri, ROOM['seesRecently'],
+                ROOM['motion'] if recentMotion else ROOM['noMotion'])        
+    
     def watchPrefixes(self):
-        return [(self.uri, ROOM['sees'])]
+        return [
+            (self.uri, ROOM['sees']),
+            (self.uri, ROOM['seesRecently']),
+        ]
 
 @register
 class OneWire(DeviceType):