owm.py: check for w['rain']['3h']
[chrisu/seepark.git] / seewasser.py
index a17e794..922a990 100755 (executable)
@@ -1,10 +1,18 @@
 #! /usr/bin/python3
 
+import argparse
+import logging
 import datetime
 import re
 import sys
 import csv
 import os
+import configparser
+from sqlalchemy import create_engine
+import warnings
+import MySQLdb.cursors
+
+logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
 
 #May 27 21:32 /sys/bus/w1/devices/28-0416a1bab9ff
 #May 27 21:33 /sys/bus/w1/devices/28-0416a1ac66ff
@@ -16,8 +24,12 @@ import os
 # einen Sensor auslesen
 #/sys/bus/w1/devices/
 
-def readsensor(sensorID):
-    sensorfile = "/sys/bus/w1/devices/28-{}/w1_slave".format(sensorID)
+class ReadsensorError(RuntimeError):
+    pass
+    
+    
+def readsensor(sensor_id):
+    sensorfile = "/sys/bus/w1/devices/28-{}/w1_slave".format(sensor_id)
     file = open(sensorfile)
 
     # Inhalt des Sensors:
@@ -30,40 +42,85 @@ def readsensor(sensorID):
 
     yesno = match.group(1)
     if yesno != "YES":
-        return None
+        raise ReadsensorError('Could not evaluate sensorfile "{}"'.format(sensorfile))
         
     # Temperatur raus
     linetemp = file.readline()
     match = re.search(" t=([-0-9]+)",linetemp)
 
-    temp = match.group(1)
-    temp = float(temp)/1000
+    temp_raw = match.group(1)
+    temp = float(temp_raw)/1000
 
     file.close()
-
     jetzt = datetime.datetime.now()
-    # print(jetzt)
 
+    return jetzt, temp_raw, temp
+
+
+def writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
     # Schreiben des csv-files
-    file2 = open("badesaison.csv", "a", newline = "")
-    writer = csv.writer(file2, dialect = "excel")
-    writer.writerow([jetzt.strftime("%Y-%m-%d %H:%M"), sensorID, "{:.1f}".format(temp)])
+    with open(os.path.expanduser(config.get("csv", "filename")), "a", newline = "") as file:
+        writer = csv.writer(file, dialect = "excel")
+        writer.writerow([timestamp.strftime("%Y-%m-%d %H:%M"), sensor_id, sensor_name, "{:.1f}".format(value)])
 
-    file2.close()
 
-    # Nur für die Ausgabe wird gerundet
-    return temp
-    
-sensoren = ["0416a1bab9ff","0416a1ac66ff","0516a207a4ff","0316a2193bff","0316a21383ff"]
+def readcsvfile(csvfile):
+    with open(csvfile, "r", newline="") as file:
+        yield from csv.DictReader(file, dialect = "excel", fieldnames=("timestamp", "sensor_id", "sensor_name", "value"))
 
-error = False
 
-for sensorID in sensoren:
-    temp = readsensor(sensorID)
-    if temp is None:
-        print("Fehler bei Sensor{}".format(sensorID))
-        error = True
-    else:
-        print('Sensor {}: {:.1f}°C'.format(sensorID,temp))
+def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
+    # Schreiben des db-files
+    user = config.get('database', 'user')
+    pwd = config.get('database','password')
+    host = config.get('database','hostname')
+    db = config.get('database','database')
+    
+    engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
+    conn = engine.connect()
+    with warnings.catch_warnings():
+        # ignore _mysql_exceptions.Warning: Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'
+        warnings.simplefilter("ignore", category=MySQLdb.cursors.Warning)
+        if args.verbose:
+            logging.info("Writing to database: sensor_id={}, sensor_name={}, value_type={}, value_raw={}, value={}, timestamp={}".format(sensor_id, sensor_name, value_type, value_raw, value, timestamp))
+        conn.execute("insert ignore into sensors (sensor_id, sensor_name, value_type, value_raw, value, timestamp) values (%s,%s,%s,%s,%s,%s)", sensor_id, sensor_name, value_type, value_raw, value, timestamp)
+    conn.close()
+
+
+def process_csv_file(config, fromcsvfile, value_type, sensors):
+    # "timestamp", "sensor_id", "sensor_name", "value"
+    sensors_ids = [sensor_id for sensor_id, sensor_name in sensors]
+    for record in readcsvfile(fromcsvfile):
+        timestamp, sensor_id, sensor_name, value = (record["timestamp"], record["sensor_id"], record["sensor_name"], float(record["value"]))
+        if sensor_id in sensors_ids:
+            value_raw = None
+            writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
+
+
+def main(configfile, fromcsvfile):
+    config = configparser.ConfigParser()
+    config.read(configfile)
+    sensors = config.items('temperature')
+    value_type = "Wassertemperatur"
+
+    if fromcsvfile:
+        process_csv_file(config, fromcsvfile, value_type, sensors)
+        return
+
+    for sensor_id, sensor_name in sensors:
+        timestamp, value_raw, value = readsensor(sensor_id)
+        writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
+        writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
+        if args.verbose:
+            logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value))
+
+
+if __name__ == '__main__':
+    default_config_file = os.path.expanduser('~/seewasser.ini')
+    parser = argparse.ArgumentParser(description='Read sensor data')
+    parser.add_argument('--config', default=default_config_file, help='configuration file')
+    parser.add_argument('--fromcsvfile', help='write values from csv file to database')
+    parser.add_argument("--verbose", help='write some output', action='store_true')
+    args = parser.parse_args()
+    main(args.config, args.fromcsvfile)
 
-sys.exit(1 if error else 0)
\ No newline at end of file