diff service/piNode/piNode.py @ 1681:9d074317e16a

checkpoint service/piNode
author drewp@bigasterisk.com
date Mon, 27 Sep 2021 23:04:36 -0700
parents 0a39cb133ce5
children
line wrap: on
line diff
--- a/service/piNode/piNode.py	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/piNode.py	Mon Sep 27 23:04:36 2021 -0700
@@ -9,6 +9,8 @@
 import etcd3
 from greplin import scales
 from greplin.scales.cyclonehandler import StatsHandler
+import os
+#os.environ['PIGPIO_ADDR'] = 'pigpio' # (aka the docker host)
 import pigpio
 import treq
 
@@ -34,15 +36,18 @@
                           scales.PmfStat('pollAll'),
                           scales.PmfStat('sendOneshot'),
                           scales.PmfStat('outputStatements'),
-
+                          scales.IntStat('oneshotSuccess'),
+                          scales.IntStat('oneshotFail'),
 )
 
 class Config(object):
-    def __init__(self, masterGraph, hubHost):
-        self.etcd = etcd3.client(host=hubHost, port=9022)
+    def __init__(self, masterGraph):
+        log.info('connect to etcd-homeauto')
+        self.etcd = etcd3.client(host='etcd-homeauto', port=9022)
+        log.info('version %r', self.etcd.status().version)
+
 
         self.masterGraph = masterGraph
-        self.hubHost = hubHost
         self.configGraph = ConjunctiveGraph()
         self.boards = []
         self.etcPrefix = 'pi/'
@@ -96,7 +101,7 @@
             return
 
         log.info("found config for board %r" % thisBoard)
-        self.boards = [Board(self.configGraph, self.masterGraph, thisBoard, self.hubHost)]
+        self.boards = [Board(self.configGraph, self.masterGraph, thisBoard)]
 
 
 class DeviceRunner(object):
@@ -175,11 +180,30 @@
             self.dev.sendOutput(stmts)
         self.syncMasterGraphToHostStatements()
 
+def sendOneshot(oneshot):
+    body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3())
+                     for s,p,o in oneshot)).encode('utf8')
+    url = 'http://reasoning:9071/oneShot'
+    log.debug('post to %r', url)
+    d = treq.post(
+              url=url.encode('ascii'),
+              headers={b'Content-Type': [b'text/n3']},
+              data=body,
+              timeout=5)
+
+    def ok(k):
+        log.debug('sendOneshot to %r success', url)
+        STATS.oneshotSuccess += 1
+    def err(e):
+        log.info('oneshot post to %r failed:  %s',
+                 url, e.getErrorMessage())
+        STATS.oneshotFail += 1
+    d.addCallbacks(ok, err)
+
 class Board(object):
     """similar to arduinoNode.Board but without the communications stuff"""
-    def __init__(self, graph, masterGraph, uri, hubHost):
+    def __init__(self, graph, masterGraph, uri):
         self.graph, self.uri = graph, uri
-        self.hubHost = hubHost
         self.masterGraph = masterGraph
 
         self.masterGraph.setToGraph(self.staticStmts())
@@ -192,18 +216,7 @@
 
     @STATS.sendOneshot.time()
     def sendOneshot(self, oneshot):
-        body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3())
-                         for s,p,o in oneshot)).encode('utf8')
-        url = 'http://%s:9071/oneShot' % self.hubHost
-        d = treq.post(
-                  url=url.encode('ascii'),
-                  headers={b'Content-Type': [b'text/n3']},
-                  data=body,
-                  timeout=5)
-        def err(e):
-            log.info('oneshot post to %r failed:  %s',
-                     url, e.getErrorMessage())
-        d.addErrback(err)
+        sendOneshot(oneshot)
 
     @STATS.outputStatements.time()
     def outputStatements(self, stmts: set):
@@ -221,7 +234,7 @@
                 raise NotImplementedError(f'dev {devRunner.dev.uri} wanted only {wanted}')
         else:
             log.info("Board %s doesn't care about these statements:", self.uri)
-            for s in unused:
+            for s in unwanted:
                 log.warn("%r", s)
 
     def staticStmts(self):
@@ -274,7 +287,6 @@
 
     -v           Verbose
     --ow         Just report onewire device URIs and readings, then exit.
-    --hub=HOST   Hostname for etc3 and oneshot posts. [default: bang.vpn-home.bigasterisk.com]
     """)
     verboseLogging(arg['-v'])
 
@@ -287,7 +299,7 @@
     patchRandid()
 
     masterGraph = PatchableGraph()
-    config = Config(masterGraph, arg['--hub'])
+    config = Config(masterGraph)
 
     static = pkg_resources.resource_filename('homeauto_anynode', 'static/')
 
@@ -304,4 +316,5 @@
     log.warn('serving on 9059')
     reactor.run()
 
-main()
+if __name__ == '__main__':
+    main()