1 from random import uniform
7 from collections import defaultdict
8 from flask import Flask, render_template, jsonify, request
10 from sqlalchemy import create_engine
14 from seeparklib.openweathermap import openweathermap_json, OpenWeatherMapError
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)
24 def parse_datetime(date_str):
25 return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S')
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')
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)
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]
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)
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]
64 mode = request.args.get('mode', 'full')
65 if mode == 'consolidated':
66 if begin is None or end is None:
69 # copied from munin/master/_bin/munin-cgi-graph.in
76 duration = (end - begin).total_seconds()
79 resolution = resolutions['day']
80 elif duration < 7 * day:
81 resolution = resolutions['week']
82 elif duration < 31 * day:
83 resolution = resolutions['month']
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
89 format = request.args.get('format', 'default')
91 c3result = defaultdict(list)
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)
100 def currentairtemperature(apikey, cityid):
101 """Retruns the tuple temperature, datetime (as float, datetime) in case of success, otherwise None, None."""
103 weatherdata = openweathermap_json(apikey, cityid)
104 return weatherdata['main']['temp'], datetime.datetime.fromtimestamp(weatherdata['dt'])
105 except OpenWeatherMapError:
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']
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)
127 @app.route('/api/<version>/sensor/id/<sensor_id>')
128 def sensorid(version, sensor_id):
129 """Return all data for a specific sensor
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
137 result = select_sensordata('sensor_id=%s', sensor_id)
138 return jsonify(result)
141 @app.route('/api/<version>/sensor/type/<sensor_type>')
142 def sensortype(version, sensor_type):
143 """Return all data for a specific sensor type
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
151 result = select_sensordata('value_type=%s', sensor_type)
152 return jsonify(result)
155 @app.route('/data/', defaults={'timespan': 1})
156 @app.route("/data/<int:timespan>", methods=['GET'])
158 granularity = 5 * timespan # (every) minute(s) per day
159 samples = 60/granularity * 24 * timespan # per hour over whole timespan
165 start = end - samples * granularity * 60
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)))
177 '0316a2193bff_x': s4m_x,
179 '0316a21383ff_x': s5m_x,
187 airvalue, airtime = currentairtemperature(apikey, cityid)
188 watervalue, watertime = currentwatertemperature('0316a21383ff') # config? mainwatertemp?
190 return render_template(
193 watervalue=watervalue,