-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgalaxy_3500.py
More file actions
143 lines (128 loc) · 4.79 KB
/
galaxy_3500.py
File metadata and controls
143 lines (128 loc) · 4.79 KB
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
# pylint: disable=C0325
""" Python interface for Galaxy 3500 UPS. The driver uses the
telnet interface of the device.
"""
from __future__ import print_function
import telnetlib
try:
from credentials import upsuser, upspasswd
except ImportError:
print("Did not find user/pswd settings in credentials.py. Using default!")
upsuser = 'apc'
upspasswd = 'apc'
from PyExpLabSys.common.supported_versions import python2_and_3
python2_and_3(__file__)
class Galaxy3500(object):
""" Interface driver for a Galaxy3500 UPS. """
def __init__(self, hostname):
self.status = {}
self.ups_handle = telnetlib.Telnet(hostname)
self.ups_handle.expect([b': '])
self.ups_handle.write('{}\r'.format(upsuser).encode())
self.ups_handle.expect([b': '])
self.ups_handle.write('{}\r'.format(upspasswd).encode())
self.ups_handle.expect([b'apc>'])
def comm(self, command, keywords=None):
""" Send a command to the ups """
self.ups_handle.write(command.encode('ascii') + b'\r')
echo = self.ups_handle.expect([b'\r'])[2]
assert echo == command.encode('ascii') + b'\r'
code = self.ups_handle.expect([b'\r'])[2]
assert 'E000' in code.decode()
output = self.ups_handle.expect([b'apc>'])[2]
output = output.decode()
if keywords is not None:
return_val = {}
for param in list(keywords):
pos = output.find(param)
line = output[pos + len(param) + 1 : pos + len(param) + 8].strip()
for i in range(0, 3):
try:
value = float(line[: -1 * i])
break
except ValueError:
pass
return_val[param] = value
else:
return_val = output[1:-5]
return return_val
def alarms(self):
""" Return list of active alarms """
warnings = self.comm('alarmcount -p warning', ['WarningAlarmCount'])
criticals = self.comm('alarmcount -p critical', ['CriticalAlarmCount'])
warnings_value = int(warnings['WarningAlarmCount'])
criticals_value = int(criticals['CriticalAlarmCount'])
self.status['WarningAlarmCount'] = warnings_value
self.status['CriticalAlarmCount'] = criticals_value
return (warnings_value, criticals_value)
def battery_charge(self):
""" Return the battery charge state """
keyword = 'Battery State Of Charge'
charge = self.comm('detstatus -soc', [keyword])
self.status[keyword] = charge[keyword]
return charge[keyword]
def temperature(self):
""" Return the temperature of the UPS """
keyword = 'Internal Temperature'
temp = self.comm('detstatus -tmp', [keyword])
self.status[keyword] = temp[keyword]
return temp[keyword]
def battery_status(self):
""" Return the battery voltage """
params = ['Battery Voltage', 'Battery Current']
output = self.comm('detstatus -bat', params)
for param in params:
self.status[param] = output[param]
return output
def output_measurements(self):
""" Return status of the device's output """
params = [
'Output Voltage L1',
'Output Voltage L2',
'Output Voltage L3',
'Output Frequency',
'Output Watts Percent L1',
'Output Watts Percent L2',
'Output Watts Percent L3',
'Output VA Percent L1',
'Output VA Percent L2',
'Output VA Percent L3',
'Output kVA L1',
'Output kVA L2',
'Output kVA L3',
'Output Current L1',
'Output Current L2',
'Output Current L3',
]
output = self.comm('detstatus -om', params)
for param in params:
self.status[param] = output[param]
return output
def input_measurements(self):
""" Return status of the device's output """
params = [
'Input Voltage L1',
'Input Voltage L2',
'Input Voltage L3',
'Input Frequency',
'Bypass Input Voltage L1',
'Bypass Input Voltage L2',
'Bypass Input Voltage L3',
'Input Current L1',
'Input Current L2',
'Input Current L3',
]
output = self.comm('detstatus -im', params)
for param in params:
self.status[param] = output[param]
return output
if __name__ == '__main__':
UPS = Galaxy3500('ups-b312')
print(UPS.alarms())
print(UPS.battery_charge())
print(UPS.output_measurements())
print(UPS.input_measurements())
print(UPS.battery_status())
print(UPS.temperature())
print('---')
print(UPS.status)