Advertisement
Guest User

prasíme - hampl

a guest
Mar 30th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. """
  4. from __future__ import division, print_function, unicode_literals
  5. from datetime import datetime
  6. import StringIO
  7. import base64
  8. from time import mktime
  9.  
  10. from flask import Flask, render_template, send_from_directory, request
  11. from matplotlib.pyplot import figure, title, xlabel, ylabel, plot, legend, axis, savefig
  12. import os
  13. import matplotlib
  14. from scipy import arange
  15.  
  16.  
  17. matplotlib.use('Agg')
  18. import psycopg2
  19.  
  20. # set HOME environment variable to a directory the httpd server can write to
  21. PWD = os.path.dirname(__file__)
  22. app = Flask(__name__)
  23. ######################################################################################
  24. conn = psycopg2.connect(host='localhost',
  25.                         user='postgres',
  26.                         password='postgres',
  27.                         database='hampl-pocasi')
  28. curs = conn.cursor()
  29.  
  30.  
  31. @app.route('/')
  32. def index():
  33.     od, do = -1, -1
  34.     if "od" in request.args and request.args["od"]:
  35.         od = request.args["od"]
  36.         od = datetime.strptime(od, "%Y-%m-%d")
  37.         od = mktime(od.timetuple())
  38.  
  39.     if "do" in request.args and request.args["do"]:
  40.         do = request.args["do"]
  41.         do = datetime.strptime(do, "%Y-%m-%d")
  42.         do = mktime(do.timetuple())
  43.  
  44.     if "do" not in request.args:
  45.         request.args["do"] = ""
  46.  
  47.     if "od" not in request.args:
  48.         request.args["od"] = ""
  49.  
  50.     return render_template('base1.html', img=image_gen(od, do), od=request.args["od"], do=request.args["do"])
  51.  
  52.  
  53. def image_gen(od=-1, do=-1):
  54.     if od < 0 and do < 0:
  55.         curs.execute(
  56.             "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino ORDER BY cas ASC")
  57.     elif od > 0 and do > 0:
  58.         od = datetime.fromtimestamp(od)
  59.         do = datetime.fromtimestamp(do)
  60.         curs.execute(
  61.             "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino WHERE cas > '{}' AND cas < '{}' ORDER BY cas ASC".format(
  62.                 str(od.year) + "-" + str(od.month) + "-" + str(od.day),
  63.                 str(do.year) + "-" + str(do.month) + "-" + str(do.day)))
  64.     elif od > 0:
  65.         od = datetime.fromtimestamp(od)
  66.         curs.execute(
  67.             "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino WHERE cas > '{}' ORDER BY cas ASC".format(
  68.                 str(od.year) + "-" + str(od.month) + "-" + str(od.day)))
  69.     elif do > 0:
  70.         do = datetime.fromtimestamp(do)
  71.         curs.execute(
  72.             "SELECT teplota, vlhkost, cas, vlhkostvenku, teplotavenku FROM arduino WHERE cas > '{}' ORDER BY cas ASC".format(
  73.                 str(do.year) + "-" + str(do.month) + "-" + str(do.day)))
  74.  
  75.  
  76.  
  77.     print('*'*20)
  78.     print(curs.query)
  79.     print('*'*20)
  80.     data = curs.fetchall()
  81.     t = arange(0, len(data), 1)
  82.  
  83.     tep = list()
  84.     vlh = list()
  85.     teplotavenku = list()
  86.     vlhkostvenku = list()
  87.     for d in data:
  88.         tep.append(d[0])
  89.         vlh.append(d[1])
  90.         teplotavenku.append(d[3])
  91.         vlhkostvenku.append(d[4])
  92.  
  93.     fig = figure()
  94.     graf = fig.add_subplot(111)
  95.  
  96.     graf.grid(True)
  97.     title("Teplota a vlhkost v čase")
  98.  
  99.     xlabel("t")
  100.     ylabel("Teplota[°C], Vlhkost[%], Teplota venku[°C], Vlhkost venku[%]")
  101.  
  102.     plot(t, tep, label="teplota")
  103.     plot(t, vlh, label="vlhkost", linewidth=2)
  104.     plot(t, teplotavenku, label="teplota venku")
  105.     plot(t, vlhkostvenku, label="vlhkost venku")
  106.     if data:
  107.         axis([0, len(data), min(min(tep), min(vlh), min(teplotavenku), min(vlhkostvenku)) - 5,
  108.               max(max(tep), max(vlh), max(teplotavenku), max(vlhkostvenku)) + 20])
  109.     legend()
  110.     output = StringIO.StringIO()
  111.     savefig(output)
  112.     r = base64.b64encode(output.getvalue())
  113.     output.close()
  114.     return r
  115.  
  116.  
  117. @app.route('/tmp/<path:filename>')
  118. def static_data(filename):
  119.     return send_from_directory('tmp', filename)
  120.  
  121.  
  122. ############################################################################
  123.  
  124. if __name__ == '__main__':
  125.     app.run(host='127.0.0.1', port=8080, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement