Ejemplo n.º 1
0
class Rotor:
    def __init__(self, config):
        if config is not None and isinstance(config, dict):
            self.__reg_speed_getter = Reg(config.get("speed_getter"))
            self.__reg_speed_setter = Reg(config.get("speed_setter"))
            self.__speed_max = config.get("speed_max")
        else:
            raise ValueError("init rotor Error: {}".format(config))

    def get_speed(self):
        try:
            return int(self.__reg_speed_getter.decode())
        except Exception as e:
            logger.error(e.message)

        return 0

    def set_speed(self, speed):
        try:
            return self.__reg_speed_setter.encode(speed)
        except Exception as e:
            logger.error(e.message)

        return False

    def get_speed_percentage(self):
        try:
            speed = self.get_speed()
            return (100 * speed) / self.__speed_max
        except Exception as e:
            logger.error(e.message)

        return 0
Ejemplo n.º 2
0
class Thermal(ThermalBase):
    def __init__(self, name, config=None, hal_thermal=None):
        self.name = name
        if config:
            self.__reg_low_threshold = Reg(config.get("low"))
            self.__reg_high_thresnold = Reg(config.get("high"))
            self.__reg_crit_low_threshold = Reg(config.get("crit_low"))
            self.__reg_crit_high_thresnold = Reg(config.get("crit_high"))
            self.__reg_temperature = Reg(config.get("temperature"))

    def get_name(self):
        """
        Retrieves the name of the thermal

        Returns:
            string: The name of the thermal
        """
        return self.name

    def get_presence(self):
        """
        Retrieves the presence of the thermal

        Returns:
            bool: True if thermal is present, False if not
        """
        return True

    def get_model(self):
        """
        Retrieves the model number (or part number) of the Thermal

        Returns:
            string: Model/part number of Thermal
        """
        return "NA"

    def get_serial(self):
        """
        Retrieves the serial number of the Thermal

        Returns:
            string: Serial number of Thermal
        """
        return "NA"

    def get_status(self):
        """
        Retrieves the operational status of the thermal

        Returns:
            A boolean value, True if thermal is operating properly,
            False if not
        """
        if self.get_temperature() == 0.0:
            return False

        return True

    def get_temperature(self):
        """
        Retrieves current temperature reading from thermal

        Returns:
            A float number of current temperature in Celsius up to nearest thousandth
            of one degree Celsius, e.g. 30.125 
        """
        try:
            if isinstance(self.__reg_temperature, Reg):
                return self.__reg_temperature.decode()
        except Exception as e:
            logger.error(e.message)

        return None

    def get_high_threshold(self):
        """
        Retrieves the high threshold temperature of thermal

        Returns:
            A float number, the high threshold temperature of thermal in Celsius
            up to nearest thousandth of one degree Celsius, e.g. 30.125
        """
        try:
            if isinstance(self.__reg_high_thresnold, Reg):
                return float(self.__reg_high_thresnold.decode())
        except Exception as e:
            logger.error(e.message)

        return None

    def get_low_threshold(self):
        """
        Retrieves the low threshold temperature of thermal

        Returns:
            A float number, the low threshold temperature of thermal in Celsius
            up to nearest thousandth of one degree Celsius, e.g. 30.125
        """
        try:
            if isinstance(self.__reg_low_threshold, Reg):
                return float(self.__reg_low_threshold.decode())
        except Exception as e:
            logger.error(e.message)

        return None

    def set_high_threshold(self, temperature):
        """
        Sets the high threshold temperature of thermal

        Args : 
            temperature: A float number up to nearest thousandth of one degree Celsius, 
            e.g. 30.125

        Returns:
            A boolean, True if threshold is set successfully, False if not
        """
        try:
            if isinstance(self.__reg_high_thresnold, Reg):
                temp_val = str(int(temperature * 1000))
                return self.__reg_high_thresnold.encode(temp_val)
        except Exception as e:
            logger.error(str(e))

        return False

    def set_low_threshold(self, temperature):
        """
        Sets the low threshold temperature of thermal

        Args : 
            temperature: A float number up to nearest thousandth of one degree Celsius,
            e.g. 30.125

        Returns:
            A boolean, True if threshold is set successfully, False if not
        """
        # not supported
        return False

    def get_high_critical_threshold(self):
        """
        Retrieves the high critical threshold temperature of thermal

        Returns:
            A float number, the high critical threshold temperature of thermal in Celsius
            up to nearest thousandth of one degree Celsius, e.g. 30.125
        """
        try:
            if isinstance(self.__reg_crit_high_thresnold, Reg):
                return float(self.__reg_crit_high_thresnold.decode())
        except Exception as e:
            logger.error(e.message)

        return None

    def get_low_critical_threshold(self):
        """
        Retrieves the low critical threshold temperature of thermal

        Returns:
            A float number, the low critical threshold temperature of thermal in Celsius
            up to nearest thousandth of one degree Celsius, e.g. 30.125
        """
        try:
            if isinstance(self.__reg_crit_low_threshold, Reg):
                return float(self.__reg_crit_low_threshold.decode())
        except Exception as e:
            logger.error(e.message)

        return None
Ejemplo n.º 3
0
class Fan(FanBase):
    """Ruijie Platform-specific Fan class"""

    MAX_SPEED_CODE = 255

    def __init__(self, index, config=None, hal_fan=None, is_psu_fan=False):
        self.index = index
        self.is_psu_fan = is_psu_fan
        if config:
            if self.is_psu_fan:
                self.name = "Psu{}-{}".format(self.index, config.get("name"))
            else:
                self.name = config.get("name")
            self.__reg_sn = Reg(config.get("sn"))
            self.__reg_present = Reg(config.get("present"))
            self.__reg_status = Reg(config.get("status"))
            self.__reg_led = Reg(config.get("led"))
            self.__reg_pn = Reg(config.get("pn"))
            self.__led_colors = config.get("led_colors")

            # rotors
            rotors = config.get("rotors")
            if isinstance(rotors, list):
                self.__rotors = []
                for rotor in rotors:
                    self.__rotors.append(Rotor(rotor))

        self._hal_fan = hal_fan

    def _reg_setter(self, target, val):
        if isinstance(val, dict):
            target = Reg(val)
        elif isinstance(val, Reg):
            target = val
        else:
            raise ValueError
        return target

    @property
    def reg_sn(self):
        return self.__reg_sn

    @reg_sn.setter
    def reg_sn(self, val):
        self._reg_setter(self.__reg_sn, val)

    @property
    def reg_present(self):
        return self.__reg_present

    @reg_present.setter
    def reg_present(self, val):
        self._reg_setter(self.__reg_present, val)

    @property
    def reg_status(self):
        return self.__reg_status

    @reg_status.setter
    def reg_status(self, val):
        self._reg_setter(self.__reg_status, val)

    @property
    def reg_led(self):
        return self.__reg_led

    @reg_led.setter
    def reg_led(self, val):
        self._reg_setter(self.__reg_led, val)

    def get_name(self):
        """
        Retrieves the fan name
        Returns:
            string: The name of the device
        """
        return self.name

    def get_model(self):
        """
        Retrieves the part number of the FAN
        Returns:
            string: Part number of FAN
        """
        if self._hal_fan:
            return self._hal_fan.pn()

        try:
            if isinstance(self.__reg_pn, Reg):
                return self.__reg_pn.decode()
        except Exception as e:
            logger.error(str(e))

        return "NA"

    def get_serial(self):
        """
        Retrieves the serial number of the FAN
        Returns:
            string: Serial number of FAN
        """
        if self._hal_fan:
            return self._hal_fan.sn()

        try:
            if isinstance(self.__reg_sn, Reg):
                return self.__reg_sn.decode()
        except Exception as e:
            logger.error(str(e))

        return "NA"

    def get_presence(self):
        """
        Retrieves the presence of the FAN
        Returns:
            bool: True if fan is present, False if not
        """

        # print self.fan_presence_reg.decode()
        # return True if self.fan_presence_reg.decode() == 0 else False
        if self._hal_fan:
            return self._hal_fan.get_presence()

        try:
            if isinstance(self.__reg_present, Reg):
                present = self.__reg_present.decode()
                if present == 0 or present == "0":
                    return True
        except Exception as e:
            logger.error(str(e))

        return False

    def get_status(self):
        """
        Retrieves the operational status of the FAN
        Returns:
            bool: True if FAN is operating properly, False if not
        """

        # return True if self.fan_status_reg.decode() == 1 else False
        if self._hal_fan:
            return self._hal_fan.get_status()

        try:
            if isinstance(self.__reg_status, Reg):
                status = self.__reg_status.decode()
                if status == 1 or status == "1":
                    return True
        except Exception as e:
            pass

        return False

    def get_direction(self):
        """
        Retrieves the fan airflow direction
        Returns:
            A string, either FAN_DIRECTION_INTAKE or FAN_DIRECTION_EXHAUST
            depending on fan direction

        Notes:
            - Forward/Exhaust : Air flows from Port side to Fan side.
            - Reverse/Intake  : Air flows from Fan side to Port side.
        """

        # TODO
        return self.FAN_DIRECTION_EXHAUST

    def get_speed(self):
        """
        Retrieves the speed of fan
        Returns:
            int: percentage of the max fan speed
        """
        if self.get_presence():
            maxspeed = 0
            for r in self.__rotors:
                speed = r.get_speed_percentage()
                if speed > maxspeed:
                    maxspeed = speed
            return maxspeed
        else:
            return 0

    def get_speed_tolerance(self):
        """
        Retrieves the speed tolerance of the fan
        Returns:
            An integer, the percentage of variance from target speed which is
        considered tolerable
        """
        # TODO
        return 0

    def set_speed(self, speed):
        """
        Set fan speed to expected value
        Args:
            speed: An integer, the percentage of full fan speed to set fan to,
                   in the range 0 (off) to 100 (full speed)
        Returns:
            bool: True if set success, False if fail.
        """
        if self.__rotors:
            speed_code = hex(
                int(ceil(float(self.MAX_SPEED_CODE) / 100 * speed)))
            return self.__rotors[0].set_speed(speed_code)

        return False

    def set_status_led(self, color):
        """
        Set led to expected color
        Args:
            color: A string representing the color with which to set the
                   fan module status LED
        Returns:
            bool: True if set success, False if fail.
        """
        # TODO
        if self.is_psu_fan:
            # No LED available for PSU Fan
            return False
        try:
            if color not in self.__led_colors:
                logger.error("color:%s not defined." % color)
                return False
            val = hex(self.__led_colors[color])
            if isinstance(self.__reg_led, Reg):
                return self.__reg_led.encode(val)
            return ret
        except Exception as e:
            logger.error(str(e))
        return False

    def get_status_led(self):
        """
        Gets the state of the Fan status LED

        Returns:
            A string, one of the predefined STATUS_LED_COLOR_* strings.
        """
        # TODO
        if self.is_psu_fan:
            # No LED available for PSU Fan
            return None
        else:
            try:
                if isinstance(self.__reg_led, Reg):
                    led_color = self.__reg_led.decode()
                    for color, code in self.__led_colors.items():
                        if code ^ led_color == 0:
                            return color
            except Exception as e:
                logger.error(str(e))

            return None

    def get_target_speed(self):
        """
        Retrieves the target (expected) speed of the fan
        Returns:
            An integer, the percentage of full fan speed, in the range 0 (off)
                 to 100 (full speed)
        """
        # TODO
        return 0