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