Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- """
- from __future__ import division, print_function, unicode_literals
- from datetime import datetime
- import StringIO
- import base64
- from time import mktime
- from flask import Flask, render_template, send_from_directory, request
- from matplotlib.pyplot import figure, title, xlabel, ylabel, plot, legend, axis, savefig
- import os
- import matplotlib
- from scipy import arange
- matplotlib.use('Agg')
- import psycopg2
- # set HOME environment variable to a directory the httpd server can write to
- PWD = os.path.dirname(__file__)
- app = Flask(__name__)
- ######################################################################################
- conn = psycopg2.connect(host='localhost',
- user='postgres',
- password='postgres',
- database='hampl-pocasi')
- curs = conn.cursor()
- @app.route('/')
- def index():
- od, do = -1, -1
- if "od" in request.args and request.args["od"]:
- od = request.args["od"]
- od = datetime.strptime(od, "%Y-%m-%d")
- od = mktime(od.timetuple())
- if "do" in request.args and request.args["do"]:
- do = request.args["do"]
- do = datetime.strptime(do, "%Y-%m-%d")
- do = mktime(do.timetuple())
- if "do" not in request.args:
- request.args["do"] = ""
- if "od" not in request.args:
- request.args["od"] = ""
- return render_template('base1.html', img=image_gen(od, do), od=request.args["od"], do=request.args["do"])
- def image_gen(od=-1, do=-1):
- if od < 0 and do < 0:
- curs.execute(
- "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino ORDER BY cas ASC")
- elif od > 0 and do > 0:
- od = datetime.fromtimestamp(od)
- do = datetime.fromtimestamp(do)
- curs.execute(
- "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino WHERE cas > '{}' AND cas < '{}' ORDER BY cas ASC".format(
- str(od.year) + "-" + str(od.month) + "-" + str(od.day),
- str(do.year) + "-" + str(do.month) + "-" + str(do.day)))
- elif od > 0:
- od = datetime.fromtimestamp(od)
- curs.execute(
- "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino WHERE cas > '{}' ORDER BY cas ASC".format(
- str(od.year) + "-" + str(od.month) + "-" + str(od.day)))
- elif do > 0:
- do = datetime.fromtimestamp(do)
- curs.execute(
- "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino WHERE cas > '{}' ORDER BY cas ASC".format(
- str(do.year) + "-" + str(do.month) + "-" + str(do.day)))
- print('*'*20)
- print(curs.query)
- print('*'*20)
- data = curs.fetchall()
- t = arange(0, len(data), 1)
- tep = list()
- vlh = list()
- teplotavenku = list()
- vlhkostvenku = list()
- for d in data:
- tep.append(d[0])
- vlh.append(d[1])
- teplotavenku.append(d[3])
- vlhkostvenku.append(d[4])
- fig = figure()
- graf = fig.add_subplot(111)
- graf.grid(True)
- title("Teplota a vlhkost v čase")
- xlabel("t")
- ylabel("Teplota[°C], Vlhkost[%], Teplota venku[°C], Vlhkost venku[%]")
- plot(t, tep, label="teplota")
- plot(t, vlh, label="vlhkost", linewidth=2)
- plot(t, teplotavenku, label="teplota venku")
- plot(t, vlhkostvenku, label="vlhkost venku")
- if data:
- axis([0, len(data), min(min(tep), min(vlh), min(teplotavenku), min(vlhkostvenku)) - 5,
- max(max(tep), max(vlh), max(teplotavenku), max(vlhkostvenku)) + 20])
- legend()
- output = StringIO.StringIO()
- savefig(output)
- r = base64.b64encode(output.getvalue())
- output.close()
- return r
- @app.route('/tmp/<path:filename>')
- def static_data(filename):
- return send_from_directory('tmp', filename)
- ############################################################################
- if __name__ == '__main__':
- app.run(host='127.0.0.1', port=8080, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement