sensor API: rename '/api/<version>/sensor/<sensor_id>'
[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
10
11 class JSONEncoder(flask.json.JSONEncoder):
12     def default(self, object):
13         if isinstance(object, datetime.datetime):
14             return object.isoformat()
15         return super().default(object)
16
17
18 def parse_datetime(date_str):
19     return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S')
20
21
22 app = Flask(__name__)
23 app.json_encoder = JSONEncoder
24 config = configparser.ConfigParser()
25 config.read(os.environ['SEEPARKINI'])
26 apikey = config.get('openweathermap', 'apikey')
27
28
29 def open_engine(config):
30     user = config.get('database', 'user')
31     pwd = config.get('database', 'password')
32     host = config.get('database', 'hostname')
33     db = config.get('database', 'database')
34     engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
35     return engine
36
37
38 def select_sensordata(initial_where, initial_sql_args):
39     engine = open_engine(config)
40     with engine.connect() as conn:
41         where = [initial_where]
42         sql_args = [initial_sql_args]
43         begin = None
44         end = None
45         if 'begin' in request.args:
46             where.append('timestamp>=%s')
47             begin = request.args.get('begin', None, parse_datetime)
48             sql_args.append(begin)
49         if 'end' in request.args:
50             where.append('timestamp<=%s')
51             end = request.args.get('end', None, parse_datetime)
52             sql_args.append(end)
53         sql = 'select * from sensors where {} order by id'.format(' and '.join(where))
54         cursor = conn.execute(sql, *sql_args)
55         result = [dict(row) for row in cursor]
56
57         mode = request.args.get('mode', 'full')
58         if mode == 'consolidated':
59             if begin is None or end is None:
60                 pass
61             else:
62                 # copied from munin/master/_bin/munin-cgi-graph.in
63                 resolutions = dict(
64                     day   =   300,
65                     week  =  1800,
66                     month =  7200,
67                     year  = 86400,
68                 )
69                 duration = (end - begin).total_seconds()
70                 day = 60 * 60 * 24
71                 if duration < day:
72                     resolution = resolutions['day']
73                 elif duration < 7 * day:
74                     resolution = resolutions['week']
75                 elif duration < 31 * day:
76                     resolution = resolutions['month']
77                 else:
78                     resolution = resolutions['year']
79                 # TODO: filter out samples from 'result'
80                 # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
81
82     return result
83
84
85 @app.route('/api/<version>/sensors/')
86 def sensors(version):
87     """List all sensors found in the database"""
88     engine = open_engine(config)
89     with engine.connect() as conn:
90         cursor = conn.execute('select distinct sensor_id, sensor_name, value_type from sensors')
91         result = [dict(row) for row in cursor]
92         return jsonify(result)
93
94
95 @app.route('/api/<version>/sensor/id/<sensor_id>')
96 def sensorid(version, sensor_id):
97     """Return all data for a specific sensor
98
99     URL parameters:
100     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
101     end=<datetime>, optional, format like "2018-05-19T21:07:53"
102     mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
103     """
104     result = select_sensordata('sensor_id=%s', sensor_id)
105     return jsonify(result)
106
107
108 @app.route('/api/<version>/sensor/type/<sensor_type>')
109 def sensortype(version, sensor_type):
110     """Return all data for a specific sensor type
111
112     URL parameters:
113     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
114     end=<datetime>, optional, format like "2018-05-19T21:07:53"
115     mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
116     """
117     result = select_sensordata('value_type=%s', sensor_type)
118     return jsonify(result)
119
120
121 @app.route('/data/', defaults={'timespan': 1})
122 @app.route("/data/<int:timespan>", methods=['GET'])
123 def data(timespan):
124     granularity = 5 * timespan               # (every) minute(s) per day
125     samples = 60/granularity * 24 * timespan # per hour over whole timespan
126     s4m   = []
127     s4m_x = []
128     s5m   = []
129     s5m_x = []
130     end   = time.time()
131     start = end - samples * granularity * 60
132
133     for i in range(int(samples)):
134         s4m.append(uniform(-10,30))
135         s5m.append(uniform(-10,30))
136         s4mt = uniform(start, end)
137         s5mt = uniform(start, end)
138         s4m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s4mt)))
139         s5m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s5mt)))
140
141     data = {
142         '0316a2193bff':   s4m,
143         '0316a2193bff_x': s4m_x,
144         '0316a21383ff':   s5m,
145         '0316a21383ff_x': s5m_x,
146         }
147
148     return jsonify(data)
149
150
151 @app.route("/")
152 def index():
153     return render_template('seepark_web.html', apikey=apikey)
154