-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcckWh.py
executable file
·68 lines (54 loc) · 1.48 KB
/
cckWh.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
#!/bin/env python
import calendar
import datetime
import sqlite3
import sys
import time
values = {
# 'phase1': [],
# 'phase2': [],
# 'phase3': [],
'power': [],
}
kWhs = {
# 'phase1': 0,
# 'phase2': 0,
# 'phase3': 0,
'power': 0,
}
if len(sys.argv) == 1:
print "Last 1 hour"
end = calendar.timegm(time.gmtime())
start = end - 3600
else:
start = datetime.datetime(2013, 05, int(sys.argv[1]), 15, 0)
end = datetime.datetime(2013, 05, int(sys.argv[1]), 20, 0)
print "From %s to %s (UTC)" % (start, end,)
start = calendar.timegm(start.timetuple())
end = calendar.timegm(end.timetuple())
database = sqlite3.connect('/opt/cc2p.data/power.sqlite')
cursor = database.cursor()
# Create tables
for value in values:
cursor.execute('''SELECT * FROM %s WHERE timestamp > ? AND timestamp <= ?''' % value, (start, end,))
values[value] = cursor.fetchall()
for i in values[value]:
kWhs[value] = kWhs[value] + i[1]
samples = len(values[value])
hours = (end - start) / float(3600)
kWhs[value] = (float(kWhs[value]) / float(samples)) / float(1000)
cursor.close()
database.close()
total = 0
for kWh in kWhs:
total = total + kWhs[kWh]
#print kWhs
print "From %s samples, over %s:%02d hours & minutes: \n" \
" -> the average kWh value is: %.2fkWh \n" \
" -> kWh's consumed over the period: %.1fkWhs" % (
samples,
int(hours),
int(hours * 60) % int(60),
total,
total * hours,
)