Shortened readcsvfile.
[chrisu/seepark.git] / seewasser.py
1 #! /usr/bin/python3
2
3 import argparse
4 import logging
5 import datetime
6 import re
7 import sys
8 import csv
9 import os
10 import configparser
11 from sqlalchemy import create_engine, exc
12 import warnings
13 import MySQLdb.cursors
14
15 #May 27 21:32 /sys/bus/w1/devices/28-0416a1bab9ff
16 #May 27 21:33 /sys/bus/w1/devices/28-0416a1ac66ff
17 #May 27 21:35 /sys/bus/w1/devices/28-0516a207a4ff
18 #May 27 21:38 /sys/bus/w1/devices/28-0316a2193bff
19 #May 27 21:38 /sys/bus/w1/devices/28-0316a21383ff
20
21 # Eine Funktion:
22 # einen Sensor auslesen
23 #/sys/bus/w1/devices/
24
25 class ReadsensorError(RuntimeError):
26     pass
27     
28     
29 def readsensor(sensor_id):
30     sensorfile = "/sys/bus/w1/devices/28-{}/w1_slave".format(sensor_id)
31     file = open(sensorfile)
32
33     # Inhalt des Sensors:
34     # 64 01 4b 46 7f ff 0c 10 01 : crc=01 YES
35     # 64 01 4b 46 7f ff 0c 10 01 t=22250
36
37     # Suche nach YES
38     linecrc = file.readline()
39     match = re.search(": crc=[0-9a-f]{2} (YES|NO)",linecrc)
40
41     yesno = match.group(1)
42     if yesno != "YES":
43         raise ReadsensorError('Could not evaluate sensorfile "{}"'.format(sensorfile))
44         
45     # Temperatur raus
46     linetemp = file.readline()
47     match = re.search(" t=([-0-9]+)",linetemp)
48
49     temp_raw = match.group(1)
50     temp = float(temp_raw)/1000
51
52     file.close()
53     jetzt = datetime.datetime.now()
54
55     return jetzt, temp_raw, temp
56
57
58 def writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
59     # Schreiben des csv-files
60     with open(os.path.expanduser(config.get("csv", "filename")), "a", newline = "") as file:
61         writer = csv.writer(file, dialect = "excel")
62         writer.writerow([timestamp.strftime("%Y-%m-%d %H:%M"), sensor_id, sensor_name, "{:.1f}".format(value)])
63
64
65 def readcsvfile(csvfile):
66     with open(csvfile, "r", newline="") as file:
67         yield from csv.DictReader(file, dialect = "excel", fieldnames=("timestamp", "sensor_id", "sensor_name", "value"))
68
69
70 def writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
71     # Schreiben des db-files
72     user = config.get('database', 'user')
73     pwd = config.get('database','password')
74     host = config.get('database','hostname')
75     db = config.get('database','database')
76     
77     engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
78     conn = engine.connect()
79     with warnings.catch_warnings():
80         # ignore _mysql_exceptions.IntegrityError: (1062, "Duplicate entry '0316a2193bff-2018-08-29 14:30:00' for key 'sensorid_timestamp'")
81         # TODO: the following doesn't work
82         warnings.simplefilter("ignore", category=MySQLdb.cursors.Warning)
83         # TODO: this does but …?!
84         try:
85             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)
86         except exc.IntegrityError as e:
87             if "1062" in str(e):
88                 pass
89             else:
90                 raise
91     conn.close()
92
93
94 def main(configfile, fromcsvfile):
95     config = configparser.ConfigParser()
96     config.read(configfile)
97     
98     value_type = "Wassertemperatur"
99     for sensor_id, sensor_name in config.items('temperature'):
100         if fromcsvfile:
101             # "timestamp", "sensor_id", "sensor_name", "value"
102             for record in readcsvfile(fromcsvfile):
103                 timestamp, sensor_id, sensor_name, value = (record["timestamp"], record["sensor_id"], record["sensor_name"], float(record["value"]))
104                 value_raw = None
105                 writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
106         else:
107             timestamp, value_raw, value = readsensor(sensor_id)
108             writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
109             writesensordatadb(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value)
110         logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value))
111
112
113 if __name__ == '__main__':
114     default_config_file = os.path.expanduser('~/seewasser.ini')
115     parser = argparse.ArgumentParser(description='Read sensor data')
116     parser.add_argument('--config', default=default_config_file, help='configuration file')
117     parser.add_argument('--fromcsvfile', help='write values from csv file to database')
118     args = parser.parse_args()
119     main(args.config, args.fromcsvfile)
120