Use defaultdict to simplify code.
[chrisu/seepark.git] / web / seepark_web.py
index 74daa2fdc248ecb7625818ad22dd01b2fa0be295..95c3f9dea734df366631f66bc8d3f1b12eb2aa01 100644 (file)
-from flask import Flask, render_template, jsonify
+from random import uniform
+import datetime
+import time
+import configparser
+import os
+import sys
+from collections import defaultdict
+from flask import Flask, render_template, jsonify, request
+import flask.json
+from sqlalchemy import create_engine
+import requests
+
+sys.path.append('..')
+from seeparklib.openweathermap import openweathermap_json, OpenWeatherMapError
+
+
+class JSONEncoder(flask.json.JSONEncoder):
+    def default(self, object):
+        if isinstance(object, datetime.datetime):
+            return object.isoformat()
+        return super().default(object)
+
+
+def parse_datetime(date_str):
+    return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S')
+
+
 app = Flask(__name__)
+app.json_encoder = JSONEncoder
+config = configparser.ConfigParser()
+config.read(os.environ['SEEPARKINI'])
+apikey = config.get('openweathermap', 'apikey')
+cityid = config.get('openweathermap', 'cityid')
+
+
+def open_engine(config):
+    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)
+    return engine
+
+
+def select_sensordata(initial_where, initial_sql_args):
+    engine = open_engine(config)
+    with engine.connect() as conn:
+        where = [initial_where]
+        sql_args = [initial_sql_args]
+        begin = None
+        end = None
+        if 'begin' in request.args:
+            where.append('timestamp>=%s')
+            begin = request.args.get('begin', None, parse_datetime)
+            sql_args.append(begin)
+        if 'end' in request.args:
+            where.append('timestamp<=%s')
+            end = request.args.get('end', None, parse_datetime)
+            sql_args.append(end)
+        sql = 'select * from sensors where {} order by id'.format(' and '.join(where))
+        cursor = conn.execute(sql, *sql_args)
+        result = [dict(row) for row in cursor]
+
+    mode = request.args.get('mode', 'full')
+    if mode == 'consolidated':
+        if begin is None or end is None:
+            pass
+        else:
+            # copied from munin/master/_bin/munin-cgi-graph.in
+            resolutions = dict(
+                day   =   300,
+                week  =  1800,
+                month =  7200,
+                year  = 86400,
+            )
+            duration = (end - begin).total_seconds()
+            day = 60 * 60 * 24
+            if duration < day:
+                resolution = resolutions['day']
+            elif duration < 7 * day:
+                resolution = resolutions['week']
+            elif duration < 31 * day:
+                resolution = resolutions['month']
+            else:
+                resolution = resolutions['year']
+            # TODO: filter out samples from 'result'
+            # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
+
+    format = request.args.get('format', 'default')
+    if format == 'c3':
+        c3result = defaultdict(list)
+        for row in result:
+            c3result[row['sensor_id']].append(row['value'])
+            dt = row['timestamp'].strftime('%Y-%m-%d %H:%M:%S')
+            c3result[row['sensor_id'] + '_x'].append(dt)
+        result = c3result
+    return result
+
+
+def currentairtemperature(apikey, cityid):
+    """Retruns the tuple temperature, datetime (as float, datetime) in case of success, otherwise None, None."""
+    try:
+        weatherdata = openweathermap_json(apikey, cityid)
+        return weatherdata['main']['temp'], datetime.datetime.fromtimestamp(weatherdata['dt'])
+    except OpenWeatherMapError:
+        return None, None
+
+
+def currentwatertemperature(sensorid):
+    engine = open_engine(config)
+    with engine.connect() as conn:
+        cursor = conn.execute('select value, timestamp from sensors where sensor_id=%s order by timestamp desc limit 1', sensorid)
+        result = [dict(row) for row in cursor]
+        return result[0]['value'], result[0]['timestamp']
+
+
+@app.route('/api/<version>/sensors/')
+def sensors(version):
+    """List all sensors found in the database"""
+    engine = open_engine(config)
+    with engine.connect() as conn:
+        cursor = conn.execute('select distinct sensor_id, sensor_name, value_type from sensors')
+        result = [dict(row) for row in cursor]
+        return jsonify(result)
+
+
+@app.route('/api/<version>/sensor/id/<sensor_id>')
+def sensorid(version, sensor_id):
+    """Return all data for a specific sensor
+
+    URL parameters:
+    begin=<datetime>, optional, format like "2018-05-19T21:07:53"
+    end=<datetime>, optional, format like "2018-05-19T21:07:53"
+    mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
+    format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
+    """
+    result = select_sensordata('sensor_id=%s', sensor_id)
+    return jsonify(result)
+
+
+@app.route('/api/<version>/sensor/type/<sensor_type>')
+def sensortype(version, sensor_type):
+    """Return all data for a specific sensor type
+
+    URL parameters:
+    begin=<datetime>, optional, format like "2018-05-19T21:07:53"
+    end=<datetime>, optional, format like "2018-05-19T21:07:53"
+    mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
+    format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
+    """
+    result = select_sensordata('value_type=%s', sensor_type)
+    return jsonify(result)
+
+
+@app.route('/data/', defaults={'timespan': 1})
+@app.route("/data/<int:timespan>", methods=['GET'])
+def data(timespan):
+    granularity = 5 * timespan               # (every) minute(s) per day
+    samples = 60/granularity * 24 * timespan # per hour over whole timespan
+    s4m   = []
+    s4m_x = []
+    s5m   = []
+    s5m_x = []
+    end   = time.time()
+    start = end - samples * granularity * 60
+
+    for i in range(int(samples)):
+        s4m.append(uniform(-10,30))
+        s5m.append(uniform(-10,30))
+        s4mt = uniform(start, end)
+        s5mt = uniform(start, end)
+        s4m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s4mt)))
+        s5m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s5mt)))
 
-@app.route("/data")
-def data():
     data = {
-        'water_temperature': [20, 19, 20, 21, 20, 21],
-        'water_temperature_x': ['2018-05-20', '2018-05-21', '2018-05-22', '2018-05-23', '2018-05-24', '2018-05-27'],
-        'air_temperature': [27, 32, 26, 29, 30, 31],
-        'air_temperature_x': ['2018-05-21', '2018-05-22', '2018-05-23', '2018-05-24', '2018-05-25', '2018-05-27'],
+        '0316a2193bff':   s4m,
+        '0316a2193bff_x': s4m_x,
+        '0316a21383ff':   s5m,
+        '0316a21383ff_x': s5m_x,
         }
+
     return jsonify(data)
 
 
 @app.route("/")
-def hello():
-    return render_template('seepark_web.html')
+def index():
+    airvalue, airtime     = currentairtemperature(apikey, cityid)
+    watervalue, watertime = currentwatertemperature('0316a21383ff') # config? mainwatertemp?
 
+    return render_template(
+        'seepark_web.html',
+        apikey=apikey,
+        watervalue=watervalue,
+        watertime=watertime,
+        airvalue=airvalue,
+        airtime=airtime,
+    )