14
14
try :
15
15
from sonic_platform_base .fan_base import FanBase
16
16
from .led import FanLed , ComponentFaultyIndicator
17
+ from .utils import read_int_from_file , read_str_from_file , write_file
17
18
except ImportError as e :
18
19
raise ImportError (str (e ) + "- required module not found" )
19
20
@@ -52,17 +53,18 @@ def __init__(self, fan_index, fan_drawer, psu_fan = False):
52
53
else :
53
54
self .led = FanLed (self .index )
54
55
55
- self .fan_min_speed_path = "fan{}_min" .format (self .index )
56
56
if not self .is_psu_fan :
57
57
self .fan_speed_get_path = "fan{}_speed_get" .format (self .index )
58
58
self .fan_speed_set_path = "fan{}_speed_set" .format (self .index )
59
- self .fan_max_speed_path = "fan{}_max" .format (self .index )
59
+ self .fan_max_speed_path = os .path .join (FAN_PATH , "fan{}_max" .format (self .index ))
60
+ self .fan_min_speed_path = os .path .join (FAN_PATH , "fan{}_min" .format (self .index ))
60
61
self ._name = "fan{}" .format (self .index )
61
62
else :
62
63
self .fan_speed_get_path = "psu{}_fan1_speed_get" .format (self .index )
63
64
self .fan_presence_path = "psu{}_fan1_speed_get" .format (self .index )
64
65
self ._name = 'psu_{}_fan_{}' .format (self .index , 1 )
65
- self .fan_max_speed_path = None
66
+ self .fan_max_speed_path = os .path .join (CONFIG_PATH , "psu_fan_max" )
67
+ self .fan_min_speed_path = os .path .join (CONFIG_PATH , "psu_fan_min" )
66
68
self .psu_i2c_bus_path = os .path .join (CONFIG_PATH , 'psu{0}_i2c_bus' .format (self .index ))
67
69
self .psu_i2c_addr_path = os .path .join (CONFIG_PATH , 'psu{0}_i2c_addr' .format (self .index ))
68
70
self .psu_i2c_command_path = os .path .join (CONFIG_PATH , 'fan_command' )
@@ -109,11 +111,7 @@ def get_status(self):
109
111
if self .is_psu_fan :
110
112
status = 0
111
113
else :
112
- try :
113
- with open (os .path .join (FAN_PATH , self .fan_status_path ), 'r' ) as fault_status :
114
- status = int (fault_status .read ().strip ())
115
- except (ValueError , IOError ):
116
- status = 1
114
+ status = read_int_from_file (os .path .join (FAN_PATH , self .fan_status_path ), 1 )
117
115
118
116
return status == 0
119
117
@@ -135,29 +133,6 @@ def get_presence(self):
135
133
else :
136
134
return self .fan_drawer .get_presence ()
137
135
138
-
139
- def _get_min_speed_in_rpm (self ):
140
- speed = 0
141
- try :
142
- with open (os .path .join (FAN_PATH , self .fan_min_speed_path ), 'r' ) as min_fan_speed :
143
- speed = int (min_fan_speed .read ())
144
- except (ValueError , IOError ):
145
- speed = 0
146
-
147
- return speed
148
-
149
-
150
- def _get_max_speed_in_rpm (self ):
151
- speed = 0
152
- try :
153
- with open (os .path .join (FAN_PATH , self .fan_max_speed_path ), 'r' ) as max_fan_speed :
154
- speed = int (max_fan_speed .read ().strip ())
155
- except (ValueError , IOError ):
156
- speed = 0
157
-
158
- return speed
159
-
160
-
161
136
def get_speed (self ):
162
137
"""
163
138
Retrieves the speed of fan
@@ -166,17 +141,12 @@ def get_speed(self):
166
141
int: percentage of the max fan speed
167
142
"""
168
143
speed = 0
169
- try :
170
- with open (os .path .join (FAN_PATH , self .fan_speed_get_path ), 'r' ) as fan_curr_speed :
171
- speed_in_rpm = int (fan_curr_speed .read ().strip ())
172
- except (ValueError , IOError ):
173
- speed_in_rpm = 0
144
+ speed_in_rpm = read_int_from_file (os .path .join (FAN_PATH , self .fan_speed_get_path ))
174
145
175
- if self .fan_max_speed_path is None :
176
- # in case of max speed unsupported, we just return speed in unit of RPM.
146
+ max_speed_in_rpm = read_int_from_file ( self .fan_max_speed_path )
147
+ if max_speed_in_rpm == 0 :
177
148
return speed_in_rpm
178
149
179
- max_speed_in_rpm = self ._get_max_speed_in_rpm ()
180
150
speed = 100 * speed_in_rpm / max_speed_in_rpm
181
151
if speed > 100 :
182
152
speed = 100
@@ -192,18 +162,15 @@ def get_target_speed(self):
192
162
int: percentage of the max fan speed
193
163
"""
194
164
if self .is_psu_fan :
195
- # Not like system fan, psu fan speed can not be modified, so target speed is N/A
196
- return self .get_speed ()
165
+ try :
166
+ # Get PSU fan target speed according to current system cooling level
167
+ cooling_level = self .get_cooling_level ()
168
+ return int (self .PSU_FAN_SPEED [cooling_level ], 16 )
169
+ except Exception :
170
+ return self .get_speed ()
197
171
198
- try :
199
- with open (os .path .join (FAN_PATH , self .fan_speed_set_path ), 'r' ) as fan_pwm :
200
- pwm = int (fan_pwm .read ().strip ())
201
- except (ValueError , IOError ):
202
- pwm = 0
203
-
204
- speed = int (round (pwm * 100.0 / PWM_MAX ))
205
-
206
- return speed
172
+ pwm = read_int_from_file (os .path .join (FAN_PATH , self .fan_speed_set_path ))
173
+ return int (round (pwm * 100.0 / PWM_MAX ))
207
174
208
175
209
176
def set_speed (self , speed ):
@@ -224,12 +191,9 @@ def set_speed(self, speed):
224
191
return False
225
192
from .thermal import logger
226
193
try :
227
- with open (self .psu_i2c_bus_path , 'r' ) as f :
228
- bus = f .read ().strip ()
229
- with open (self .psu_i2c_addr_path , 'r' ) as f :
230
- addr = f .read ().strip ()
231
- with open (self .psu_i2c_command_path , 'r' ) as f :
232
- command = f .read ().strip ()
194
+ bus = read_str_from_file (self .psu_i2c_bus_path , raise_exception = True )
195
+ addr = read_str_from_file (self .psu_i2c_addr_path , raise_exception = True )
196
+ command = read_str_from_file (self .psu_i2c_command_path , raise_exception = True )
233
197
speed = Fan .PSU_FAN_SPEED [int (speed / 10 )]
234
198
command = "i2cset -f -y {0} {1} {2} {3} wp" .format (bus , addr , command , speed )
235
199
subprocess .check_call (command , shell = True )
@@ -248,8 +212,7 @@ def set_speed(self, speed):
248
212
speed = self .min_cooling_level * 10
249
213
self .set_cooling_level (cooling_level , cooling_level )
250
214
pwm = int (round (PWM_MAX * speed / 100.0 ))
251
- with open (os .path .join (FAN_PATH , self .fan_speed_set_path ), 'w' ) as fan_pwm :
252
- fan_pwm .write (str (pwm ))
215
+ write_file (os .path .join (FAN_PATH , self .fan_speed_set_path ), pwm , raise_exception = True )
253
216
except (ValueError , IOError ):
254
217
status = False
255
218
@@ -311,21 +274,17 @@ def set_cooling_level(cls, level, cur_state):
311
274
# Reset FAN cooling level vector. According to low level team,
312
275
# if we need set cooling level to X, we need first write a (10+X)
313
276
# to cooling_cur_state file to reset the cooling level vector.
314
- with open (COOLING_STATE_PATH , 'w' ) as cooling_state :
315
- cooling_state .write (str (level + 10 ))
277
+ write_file (COOLING_STATE_PATH , level + 10 , raise_exception = True )
316
278
317
279
# We need set cooling level after resetting the cooling level vector
318
- with open (COOLING_STATE_PATH , 'w' ) as cooling_state :
319
- cooling_state .write (str (cur_state ))
280
+ write_file (COOLING_STATE_PATH , cur_state , raise_exception = True )
320
281
except (ValueError , IOError ) as e :
321
282
raise RuntimeError ("Failed to set cooling level - {}" .format (e ))
322
283
323
284
@classmethod
324
285
def get_cooling_level (cls ):
325
286
try :
326
- with open (COOLING_STATE_PATH , 'r' ) as cooling_state :
327
- cooling_level = int (cooling_state .read ().strip ())
328
- return cooling_level
287
+ return read_int_from_file (COOLING_STATE_PATH , raise_exception = True )
329
288
except (ValueError , IOError ) as e :
330
289
raise RuntimeError ("Failed to get cooling level - {}" .format (e ))
331
290
0 commit comments