-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
193 lines (175 loc) · 5.05 KB
/
reader.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
"""
LINEAGE J85480S1 CPL Monitor, GPIO Reader module.
J1 Connector Pinout
-------------------
- {J1 Pin Name}: {J1 Pin number}
- POWER_CAP_1: 1
- POWER_CAP_2: 2
- POWER_CAP_3: 3
- POWER_CAP_4: 4
- MOD_PRES_1: 5
- MOD_PRES_2: 6
- MOD_PRES_3: 7
- MOD_PRES_4: 8
- PFW_1: 9
- PFW_2: 10
- PFW_3: 11
- PFW_4: 12
- Fault: 17
- OTW: 25
- GND: 20 (or 22)
- 5V: 24 (max 750mA)
Note
----
On the J1 connector, pin 1 is in the lower left corner,
pin 2 is on the upper left corner, etc...
2 4 6 ...
+--------------
| : : : : : : : ...
+------------------
1 3 5 7...
"""
import time, collections
import RPi.GPIO as GPIO
import config
# Raw value to logical transform functions
to_human = {'POWER_CAP_1': lambda v: to_values['POWER_CAP'][bool(v)],
'POWER_CAP_2': lambda v: to_values['POWER_CAP'][bool(v)],
'POWER_CAP_3': lambda v: to_values['POWER_CAP'][bool(v)],
'POWER_CAP_4': lambda v: to_values['POWER_CAP'][bool(v)],
'MOD_PRES_1': lambda v: not v,
'MOD_PRES_2': lambda v: not v,
'MOD_PRES_3': lambda v: not v,
'MOD_PRES_4': lambda v: not v,
'PFW_1': lambda v: not v,
'PFW_2': lambda v: not v,
'PFW_3': lambda v: not v,
'PFW_4': lambda v: not v,
'Fault': lambda v: not v,
'OTW': lambda v: not v}
to_values = {'POWER_CAP': {False: '1200W',
True: '2000W'}}
# Alarm table definition
to_alarms = {
'OK': {
'Fault': 1,
'OTW': 1,
'PFW': 1,
'MOD_PRES': 0},
'Thermal Shutdown': {
'Fault': 0,
'OTW': 0,
'PFW': 0,
'MOD_PRES': 0},
'Defective Fan': {
'Fault': 0,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'Blown AC Fuse in Unit': {
'Fault': 0,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'No AC <15mS (single unit)': {
'Fault': 1,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'AC Present but not within limits': {
'Fault': 1,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'AC not present': {
'Fault': 1,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'Boost Stage Failure': {
'Fault': 0,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'Over Voltage Latched Shutdown': {
'Fault': 0,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'Over Current': {
'Fault': 1,
'OTW': 1,
'PFW': 0,
'MOD_PRES': 0},
'Non-catastrophic Internal Failure': {
'Fault': 0,
'OTW': 1,
'PFW': 1,
'MOD_PRES': 0},
# '1 Missing Module': {
# 'Fault': None,
# 'OTW': None,
# 'PFW': None,
# 'MOD_PRES': 1},
}
def init():
"""
Initialize GPIO (pin mode, pull-ups, etc).
"""
GPIO.setmode(GPIO.BCM)
for pin in config.pinout.viewkeys():
print 'Setting pin %s as INPUT with PULL-UP (50..65k)' % pin
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def cleanup():
"""
Cleans up the GPIO state.
"""
print GPIO.cleanup()
def read_raw():
"""
Returns the raw measurements.
"""
return {signal: GPIO.input(pin) for pin, signal in config.pinout.viewitems()}
def read_latch(n=8, interval=0.5):
"""
Returns the raw measurements, latched.
Multiple measurements are performed at regular interval, keeping worst values.
NOTE: The reason for this implementation is that PFW and Fault flap
during a module failure (the pin state is reseted to not fault every time
the module is trying to restart, presumably).
"""
latch_hi = lambda values: int(any(values)) # returns 1 if any value is 1
latch_low = lambda values: int(all(values)) # returns 0 if any value is 0
latch_map = {'POWER_CAP': latch_low,
'MOD_PRES': latch_hi,
'PFW': latch_low,
'Fault': latch_low,
'OTW': latch_low}
readings = []
for i in range(n):
readings.append(read_raw())
time.sleep(interval)
latched = collections.defaultdict(list)
for reading in readings:
for k, v in reading.viewitems():
latched[k].append(v)
for k, v in latched.viewitems():
latched[k] = latch_map.get(k, latch_map.get(k[:-2]))(latched[k])
return dict(latched)
def human(raw_values=None):
"""
Returns the given raw measurement converted to human readable values.
"""
return {signal: to_human[signal](value)
for signal, value in (raw_values or read_raw()).viewitems()}
def alarms(raw_values=None):
"""
Returns the given raw measurements converted to alarms,
according the device documentation.
"""
reading = raw_values or read_raw()
return {module: {name: all([value == reading.get('%s_%s' % (signal, module),
reading.get(signal))
for signal, value in signals.viewitems()])
for name, signals in to_alarms.viewitems()}
for module in [1, 2, 3, 4]}