X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/blobdiff_plain/bd153e1ffc7014a5b11ba3dd42c19e0fdfa739ce..5ec23d237a6f87fe54a47f04c9a27fe157f59c40:/web/seepark_web.py?ds=inline diff --git a/web/seepark_web.py b/web/seepark_web.py index 68929fc..ce4347f 100644 --- a/web/seepark_web.py +++ b/web/seepark_web.py @@ -6,6 +6,7 @@ import os from flask import Flask, render_template, jsonify, request import flask.json from sqlalchemy import create_engine +import requests class JSONEncoder(flask.json.JSONEncoder): @@ -24,6 +25,7 @@ app.json_encoder = JSONEncoder config = configparser.ConfigParser() config.read(os.environ['SEEPARKINI']) apikey = config.get('openweathermap', 'apikey') +cityid = config.get('openweathermap', 'cityid') def open_engine(config): @@ -54,34 +56,69 @@ def select_sensordata(initial_where, initial_sql_args): cursor = conn.execute(sql, *sql_args) result = [dict(row) for row in cursor] - mode = request.args.get('mode', 'full') - if mode == 'consolidated': - if begin is None or end is None: - pass + mode = request.args.get('mode', 'full') + if mode == 'consolidated': + if begin is None or end is None: + pass + else: + # copied from munin/master/_bin/munin-cgi-graph.in + resolutions = dict( + day = 300, + week = 1800, + month = 7200, + year = 86400, + ) + duration = (end - begin).total_seconds() + day = 60 * 60 * 24 + if duration < day: + resolution = resolutions['day'] + elif duration < 7 * day: + resolution = resolutions['week'] + elif duration < 31 * day: + resolution = resolutions['month'] else: - # copied from munin/master/_bin/munin-cgi-graph.in - resolutions = dict( - day = 300, - week = 1800, - month = 7200, - year = 86400, - ) - duration = (end - begin).total_seconds() - day = 60 * 60 * 24 - if duration < day: - resolution = resolutions['day'] - elif duration < 7 * day: - resolution = resolutions['week'] - elif duration < 31 * day: - resolution = resolutions['month'] - else: - resolution = resolutions['year'] - # TODO: filter out samples from 'result' - # like loop over results and skip if timestamp(n+1)-timestamp(n)/sensors/') def sensors(version): """List all sensors found in the database""" @@ -100,6 +137,7 @@ def sensorid(version, sensor_id): begin=, optional, format like "2018-05-19T21:07:53" end=, optional, format like "2018-05-19T21:07:53" mode=, optional. return all rows (default) or with lower resolution (for charts) + format=, optional. return result as returned by sqlalchemy (default) or formatted for c3.js """ result = select_sensordata('sensor_id=%s', sensor_id) return jsonify(result) @@ -113,6 +151,7 @@ def sensortype(version, sensor_type): begin=, optional, format like "2018-05-19T21:07:53" end=, optional, format like "2018-05-19T21:07:53" mode=, optional. return all rows (default) or with lower resolution (for charts) + format=, optional. return result as returned by sqlalchemy (default) or formatted for c3.js """ result = select_sensordata('value_type=%s', sensor_type) return jsonify(result) @@ -150,5 +189,16 @@ def data(timespan): @app.route("/") def index(): - return render_template('seepark_web.html', apikey=apikey) - + airvalue, airtime = currentairtemperature(apikey, cityid) + airvalue = "{:.1f}".format(airvalue) if isinstance(airvalue, float) else airvalue + watervalue, watertime = currentwatertemperature('0316a21383ff') # config? mainwatertemp? + watervalue = "{:.1f}".format(watervalue) if isinstance(watervalue, float) else watervalue + + return render_template( + 'seepark_web.html', + apikey=apikey, + watervalue=watervalue, + watertime=watertime, + airvalue=airvalue, + airtime=airtime, + )