powerguru-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Powerguru-commit] [SCM] powerguru branch, master, updated. b9ee84ecf5f3


From: Rob Savoye
Subject: [Powerguru-commit] [SCM] powerguru branch, master, updated. b9ee84ecf5f346226796ff920112c5d9c25e9a7c
Date: Mon, 11 Feb 2019 19:21:52 -0500 (EST)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "powerguru".

The branch, master has been updated
       via  b9ee84ecf5f346226796ff920112c5d9c25e9a7c (commit)
       via  1993d186647bcadf9fcce8e1b2e8c0b066f308c1 (commit)
       via  9958ff368a29afd5597451eab46a41c6da5464a0 (commit)
       via  0286069aa54e2328e7bf67b951eccba1402727e1 (commit)
       via  dd58872b46e7e2df2eab9952750ee409531d4cab (commit)
       via  a0963ca070866feb5968dfc4205ba2e9cdf307ab (commit)
       via  17046bef031bb7e59bd9df91c1d03df1e6a9a324 (commit)
       via  f3bd3120475cc5a4985aa980cf1b42b4b430c589 (commit)
      from  4e22da3365a39d6a57b36329df17d11dcf00714c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=b9ee84ecf5f346226796ff920112c5d9c25e9a7c


commit b9ee84ecf5f346226796ff920112c5d9c25e9a7c
Author: Rob Savoye <address@hidden>
Date:   Mon Feb 11 17:21:40 2019 -0700

    Log data from rtl 433 weather sensors

diff --git a/python/rtl433.py b/python/rtl433.py
old mode 100644
new mode 100755
index 78b2025..79935f1
--- a/python/rtl433.py
+++ b/python/rtl433.py
@@ -27,64 +27,74 @@ import logging
 import time
 import os
 import psycopg2
+import sensor
+import itertools, operator
 ON_POSIX = 'posix' in sys.builtin_module_names
 
-def rtl433_handler(args):
-    logging.debug("Start rtl_433 %r" % args)
-    cmd = [ 'rtl_433', '-F', 'csv', '-R', '40', '-T', '15']
+def rtl433_handler(options, sensors):
+    logging.debug("Start rtl_433 %r" % options)
+
+    # Connect to a postgresql database
+    try:
+        dbname = "powerguru"
+        connect = "dbname=" + dbname
+        dbshell = psycopg2.connect(connect)
+        if dbshell.closed == 0:
+            dbshell.autocommit = True
+            logging.info("Opened connection to %r" % dbname)
+            dbcursor = dbshell.cursor()
+            if dbcursor.closed == 0:
+                logging.info("Opened cursor in %r" % dbname)
+
+    except Exception as e:
+        logging.warning("Couldn't connect to database: %r" % e)
 
     #ppp = Popen(cmd, stdout=PIPE, stderr=STDOUT, bufsize=1, 
close_fds=ON_POSIX)
-    retries = 10
-    while retries > 0:
+    cmd = [ 'rtl_433', '-F', 'csv', '-R', '40', '-T', '15']
+    while True:
         ppp = Popen(cmd, stdout=PIPE, bufsize=0, close_fds=ON_POSIX)
-        try:
-            out, err = ppp.communicate()
-            #out, err = ppp.communicate(timeout=0.2)
-        except subprocess.TimeoutExpired:
-            logging.warning('subprocess did not terminate in time')
+        out, err = ppp.communicate()
+        #out, err = ppp.communicate(timeout=0.5)
         #print("FIXME0: %r" % retries)
-        #epdb.set_trace()
         for line in out.splitlines():
+            mapper = map
             #for line in ppp.readline():
-            print("FIXME: %r" % line)
+            #print("FIXME: %r" % line)
             str = line.decode('utf8')
             tokens = str.split(',')
             # this is just the csv header fields
             if tokens[0] == 'time':
                 continue
-            sensors = dict()
-            sensors['model'] = tokens[3]
-            sensors['id'] = tokens[5]
-            sensors['channel'] = tokens[6]
-            sensors['temperature'] = tokens[7]
-            sensors['humidity'] = tokens[8]
-            # Dump data
-            print("MODEL: %r" % sensors['model'])
-            print("\tID: %r" % sensors['id'])
-            print("\tCHANNEL: %r" % sensors['channel'])
-            print("\tTEMPERATURE: %rC" % sensors['temperature'])
-            print("\tHUMIDITY: %r" % sensors['humidity'])
-            print("")
-            time.sleep(1)
-        retries -= 1    
-        epdb.set_trace()
+            temp = dict()
+            temp['timestamp'] = tokens[0]
+            temp['model'] = tokens[3]
+            temp['id'] = tokens[4]
+            temp['channel'] = tokens[6]
+            temp['temperature'] = tokens[7]
+            temp['humidity'] = tokens[8]
+            if sensors.get(temp['id']) is None:
+                print("New sensor %r found!" % temp['id'])
+                sense = sensor.SensorDevice()
+                sense.set('id', temp['id'])
+                sense.set('alias', temp['model'])
+                sense.set('device', sensor.DeviceType.RTL433)
+                sense.set('sensor', sensor.SensorType.TEMPERATURE)
+                sense.set('channel', temp['channel'])
+                sensors.add(sense)
+            #else:
+                #sensor.sensors[temp['id']]['channel'] = temp['channel']
+                #sensor.sensors[temp['id']]['device'] = DeviceType.RTL433
+            sensors.dump()
+            # Convert from Celcius if needed
+            if (options['scale'] == 'F'):
+                temp['temperature'] = (float(temp['temperature']) * 1.8) + 
32.0;
+                #temp['lowtemp'] =  (float(temp['lowtemp']) * 1.8) + 32.0;
+                #temp['hightemp'] =  (float(temp['hightemp']) * 1.8) + 32.0;
+            query = """INSERT INTO weather VALUES( '%s', %s, %s, %s, %s, '%s', 
'%s' )  ON CONFLICT DO NOTHING;; """ % (temp['id'], temp['temperature'], "0", 
"0",  temp['humidity'], options['scale'], temp['timestamp'])
+            logging.debug(query)
+            dbcursor.execute(query)
+        #time.sleep(30)
+        time.sleep(int(options['interval']))
  
-    # # Connect to a postgresql database
-    # try:
-    #     dbname = "powerguru"
-    #     connect = "dbname=" + dbname
-    #     dbshell = psycopg2.connect(connect)
-    #     if dbshell.closed == 0:
-    #         dbshell.autocommit = True
-    #         logging.info("Opened connection to %r" % dbname)
-            
-    #         dbcursor = dbshell.cursor()
-    #         if dbcursor.closed == 0:
-    #             logging.info("Opened cursor in %r" % dbname)
-                
-    # except Exception as e:
-    #     logging.warning("Couldn't connect to database: %r" % e)
-        
-
     # _sensors = list()
 

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=1993d186647bcadf9fcce8e1b2e8c0b066f308c1


commit 1993d186647bcadf9fcce8e1b2e8c0b066f308c1
Author: Rob Savoye <address@hidden>
Date:   Mon Feb 11 17:20:38 2019 -0700

    Create new class to hold array of sensors

diff --git a/python/sensor.py b/python/sensor.py
index 0b685ba..5eb211c 100755
--- a/python/sensor.py
+++ b/python/sensor.py
@@ -26,7 +26,6 @@ import psycopg2
 from datetime import datetime
 from enum import Enum
 
-
 class SensorType(Enum):
     UNKNOWN = 0
     ACVOLTAGE = 1
@@ -49,31 +48,90 @@ class DeviceType(Enum):
     SERIAL = 6
     GPIO = 7
 
-class Sensor(object):
+
+class Sensors(object):
+    """Data about all the sensors"""
+    def __init__(self, data=dict()):
+        self.sensors = dict()
+        # Connect to a postgresql database
+        try:
+            dbname = "powerguru"
+            connect = "dbname=" + dbname
+            self.dbshell = psycopg2.connect(connect)
+            if self.dbshell.closed == 0:
+                self.dbshell.autocommit = True
+                logging.info("Opened connection to %r" % dbname)
+
+            self.dbcursor = self.dbshell.cursor()
+            if self.dbcursor.closed == 0:
+                logging.info("Opened cursor in %r" % dbname)
+
+        except Exception as e:
+            logging.warning("Couldn't connect to database: %r" % e)
+
+        # Get any existing sensor data
+        query = """SELECT * FROM sensors;"""
+        logging.debug(query)
+        self.dbcursor.execute(query)
+        logging.debug("Got %r sensor records" % self.dbcursor.rowcount)
+        for id,alias,location,device,sense,channel in self.dbcursor:
+            data = dict()
+            if id is not None:
+                data['id'] = id
+            if alias is not None:
+                data['alias'] = alias
+            if location is not None:
+                data['location'] = location
+            if device is not None:
+                data['device'] = device
+            if sense is not None:
+                data['sensor'] = sense
+            if channel is not None:
+                data['channel'] = channel
+            self.sensors[id] = SensorDevice(data)
+
+    def dump(self):
+        logging.debug("Sensor.dump(%r entries)" % len(self.sensors))
+        for id,sensor in self.sensors.items():
+            sensor.dump()
+
+    def add(self, sensor):
+        id = sensor.get('id')
+        self.sensors[id] = sensor
+        self.dbcursor.execute(sensor.MakeSQL())
+
+    def get(self, id):
+        try:
+            sensor = self.sensors[id]
+        except:
+            sensor = None
+        return sensor
+
+class SensorDevice(object):
+    """A class to hold sensor data"""
     def __init__(self, data=dict()):
-        """A class to hold weather sensor data"""
         self.data = dict()
-        if data['id'] is not None and data['id'] is not "":
+        if ('id' in data) is True:
             self.data['id'] = data['id']
         else:
             self.data['id'] = None
-        if data['alias'] is not None and data['alias'] is not "":
+        if ('alias' in data) == True:
             self.data['alias'] = data['alias']
         else:
             self.data['alias'] = None
-        if data['location'] is not None and data['location'] is not "":
+        if ('location' in data) is True:
             self.data['location'] = data['location']
         else:
             self.data['location'] = None
-        if data['device'] is not None and data['device'] is not "":
+        if ('device' in data) is True:
             self.data['device'] = data['device']
         else:
             self.data['device'] = DeviceType.UNKNOWN
-        if data['sensor'] is not None and data['sensor'] is not "":
+        if ('sensor' in data) is True:
             self.data['sensor'] = data['sensor']
         else:
             self.data['sensor'] = SensorType.UNKNOWN
-        if data['channel'] is not None and data['channel'] is not "":
+        if ('channel' in data) is True:
             self.data['channel'] = data['channel']
         else:
             self.data['channel'] = None
@@ -95,46 +153,46 @@ class Sensor(object):
     def dump(self):
         """ Dump the data about this sensor"""
         print("ID: %r" % self.data['id'])
-        print("Alias: %r" % self.data['alias'])
-        print("Location: %r" % self.data['location'])
-        print("Channel: %r" % self.data['channel'])
+        print("\tAlias: %r" % self.data['alias'])
+        print("\tLocation: %r" % self.data['location'])
+        print("\tChannel: %r" % self.data['channel'])
         if self.data['device'] ==  DeviceType.UNKNOWN:
-            print("Device: UNKNOWN")
+            print("\tDevice: UNKNOWN")
         elif self.data['device'] ==  DeviceType.ONEWIRE:
-            print("Device: ONEWIRE")
+            print("\tDevice: ONEWIRE")
         elif self.data['device'] ==  DeviceType.OWNET:
-            print("Device: OWNET")
+            print("\tDevice: OWNET")
         elif self.data['device'] ==  DeviceType.RTL433:
-            print("Device: RTL433")
+            print("\tDevice: RTL433")
         elif self.data['device'] ==  DeviceType.RTLSDR:
-            print("Device: RTLSDR")
+            print("\tDevice: RTLSDR")
         elif self.data['device'] ==  DeviceType.USB:
-            print("Device: USB")
+            print("\tDtevice: USB")
         elif self.data['device'] ==  DeviceType.SERIAL:
-            print("Device: SERIAL")
+            print("\tDevice: SERIAL")
         elif self.data['device'] ==  DeviceType.GPIO:
-            print("Device: GPIO")
+            print("\tDevice: GPIO")
 
         if self.data['sensor'] ==  SensorType.UNKNOWN:
-            print("Sensor: UNKNOWN")
+            print("\tSensor: UNKNOWN")
         elif self.data['sensor'] ==  SensorType.ACVOLTAGE:
-            print("Sensor: ACVOLTAGE")
+            print("\tSensor: ACVOLTAGE")
         elif self.data['sensor'] ==  SensorType.DCVOLTAGE:
-            print("Sensor: DCVOLTAGE")
+            print("\tSensor: DCVOLTAGE")
         elif self.data['sensor'] ==  SensorType.AUTH:
-            print("Sensor: AUTH")
+            print("\tSensor: AUTH")
         elif self.data['sensor'] ==  SensorType.BATTERY:
-            print("Sensor: BATTERY")
+            print("\tSensor: BATTERY")
         elif self.data['sensor'] ==  SensorType.POWER:
-            print("Sensor: POWER")
+            print("\tSensor: POWER")
         elif self.data['sensor'] ==  SensorType.CLOCK:
-            print("Sensor: CLOCK")
+            print("\tSensor: CLOCK")
         elif self.data['sensor'] ==  SensorType.TEMPERATURE:
-            print("Sensor: TEMPERATURE")
+            print("\tSensor: TEMPERATURE")
         elif self.data['sensor'] ==  SensorType.MOISTURE:
-            print("Sensor: MOISTURE")
+            print("\tSensor: MOISTURE")
         elif self.data['sensor'] ==  SensorType.UNSUPPORTED:
-            print("Sensor: UNSUPPORTED")
+            print("\tSensor: UNSUPPORTED")
 
     def MakeSQL(self):
         """ Format the SQL query to add this sensor"""
@@ -181,35 +239,32 @@ class Sensor(object):
         else:
             channel = ''
 
-        query = """INSERT INTO sensors VALUES (%r, %r, %r, %r, %r, %r) ON 
CONFLICT DO NOTHING;""" % (self.data['id'], self.data['alias'], 
self.data['location'], device, sensor, channel)
+        if self.data['id'] is not None:
+            id = self.data['id']
+        else:
+            id = ""
+
+        if self.data['alias'] is not None:
+            alias = self.data['alias']
+        else:
+            alias = ""
+
+        if self.data['location'] is not None:
+            location = self.data['location']
+        else:
+            location = ""
+
+        query = """INSERT INTO sensors VALUES (%r, %r, %r, %r, %r, %r) ON 
CONFLICT DO NOTHING;""" % (id, alias, location, device, sensor, channel)
 
         logging.debug(query)
         return (query)
 
-    def populate(self, result):
+    def populate(self, result=""):
         """Populate the internal data from an SQL query """
+
         self.data['id'] = data['id']
         self.data['alias'] = data['alias']
         self.data['location'] = data['location']
         self.data['device'] = data['device']
-            self.data['sensor'] = SensorType.UNKNOWN
-        else:
-            self.data['sensor'] = SensorType.UNKNOWN
-        if data['channel'] is not None and data['channel'] is not "":
-            self.data['channel'] = data['channel']
-        else:
-            self.data['channel'] = None
-        
-data = dict()
-data['id'] = "1"
-data['alias'] = "AAA"
-data['location'] = "LLL"
-data['device'] = DeviceType.UNKNOWN
-data['sensor'] = SensorType.UNKNOWN
-data['channel'] = None
-            
-sense = Sensor(data)
-sense.dump()
-
-sense.set('id', '0')
-print(sense.MakeSQL())
+        self.data['sensor'] = data['sensor']
+        self.data['channel'] = data['channel']

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=9958ff368a29afd5597451eab46a41c6da5464a0


commit 9958ff368a29afd5597451eab46a41c6da5464a0
Author: Rob Savoye <address@hidden>
Date:   Mon Feb 11 17:18:44 2019 -0700

    Work with newer weather schema

diff --git a/python/chart.py b/python/chart.py
index 2a5a022..93e7a2f 100755
--- a/python/chart.py
+++ b/python/chart.py
@@ -42,8 +42,8 @@ options = dict()
 options['dbserver'] = "pi"  # hostname of the database
 options['dbname'] = "powerguru"  # hostname of the database
 options['interval'] = 100        # interval in seconds between data updates
-options['starttime'] = 0
-options['endtime'] = 0
+options['starttime'] = ""
+options['endtime'] = ""
 
 #import matplotlib
 #matplotlib.use('agg')
@@ -165,7 +165,7 @@ colors.append("black")
 def animate(i):
     logging.debug("Refreshing data...")
     ids = list()
-    query = "SELECT DISTINCT id FROM temperature"
+    query = "SELECT DISTINCT id FROM weather"
     logging.debug(query)
     dbcursor.execute(query)
     logging.debug("Query returned %r records" % dbcursor.rowcount)
@@ -175,13 +175,13 @@ def animate(i):
 
     cur = 0
     for id in ids:
-        query = "SELECT id,temperature,timestamp FROM temperature WHERE 
(id='%s' %s %s) ORDER BY timestamp;" % (id[0], start, end)
+        query = "SELECT id,temperature,humidity,timestamp FROM weather WHERE 
(id='%s' %s %s) ORDER BY timestamp;" % (id[0], start, end)
         logging.debug(query)
         dbcursor.execute(query)
         logging.debug("Query returned %r records" % dbcursor.rowcount)
         x = list()
         y = list()
-        for id,temperature,timestamp in dbcursor:
+        for id,temperature,humidity,timestamp in dbcursor:
             #print("TEMP: %r, %r" % (temperature,timestamp))
             x.append(timestamp)
             y.append(temperature)
@@ -198,14 +198,14 @@ def animate(i):
     xx = list()
     yy = list()
     zz = list()
-    query = "SELECT DISTINCT id FROM battery"
+    query = "SELECT DISTINCT id FROM power"
     logging.debug(query)
     dbcursor.execute(query)
     logging.debug("Query returned %r records" % dbcursor.rowcount)
     if  dbcursor.rowcount > 0:
         for id in dbcursor:
             ids.append(id)
-            query = "SELECT id,current,volts,timestamp FROM battery WHERE 
(id='%s' %s %s) ORDER BY timestamp " % (id[0], start, end)
+            query = "SELECT id,current,volts,timestamp FROM power WHERE 
(id='%s' %s %s) ORDER BY timestamp " % (id[0], start, end)
             logging.debug(query)
             dbcursor.execute(query)
             logging.debug("Query returned %r records" % dbcursor.rowcount)

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=0286069aa54e2328e7bf67b951eccba1402727e1


commit 0286069aa54e2328e7bf67b951eccba1402727e1
Author: Rob Savoye <address@hidden>
Date:   Mon Feb 11 12:08:53 2019 -0700

    Implement

diff --git a/python/sensor.py b/python/sensor.py
index eee0344..0b685ba 100755
--- a/python/sensor.py
+++ b/python/sensor.py
@@ -70,7 +70,7 @@ class Sensor(object):
         else:
             self.data['device'] = DeviceType.UNKNOWN
         if data['sensor'] is not None and data['sensor'] is not "":
-            self.data['sensor'] = SensorType.UNKNOWN
+            self.data['sensor'] = data['sensor']
         else:
             self.data['sensor'] = SensorType.UNKNOWN
         if data['channel'] is not None and data['channel'] is not "":
@@ -185,3 +185,31 @@ class Sensor(object):
 
         logging.debug(query)
         return (query)
+
+    def populate(self, result):
+        """Populate the internal data from an SQL query """
+        self.data['id'] = data['id']
+        self.data['alias'] = data['alias']
+        self.data['location'] = data['location']
+        self.data['device'] = data['device']
+            self.data['sensor'] = SensorType.UNKNOWN
+        else:
+            self.data['sensor'] = SensorType.UNKNOWN
+        if data['channel'] is not None and data['channel'] is not "":
+            self.data['channel'] = data['channel']
+        else:
+            self.data['channel'] = None
+        
+data = dict()
+data['id'] = "1"
+data['alias'] = "AAA"
+data['location'] = "LLL"
+data['device'] = DeviceType.UNKNOWN
+data['sensor'] = SensorType.UNKNOWN
+data['channel'] = None
+            
+sense = Sensor(data)
+sense.dump()
+
+sense.set('id', '0')
+print(sense.MakeSQL())

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=dd58872b46e7e2df2eab9952750ee409531d4cab


commit dd58872b46e7e2df2eab9952750ee409531d4cab
Author: Rob Savoye <address@hidden>
Date:   Mon Feb 11 12:02:02 2019 -0700

    Implement

diff --git a/python/sensor.py b/python/sensor.py
new file mode 100755
index 0000000..eee0344
--- /dev/null
+++ b/python/sensor.py
@@ -0,0 +1,187 @@
+#!/usr/bin/python3
+
+# 
+#   Copyright (C) 2019 Free Software Foundation, Inc.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+# 
+
+# API documentation at: https://pyownet.readthedocs.io/en/latest/
+
+import sys
+import epdb
+import logging
+import psycopg2
+from datetime import datetime
+from enum import Enum
+
+
+class SensorType(Enum):
+    UNKNOWN = 0
+    ACVOLTAGE = 1
+    DCVOLTAGE = 2
+    AUTH = 3
+    BATTERY = 4
+    POWER = 5
+    CLOCK = 6
+    TEMPERATURE = 7
+    MOISTURE = 8
+    UNSUPPORTED = 9
+
+class DeviceType(Enum):
+    UNKNOWN = 0
+    ONEWIRE = 1
+    OWNET = 2
+    RTL433 = 3
+    RTLSDR = 4
+    USB = 5
+    SERIAL = 6
+    GPIO = 7
+
+class Sensor(object):
+    def __init__(self, data=dict()):
+        """A class to hold weather sensor data"""
+        self.data = dict()
+        if data['id'] is not None and data['id'] is not "":
+            self.data['id'] = data['id']
+        else:
+            self.data['id'] = None
+        if data['alias'] is not None and data['alias'] is not "":
+            self.data['alias'] = data['alias']
+        else:
+            self.data['alias'] = None
+        if data['location'] is not None and data['location'] is not "":
+            self.data['location'] = data['location']
+        else:
+            self.data['location'] = None
+        if data['device'] is not None and data['device'] is not "":
+            self.data['device'] = data['device']
+        else:
+            self.data['device'] = DeviceType.UNKNOWN
+        if data['sensor'] is not None and data['sensor'] is not "":
+            self.data['sensor'] = SensorType.UNKNOWN
+        else:
+            self.data['sensor'] = SensorType.UNKNOWN
+        if data['channel'] is not None and data['channel'] is not "":
+            self.data['channel'] = data['channel']
+        else:
+            self.data['channel'] = None
+
+    def set(self, index, value):
+        """Set an internal value for this sensor"""
+        if (index in self.data) == True:
+            self.data[index] = value
+        else:
+            logging.warning("Key %r doesn't exist in data structure" % index)
+
+    def get(self, index):
+        """Get an internal value for this sensor"""
+        if (index in self.data) == True:
+            return self.data[index]
+        else:
+            logging.warning("Key %r doesn't exist in data structure" % index)
+
+    def dump(self):
+        """ Dump the data about this sensor"""
+        print("ID: %r" % self.data['id'])
+        print("Alias: %r" % self.data['alias'])
+        print("Location: %r" % self.data['location'])
+        print("Channel: %r" % self.data['channel'])
+        if self.data['device'] ==  DeviceType.UNKNOWN:
+            print("Device: UNKNOWN")
+        elif self.data['device'] ==  DeviceType.ONEWIRE:
+            print("Device: ONEWIRE")
+        elif self.data['device'] ==  DeviceType.OWNET:
+            print("Device: OWNET")
+        elif self.data['device'] ==  DeviceType.RTL433:
+            print("Device: RTL433")
+        elif self.data['device'] ==  DeviceType.RTLSDR:
+            print("Device: RTLSDR")
+        elif self.data['device'] ==  DeviceType.USB:
+            print("Device: USB")
+        elif self.data['device'] ==  DeviceType.SERIAL:
+            print("Device: SERIAL")
+        elif self.data['device'] ==  DeviceType.GPIO:
+            print("Device: GPIO")
+
+        if self.data['sensor'] ==  SensorType.UNKNOWN:
+            print("Sensor: UNKNOWN")
+        elif self.data['sensor'] ==  SensorType.ACVOLTAGE:
+            print("Sensor: ACVOLTAGE")
+        elif self.data['sensor'] ==  SensorType.DCVOLTAGE:
+            print("Sensor: DCVOLTAGE")
+        elif self.data['sensor'] ==  SensorType.AUTH:
+            print("Sensor: AUTH")
+        elif self.data['sensor'] ==  SensorType.BATTERY:
+            print("Sensor: BATTERY")
+        elif self.data['sensor'] ==  SensorType.POWER:
+            print("Sensor: POWER")
+        elif self.data['sensor'] ==  SensorType.CLOCK:
+            print("Sensor: CLOCK")
+        elif self.data['sensor'] ==  SensorType.TEMPERATURE:
+            print("Sensor: TEMPERATURE")
+        elif self.data['sensor'] ==  SensorType.MOISTURE:
+            print("Sensor: MOISTURE")
+        elif self.data['sensor'] ==  SensorType.UNSUPPORTED:
+            print("Sensor: UNSUPPORTED")
+
+    def MakeSQL(self):
+        """ Format the SQL query to add this sensor"""
+        if self.data['device'] ==  DeviceType.UNKNOWN:
+            device = "UNKNOWN"
+        elif self.data['device'] ==  DeviceType.ONEWIRE:
+            device = "ONEWIRE"
+        elif self.data['device'] ==  DeviceType.OWNET:
+            device = "OWNET"
+        elif self.data['device'] ==  DeviceType.RTL433:
+            device = "RTL433"
+        elif self.data['device'] ==  DeviceType.RTLSDR:
+            device = "RTLSDR"
+        elif self.data['device'] ==  DeviceType.USB:
+            device = "USB"
+        elif self.data['device'] ==  DeviceType.SERIAL:
+            device = "SERIAL"
+        elif self.data['device'] ==  DeviceType.GPIO:
+            device = "GPIO"
+
+        if self.data['sensor'] ==  SensorType.UNKNOWN:
+            sensor = "UNKNOWN"
+        elif self.data['sensor'] ==  SensorType.ACVOLTAGE:
+            sensor = "ACVOLTAGE"
+        elif self.data['sensor'] ==  SensorType.DCVOLTAGE:
+            sensor = "DCVOLTAGE"
+        elif self.data['sensor'] ==  SensorType.AUTH:
+            sensor = "AUTH"
+        elif self.data['sensor'] ==  SensorType.BATTERY:
+            sensor = "BATTERY"
+        elif self.data['sensor'] ==  SensorType.POWER:
+            sensor = "POWER"
+        elif self.data['sensor'] ==  SensorType.CLOCK:
+            sensor = "CLOCK"
+        elif self.data['sensor'] ==  SensorType.TEMPERATURE:
+            sensor = "TEMPERATURE"
+        elif self.data['sensor'] ==  SensorType.MOISTURE:
+            sensor = "MOISTURE"
+        elif self.data['sensor'] ==  SensorType.UNSUPPORTED:
+            sensor = "UNSUPPORTED"
+
+        if self.data['channel'] is not None:
+            channel = self.data['channel']
+        else:
+            channel = ''
+
+        query = """INSERT INTO sensors VALUES (%r, %r, %r, %r, %r, %r) ON 
CONFLICT DO NOTHING;""" % (self.data['id'], self.data['alias'], 
self.data['location'], device, sensor, channel)
+
+        logging.debug(query)
+        return (query)

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=a0963ca070866feb5968dfc4205ba2e9cdf307ab


commit a0963ca070866feb5968dfc4205ba2e9cdf307ab
Author: Rob Savoye <address@hidden>
Date:   Mon Feb 11 10:43:04 2019 -0700

    CHange battery to power, add enums, add sensor table

diff --git a/powerguru.sql b/powerguru.sql
index b6f0fc5..1a9f6a8 100644
--- a/powerguru.sql
+++ b/powerguru.sql
@@ -1,21 +1,21 @@
 -- 
---   Copyright (C) 2005, 2006-2018
---   Free Software Foundation, Inc.
---
---   This program is free software; you can redistribute it and/or modify
---   it under the terms of the GNU General Public License as published by
---   the Free Software Foundation; either version 2 of the License, or
---   (at your option) any later version.
---
---   This program is distributed in the hope that it will be useful,
---   but WITHOUT ANY WARRANTY; without even the implied warranty of
---   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
---   GNU General Public License for more details.
---
---   You should have received a copy of the GNU General Public License
---   along with this program; if not, write to the Free Software
---   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
---
+-- Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
+--               2014, 2015, 2016, 2017, 2018, 2019
+--     Free Software Foundation, Inc.
+-- 
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 3 of the License, or
+-- (at your option) any later version.
+-- 
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+-- 
+-- You should have received a copy of the GNU General Public License
+-- along with this program; if not, write to the Free Software
+-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 -- MySQL dump 10.8
 --
@@ -34,16 +34,39 @@
 --
 -- Name: data; Type: TYPE; Schema: public; Owner: rob
 --
+DROP TYPE IF EXISTS channel_type;
+CREATE TYPE public.channel_type AS ENUM (
+    'A',
+    'B',
+    'C'
+);
+
+DROP TYPE IF EXISTS volt_type;
 CREATE TYPE public.volt_type AS ENUM (
     'AC',
     'DC'
 );
 
-CREATE TYPE public.wire_type AS ENUM (
+DROP TYPE IF EXISTS device_type;
+CREATE TYPE public.device_type AS ENUM (
+    'unknown',
+    'onewire',
+    'ownet',
+    'rtl433',
+    'rtlsdr',
+    'usb',
+    'serial',
+    'gpio'
+);
+
+DROP TYPE IF EXISTS sensor_type;
+CREATE TYPE public.sensor_type AS ENUM (
+    'UNKNOWN',
     'ACVOLTAGE',
     'DCVOLTAGE',
     'AUTH',
     'BATTERY',
+    'POWER',
     'CLOCK',
     'TEMPERATURE',
     'MOISTURE',
@@ -72,13 +95,14 @@ CREATE TABLE meters (
   battery_tempcomp float NOT NULL default '0'
 );
 
-DROP TABLE IF EXISTS onewire;
-CREATE TABLE onewire (
-  family char(2) NOT NULL default '0',
+DROP TABLE IF EXISTS sensor;
+CREATE TABLE sensor (
   id varchar(12) NOT NULL default '0',
   alias varchar(12) NOT NULL default '0',
-  type  wire_type NOT NULL default 'UNSUPPORTED',
-  "timestamp" timestamp without time zone UNIQUE
+  location varchar(12) NOT NULL default '0',
+  device device_type NOT NULL default '1wire',
+  type sensor_type NOT NULL default 'UNKNOWN',
+  channel channel_type NOT NULL default 'A'
 );
 
 DROP TABLE IF EXISTS temperature;
@@ -87,12 +111,13 @@ CREATE TABLE temperature (
   temperature float NOT NULL default '0',
   temphigh float NOT NULL default '0',
   templow float NOT NULL default '0',
+  humidity float NOT NULL default '0',
   scale char(1) NOT NULL default 'F',
   "timestamp" timestamp without time zone UNIQUE
 );
 
-DROP TABLE IF EXISTS battery;
-CREATE TABLE battery (
+DROP TABLE IF EXISTS power;
+CREATE TABLE power (
   id char(16) NOT NULL default '0',
   current float NOT NULL default '0',
   volts float NOT NULL default '0',

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=17046bef031bb7e59bd9df91c1d03df1e6a9a324


commit 17046bef031bb7e59bd9df91c1d03df1e6a9a324
Author: Rob Savoye <address@hidden>
Date:   Sun Feb 10 21:57:37 2019 -0700

    Use rtl_sdr for remote wireless sensors

diff --git a/python/rtlsdr.py b/python/rtlsdr.py
new file mode 100644
index 0000000..b2fa5ab
--- /dev/null
+++ b/python/rtlsdr.py
@@ -0,0 +1,51 @@
+#!/usr/bin/python3
+
+# 
+#   Copyright (C) 2019 Free Software Foundation, Inc.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+# 
+
+# API documentation at: https://pyownet.readthedocs.io/en/latest/
+
+#import epdb
+import logging
+#import time
+#import psycopg2
+#from rtlsdr import RtlSdr
+
+
+def rtlsdr_handler(args):
+    logging.debug("Start rtl_sdr %r" % args)
+    #self.sdr = RtlSdr(1, True,"00000001")
+
+    # # Connect to a postgresql database
+    # try:
+    #     dbname = "powerguru"
+    #     connect = "dbname=" + dbname
+    #     dbshell = psycopg2.connect(connect)
+    #     if dbshell.closed == 0:
+    #         dbshell.autocommit = True
+    #         logging.info("Opened connection to %r" % dbname)
+            
+    #         dbcursor = dbshell.cursor()
+    #         if dbcursor.closed == 0:
+    #             logging.info("Opened cursor in %r" % dbname)
+                
+    # except Exception as e:
+    #     logging.warning("Couldn't connect to database: %r" % e)
+        
+
+    _sensors = list()
+

http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=f3bd3120475cc5a4985aa980cf1b42b4b430c589


commit f3bd3120475cc5a4985aa980cf1b42b4b430c589
Author: Rob Savoye <address@hidden>
Date:   Sun Feb 10 21:57:12 2019 -0700

    Use rtl_433 for remote wireless sensors

diff --git a/python/rtl433.py b/python/rtl433.py
new file mode 100644
index 0000000..78b2025
--- /dev/null
+++ b/python/rtl433.py
@@ -0,0 +1,90 @@
+#!/usr/bin/python3
+
+# 
+#   Copyright (C) 2019 Free Software Foundation, Inc.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+# 
+
+# API documentation at: https://pyownet.readthedocs.io/en/latest/
+
+import sys
+from subprocess import PIPE, Popen, STDOUT
+from threading  import Thread
+import epdb
+import logging
+import time
+import os
+import psycopg2
+ON_POSIX = 'posix' in sys.builtin_module_names
+
+def rtl433_handler(args):
+    logging.debug("Start rtl_433 %r" % args)
+    cmd = [ 'rtl_433', '-F', 'csv', '-R', '40', '-T', '15']
+
+    #ppp = Popen(cmd, stdout=PIPE, stderr=STDOUT, bufsize=1, 
close_fds=ON_POSIX)
+    retries = 10
+    while retries > 0:
+        ppp = Popen(cmd, stdout=PIPE, bufsize=0, close_fds=ON_POSIX)
+        try:
+            out, err = ppp.communicate()
+            #out, err = ppp.communicate(timeout=0.2)
+        except subprocess.TimeoutExpired:
+            logging.warning('subprocess did not terminate in time')
+        #print("FIXME0: %r" % retries)
+        #epdb.set_trace()
+        for line in out.splitlines():
+            #for line in ppp.readline():
+            print("FIXME: %r" % line)
+            str = line.decode('utf8')
+            tokens = str.split(',')
+            # this is just the csv header fields
+            if tokens[0] == 'time':
+                continue
+            sensors = dict()
+            sensors['model'] = tokens[3]
+            sensors['id'] = tokens[5]
+            sensors['channel'] = tokens[6]
+            sensors['temperature'] = tokens[7]
+            sensors['humidity'] = tokens[8]
+            # Dump data
+            print("MODEL: %r" % sensors['model'])
+            print("\tID: %r" % sensors['id'])
+            print("\tCHANNEL: %r" % sensors['channel'])
+            print("\tTEMPERATURE: %rC" % sensors['temperature'])
+            print("\tHUMIDITY: %r" % sensors['humidity'])
+            print("")
+            time.sleep(1)
+        retries -= 1    
+        epdb.set_trace()
+ 
+    # # Connect to a postgresql database
+    # try:
+    #     dbname = "powerguru"
+    #     connect = "dbname=" + dbname
+    #     dbshell = psycopg2.connect(connect)
+    #     if dbshell.closed == 0:
+    #         dbshell.autocommit = True
+    #         logging.info("Opened connection to %r" % dbname)
+            
+    #         dbcursor = dbshell.cursor()
+    #         if dbcursor.closed == 0:
+    #             logging.info("Opened cursor in %r" % dbname)
+                
+    # except Exception as e:
+    #     logging.warning("Couldn't connect to database: %r" % e)
+        
+
+    # _sensors = list()
+

-----------------------------------------------------------------------

Summary of changes:
 powerguru.sql    |  75 ++++++++++------
 python/chart.py  |  14 +--
 python/rtl433.py | 100 +++++++++++++++++++++
 python/rtlsdr.py |  51 +++++++++++
 python/sensor.py | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 478 insertions(+), 32 deletions(-)
 create mode 100755 python/rtl433.py
 create mode 100644 python/rtlsdr.py
 create mode 100755 python/sensor.py


hooks/post-receive
-- 
powerguru



reply via email to

[Prev in Thread] Current Thread [Next in Thread]