diff service/piNode/piNode.py @ 1056:d2007482aec5

start sending oneshot events from some devices Ignore-this: 2c98200e9bab1acca872f4cdcaf88e4d darcs-hash:b2f012ddfc31f4800c4c1c34ebb6243169b70a67
author drewp <drewp@bigasterisk.com>
date Fri, 12 Feb 2016 02:41:29 -0800
parents f3c7f617c335
children 0c4ec87d4498
line wrap: on
line diff
--- a/service/piNode/piNode.py	Tue Feb 09 22:10:38 2016 -0800
+++ b/service/piNode/piNode.py	Fri Feb 12 02:41:29 2016 -0800
@@ -1,6 +1,7 @@
 from __future__ import division
 import sys, logging, socket, json, time, os
 import cyclone.web
+from cyclone.httpclient import fetch
 from rdflib import Namespace, URIRef, Literal, Graph, RDF, ConjunctiveGraph
 from rdflib.parser import StringInputSource
 from twisted.internet import reactor, task
@@ -72,7 +73,13 @@
                 self._lastPollTime.get(i.uri, 0) + i.pollPeriod > now):
                 continue
             new = i.poll()
+            if isinstance(new, dict): # new style
+                oneshot = new['oneshot']
+                new = new['latest']
+            else:
+                oneshot = None
             prev = self._statementsFromInputs.get(i.uri, [])
+
             if new or prev:
                 self._statementsFromInputs[i.uri] = new
                 # it's important that quads from different devices
@@ -84,9 +91,21 @@
                 #     fail since the '2' statement is gone)
                 self.masterGraph.patch(Patch.fromDiff(inContext(prev, i.uri),
                                                       inContext(new, i.uri)))
+
+            if oneshot:
+                self._sendOneshot(oneshot)
             self._lastPollTime[i.uri] = now
         self._exportToGraphite()
 
+    def _sendOneshot(self, oneshot):
+        body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3())
+                         for s,p,o in oneshot)).encode('utf8')
+        bang6 = 'fcb8:4119:fb46:96f8:8b07:1260:0f50:fcfa'
+        fetch(method='POST',
+              url='http://[%s]:9071/oneShot' % bang6,
+              headers={'Content-Type': ['text/n3']}, postdata=body,
+              timeout=5)
+
     def _exportToGraphite(self):
         # note this is writing way too often- graphite is storing at a lower res
         now = time.time()