Skip to content

Commit

Permalink
Added BFN PSU cache (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Kokhan <[email protected]>
  • Loading branch information
Andriy Kokhan authored and VadymYashchenko committed Jan 13, 2022
1 parent a7ad772 commit bc20eca
Showing 1 changed file with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
try:
import os
import sys
import time

sys.path.append(os.path.dirname(__file__))

Expand All @@ -18,6 +19,8 @@ class Psu(PsuBase):
def __init__(self, index):
PsuBase.__init__(self)
self.__index = index
self.__info = None
self.__ts = 0
# STUB IMPLEMENTATION
self.color = ""

Expand All @@ -33,7 +36,16 @@ def __info_get(self):
def psu_info_get(client):
return client.pltfm_mgr.pltfm_mgr_pwr_supply_info_get(self.__index)

return thrift_try(psu_info_get)
# Update cache once per 2 seconds
if self.__ts + 2 < time.time():
self.__info = None
try:
self.__info = thrift_try(psu_info_get, attempts=1)
finally:
self.__ts = time.time()
return self.__info
return self.__info


@staticmethod
def get_num_psus():
Expand All @@ -51,6 +63,8 @@ def get_powergood_status(self):
:return: Boolean, True if PSU is operating properly, False if PSU is faulty
"""
info = self.__info_get()
if info is None:
return False
return info.ffault == False and info.vout != 0

def get_voltage(self):
Expand All @@ -61,7 +75,8 @@ def get_voltage(self):
A float number, the output voltage in volts,
e.g. 12.1
"""
return float(self.__info_get().vout)
info = self.__info_get()
return float(info.vout) if info else 0

def get_current(self):
"""
Expand All @@ -70,7 +85,8 @@ def get_current(self):
Returns:
A float number, the electric current in amperes, e.g 15.4
"""
return self.__info_get().iout / 1000.
info = self.__info_get()
return info.iout / 1000 if info else 0

def get_power(self):
"""
Expand All @@ -79,7 +95,8 @@ def get_power(self):
Returns:
A float number, the power in watts, e.g. 302.6
"""
return self.__info_get().pwr_out / 1000.
info = self.__info_get()
return info.pwr_out / 1000 if info else 0

def get_presence(self):
"""
Expand All @@ -91,8 +108,11 @@ def get_presence(self):
def psu_present_get(client):
return client.pltfm_mgr.pltfm_mgr_pwr_supply_present_get(self.__index)

status = thrift_try(psu_present_get)
return status
status = False
try:
status = thrift_try(psu_present_get)
finally:
return status

def set_status_led(self, color):
"""
Expand Down Expand Up @@ -127,7 +147,8 @@ def get_serial(self):
Returns:
string: Serial number of device
"""
return self.__info_get().serial
info = self.__info_get()
return info.serial if info else "N/A"

def get_model(self):
"""
Expand All @@ -136,7 +157,8 @@ def get_model(self):
Returns:
string: Model/part number of device
"""
return self.__info_get().model
info = self.__info_get()
return info.model if info else "N/A"

def is_replaceable(self):
"""
Expand All @@ -153,7 +175,8 @@ def get_revision(self):
Returns:
string: Revision value of device
"""
return self.__info_get().rev
info = self.__info_get()
return info.rev if info else "N/A"

def get_status(self):
"""
Expand Down

0 comments on commit bc20eca

Please sign in to comment.