def manage_fans()

in platform/broadcom/sonic-platform-modules-accton/as9726-32d/utils/accton_as9726_32d_monitor.py [0:0]


    def manage_fans(self):
       
        global fan_policy_state
        global fan_policy_alarm
        global send_yellow_alarm
        global send_red_alarm
        global fan_fail
        global count_check
        global test_temp
        global test_temp_list
        global temp_test_data
        global test_temp_revert
      
        CHECK_TIMES=3
           
        LEVEL_FAN_INIT=0
        LEVEL_FAN_MIN=1
        LEVEL_FAN_MID=2       
        LEVEL_FAN_MAX=3
        LEVEL_FAN_YELLOW_ALARM=4
        LEVEL_FAN_RED_ALARM=5
        LEVEL_FAN_SHUTDOWN=6
    
        fan_policy_f2b = {  #AFO
            LEVEL_FAN_MIN: [50,  0x7],
            LEVEL_FAN_MID: [75,  0xb],
            LEVEL_FAN_MAX: [100, 0xf]
        }
        fan_policy_b2f = { #AFI
            LEVEL_FAN_MID: [75,  0xb],
            LEVEL_FAN_MAX: [100, 0xf]
        }
         
        afi_thermal_spec={
            "mid_to_max_temp":[61500, 51500, 49400, 49400, 45100, 46750, 48000, 38500],
            "max_to_mid_temp":[57000, 47300, 45000, 45100, 40750, 42100, 44000, 35000],
            "max_to_yellow_alarm":   [57900, 51900, 48900, 55900, 48500, 52000, 41800], 
            "yellow_to_red_alarm":   [62900, 56900, 53900, 58900, 53500, 57000, 46800], 
            "red_alarm_to_shutdown": [67900, 61900, 58900, 63900, 58500, 62000, 51800]
        }
        afo_thermal_spec={
            "min_to_mid_temp": [63000, 60500, 60000, 60000, 61000, 72000, 50000],
            "mid_to_max_temp": [63000, 60000, 60000, 59000, 60000, 69000, 51500],
            "max_to_mid_temp": [56000, 53500, 52500, 52000, 52800, 62000, 45800],
            "mid_to_min_temp": [50000, 47300, 46400, 44600, 47000, 56000, 38800],
            "max_to_yellow_alarm":   [67000, 65000, 64000, 62000, 64000, 73000, 67000], 
            "yellow_to_red_alarm":   [72000, 70000, 69000, 67000, 69000, 78000, 72000], 
            "red_alarm_to_shutdown": [77000, 75000, 74000, 72000, 74000, 83000, 77000] 
        }

        thermal_val=[0,0,0,0,0,0,0]        
        max_to_mid=0
        mid_to_min=0
        
        fan = self.fan
        if fan_policy_state==LEVEL_FAN_INIT:
            fan_policy_state=LEVEL_FAN_MAX #This is default state
            logging.debug("fan_policy_state=LEVEL_FAN_MAX")
            return
    
        count_check=count_check+1
        if count_check < CHECK_TIMES:
            return
        else:
            count_check=0
        
        thermal = self.thermal
        fan_dir=fan.get_fan_dir(1)
       
        if fan_dir==1:   # AFI
            fan_thermal_spec = afi_thermal_spec
            fan_policy=fan_policy_b2f
        elif fan_dir==0: # AFO
            fan_thermal_spec = afo_thermal_spec
            fan_policy=fan_policy_f2b           
        else:
            logging.debug( "NULL case")
        
        ori_duty_cycle=fan.get_fan_duty_cycle()
        new_duty_cycle=0

        if test_temp_revert==0:
            temp_test_data=temp_test_data+2000
        else:            
            temp_test_data=temp_test_data-2000
            
        if test_temp==0:
            for i in range (thermal.THERMAL_NUM_1_IDX, thermal.THERMAL_NUM_MAX+1):
                thermal_val[i-1]=thermal._get_thermal_val(i)
        else:
            for i in range (thermal.THERMAL_NUM_1_IDX, thermal.THERMAL_NUM_MAX+1):
                thermal_val[i-1]=test_temp_list[i-1]
                thermal_val[i-1]= thermal_val[i-1] + temp_test_data
            logging.debug(thermal_val)
            fan_fail=0
       
        ori_state=fan_policy_state;
        current_state=fan_policy_state;
       
        if fan_dir==1: #AFI
            for i in range (0, thermal.THERMAL_NUM_MAX):
                if ori_state==LEVEL_FAN_MID:
                    if thermal_val[i] >= fan_thermal_spec["mid_to_max_temp"][i]:
                        current_state=LEVEL_FAN_MAX
                        logging.debug("current_state=LEVEL_FAN_MAX")
                        break
                else:
                    if (thermal_val[i] <= fan_thermal_spec["max_to_mid_temp"][i]):
                        max_to_mid=max_to_mid+1
                    if fan_policy_alarm==0: 
                        if thermal_val[i] >= fan_thermal_spec["max_to_yellow_alarm"][i]:
                            if send_yellow_alarm==0:
                                logging.warning('Alarm-Yellow for temperature high is detected')
                                fan_policy_alarm=LEVEL_FAN_YELLOW_ALARM
                                send_yellow_alarm=1
                    elif fan_policy_alarm==LEVEL_FAN_YELLOW_ALARM:
                        if thermal_val[i] >= fan_thermal_spec["yellow_to_red_alarm"][i]:
                            if send_red_alarm==0:
                                logging.warning('Alarm-Red for temperature high is detected')
                                fan_policy_alarm=LEVEL_FAN_RED_ALARM
                                send_red_alarm=1
                    elif fan_policy_alarm==LEVEL_FAN_RED_ALARM:
                        if thermal_val[i] >= fan_thermal_spec["red_alarm_to_shutdown"][i]:
                            logging.critical('Alarm-Critical for temperature high is detected, shutdown DUT')                            
                            fan_policy_alarm=LEVEL_FAN_SHUTDOWN
                            time.sleep(2)
                            power_off_dut()
        
            if max_to_mid==thermal.THERMAL_NUM_MAX and  fan_policy_state==LEVEL_FAN_MAX:
                current_state=LEVEL_FAN_MID
                if fan_policy_alarm!=0:
                    logging.warning('Alarm for temperature high is cleared')
                    fan_policy_alarm=0
                    send_yellow_alarm=0
                    send_red_alarm=0
                    test_temp_revert=0
                logging.debug("current_state=LEVEL_FAN_MID")
                
        else: #AFO
            psu_full_load=check_psu_loading()
            for i in range (0, thermal.THERMAL_NUM_MAX):
                if ori_state==LEVEL_FAN_MID:
                    if thermal_val[i] >= fan_thermal_spec["mid_to_max_temp"][i]:
                        current_state=LEVEL_FAN_MAX
                        break
                    else:
                        if psu_full_load!=True and thermal_val[i] <= fan_thermal_spec["mid_to_min_temp"][i]:
                            mid_to_min=mid_to_min+1

                elif ori_state==LEVEL_FAN_MIN:
                    if psu_full_load==True:
                        current_state=LEVEL_FAN_MID
                        logging.debug("psu_full_load, set current_state=LEVEL_FAN_MID")
                    if thermal_val[i] >= fan_thermal_spec["min_to_mid_temp"][i]:
                        current_state=LEVEL_FAN_MID

                else:
                    if thermal_val[i] <= fan_thermal_spec["max_to_mid_temp"][i] :
                        max_to_mid=max_to_mid+1
                    if fan_policy_alarm==0: 
                        if thermal_val[i] >= fan_thermal_spec["max_to_yellow_alarm"][i]:
                            if send_yellow_alarm==0:
                                logging.warning('Alarm-Yellow for temperature high is detected')
                                fan_policy_alarm=LEVEL_FAN_YELLOW_ALARM
                                send_yellow_alarm=1
                    elif fan_policy_alarm==LEVEL_FAN_YELLOW_ALARM:
                        if thermal_val[i] >= fan_thermal_spec["yellow_to_red_alarm"][i]:
                            if send_red_alarm==0:
                                logging.warning('Alarm-Red for temperature high is detected')
                                fan_policy_alarm=LEVEL_FAN_RED_ALARM
                                send_red_alarm=1
                    elif fan_policy_alarm==LEVEL_FAN_RED_ALARM:
                        if thermal_val[i] >= fan_thermal_spec["red_alarm_to_shutdown"][i]:
                            logging.critical('Alarm-Critical for temperature high is detected, shutdown DUT')                            
                            fan_policy_alarm=LEVEL_FAN_SHUTDOWN
                            time.sleep(2)
                            power_off_dut()
                        
            if max_to_mid==thermal.THERMAL_NUM_MAX and ori_state==LEVEL_FAN_MAX:
                current_state=LEVEL_FAN_MID
                if fan_policy_alarm!=0:
                    logging.warning('Alarm for temperature high is cleared')
                    fan_policy_alarm=0
                    send_yellow_alarm=0
                    send_red_alarm=0
                    test_temp_revert=0
                logging.debug("current_state=LEVEL_FAN_MID")
            
            if mid_to_min==thermal.THERMAL_NUM_MAX and ori_state==LEVEL_FAN_MID:
                if psu_full_load==0:
                    current_state=LEVEL_FAN_MIN
                    logging.debug("current_state=LEVEL_FAN_MIN")
           
        #Check Fan fault status. True: fan not fault/present, 1: fan fault/un-present
        for i in range (fan.FAN_NUM_1_IDX, fan.FAN_NUM_ON_MAIN_BROAD+1):
            if fan.get_fan_status(i)==False:
                new_duty_cycle=100
                current_state=LEVEL_FAN_MAX
                logging.debug('fan_%d fail, set duty_cycle to 100',i)                
                if test_temp==0:
                    fan_fail=1
                    fan.set_fan_duty_cycle(new_duty_cycle)
                    break
            else:
                fan_fail=0

        if current_state!=ori_state:
            fan_policy_state=current_state
            new_duty_cycle=fan_policy[current_state][0]
            logging.debug("fan_policy_state=%d, new_duty_cycle=%d", fan_policy_state, new_duty_cycle)
            if new_duty_cycle!=ori_duty_cycle and fan_fail==0:
                fan.set_fan_duty_cycle(new_duty_cycle)
                return True
            if new_duty_cycle==0 and fan_fail==0:
                fan.set_fan_duty_cycle(FAN_DUTY_CYCLE_MAX)

        return True