import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib') 
from gpiozero import PWMOutputDevice, CPUTemperature
import time
import logging
logging.basicConfig(filename='/storage/.kodi/temp/fan.log', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d %b %Y %T', level=logging.DEBUG)

GPIO_PIN = 18
SLEEP = 5
DEFAULT_PWM = 1.0
debugFlag=False  # set to True to debug

if debugFlag: 
    cpu_temp = str(CPUTemperature()).split("=")
    temp = float(str(cpu_temp[1])[:-1])
    print("Fan Starting - TEMP: " + str(temp) + "°C")
    logging.info("Fan Starting - TEMP: " + str(temp) + "°C")

if __name__ == "__main__":
    fan = PWMOutputDevice(GPIO_PIN)
    fan.frequency=300
    cpu = CPUTemperature()
    fan.value = DEFAULT_PWM
    try:
        while True:
            cpu_temp = str(CPUTemperature()).split("=")           
            temp = float(str(cpu_temp[1])[:-1])
            if temp > 70.0:
                fan.value=0.9
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 90%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 90%")
            elif temp > 65.0:
                fan.value=0.8
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 80%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 80%")        
            elif temp > 60.0:
                fan.value=0.7
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 70%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 70%")        
            elif temp > 55.0:
                fan.value=0.6
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 60%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 60%")
            elif temp > 50.0:
                fan.value=0.5
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 50%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 50%")
            elif temp > 45.0:
                fan.value=0.4
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 40%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 40%")
            else:
                fan.value=0.3
                if debugFlag: 
                    print("TEMP: " + str(temp) + "°C - PWM set at: 30%")
                    logging.debug("TEMP: " + str(temp) + "°C - PWM set at: 30%")                    
            time.sleep(SLEEP)  
    
    except KeyboardInterrupt:
        print("Fan control interrupted by keyboard")
        print("Set fan PWM default to 1.0")
        fan.value = DEFAULT_PWM
        sys.exit()
    except Exception as e:
        print("Unhandled exception")
        print(e)
        print("Set fan PWM default to 1.0")
        fan.value = DEFAULT_PWM    