-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawTemp.py
124 lines (90 loc) · 3.31 KB
/
drawTemp.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
#!/usr/bin/python
import numpy as np
import sqlite3
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
avg = 0
def write_stats(stats):
global avg
avgt = stats[0]
last = stats[1]
mint = stats[2]
maxt = stats[3]
avg = avgt[2]
with open("/var/www/html/data/stats.php", "w") as f:
f.write("<?php\n\n")
f.write("$avg = array(\"{}\",{});\n".format(avgt[1],avgt[2] ))
f.write("$last = array(\"{}\",{});\n".format(last[1],last[2]))
f.write("$min = array(\"{}\",{});\n".format(mint[1],mint[2]))
f.write("$max = array(\"{}\",{});\n".format(maxt[1],maxt[2]))
f.write("\n\n?>\n")
## Read data from file
def get_data_file(interval):
with open("plotdata.txt") as f:
data = list(line for line in (l.strip() for l in f) if line)
x = [datetime.strptime(' '.join(row.split(' ')[0:2]),"%Y-%m-%d %H:%M:%S") for row in data]
y = [row.split(' ')[3] for row in data]
## Load data from DB
def get_data(interval):
conn=sqlite3.connect('/home/nacre/kastelu/temp.db')
curs=conn.cursor()
if interval == None:
curs.execute("SELECT * FROM temps")
else:
curs.execute("SELECT timestamp,temperature FROM temps WHERE timestamp>datetime('now','-%s hours')" % interval)
rows=curs.fetchall()
# fetch statistics
curs.execute("""SELECT 1,DATETIME(datetime('now', '+3 hours')),avg(temperature)
FROM temps
WHERE timestamp>datetime('now','-21 hours')
UNION
SELECT 2,*
FROM (
SELECT timestamp,temperature
FROM temps
ORDER BY timestamp DESC
LIMIT 1
)
UNION
SELECT 3,DATETIME(datetime('now', '+3 hours')),min(temperature)
FROM temps
WHERE timestamp>datetime('now','-21 hours')
UNION
SELECT 4,DATETIME(datetime('now', '+3 hours')),max(temperature)
FROM temps
WHERE timestamp>datetime('now','-21 hours')
ORDER BY 1""")
stats=curs.fetchall()
write_stats(stats)
conn.close()
return rows
data = get_data(24) # last 24h
x = [datetime.strptime(row[0],"%Y-%m-%d %H:%M:%S") for row in data]
y = zip(*data)[1]
## Print data
plt.ioff()
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title(u'Parvekelämpötila')
ax1.set_xlabel('Aika')
ax1.set_ylabel(u'Lampotila °C')
#ax1.set_xticks(x) # Tickmark + label at every plotted point
ax1.xaxis.set_minor_locator(mdates.HourLocator(byhour=range(0,24,3)))
ax1.xaxis.set_major_locator(mdates.DayLocator())
ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M'))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('\n%d/%m/%Y'))
ax1.plot_date(x, y, ls='-', marker="")
ax1.grid(b=True, which='major')
ax1.grid(b=True, which='minor', linestyle='--')
#fig.autofmt_xdate(rotation=70)
fig.tight_layout()
# add avg as horizontal line
ax1.axhline(y=avg, color='k', linestyle='--', linewidth=1)
#plt.setp(ax1.get_xticklabels(), rotation=45)
#fig.show()
#plt.show()
fig.savefig("/var/www/html/data/kuvaaja.png")