brown paper bag commit: always test after changing variable names
[chrisu/seepark.git] / seewasser.py
index e573e812542194029e45c839948f75da823a2b91..724beb694fef691592b7a60bb370b9d898102eb1 100755 (executable)
@@ -1,11 +1,13 @@
 #! /usr/bin/python3
 
+import logging
 import datetime
 import re
 import sys
 import csv
 import os
-import glob
+import configparser
+from sqlalchemy import create_engine
 
 #May 27 21:32 /sys/bus/w1/devices/28-0416a1bab9ff
 #May 27 21:33 /sys/bus/w1/devices/28-0416a1ac66ff
@@ -17,8 +19,12 @@ import glob
 # einen Sensor auslesen
 #/sys/bus/w1/devices/
 
-def readsensor(sensorID):
-    sensorfile = "/sys/bus/w1/devices/28-{}/w1_slave".format(sensorID)
+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:
@@ -31,37 +37,53 @@ def readsensor(sensorID):
 
     yesno = match.group(1)
     if yesno != "YES":
-        sys.exit(1)
-
+        raise ReadsensorError('Could not evaluate sensorfile "{}"'.format(sensorfile))
+        
     # Temperatur raus
     linetemp = file.readline()
     match = re.search(" t=([-0-9]+)",linetemp)
 
-    temp = match.group(1)
-    temp = float(temp)/1000
+    temp_raw = match.group(1)
+    temp = float(temp_raw)/1000
 
     file.close()
-
     jetzt = datetime.datetime.now()
-    # print(jetzt)
 
+    return jetzt, temp_raw, temp
+
+
+def writesensordatacsv(config, sensor_id, sensor_name, timestamp, value_type, value_raw, value):
     # Schreiben des csv-files
-    file2 = open("badesaison.csv", "a", newline = "")
-    writer = csv.writer(file2, dialect = "excel")
-    writer.writerow([jetzt.strftime("%Y-%m-%d %H:%M"), sensorID, "{:.1f}".format(temp)])
+    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)])
 
-    file2.close()
+    file.close()
 
 
-    print(temp)
+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()
+    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)
+    conn.close()
 
-    # Nur für die Ausgabe wird gerundet
-    print('Die Seetemperatur ist {:.1f}°C'.format(temp))
+
+def main():
+    config = configparser.ConfigParser()
+    config.read(os.path.expanduser('~/seewasser.ini'))
     
-#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
+    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)
+        logging.info('Sensor {}: {:.1f}°C'.format(sensor_id, value))
+
 
-readsensor("0416a1bab9ff")
\ No newline at end of file
+main()