Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- import sys
- import psycopg2
- import string
- import datetime
- from decimal import Decimal
- import time
- from time import gmtime, strftime
- import os
- from bokeh.plotting import figure, show, output_file
- from bokeh.models import PrintfTickFormatter
- #Odpri povezavo z bazo
- db_con = psycopg2.connect(database='senzor', host='xxxxxxx', user='xxx', password='xxx')
- cursor = db_con.cursor()
- query = "set timezone to 'Europe/Ljubljana';"
- cursor.execute(query)
- db_con.commit()
- query = """
- select date_trunc('hour', datumcas) as interv_start, date_trunc('hour', datumcas) + interval '1 hours' as interv_end, avg(temperatura)
- from temperatura
- where (datumcas >= current_date and datumcas < (current_date + interval '1 day')) group by date_trunc('hour', datumcas)
- order by interv_start;"""
- cursor.execute(query)
- db_con.commit()
- avg_hourly_temp = cursor.fetchall()
- # Red sensor:
- query = """
- select date_trunc('hour', datumcas) as interv_start, date_trunc('hour', datumcas) + interval '1 hours' as interv_end, avg(temperatura)
- from temperatura
- where (datumcas >= current_date and datumcas < (current_date + interval '1 day')) and (ime_senzorja = 'rdeči') group by date_trunc('hour', datumcas)
- order by interv_start;"""
- cursor.execute(query)
- db_con.commit()
- avg_hourly_temp_red = cursor.fetchall()
- # Yellow sensor:
- query = """
- select date_trunc('hour', datumcas) as interv_start, date_trunc('hour', datumcas) + interval '1 hours' as interv_end, avg(temperatura)
- from temperatura
- where (datumcas >= current_date and datumcas < (current_date + interval '1 day'))
- and (ime_senzorja = 'rumeni') group by date_trunc('hour', datumcas)
- order by interv_start;"""
- cursor.execute(query)
- db_con.commit()
- avg_hourly_temp_yellow = cursor.fetchall()
- print "AVERAGE TEMPERATURE PER HOUR"
- print "Average temperature per hour today:"
- for reading in avg_hourly_temp:
- print "Between %s and %s houraverage temperature was %s degrees C." % (reading[0].strftime("%H"), reading[1].strftime("%H"), round(reading[2],1))
- # time intervals
- x = [row[0].hour for row in avg_hourly_temp]
- # average temerature on both sensors
- y = [row[2] for row in avg_hourly_temp]
- #temperature on single sensor only
- y_red = [row[2] for row in avg_hourly_temp_red]
- y_yellow = [row[2] for row in avg_hourly_temp_yellow]
- output_file("graf.html", title='Temperature measurment')
- # todays date
- datum = reading[0].strftime("%-d. %-m. %Y")
- labels = ['[%d-%d]' % (i, i+1) for i in x]
- p = figure(plot_width=900, plot_height=400, tools="save", x_range=(labels), y_range=(round(min(y),0)-1,round(max(y),0)+2), x_axis_location='above')
- p.vbar(x=x, width=0.7, bottom=0, top=y, color="firebrick")
- p.toolbar.logo = None
- #p.toolbar_location = None
- p.title.text_font_size = '20pt'
- p.title.text_font = 'helvetica'
- p.title.text = u'Temperature on a day %s' % datum
- p.xaxis.axis_label='ura/hour'
- p.xaxis.axis_label_text_font_size = "10pt"
- p.xaxis.axis_label_text_font_style = "normal"
- p.xaxis[0].formatter = PrintfTickFormatter(format='%s')
- p.xaxis.major_label_orientation = "vertical"
- p.yaxis.axis_label='temperatura/temperature'
- p.yaxis.axis_label_text_font_size = "12pt"
- p.yaxis.axis_label_text_font_style = "normal"
- p.yaxis[0].formatter = PrintfTickFormatter(format='%s °C')
- p.line(x, y_red, line_width=2, line_color="red", legend="rdeči (red) senzor")
- p.circle(x, y_red, line_width=2, line_color="red", fill_color="red", size=4, legend="rdeči (red) senzor")
- p.line(x, y_yellow, line_width=2, line_color="yellow", legend="rumeni (yellow) senzor")
- p.circle(x, y_yellow, line_width=2, line_color="yellow", fill_color="yellow", size=4, legend="rumeni (yellow) senzor")
- show(p)
- if db_con:
- db_con.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement