sonic-buildimage/device/common/pddf/plugins/fanutil.py
Joe LeVeque 7f4ab8fbd8
[sonic-utilities] Update submodule; Build and install as a Python 3 wheel (#5926)
Submodule updates include the following commits:

* src/sonic-utilities 9dc58ea...f9eb739 (18):
  > Remove unnecessary calls to str.encode() now that the package is Python 3; Fix deprecation warning (#1260)
  > [generate_dump] Ignoring file/directory not found Errors (#1201)
  > Fixed porstat rate and util issues (#1140)
  > fix error: interface counters is mismatch after warm-reboot (#1099)
  > Remove unnecessary calls to str.decode() now that the package is Python 3 (#1255)
  > [acl-loader] Make list sorting compliant with Python 3 (#1257)
  > Replace hard-coded fast-reboot with variable. And some typo corrections (#1254)
  > [configlet][portconfig] Remove calls to dict.has_key() which is not available in Python 3 (#1247)
  > Remove unnecessary conversions to list() and calls to dict.keys() (#1243)
  > Clean up LGTM alerts (#1239)
  > Add 'requests' as install dependency in setup.py (#1240)
  > Convert to Python 3 (#1128)
  > Fix mock SonicV2Connector in python3: use decode_responses mode so caller code will be the same as python2 (#1238)
  > [tests] Do not trim from PATH if we did not append to it; Clean up/fix shebangs in scripts (#1233)
  > Updates to bgp config and show commands with BGP_INTERNAL_NEIGHBOR table (#1224)
  > [cli]: NAT show commands newline issue after migrated to Python3 (#1204)
  > [doc]: Update Command-Reference.md (#1231)
  > Added 'import sys' in feature.py file (#1232)

* src/sonic-py-swsssdk 9d9f0c6...1664be9 (2):
  > Fix: no need to decode() after redis client scan, so it will work for both python2 and python3 (#96)
  > FieldValueMap `contains`(`in`)  will also work when migrated to libswsscommon(C++ with SWIG wrapper) (#94)

- Also fix Python 3-related issues:
    - Use integer (floor) division in config_samples.py (sonic-config-engine)
    - Replace print statement with print function in eeprom.py plugin for x86_64-kvm_x86_64-r0 platform
    - Update all platform plugins to be compatible with both Python 2 and Python 3
    - Remove shebangs from plugins files which are not intended to be executable
    - Replace tabs with spaces in Python plugin files and fix alignment, because Python 3 is more strict
    - Remove trailing whitespace from plugins files
2020-11-25 10:28:36 -08:00

200 lines
5.3 KiB
Python
Executable File

# Sample pddf_fanutil file
# All the supported FAN SysFS aattributes are
#- fan<idx>_present
#- fan<idx>_direction
#- fan<idx>_input
#- fan<idx>_pwm
#- fan<idx>_fault
# where idx is in the range [1-12]
#
import os.path
import sys
sys.path.append('/usr/share/sonic/platform/plugins')
import pddfparse
import json
try:
from sonic_fan.fan_base import FanBase
except ImportError as e:
raise ImportError(str(e) + "- required module not found")
class FanUtil(FanBase):
"""PDDF generic FAN util class"""
def __init__(self):
FanBase.__init__(self)
global pddf_obj
global plugin_data
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)) + '/../pddf/pd-plugin.json')) as pd:
plugin_data = json.load(pd)
pddf_obj = pddfparse.PddfParse()
self.platform = pddf_obj.get_platform()
self.num_fans = (self.platform['num_fantrays'] * self.platform['num_fans_pertray'])
def get_num_fans(self):
return self.num_fans
def get_presence(self, idx):
# 1 based fan index
if idx < 1 or idx > self.num_fans:
print("Invalid fan index %d\n" % idx)
return False
attr_name = "fan" + str(idx) + "_present"
output = pddf_obj.get_attr_name_output("FAN-CTRL", attr_name)
if not output:
return False
mode = output['mode']
presence = output['status'].rstrip()
vmap = plugin_data['FAN']['present'][mode]['valmap']
if presence in vmap:
status = vmap[presence]
else:
status = False
return status
def get_status(self, idx):
# 1 based fan index
if idx < 1 or idx > self.num_fans:
print("Invalid fan index %d\n" % idx)
return False
speed = self.get_speed(idx)
status = True if (speed != 0) else False
return status
def get_direction(self, idx):
# 1 based fan index
if idx < 1 or idx > self.num_fans:
print("Invalid fan index %d\n" % idx)
return None
attr = "fan" + str(idx) + "_direction"
output = pddf_obj.get_attr_name_output("FAN-CTRL", attr)
if not output:
return None
mode = output['mode']
val = output['status']
val = val.rstrip()
vmap = plugin_data['FAN']['direction'][mode]['valmap']
if val in vmap:
direction = vmap[val]
else:
direction = val
return direction
def get_directions(self):
num_fan = self.get_num_fan()
for i in range(1, num_fan+1):
attr = "fan" + str(i) + "_direction"
output = pddf_obj.get_attr_name_output("FAN-CTRL", attr)
if not output:
return None
mode = output['mode']
val = output['status']
val = val.rstrip()
vmap = plugin_data['FAN']['direction'][mode]['valmap']
direction = vmap[str(val)]
print("FAN-%d direction is %s" % (i, direction))
return 0
def get_speed(self, idx):
# 1 based fan index
if idx < 1 or idx > self.num_fans:
print("Invalid fan index %d\n" % idx)
return 0
attr = "fan" + str(idx) + "_input"
output = pddf_obj.get_attr_name_output("FAN-CTRL", attr)
if not output:
return 0
#mode = output['mode']
val = output['status'].rstrip()
if val.isalpha():
return 0
else:
rpm_speed = int(float(val))
return rpm_speed
def get_speeds(self):
num_fan = self.get_num_fan()
ret = "FAN_INDEX\t\tRPM\n"
for i in range(1, num_fan+1):
attr1 = "fan" + str(i) + "_input"
output = pddf_obj.get_attr_name_output("FAN-CTRL", attr1)
if not output:
return ""
#mode = output['mode']
val = output['status'].rstrip()
if val.isalpha():
frpm = 0
else:
frpm = int(val)
ret += "FAN-%d\t\t\t%d\n" % (i, frpm)
return ret
def set_speed(self, val):
if val < 0 or val > 100:
print("Error: Invalid speed %d. Please provide a valid speed percentage" % val)
return False
num_fan = self.num_fans
if 'duty_cycle_to_pwm' not in plugin_data['FAN']:
print("Setting fan speed is not allowed !")
return False
else:
duty_cycle_to_pwm = eval(plugin_data['FAN']['duty_cycle_to_pwm'])
pwm = duty_cycle_to_pwm(val)
print("New Speed: %d%% - PWM value to be set is %d\n" % (val, pwm))
for i in range(1, num_fan+1):
attr = "fan" + str(i) + "_pwm"
node = pddf_obj.get_path("FAN-CTRL", attr)
if node is None:
return False
try:
with open(node, 'w') as f:
f.write(str(pwm))
except IOError:
return False
return True
def dump_sysfs(self):
return pddf_obj.cli_dump_dsysfs('fan')
def get_change_event(self):
"""
TODO: This function need to be implemented
when decide to support monitoring FAN(fand)
on this platform.
"""
raise NotImplementedError