owm.py: check for w['rain']['3h']
[chrisu/seepark.git] / seewasser.py
index ecd67d6..922a990 100755 (executable)
@@ -8,10 +8,12 @@ import sys
 import csv
 import os
 import configparser
-from sqlalchemy import create_engine, exc
+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
 #May 27 21:35 /sys/bus/w1/devices/28-0516a207a4ff
@@ -77,17 +79,11 @@ 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
+        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()
 
 
@@ -115,7 +111,8 @@ def main(configfile, fromcsvfile):
         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))
+        if args.verbose:
+            logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value))
 
 
 if __name__ == '__main__':
@@ -123,6 +120,7 @@ if __name__ == '__main__':
     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)