add an openweathermap widget to the webpage
[chrisu/seepark.git] / web / seepark_web.py
1 from flask import Flask, render_template, jsonify
2 from random import uniform
3 import time
4 import configparser
5 import os
6 app = Flask(__name__)
7
8 config = configparser.ConfigParser()
9 config.read(os.environ['SEEPARKINI'])
10 apikey = config.get('openweathermap', 'apikey');
11
12 @app.route('/data/', defaults={'timespan': 1})
13 @app.route("/data/<int:timespan>", methods=['GET'])
14 def data(timespan):
15
16     granularity = 5 * timespan               # (every) minute(s) per day
17     samples = 60/granularity * 24 * timespan # per hour over whole timespan
18     s4m   = []
19     s4m_x = []
20     s5m   = []
21     s5m_x = []
22     end   = time.time()
23     start = end - samples * granularity * 60
24
25     for i in range(int(samples)):
26         s4m.append(uniform(-10,30))
27         s5m.append(uniform(-10,30))
28         s4mt = uniform(start, end)
29         s5mt = uniform(start, end)
30         s4m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s4mt)))
31         s5m_x.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(s5mt)))
32
33     data = {
34         '0316a2193bff':   s4m,
35         '0316a2193bff_x': s4m_x,
36         '0316a21383ff':   s5m,
37         '0316a21383ff_x': s5m_x,
38         }
39
40     return jsonify(data)
41
42
43 @app.route("/")
44 def index():
45     return render_template('seepark_web.html', apikey=apikey)
46