12
12
raise ImportError (str (e ) + "- required module not found" )
13
13
14
14
15
+ GPIO_DIR = "/sys/class/gpio"
16
+ GPIO_LABEL = "pca9505"
17
+ DX010_MAX_PSUS = 2
18
+
19
+
15
20
class PsuUtil (PsuBase ):
16
21
"""Platform-specific PSUutil class"""
17
22
18
23
def __init__ (self ):
19
24
PsuBase .__init__ (self )
20
25
# DX010 PSU pin mapping
21
- self .psu = [
22
- {'base' : self .get_gpio_base ()},
23
- {'abs' : 27 , 'power' : 22 },
24
- {'abs' : 28 , 'power' : 25 }
26
+ self .dx010_psu_gpio = [
27
+ {'base' : self .__get_gpio_base ()},
28
+ {'prs' : 27 , 'status' : 22 },
29
+ {'prs' : 28 , 'status' : 25 }
25
30
]
26
31
27
- def get_gpio_base (self ):
28
- sys_gpio_dir = "/sys/class/gpio"
29
- for r in os .listdir (sys_gpio_dir ):
30
- if "gpiochip" in r :
31
- return int (r [8 :],10 )
32
- return 216 #Reserve
33
-
34
-
35
- # Get a psu status and presence
36
- def read_psu_statuses (self , pinnum ):
37
- sys_gpio_dir = "/sys/class/gpio"
38
- gpio_base = self .psu [0 ]['base' ]
39
-
40
- gpio_dir = sys_gpio_dir + '/gpio' + str (gpio_base + pinnum )
41
- gpio_file = gpio_dir + "/value"
42
-
32
+ def __read_txt_file (self , file_path ):
43
33
try :
44
- with open (gpio_file , 'r' ) as fd :
45
- retval = fd .read ()
34
+ with open (file_path , 'r' ) as fd :
35
+ data = fd .read ()
36
+ return data .strip ()
46
37
except IOError :
47
- raise IOError ("Unable to open " + gpio_file + "file !" )
48
-
49
- retval = retval .rstrip ('\r \n ' )
50
- return retval
38
+ pass
39
+ return ""
40
+
41
+ def __get_gpio_base (self ):
42
+ for r in os .listdir (GPIO_DIR ):
43
+ label_path = os .path .join (GPIO_DIR , r , "label" )
44
+ if "gpiochip" in r and GPIO_LABEL in self .__read_txt_file (label_path ):
45
+ return int (r [8 :], 10 )
46
+ return 216 # Reserve
47
+
48
+ def __get_gpio_value (self , pinnum ):
49
+ gpio_base = self .dx010_psu_gpio [0 ]['base' ]
50
+ gpio_dir = GPIO_DIR + '/gpio' + str (gpio_base + pinnum )
51
+ gpio_file = gpio_dir + "/value"
52
+ retval = self .__read_txt_file (gpio_file )
53
+ return retval .rstrip ('\r \n ' )
51
54
52
55
def get_num_psus (self ):
53
56
"""
54
57
Retrieves the number of PSUs available on the device
55
58
:return: An integer, the number of PSUs available on the device
56
59
"""
57
- DX010_MAX_PSUS = 2
58
60
return DX010_MAX_PSUS
59
61
60
62
def get_psu_status (self , index ):
@@ -65,14 +67,9 @@ def get_psu_status(self, index):
65
67
:return: Boolean, True if PSU is operating properly, False if PSU is\
66
68
faulty
67
69
"""
68
- status = 0
69
- psu_status = self .read_psu_statuses (self .psu [index ]['power' ])
70
- psu_status = int (psu_status , 10 )
71
- # Check for PSU status
72
- if (psu_status == 1 ):
73
- status = 1
74
-
75
- return status
70
+ raw = self .__get_gpio_value (
71
+ self .dx010_psu_gpio [index ]['status' ])
72
+ return int (raw , 10 ) == 1
76
73
77
74
def get_psu_presence (self , index ):
78
75
"""
@@ -81,11 +78,5 @@ def get_psu_presence(self, index):
81
78
:param index: An integer, index of the PSU of which to query status
82
79
:return: Boolean, True if PSU is plugged, False if not
83
80
"""
84
- status = 0
85
- psu_absence = self .read_psu_statuses (self .psu [index ]['abs' ])
86
- psu_absence = (int (psu_absence , 10 ))
87
- # Check for PSU presence
88
- if (psu_absence == 0 ):
89
- status = 1
90
-
91
- return status
81
+ raw = self .__get_gpio_value (self .dx010_psu_gpio [index ]['prs' ])
82
+ return int (raw , 10 ) == 0
0 commit comments