Start to use sqlalchemy (not all API calls work).
[chrisu/seepark.git] / web / seepark_web.py
1 from random import uniform
2 import datetime
3 import time
4 import configparser
5 import os
6 import sys
7 from collections import defaultdict
8 from flask import Flask, render_template, jsonify, request
9 import flask.json
10 from flask_sqlalchemy import SQLAlchemy
11
12
13 sys.path.append('..')
14 from seeparklib.openweathermap import openweathermap_json, OpenWeatherMapError
15
16
17 class JSONEncoder(flask.json.JSONEncoder):
18     def default(self, object):
19         if isinstance(object, datetime.datetime):
20             return object.isoformat()
21         return super().default(object)
22
23
24 def parse_datetime(date_str):
25     return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S')
26
27
28 def get_sqlalchemy_database_uri(config):
29     user = config.get('database', 'user')
30     pwd = config.get('database', 'password')
31     host = config.get('database', 'hostname')
32     db = config.get('database', 'database')
33     return 'mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db)
34
35
36 config = configparser.ConfigParser()
37 config.read(os.environ['SEEPARKINI'])
38 apikey = config.get('openweathermap', 'apikey')
39 cityid = config.get('openweathermap', 'cityid')
40
41 app = Flask(__name__)
42 app.json_encoder = JSONEncoder
43 app.config['SQLALCHEMY_DATABASE_URI'] = get_sqlalchemy_database_uri(config)
44 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
45 db = SQLAlchemy(app)
46 db.reflect(app=app)
47
48
49 class Sensors(db.Model):
50     __tablename__ = 'sensors'
51
52
53 def select_sensordata(initial_where):
54     query = Sensors.query.filter(initial_where)
55     begin = request.args.get('begin', None, parse_datetime)
56     end = request.args.get('end', None, parse_datetime)
57     if begin is not None:
58         query = query.filter(Sensors.timestamp >= begin)
59     if end is not None:
60         query = query.filter(Sensors.timestamp <= end)
61     result = query.all()
62
63     mode = request.args.get('mode', 'full')
64     if mode == 'consolidated':
65         if begin is None or end is None:
66             pass
67         else:
68             # copied from munin/master/_bin/munin-cgi-graph.in
69             resolutions = dict(
70                 day   =   300,
71                 week  =  1800,
72                 month =  7200,
73                 year  = 86400,
74             )
75             duration = (end - begin).total_seconds()
76             day = 60 * 60 * 24
77             if duration < day:
78                 resolution = resolutions['day']
79             elif duration < 7 * day:
80                 resolution = resolutions['week']
81             elif duration < 31 * day:
82                 resolution = resolutions['month']
83             else:
84                 resolution = resolutions['year']
85             # TODO: filter out samples from 'result'
86             # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
87
88     format = request.args.get('format', 'default')
89     if format == 'c3':
90         c3result = defaultdict(list)
91         for row in result:
92             c3result[row.sensor_id].append(row.value)
93             dt = row.timestamp.strftime('%Y-%m-%d %H:%M:%S')
94             c3result[row.sensor_id + '_x'].append(dt)
95         result = c3result
96     return result
97
98
99 def currentairtemperature(apikey, cityid):
100     """Retruns the tuple temperature, datetime (as float, datetime) in case of success, otherwise None, None."""
101     try:
102         weatherdata = openweathermap_json(apikey, cityid)
103         return weatherdata['main']['temp'], datetime.datetime.fromtimestamp(weatherdata['dt'])
104     except OpenWeatherMapError:
105         return None, None
106
107
108 def currentwatertemperature(sensorid):
109     result = Sensors.query.filter_by(sensor_id=sensorid).order_by(Sensors.timestamp.desc()).first()
110     return result.value, result.timestamp
111
112
113 @app.route('/api/<version>/sensors/')
114 def sensors(version):
115     """List all sensors found in the database"""
116     result = db.session.query(Sensors.sensor_id, Sensors.sensor_name, Sensors.value_type).distinct().all()
117     return jsonify(result)
118
119
120 @app.route('/api/<version>/sensor/id/<sensor_id>')
121 def sensorid(version, sensor_id):
122     """Return all data for a specific sensor
123
124     URL parameters:
125     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
126     end=<datetime>, optional, format like "2018-05-19T21:07:53"
127     mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
128     format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
129     """
130     result = select_sensordata(Sensors.sensor_id == sensor_id)
131     return jsonify([row._asdict() for row in result])
132
133
134 @app.route('/api/<version>/sensor/type/<sensor_type>')
135 def sensortype(version, sensor_type):
136     """Return all data for a specific sensor type
137
138     URL parameters:
139     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
140     end=<datetime>, optional, format like "2018-05-19T21:07:53"
141     mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
142     format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
143     """
144     result = select_sensordata(Sensors.value_type == sensor_type)
145     return jsonify(result)
146
147
148 @app.route('/data/', defaults={'timespan': 1})
149 @app.route("/data/<int:timespan>", methods=['GET'])
150 def data(timespan):
151     granularity = 5 * timespan               # (every) minute(s) per day
152     samples = 60/granularity * 24 * timespan # per hour over whole timespan
153     s4m   = []
154     s4m_x = []
155     s5m   = []
156     s5m_x = []
157     end   = time.time()
158     start = end - samples * granularity * 60
159
160     for i in range(int(samples)):
161         s4m.append(uniform(-10,30))
162         s5m.append(uniform(-10,30))
163         s4mt = uniform(start, end)
164         s5mt = uniform(start, end)
165         s4m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s4mt)))
166         s5m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s5mt)))
167
168     data = {
169         '0316a2193bff':   s4m,
170         '0316a2193bff_x': s4m_x,
171         '0316a21383ff':   s5m,
172         '0316a21383ff_x': s5m_x,
173         }
174
175     return jsonify(data)
176
177
178 @app.route("/")
179 def index():
180     airvalue, airtime     = currentairtemperature(apikey, cityid)
181     watervalue, watertime = currentwatertemperature('0316a21383ff') # config? mainwatertemp?
182
183     return render_template(
184         'seepark_web.html',
185         apikey=apikey,
186         watervalue=watervalue,
187         watertime=watertime,
188         airvalue=airvalue,
189         airtime=airtime,
190     )