X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/blobdiff_plain/448b21fba4a3aaa540c34fcf686017abc2032a0f..aa525e265c768dd0a046086685c47c6a238cf436:/seewasser.py diff --git a/seewasser.py b/seewasser.py index 724beb6..600bf3d 100755 --- a/seewasser.py +++ b/seewasser.py @@ -1,5 +1,6 @@ #! /usr/bin/python3 +import argparse import logging import datetime import re @@ -7,7 +8,9 @@ import sys import csv import os import configparser -from sqlalchemy import create_engine +from sqlalchemy import create_engine, exc +import warnings +import MySQLdb.cursors #May 27 21:32 /sys/bus/w1/devices/28-0416a1bab9ff #May 27 21:33 /sys/bus/w1/devices/28-0416a1ac66ff @@ -54,11 +57,18 @@ 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)]) + 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)]) - file.close() + +def readcsvfile(csvfile): + with open(csvfile, "r", newline="") as file: + reader = csv.DictReader(file, dialect = "excel", fieldnames=("timestamp", "sensor_id", "sensor_name", "value")) + records = [] + for row in reader: + records.append(row) + return records def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value): @@ -70,20 +80,45 @@ 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() - 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) + 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 + 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.close() -def main(): +def main(configfile, fromcsvfile): config = configparser.ConfigParser() - config.read(os.path.expanduser('~/seewasser.ini')) + config.read(configfile) value_type = "Wassertemperatur" for sensor_id, sensor_name in config.items('temperature'): - 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: + # "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) logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value)) -main() +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') + args = parser.parse_args() + main(args.config, args.fromcsvfile) +