forked from Woutrrr/Omnik-Data-Logger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
InverterMsg.py
191 lines (146 loc) · 5.21 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
import struct # Converting bytes to numbers
class InverterMsg(object):
"""Decode the response message from an omniksol inverter."""
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]
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 == 65535:
return -1
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 id(self):
"""ID of the inverter."""
return self.__get_string(15, 31)
@property
def temperature(self):
"""Temperature recorded by the inverter."""
return self.__get_short(31)
@property
def power(self):
"""Power output"""
print self.__get_short(59)
@property
def e_total(self):
"""Total energy generated by inverter in kWh"""
return self.__get_long(71)
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 = 33 + (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 = 39 + (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 = 45 + (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 = 51 + (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 = 57 + (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 = 59 + (i - 1) * 4
return int(self.__get_short(num, 1)) # Don't divide
@property
def e_today(self):
"""Energy generated by inverter today in kWh"""
return self.__get_short(69, 100) # Divide by 100
@property
def h_total(self):
"""Hours the inverter generated electricity"""
return int(self.__get_long(75, 1)) # Don't divide