Separate function process_csv_file.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 4 Sep 2018 18:14:51 +0000 (20:14 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 4 Sep 2018 18:14:51 +0000 (20:14 +0200)
seewasser.py

index 811ebbe..ecd67d6 100755 (executable)
@@ -91,22 +91,30 @@ def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, val
     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))