def set_ac_sleep(time_to_sleep):

    # Find the time difference in seconds between the current time and the time set to sleep 
    time_now = time.strftime("%H:%M")

    FMT = '%H:%M'
    tdelta = datetime.datetime.strptime(time_to_sleep, FMT) - datetime.datetime.strptime(time_now, FMT)

    if tdelta.days < 0:
        tdelta = datetime.timedelta(days = 0, seconds=tdelta.seconds)

    delay_time = tdelta.total_seconds()

    time.sleep(delay_time) #Delay the function upto the time at which the AC will turn off.

    send_signal('AC_TURNOFF')

    current_state = get_last_records()

    current_data = {
                    "TEMP" : str(current_state[0][2]),
                    "STATE" : 'OFF',
                    "FAN_SPEED" : current_state[0][3]
                    }

    log_remote_state('TURNOFF_AUTO', int(current_data["TEMP"]), current_data["FAN_SPEED"])

    publish_remote_state(current_data)

    publish_set_sleep('cancel')
def fan_down_control():

    state = request.form['STATE']
    temp = request.form['TEMP']
    fan_speed = request.form['FAN_SPEED']

    if fan_speed == "HI":
        update_fan_speed = "MED"
    elif fan_speed == "MED":
        update_fan_speed = "LOW"
    elif fan_speed == "LOW":
        update_fan_speed = "LOW"

    if state == "ON":
        key = "AC" + str(temp) + "_" + str(update_fan_speed) + "_" + "ON"
        send_signal(key)      

    current_data = {
                    "TEMP" : str(temp),
                    "STATE" : state,
                    "FAN_SPEED" : update_fan_speed
                   }

    if state == "ON":
        log_remote_state(state, int(temp), update_fan_speed)

    publish_remote_state(current_data)

    return '{}'
def temp_up_control():

    state = request.form['STATE']
    temp = request.form['TEMP']
    fan_speed = request.form['FAN_SPEED']

    if int(temp) < 30: #Max output air temperature of AC is 30degC
        update_temp = int(temp) + 1
    else:
        update_temp = int(temp)

    if state == "ON": # IR signal will only be sent if the AC is in the ON state
        key = "AC" + str(update_temp) + "_" + str(fan_speed) + "_" + "ON"
        send_signal(key)      

    current_data = {
                    "TEMP" : str(update_temp),
                    "STATE" : state,
                    "FAN_SPEED" : fan_speed
                   }

    if state == "ON": # Action will only be logged if the AC is in the ON state
        log_remote_state(state, update_temp, fan_speed)

    publish_remote_state(current_data)

    return '{}'
def temp_down_control():

    state = request.form['STATE']
    temp = request.form['TEMP']
    fan_speed = request.form['FAN_SPEED']

    if int(temp) > 16: #Min output air temperature of AC is 16degC
        update_temp = int(temp) - 1
    else:
        update_temp = int(temp)

    if state == "ON":
        key = "AC" + str(update_temp) + "_" + str(fan_speed) + "_" + "ON"
        send_signal(key)      

    current_data = {
                    "TEMP" : str(update_temp),
                    "STATE" : state,
                    "FAN_SPEED" : fan_speed
                 }

    if state == "ON":
        log_remote_state(state, update_temp, fan_speed)

    publish_remote_state(current_data)

    return '{}'
	    send_state = "OFF"
	    state_log = "TURNOFF_MANUAL"
	
	
    #Send the IR signal	
    send_signal(key)

    current_data = {
                    "TEMP" : temp,
                    "STATE" : send_state,
                    "FAN_SPEED" : fan_speed
                   }

    log_remote_state(state_log, int(temp), fan_speed) #Calls the function to log the updated state

    publish_remote_state(current_data) #Calls the function to publish the updated state

    return '{}'
    

# Function is used to increase the teperature of the AC 
# and publish the changed state of the AC to the clients.
@app.route("/temp_up_control", methods=['POST'])
def temp_up_control():

    state = request.form['STATE']
    temp = request.form['TEMP']
    fan_speed = request.form['FAN_SPEED']

    if int(temp) < 30: #Max output air temperature of AC is 30degC
        update_temp = int(temp) + 1