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