]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - seewasser.py
Add how to create Sphinx documentation to README.
[chrisu/seepark.git] / seewasser.py
index b8a23b6c9ba2726c4471d5f4fd5405b0304fcf77..be5f149ea49e8afd6660e5113756ca1c91b4b616 100755 (executable)
@@ -8,7 +8,7 @@ import sys
 import csv
 import os
 import configparser
-from sqlalchemy import create_engine, exc
+from sqlalchemy import create_engine
 import warnings
 import MySQLdb.cursors
 
@@ -57,22 +57,14 @@ def readsensor(sensor_id):
 
 def writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
     # Schreiben des csv-files
-    file = open(os.path.expanduser(config.get("csv", "filename")), "a", newline = "")
-    writer = csv.writer(file, dialect = "excel")
-    writer.writerow([timestamp.strftime("%Y-%m-%d %H:%M"), sensor_id, sensor_name, "{:.1f}".format(value)])
-
-    file.close()
+    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)])
 
 
 def readcsvfile(csvfile):
-    file = open(csvfile, "r", newline="")
-    reader = csv.DictReader(file, dialect = "excel", fieldnames=("timestamp", "sensor_id", "sensor_name", "value"))
-    records = []
-    for row in reader:
-        records.append(row)
-
-    file.close()
-    return records
+    with open(csvfile, "r", newline="") as file:
+        yield from csv.DictReader(file, dialect = "excel", fieldnames=("timestamp", "sensor_id", "sensor_name", "value"))
 
 
 def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
@@ -85,36 +77,36 @@ def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, val
     engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
     conn = engine.connect()
     with warnings.catch_warnings():
-        # ignore _mysql_exceptions.IntegrityError: (1062, "Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'")
-        # TODO: the following doesn't work
+        # ignore _mysql_exceptions.Warning: Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'
         warnings.simplefilter("ignore", category=MySQLdb.cursors.Warning)
-        # TODO: this does but …?!
-        try:
-            conn.execute("insert 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)
-        except exc.IntegrityError as e:
-            if "1062" in str(e):
-                pass
-            else:
-                raise
+        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"
-    for sensor_id, sensor_name in config.items('temperature'):
-        if fromcsvfile:
-            # "timestamp", "sensor_id", "sensor_name", "value"
-            for record in readcsvfile(fromcsvfile):
-                timestamp, sensor_id, sensor_name, value = (record["timestamp"], record["sensor_id"], record["sensor_name"], float(record["value"]))
-                value_raw = None
-                writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
-        else:
-            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 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)
         logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value))