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