changeset 1681:9d074317e16a

checkpoint service/piNode
author drewp@bigasterisk.com
date Mon, 27 Sep 2021 23:04:36 -0700
parents cd77bcbfd522
children d6996175ea78
files service/piNode/config/bed.n3 service/piNode/config/changing.n3 service/piNode/config/frontbed.n3 service/piNode/config/frontdoor.n3 service/piNode/config/kitchen.n3 service/piNode/config/living.n3 service/piNode/piNode.py service/piNode/tasks.py
diffstat 8 files changed, 89 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/service/piNode/config/bed.n3	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/config/bed.n3	Mon Sep 27 23:04:36 2021 -0700
@@ -8,6 +8,9 @@
 @prefix pin: <http://bigasterisk.com/homeauto/piBed/pin/> .
 @prefix ow:  <http://bigasterisk.com/homeauto/piBed/oneWire/> .
 
+# on cable from buttons, black is gnd, other 2 are the buttons.
+
+
 ha:piBed a :PiBoard;
   :hostname "bed";
   :onboardDevice ha:bedroomPiTemp;
--- a/service/piNode/config/changing.n3	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/config/changing.n3	Mon Sep 27 23:04:36 2021 -0700
@@ -13,7 +13,8 @@
   :hasPin
     pin:GPIO4,
     pin:GPIO17,
-    pin:GPIO18 .
+    pin:GPIO18;
+  :hasI2cBus pin:I2cBus1 .
 
 ha:changingPiTemp a :OnboardTemperature;
   :influxMeasurement [
@@ -54,3 +55,10 @@
   :height 6;
   :interpolate :slide;
   :rate 30 . # px/sec
+
+pin:I2cBus1 :connectedTo :babyMobileLights .
+:babyMobileLights a :PwmBoard;
+  :output
+    [:area :red; :channel 13],
+    [:area :led14; :channel 14],
+    [:area :led15; :channel 15] .
--- a/service/piNode/config/frontbed.n3	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/config/frontbed.n3	Mon Sep 27 23:04:36 2021 -0700
@@ -36,8 +36,10 @@
 
 
 ha:frontbedPiTemp a :OnboardTemperature;
-  :influxMeasurement "temperatureF";
-  :influxTag [:key "location"; :value "frontbedPi"] .
+  :influxMeasurement [
+    :measurement "temperatureF";
+    :predicate :temperatureF;
+    :tag [:key "host"; :value "frontbed"], [:key "location"; :value "frontbedPi"]] .
 
 # pin number ignored; see /boot/config.txt
 pin:GPIO17 :gpioNumber 17; :connectedTo ow: .
@@ -89,7 +91,7 @@
 :loftDeskStatus
   :startIndex 0;
   :endIndex 0;
-  :src "crib50.png";
+  :src "crib50.2.png";
   :x 0;
   :y 0;
   :height 1;
@@ -98,7 +100,7 @@
 :loftDeskStrip
   :startIndex 1;
   :endIndex 50;
-  :src "crib50.png";
+  :src "crib50.2.png";
   :x 0;
   :y 0;
   :height 50;
--- a/service/piNode/config/frontdoor.n3	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/config/frontdoor.n3	Mon Sep 27 23:04:36 2021 -0700
@@ -28,8 +28,10 @@
     pin:GPIO18 .   #
 
 ha:frontdoorPiTemp a :OnboardTemperature;
-  :influxMeasurement "temperatureF";
-  :influxTag [:key "location"; :value "frontdoorPi"] .
+  :influxMeasurement [
+    :measurement "temperatureF";
+    :predicate :temperatureF;
+    :tag [:key "host"; :value "frontdoor"], [:key "location"; :value "frontdoorPi"]] .
 
 pin:GPIO22 :gpioNumber 22; :connectedTo :frontDoorScreenButton1 . :frontDoorScreenButton1 a :Pushbutton .
 pin:GPIO27 :gpioNumber 27; :connectedTo :frontDoorScreenButton2 . :frontDoorScreenButton2 a :Pushbutton .
--- a/service/piNode/config/kitchen.n3	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/config/kitchen.n3	Mon Sep 27 23:04:36 2021 -0700
@@ -25,26 +25,26 @@
     :predicate :temperatureF;
     :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenPi"]] .
 
-pin:GPIO4 :gpioNumber 4; :connectedTo sensor:tempHumidKitchen .
-sensor:tempHumidKitchen a :TempHumidSensor;
-  :sees houseLoc:kitchenCounter;
-  :influxMeasurement [
-    :measurement "temperatureF";
-    :predicate :temperatureF;
-    :pointsAtLeastEvery 10;
-    :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenCounter"]], [
-    :measurement "humidity";
-    :predicate :humidity;
-    :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenCounter"]].
+# pin:GPIO4 :gpioNumber 4; :connectedTo sensor:tempHumidKitchen .
+# sensor:tempHumidKitchen a :TempHumidSensor;
+#   :sees houseLoc:kitchenCounter;
+#   :influxMeasurement [
+#     :measurement "temperatureF";
+#     :predicate :temperatureF;
+#     :pointsAtLeastEvery 10;
+#     :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenCounter"]], [
+#     :measurement "humidity";
+#     :predicate :humidity;
+#     :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenCounter"]].
 
-pin:GPIO17 :gpioNumber 17; :connectedTo ow: .
-ow: a :OneWire; :connectedTo ow:dev-000003a5a94c .
-ow:dev-000003a5a94c a :TemperatureSensor;
-  :position houseLoc:kitchenCounter;
-  :influxMeasurement [
-    :measurement "temperatureF";
-    :predicate :temperatureF;
-    :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenCounter_ds_test"]] .
+# pin:GPIO17 :gpioNumber 17; :connectedTo ow: .
+# ow: a :OneWire; :connectedTo ow:dev-000003a5a94c .
+# ow:dev-000003a5a94c a :TemperatureSensor;
+#   :position houseLoc:kitchenCounter;
+#   :influxMeasurement [
+#     :measurement "temperatureF";
+#     :predicate :temperatureF;
+#     :tag [:key "host"; :value "kitchen"], [:key "location"; :value "kitchenCounter_ds_test"]] .
 
 pin:GPIO14 :gpioNumber 14; :connectedTo sensor:kitchenCounterButton1 .
 sensor:kitchenCounterButton1 a :Pushbutton .
--- a/service/piNode/config/living.n3	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/config/living.n3	Mon Sep 27 23:04:36 2021 -0700
@@ -16,11 +16,13 @@
     pin:GPIO18,
     pin:GPIO4,
     pin:GPIO27,
+    pin:GPIO24,
     pin:GPIO22 .
 
 pin:GPIO17 :gpioNumber 17; :connectedTo sensor:tempHumidLivingRoomCeiling .
 pin:GPIO4  :gpioNumber 4;  :connectedTo :livingRoomLamp1 .
 pin:GPIO27 :gpioNumber 27; :connectedTo :livingRoomLamp2 .
+pin:GPIO24 :gpioNumber 24; :connectedTo :livingRoomLamp3 .
 pin:GPIO22 :gpioNumber 22; :connectedTo :bookSwitch .
 
 
@@ -59,4 +61,5 @@
 
 :livingRoomLamp1 a :LedOutput .
 :livingRoomLamp2 a :LedOutput .
+:livingRoomLamp3 a :LedOutput .
 :bookSwitch a :Pushbutton; :style :inverted .
--- 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()
--- a/service/piNode/tasks.py	Mon Sep 27 23:03:02 2021 -0700
+++ b/service/piNode/tasks.py	Mon Sep 27 23:04:36 2021 -0700
@@ -1,8 +1,8 @@
 from invoke import task
 
-JOB = 'piNode'
+JOB = 'pi_node'
 PORT = 9059
-TAG = f'bang6:5000/{JOB.lower()}_pi:latest'
+TAG = f'bang5:5000/{JOB.lower()}_pi:latest'
 ANSIBLE_TAG = 'raspi_io_node'
 
 @task
@@ -11,7 +11,7 @@
 
 @task
 def build_image_check(ctx):
-    ctx.run(f'docker build --network=host -f Dockerfile.check -t bang6:5000/pinode_check:latest .')
+    ctx.run(f'docker build --network=host -f Dockerfile.check -t bang6:5000/pi_node_check:latest .')
 
 @task(pre=[build_image])
 def push_image(ctx):
@@ -23,17 +23,21 @@
 
 @task(pre=[build_image_check])
 def check(ctx):
-    ctx.run(f'docker run --name={JOB}_check --rm -it -v `pwd`:/opt --net=host bang6:5000/pinode_check:latest pytype -d import-error mypkg/piNode.py', pty=True)
+    ctx.run(f'docker run --name={JOB}_check --rm -it -v `pwd`:/opt --net=host bang6:5000/pi_node_check:latest pytype -d import-error mypkg/piNode.py', pty=True)
 
 @task(pre=[build_image_check])
 def check_shell(ctx):
-    ctx.run(f'docker run --name={JOB}_check --rm -it -v `pwd`:/opt --net=host bang6:5000/pinode_check:latest /bin/bash', pty=True)
+    ctx.run(f'docker run --name={JOB}_check --rm -it -v `pwd`:/opt --net=host bang6:5000/pi_node_check:latest /bin/bash', pty=True)
 
 
 @task(pre=[build_image])
 def local_run(ctx):
     ctx.run(f'docker run --name={JOB}_local --rm -it {TAG} python ./piNode.py -v', pty=True)
 
+@task(pre=[build_image_check])
+def auto_button_test(ctx):
+    ctx.run(f'docker run --name={JOB}_button --rm -it --hostname demo -v `pwd`:/opt bang6:5000/pi_node_check:latest python3 -B ./auto_button_test.py', pty=True)
+
 @task(pre=[push_image])
 def redeploy(ctx):
     ctx.run(f'sudo /my/proj/ansible/playbook -l pi -t {ANSIBLE_TAG}')