-
Notifications
You must be signed in to change notification settings - Fork 0
/
testplot2.py
107 lines (93 loc) · 3.97 KB
/
testplot2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import time
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as dat
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
import sqlite3
from dateutil import tz
def getHistData (numSamples=43000,location="*",stDate=dt.datetime.utcnow()+dt.timedelta(hours=-23),endDate=dt.datetime.utcnow()):
print(location)
#if stDate.hour<=endDate.hour:
# curs.execute("SELECT * FROM sensorsData WHERE tdata >= (?) and tdata <= (?) and ttime >= (?) and ttime < (?) and zonename = (?) ORDER BY tdata DESC , ttime DESC LIMIT (?)",(dt.datetime.strftime(stDate,'%Y-%m-%d'),dt.datetime.strftime(endDate,'%Y-%m-%d'),dt.datetime.strftime(stDate,'%H:%M:%S'),dt.datetime.strftime(endDate,'%H:%M:%S'),location, numSamples))
#else:
# curs.execute("SELECT * FROM sensorsData WHERE tdata >= (?) and tdata <= (?) and (ttime <= (?) or ttime >= (?)) and zonename = (?) ORDER BY tdata DESC , ttime DESC LIMIT (?)",(dt.datetime.strftime(stDate,'%Y-%m-%d'),dt.datetime.strftime(endDate,'%Y-%m-%d'),dt.datetime.strftime(stDate,'%H:%M:%S'),dt.datetime.strftime(endDate,'%H:%M:%S'),location, numSamples))
print(stDate,endDate,dt.datetime.strftime(stDate,'%H:%M:%S'),dt.datetime.strftime(stDate,'%Y-%m-%d'))
#curs.execute("SELECT * FROM sensorsData WHERE tdata >= (?) and tdata <= (?) and ttime >= (?) and ttime < (?)) and zonename = (?) ORDER BY tdata DESC , ttime DESC LIMIT (?)",(dt.datetime.strftime(stDate,'%Y-%m-%d'),dt.datetime.strftime(endDate,'%Y-%m-%d'),dt.datetime.strftime(stDate,'%H:%M:%S'),dt.datetime.strftime(endDate,'%H:%M:%S'),location, numSamples))
curs.execute("SELECT * FROM sensorsData WHERE tdata >= (?) and tdata <= (?) and zonename = (?) ORDER BY tdata DESC , ttime DESC LIMIT (?)",(dt.datetime.strftime(stDate,'%Y-%m-%d'),dt.datetime.strftime(endDate,'%Y-%m-%d'),location, numSamples))
data = curs.fetchall()
#dates = []
timeStamps = []
temps = []
hums = []
#from_zone = tz.gettz('Europe/Rome')
#to_zone = tz.gettz('UTC')
for row in reversed(data):
#print(row[0],row[1])
timeStamp=dt.datetime.strptime(row[0]+row[1], '%Y-%m-%d%H:%M:%S')
if timeStamp>stDate and timeStamp<endDate:
timeStamp=datetime_from_utc_to_local(timeStamp)
timeStamps.append(timeStamp)
temps.append(row[4])
hums.append(row[5])
return timeStamps, temps, hums
def plot_temp():
timeStamps, temps, hums = getHistData(numSamples,location)
formatter = dat.DateFormatter('%d-%m %H:%M')
ys = temps
xs=timeStamps
fig = Figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_title("Temperature [°C]")
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_tick_params(rotation=30, labelsize=10)
ax.grid(True)
ax.plot_date(xs, ys,'o-')
fig.autofmt_xdate()
fig.tight_layout()
canvas = FigureCanvas(fig)
output = io.BytesIO()
canvas.print_png(output)
response = make_response(output.getvalue())
response.mimetype = 'image/png'
return response
def plot_hum():
timeStamps, temps, hums = getHistData(numSamples,location)
ys = hums
xs = timeStamps
formatter = dat.DateFormatter('%d-%m %H:%M')
fig = Figure()
ax = fig.add_subplot(1, 1, 1)
output = io.BytesIO()
#canvas.print_png(output)
#response = make_response(output.getvalue())
#response.mimetype = 'image/png'
#return response
#fig, ax = plt.subplots()
ax.set_title("Humidity [%]")
ax.grid(True)
ax.plot_date(xs, ys,'o-')
#ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_tick_params(rotation=30, labelsize=10)
fig.autofmt_xdate()
fig.tight_layout()
canvas = FigureCanvas(fig)
canvas.print_figure('test')
#plt.show()
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = dt.datetime.fromtimestamp(now_timestamp) - dt.datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset
conn=sqlite3.connect('tempDB.db')
curs=conn.cursor()
numSamples = 15000
location="SOGGIORNO"
plot_hum()
plot_temp()
# Create figure for plotting
#fig = plt.figure()
#ax = fig.add_subplot(1, 1, 1)
#xs = []
#ys = []