-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor_wattson.py
156 lines (124 loc) · 4.17 KB
/
monitor_wattson.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# http://www.gavinj.net/2012/06/building-python-daemon-process.html
# Inspired by:
# http://pikarinen.com/rrdwattsond/
# https://github.com/sapg/openwattson
# See also: http://wattsonexplorer.sourceforge.net/
import time
import logging
import serial
import re
# third party libs
from daemon import runner
from Monitor import Monitor
HEART_BEAT = 1
class App(Monitor):
label = "wattson"
def __init__(self, *args, **kwargs):
Monitor.__init__(self, *args, **kwargs)
self.port = '/dev/ttyUSB0'
# stty settings
self.stty = "5:0:8be:0:0:0:0:0:0:a" + ":0" * 26
def communicate(self, command):
'''communicate with wattson energy monitor'''
self.logger.debug('sending commands %s' % command)
ser = self.connection
ser.flush()
ser.flushInput()
ser.flushOutput()
ser.write(command + "\r")
time.sleep(1)
logger.debug("waiting for return data")
for i in range(5):
r = []
while ser.inWaiting() > 0:
r.append(ser.read(1))
if len(r) > 0:
break
time.sleep(0.1)
self.logger.debug("received %i bytes after %i tries" %
(len(r), i))
if len(r) == 0:
return None
# skip first character
r = ''.join(r[1:])
# remove everything after line break
r = re.sub("\r(.*|$)", "", r)
# remove new-line
r = r.rstrip()
if len(r) == 0:
return None
return r
def setup(self):
Monitor.setup(self)
self.logger.debug("wattson: port=%s" % self.port)
# 19200, 8N1
# 8 data bits, no parity bit, 1 stop bit
self.connection = serial.Serial(
port=self.port,
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
self.connection.close()
self.connection.open()
assert self.connection.isOpen()
data = self.communicate("nows")
self.logger.info("connected to wattson device %s" % data)
def monitor(self):
'''
'''
grid_status, solar_status = "ok", "ok"
# first command always fails
solar_power = self.communicate("noww")
solar_power = self.communicate("noww")
if solar_power is None:
solar_status = "fail"
solar_power = 0
elif len(solar_power) != 4:
logger.warning(
"malformed grid power: %s" % list(solar_power))
solar_status = "fail"
solar_power = 0
else:
solar_power = int(solar_power, 16)
# get current energy usage in watts
grid_power = self.communicate("nowp")
if grid_power is None:
grid_status = "fail"
grid_power = 0
elif len(grid_power) != 4:
logger.warning(
"malformed grid power: %s" % list(grid_power))
grid_status = "fail"
grid_power = None
else:
grid_power = int(grid_power, 16)
self.logger.debug(
"acquired values: grid=%s, solar=%s" %
(grid_power, solar_power))
self.logger.debug(
"status: grid=%s, solar=%s" %
(grid_status, solar_status))
values = {'Wattson.Power.Grid': grid_power,
'Wattson.Power.Solar': solar_power}
return values
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/mnt/ramdisk/wattson.log")
handler.setFormatter(formatter)
logger.addHandler(handler)
# for debugging
# logging.getLogger().addHandler(logging.StreamHandler())
app = App(logger=logger, heart_beat=HEART_BEAT)
# for debugging
# app.setup()
# app.monitor()
daemon_runner = runner.DaemonRunner(app)
# This ensures that the logger file handle does not get closed during
# daemonization
daemon_runner.daemon_context.files_preserve = [handler.stream]
daemon_runner.do_action()