-
Notifications
You must be signed in to change notification settings - Fork 29
/
InverterMsg.py
288 lines (231 loc) · 8.06 KB
/
InverterMsg.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
import struct # Converting bytes to numbers
import re
import binascii
#V4 table
offset = {
1: 1, #len(1)
2: 12, #msg(12)
3: 15, #id(15)
4: 31, #temperature(31)
5: 33, #v_pv(33,35,37)
6: 39, #i_pv(39,41,43)
7: 45, #i_ac(45,47,49)
8: 51, #v_ac(51,53,55)
9: 57, #f_ac(57,62,65)
10: 59, #p_ac(59,63,67)
11: 69, #e_today(69)
12: 71, #e_total(71)
13: 75, #h_total(75)
14: 79, #run_state(79)
15: 81, #GVFaultValue(81)
16: 83, #GVFaultValue(83)
17: 85, #GZFaultValue(85)
18: 87, #TmpFaultValue(87)
19: 89, #PVFaultValue(89)
20: 91, #GFCIFaultValue(91)
21: 93, #errorMsg(93)
22:101, #main_fwver(101)
23:121,}#slave_fwver(121)
class InverterMsg(object):
"""Decode the response message from an inverter logger."""
raw_msg = ""
def __init__(self, msg, offset=0):
self.raw_msg = msg
self.offset = offset
def __get_string(self, begin, end):
"""Extract string from message.
Args:
begin (int): starting byte index of string
end (int): end byte index of string
Returns:
str: String in the message from start to end
"""
return self.raw_msg[begin:end].decode('cp437')
#
def __get_int(self, begin):
"""Extract byte value from message.
Args:
begin (int): starting byte index of string
Returns:
int: value at offset
"""
if (len(self.raw_msg) < begin):
return 0
return int(binascii.hexlify(bytearray(self.raw_msg[begin:begin+1])), 16)
#
def __get_short(self, begin, divider=10):
"""Extract short from message.
The shorts in the message could actually be a decimal number. This is
done by storing the number multiplied in the message. So by dividing
the short the original decimal number can be retrieved.
Args:
begin (int): index of short in message
divider (int): divider to change short to float. (Default: 10)
Returns:
int or float: Value stored at location `begin`
"""
num = struct.unpack('!H', self.raw_msg[begin:begin + 2])[0]
if num > 32767:
return float(-(65536 - num)) / divider
else:
return float(num) / divider
def __get_long(self, begin, divider=10):
"""Extract long from message.
The longs in the message could actually be a decimal number. By
dividing the long, the original decimal number can be extracted.
Args:
begin (int): index of long in message
divider (int): divider to change long to float. (Default : 10)
Returns:
int or float: Value stored at location `begin`
"""
return float(
struct.unpack('!I', self.raw_msg[begin:begin + 4])[0]) / divider
@property
def len(self):
"""received data len msg."""
return self.__get_int(offset[1])
@property
def msg(self):
"""received status msg."""
return self.__get_string(offset[2], self.len+offset[2])
@property
def id(self):
"""ID of the inverter."""
return self.__get_string(offset[3], offset[3]+16).lstrip().rstrip() #Strip spaces from shorter or padded inverter SN
@property
def temp(self):
"""Temperature recorded by the inverter."""
return self.__get_short(offset[4], 10)
@property
def e_today(self):
"""Energy generated by inverter today in kWh"""
return self.__get_short(offset[11], 100) # Divide by 100
@property
def e_total(self):
"""Total energy generated by inverter in kWh"""
return self.__get_long(offset[12])
@property
def h_total(self):
"""Hours the inverter generated electricity"""
return int(self.__get_long(offset[13], 1)) # Don't divide
@property
def run_state(self):
"""RUN State"""
return int(self.__get_short(offset[14], 1))
@property
def GVFaultValue(self):
"""Grid voltage fault value in V"""
return self.__get_short(offset[15], 10) # Divide by 10
@property
def GVFaultValue(self):
"""Grid frequency fault value in Hz"""
return self.__get_short(offset[16], 100) # Divide by 100
@property
def GZFaultValue(self):
"""Grid impedance fault value in Ohm"""
return self.__get_short(offset[17], 1000) # Divide by 1000
@property
def TmpFaultValue(self):
"""Temperature fault value in oC"""
return self.__get_short(offset[18], 10) # Divide by 10
@property
def PVFaultValue(self):
"""PV voltage fault value in V"""
return self.__get_short(offset[19], 10) # Divide by 10
@property
def GFCIFaultValue(self):
"""GFCI current fault value in A"""
return self.__get_short(offset[20], 1000) # Divide by 1000
@property
def errorMsg(self):
"""errorMsg binary index value"""
return int(self.__get_long(offset[21]))
@property
def main_fwver(self):
"""Inverter main firmware version."""
if (self.__get_int(offset[22]) == 0): return ""
return re.sub('[^\x20-\x7f]', '', self.__get_string(offset[22], offset[22]+19))
@property
def slave_fwver(self):
"""Inverter slave firmware version."""
if (self.__get_int(offset[23]) == 0): return ""
return re.sub('[^\x20-\x7f]', '', self.__get_string(offset[23], offset[23]+19))
def v_pv(self, i=1):
"""Voltage of PV input channel.
Available channels are 1, 2 or 3; if not in this range the function will
default to channel 1.
Args:
i (int): input channel (valid values: 1, 2, 3)
Returns:
float: PV voltage of channel i
"""
if i not in range(1, 4):
i = 1
num = offset[5] + (i - 1) * 2
return self.__get_short(num)
def i_pv(self, i=1):
"""Current of PV input channel.
Available channels are 1, 2 or 3; if not in this range the function will
default to channel 1.
Args:
i (int): input channel (valid values: 1, 2, 3)
Returns:
float: PV current of channel i
"""
if i not in range(1, 4):
i = 1
num = offset[6] + (i - 1) * 2
return self.__get_short(num)
def i_ac(self, i=1):
"""Current of the Inverter output channel
Available channels are 1, 2 or 3; if not in this range the function will
default to channel 1.
Args:
i (int): output channel (valid values: 1, 2, 3)
Returns:
float: AC current of channel i
"""
if i not in range(1, 4):
i = 1
num = offset[7] + (i - 1) * 2
return self.__get_short(num)
def v_ac(self, i=1):
"""Voltage of the Inverter output channel
Available channels are 1, 2 or 3; if not in this range the function will
default to channel 1.
Args:
i (int): output channel (valid values: 1, 2, 3)
Returns:
float: AC voltage of channel i
"""
if i not in range(1, 4):
i = 1
num = offset[8] + (i - 1) * 2
return self.__get_short(num)
def f_ac(self, i=1):
"""Frequency of the output channel
Available channels are 1, 2 or 3; if not in this range the function will
default to channel 1.
Args:
i (int): output channel (valid values: 1, 2, 3)
Returns:
float: AC frequency of channel i
"""
if i not in range(1, 4):
i = 1
num = offset[9] + (i - 1) * 4
return self.__get_short(num, 100)
def p_ac(self, i=1):
"""Power output of the output channel
Available channels are 1, 2 or 3; if no tin this range the function will
default to channel 1.
Args:
i (int): output channel (valid values: 1, 2, 3)
Returns:
float: Power output of channel i
"""
if i not in range(1, 4):
i = 1
num = offset[10] + (i - 1) * 4
return int(self.__get_short(num, 1)) # Don't divide