-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
203 lines (130 loc) · 4.54 KB
/
run.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
from pyModbusTCP.client import ModbusClient
import time
def modbus(ip):
dip = "Offline"
# Initiate modbus client
c = ModbusClient(host=ip, port=502, auto_open=True, timeout=1)
# Read Modbus outputs
reg = c.read_holding_registers(0, 100)
# Close Modbus connection
c.close()
if reg:
# Register 49 (array position 48) is dip switch settings
# Convert float response to 8 bit binary string and reverse order (all in one line boiiii)
dip = f'{reg[48]:08b}'[::-1]
# Convert to array
dip = [c for c in dip]
return(dip)
def mode(dip):
# Get MPPT mode
if dip[0] == '0':
mode = "Charge"
elif dip[0] == '1' and dip[6] == '0' and dip[7] == '0':
mode = "Load"
elif dip[0] == '1' and dip[6] == '1' and dip[7] == '0':
mode = "Diversion"
elif dip[0] == '1' and dip[6] == '0' and dip[7] == '1':
mode = "Lighting"
return(mode)
def voltage(dip):
# Get voltage setting
if dip[1] == '0' and dip[2] == '0':
voltage = "Auto"
elif dip[1] == '0' and dip[2] == '1':
voltage = "12v"
elif dip[1] == '1' and dip[2] == '0':
voltage = "24v"
elif dip[1] == '1' and dip[2] == '1':
voltage = "48v"
return(voltage)
def setpoints(dip):
# Get set point values
if dip[3] == '0' and dip[4] == '0' and dip[5] == '0':
ab = "14.00 V"
fl = "13.4 V"
eq = "None"
elif dip[3] == '0' and dip[4] == '0' and dip[5] == '1':
ab = "14.15 V"
fl = "13.4 V"
eq = "14.2 V"
elif dip[3] == '0' and dip[4] == '1' and dip[5] == '0':
ab = "14.15 V"
fl = "13.4 V"
eq = "14.4 V"
elif dip[3] == '0' and dip[4] == '1' and dip[5] == '1':
ab = "14.35 V"
fl = "13.4 V"
eq = "15.1 V"
elif dip[3] == '1' and dip[4] == '0' and dip[5] == '0':
ab = "14.40 V"
fl = "13.4 V"
eq = "15.3 V"
elif dip[3] == '1' and dip[4] == '0' and dip[5] == '1':
ab = "14.80 V"
fl = "13.4 V"
eq = "15.3 V"
elif dip[3] == '1' and dip[4] == '1' and dip[5] == '0':
ab = "15.00 V"
fl = "13.4 V"
eq = "15.3 V"
else:
ab = "Invalid"
fl = "Invalid"
eq = "Invalid"
return(ab,fl,eq)
def equalize(dip):
# Get equalize settings
if dip[6] == '0':
eq_mode = "Manual"
elif dip[6] == "1":
eq_mode = "Automatic"
return(eq_mode)
def noise(dip):
# Noise reduction setting
if dip[7] == '0':
nrm = "PWM"
elif dip[7] == "1":
nrm = "On-Off Charging"
return(nrm)
def main():
with open("export.csv", 'r') as f:
with open("dip_sticks.csv", 'w') as o:
# .csv heading
o.write('Asset,IP,DIP 1,DIP 2,DIP 3,DIP 4,DIP 5,DIP 6,DIP 7,DIP 8,Mode,Voltage,Set Point - Absorb,Set Point - Float,Set Point - Equalize,Equalize,Noise Reduction\n')
# Each line in input .csv
for idx, line in enumerate(f):
# Skip headings
if idx == 0:
continue
# Get ip and name of MPPTs
line = line.split(',')
name = line[2]
ip = line[4]
ip = ip.replace('"', '')
# Get dip switch positions through modbus function
dip = modbus(ip)
# Convert dip switch positions to actual settings
if dip != 'Offline':
_mode = mode(dip)
_voltage = voltage(dip)
_setpoints = setpoints(dip)
_equalize = equalize(dip)
_noise = noise(dip)
print(f'{name} - {dip}')
# Write to output .csv
o.write(f'{name},'
f'{ip},'
f'{dip[0]},{dip[1]},{dip[2]},{dip[3]},{dip[4]},{dip[5]},{dip[6]},{dip[7]},'
f'{_mode},'
f'{_voltage},'
f'{_setpoints[0]},'
f'{_setpoints[1]},'
f'{_setpoints[2]},'
f'{_equalize},'
f'{_noise}\n')
elif dip == "Offline":
o.write(f'{name},{ip},Offline\n')
o.close()
f.close()
if __name__ == "__main__":
main()