forked from Energenie/pyenergenie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
91 lines (75 loc) · 2.43 KB
/
Logger.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
# Logger.py 14/05/2016 D.J.Whale
#
# A simple logger - logs to file.
import os
import time
from energenie import OpenThings
LOG_FILENAME = "energenie.csv"
HEADINGS = 'timestamp,mfrid,prodid,sensorid,flags,switch,voltage,freq,reactive,real,apparent,current,temperature'
log_file = None
def trace(msg):
print(str(msg))
def logMessage(msg):
global log_file
if log_file == None:
if not os.path.isfile(LOG_FILENAME):
log_file = open(LOG_FILENAME, 'w')
log_file.write(HEADINGS + '\n')
else:
log_file = open(LOG_FILENAME, 'a') # append
# get the header
header = msg['header']
timestamp = time.time()
mfrid = header['mfrid']
productid = header['productid']
sensorid = header['sensorid']
# set defaults for any data that doesn't appear in this message
# but build flags so we know which ones this contains
flags = [0 for i in range(8)]
switch = None
voltage = None
freq = None
reactive = None
real = None
apparent = None
current = None
temperature = None
# capture any data that we want
##trace(msg)
for rec in msg['recs']:
paramid = rec['paramid']
try:
value = rec['value']
except:
value = None
if paramid == OpenThings.PARAM_SWITCH_STATE:
switch = value
flags[0] = 1
elif paramid == OpenThings.PARAM_VOLTAGE:
flags[1] = 1
voltage = value
elif paramid == OpenThings.PARAM_FREQUENCY:
flags[2] = 1
freq = value
elif paramid == OpenThings.PARAM_REACTIVE_POWER:
flags[3] = 1
reactive = value
elif paramid == OpenThings.PARAM_REAL_POWER:
flags[4] = 1
real = value
elif paramid == OpenThings.PARAM_APPARENT_POWER:
flags[5] = 1
apparent = value
elif paramid == OpenThings.PARAM_CURRENT:
flags[6] = 1
current = value
elif paramid == OpenThings.PARAM_TEMPERATURE:
flags[7] = 1
temperature = value
# generate a line of CSV
flags = "".join([str(a) for a in flags])
csv = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % (timestamp, mfrid, productid, sensorid, flags, switch, voltage, freq, reactive, real, apparent, current, temperature)
log_file.write(csv + '\n')
log_file.flush()
##trace(csv) # testing
# END