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