X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/blobdiff_plain/a268adb8bcb7cc65713cf8a7ccd7c3de3930f1d8..7bc31204e1355e06fc8be3c28563bb0c1c4441f7:/seewasser.py diff --git a/seewasser.py b/seewasser.py index 42c96f4..be5f149 100755 --- a/seewasser.py +++ b/seewasser.py @@ -1,42 +1,120 @@ #! /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 -jetzt = datetime.datetime.now() - -# print(jetzt) +#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 +#May 27 21:38 /sys/bus/w1/devices/28-0316a2193bff +#May 27 21:38 /sys/bus/w1/devices/28-0316a21383ff +# Eine Funktion: # einen Sensor auslesen #/sys/bus/w1/devices/ -sensorfile = "/sys/bus/w1/devices/28-0316a21383ff/w1_slave" -file = open(sensorfile) +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: + # 64 01 4b 46 7f ff 0c 10 01 : crc=01 YES + # 64 01 4b 46 7f ff 0c 10 01 t=22250 + + # Suche nach YES + linecrc = file.readline() + match = re.search(": crc=[0-9a-f]{2} (YES|NO)",linecrc) + + yesno = match.group(1) + if yesno != "YES": + raise ReadsensorError('Could not evaluate sensorfile "{}"'.format(sensorfile)) + + # Temperatur raus + linetemp = file.readline() + match = re.search(" t=([-0-9]+)",linetemp) + + temp_raw = match.group(1) + temp = float(temp_raw)/1000 + + file.close() + jetzt = datetime.datetime.now() + + return jetzt, temp_raw, temp + + +def writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value): + # Schreiben des csv-files + 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): + 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): + # 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) + 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() -# Inhalt des Sensors: -# 64 01 4b 46 7f ff 0c 10 01 : crc=01 YES -# 64 01 4b 46 7f ff 0c 10 01 t=22250 -# Suche nach YES -linecrc = file.readline() -match = re.search(": crc=[0-9a-f]{2} (YES|NO)",linecrc) +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) -yesno = match.group(1) -if yesno != "YES": - sys.exit(1) -# Temperatur raus -linetemp = file.readline() -match = re.search(" t=([-0-9]+)",linetemp) +def main(configfile, fromcsvfile): + config = configparser.ConfigParser() + config.read(configfile) + sensors = config.items('temperature') + value_type = "Wassertemperatur" -temp = match.group(1) -temp = float(temp)/1000 + if fromcsvfile: + process_csv_file(config, fromcsvfile, value_type, sensors) + return -file.close() + 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)) -print(temp) +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) -# Nur für die Ausgabe wird gerundet -print('Die Seetemperatur ist {:.1f}°C'.format(temp)) \ No newline at end of file