-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor_solar.py
308 lines (245 loc) · 9.38 KB
/
monitor_solar.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# http://www.gavinj.net/2012/06/building-python-daemon-process.html
import os
import time
import logging
from Monitor import Monitor
# third party libs
from daemon import runner
# for solar monitoring
import socket
import re
import struct
HEART_BEAT = 10
#####################################################
# Send UDP broadcast packets
#####################################################
UDP_PORT = 1300
UDP_MESSAGE = "55 aa 00 40 02 00 0b 49 20 41 4d 20 53 45 52 56 45 52 04 3a"
UDP_REPEATS = 1
SOLAR_HOST = '192.168.0.45' # The remote host
# TCP connection settings
TCP_HOST = '' # Symbolic name meaning the local host
TCP_PORT = 1200 # The same port as used by the server
TCP_MESSAGE_QUERY1 = "55 aa 01 03 02 00 00 01 05"
TCP_MESSAGE_QUERY2 = "55 aa 01 00 02 00 00 01 02"
TCP_MESSAGE_QUERY3 = "55 aa 04 00 02 00 00 01 05"
TCP_MESSAGE_QUERY_DATA = "55 aa 01 02 02 00 00 01 04"
def toMsg(data):
'''convert data to message.'''
return re.sub(" ", "", data).decode('hex')
def fromMsg(data):
'''decode message from SAMIL Inverter'''
result = Result()
result.fromData(data)
return result
class Result:
'''Container for SAMIL Inverter Status.'''
def fromData(self, data):
assert len(data) == 63
fmt = "!7shhhhhhhhh20shhhhhhih"
# A, B, C, D, E are unknown content
# missing: operating time
# heat sink temperature
(A, self.internal_temperature,
self.voltage1, self.voltage2,
self.current1, self.current2,
B, C, D,
self.energy_today, D,
self.power1, self.power2,
self.grid_current, self.grid_voltage, self.grid_frequency,
self.power_total, self.energy_total, self.E) = struct.unpack(fmt, data)
# unit conversion
self.internal_temperature *= 0.1 # Celsius
self.voltage1 *= 0.1 # V
self.voltage2 *= 0.1 # V
self.current1 *= 0.1 # A
self.current2 *= 0.1 # A
self.power1 *= 1.0 # W
self.power2 *= 1.0 # W
self.grid_frequency *= 0.01 # Hz
self.grid_current *= 0.1 # A
self.grid_voltage *= 0.1 # V
self.power_total *= 1.0 # W
self.energy_today *= 0.01 # kWh
self.energy_total *= 0.1 # kWh
@classmethod
def header(cls):
return "\t".join(('T [C]',
'V1 [V]',
'V2 [V]',
'A1 [A]',
'A2 [A]',
'P1 [W]',
'P2 [W]',
'P [W]'
'Ed [kWh]',
'E [kWh]',
'GV [V]',
'GA [A]',
'Gf [Hz]'))
def __str__(self):
return "\t".join(map(str, (
self.internal_temperature,
self.voltage1,
self.voltage2,
self.current1,
self.current2,
self.power1,
self.power2,
self.power_total,
self.energy_today,
self.energy_total,
self.grid_voltage,
self.grid_current,
self.grid_frequency)))
def items(self):
return (
("Solar.InternalTemperature",
self.internal_temperature),
("Solar.Voltage.West", self.voltage1),
("Solar.Voltage.East", self.voltage2),
("Solar.Current.West", self.current1),
("Solar.Current.East", self.current2),
("Solar.Power.West", self.power1),
("Solar.Power.East", self.power2),
("Solar.Grid.Frequency",
self.grid_frequency),
("Solar.Grid.Current",
self.grid_current),
("Solar.Grid.Voltage",
self.grid_voltage),
("Solar.Power.Total",
self.power_total),
("Solar.Energy.Today",
self.energy_today),
("Solar.Energy.Total",
self.energy_total))
class App(Monitor):
label = "solar"
def __init__(self, *args, **kwargs):
Monitor.__init__(self, *args, **kwargs)
# open tcp connection
self.connection = None
self.tcp_socket = None
def setup(self):
Monitor.setup(self)
self.logger.debug("TCP:%i opening" % TCP_PORT)
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
while True:
try:
tcp.bind((TCP_HOST, TCP_PORT))
except socket.error, msg:
logger.debug(
"error connecting: %s - retry in 20s" % msg)
time.sleep(20)
continue
break
self.logger.debug("TCP:%i listening" % TCP_PORT)
tcp.settimeout(20)
while True:
pid = os.fork()
if pid == 0:
self.logger.debug("UDP:%i sending welcome" % UDP_PORT)
# wait 1 second
time.sleep(1)
udp = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
udp.bind(('', 0))
udp.setsockopt(socket.SOL_SOCKET,
socket.SO_BROADCAST, 1)
udp.setsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR, 1)
udp.sendto(toMsg(UDP_MESSAGE),
('<broadcast>', UDP_PORT))
udp.close()
self.logger.debug("UDP:%i closed" % UDP_PORT)
# exit child process
os._exit(0)
else:
self.logger.debug(
"TCP: waiting for contact from inverter")
try:
tcp.listen(1)
connection, addr = tcp.accept()
except socket.timeout:
logger.debug("no connection - retrying")
continue
self.logger.debug('TCP: connected to inverter: %s:%s' %
(str(addr), str(connection)))
self.logger.debug("sent message 1")
connection.send(toMsg(TCP_MESSAGE_QUERY1))
data = connection.recv(1024)
self.logger.debug(
"received response of length %i" % len(data))
self.logger.debug("sent message 2")
connection.send(toMsg(TCP_MESSAGE_QUERY2))
data = connection.recv(1024)
self.logger.debug(
"received response of length %i" % len(data))
self.logger.debug("sent message 3")
connection.send(toMsg(TCP_MESSAGE_QUERY3))
data = connection.recv(1024)
self.logger.debug(
"received response of length %i" % len(data))
break
self.connection = connection
self.tcp_socket = tcp
def monitor(self):
connection = self.connection
while True:
restart = False
try:
logger.debug("getting data")
connection.send(toMsg(TCP_MESSAGE_QUERY_DATA))
data = connection.recv(1024)
except (OSError, socket.error) as e:
logger.warn("error while retrieving data: %s" % e.strerror)
restart = True
# According to python sockets tutorial: "When a recv
# returns 0 bytes, it means the other side has closed (or
# is in the process of closing) the connection. You will
# not receive any more data on this connection. Ever."
if len(data) == 0:
self.logger.warn("received 0 bytes when retrieving data")
restart = True
if restart:
self.logger.warn("closing connection and restarting")
# Python docs:
# close() releases the resource associated with a
# connection but does not necessarily close the
# connection immediately. If you want to close the
# connection in a timely fashion, call shutdown()
# before close().
self.connection.shutdown(socket.SHUT_RDWR)
self.connection.close()
self.setup()
continue
if len(data) == 63:
values = fromMsg(data)
logger.info("%s" % str(values))
logger.info("status: solar=ok")
else:
self.logger.debug("received %i bytes" % len(data))
values = None
self.logger.info("status: solar=fail")
break
return values
def __del__(self):
if self.connection:
self.connection.close()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/mnt/ramdisk/solar.log")
handler.setFormatter(formatter)
logger.addHandler(handler)
app = App(logger=logger, heart_beat=HEART_BEAT)
# app.run()
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()