#include "string.h"
#include "esp_log.h"
#include "programme_app.h"
#include "system_control.h"
#include "system_freertos_define.h"
#include "parameter.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "i2c_mcu.h"

/*睡眠运行时间/min*/
uint32_t sleep_time_count = 0;

void sleep_time_count_set(uint32_t param)
{
    sleep_time_count = param;
}

uint32_t sleep_time_count_get(void)
{
    return sleep_time_count;
}

/*标记界面刷新，不代表实际运行，实际运行根据运行模式而定。1:制冷编程，2：制热编程*/
uint8_t pro_cool_heat = 1;
void pro_cool_heat_set(uint8_t param)
{
    pro_cool_heat = param;
}

uint8_t pro_cool_heat_get(void)
{
    return pro_cool_heat;
}


static TaskHandle_t Programme_TaskHandle = NULL;
static TaskHandle_t Programme_Flash_TaskHandle = NULL;

const pro_day_data_t pro_default_day_cool_data = {
    .seg[0] = {
        .state = 0,
        .time = 7*60,
        .temp = 240
            },
    .seg[1] = {
        .state = 0,
        .time = 8*60,
        .temp = 280
    },
    .seg[2] = {
        .state = 0,
        .time = 12*60,
        .temp = 240
    },
    .seg[3] = {
        .state = 0,
        .time = 14*60,
        .temp = 280
    },
    .seg[4] = {
        .state = 0,
        .time = 18*60,
        .temp = 240
    },
    .seg[5] = {
        .state = 0,
        .time = 22*60,
        .temp = 260
            },
};

const pro_day_data_t pro_default_day_cool_data_F = {
    .seg[0] = {
        .state = 0,
        .time = 7*60,
        .temp = 75
            },
    .seg[1] = {
        .state = 0,
        .time = 8*60,
        .temp = 82
    },
    .seg[2] = {
        .state = 0,
        .time = 12*60,
        .temp = 75
    },
    .seg[3] = {
        .state = 0,
        .time = 14*60,
        .temp = 82
    },
    .seg[4] = {
        .state = 0,
        .time = 18*60,
        .temp = 75
    },
    .seg[5] = {
        .state = 0,
        .time = 22*60,
        .temp = 79
            },
};

const pro_day_data_t pro_default_day_heat_data = {
    .seg[0] = {
        .state = 0,
        .time = 7*60,
        .temp = 210
            },
    .seg[1] = {
        .state = 0,
        .time = 8*60,
        .temp = 160
    },
    .seg[2] = {
        .state = 0,
        .time = 12*60,
        .temp = 210
    },
    .seg[3] = {
        .state = 0,
        .time = 14*60,
        .temp = 160
    },
    .seg[4] = {
        .state = 0,
        .time = 18*60,
        .temp = 210
    },
    .seg[5] = {
        .state = 0,
        .time = 22*60,
        .temp = 160
            },
};

const pro_day_data_t pro_default_day_heat_data_F = {
    .seg[0] = {
        .state = 0,
        .time = 7*60,
        .temp = 70
            },
    .seg[1] = {
        .state = 0,
        .time = 8*60,
        .temp = 61
    },
    .seg[2] = {
        .state = 0,
        .time = 12*60,
        .temp = 70
    },
    .seg[3] = {
        .state = 0,
        .time = 14*60,
        .temp = 61
    },
    .seg[4] = {
        .state = 0,
        .time = 18*60,
        .temp = 70
    },
    .seg[5] = {
        .state = 0,
        .time = 22*60,
        .temp = 61
            },
};

const sleep_data_t sleep_default_data = {
    .sleep_start_time = 0,
    .sleep_end_time = 0,
    .sleep_hour = 8,
    .sleep_run_state = 0,
    .temp = 160,
};

const sch_day_data_t sch_default_day_data = { 
    .seg[0] = {
        .state = 0,
        .time = 60*6,
        .on_off = 0,
        .enable = 0,
    },
    .seg[1] = {
        .state = 0,
        .time = 60*12,
        .on_off = 0,
        .enable = 0,
    },
    .seg[2] = {
        .state = 0,
        .time = 60*18,
        .on_off = 0,
        .enable = 0,
    },
    .seg[3] = {
        .state = 0,
        .time = 60*22,
        .on_off = 0,
        .enable = 0,
    },
};

/*编程数据*/
static programme_data_t programme_data_s = {
    .temp_unit = 0,
    .programme_sleep = 0,
    .programme_pro = 0,
    .programme_sch = 0,
    .pro_data_cool = {
        .day[0] = pro_default_day_cool_data,
        .day[1] = pro_default_day_cool_data,
        .day[2] = pro_default_day_cool_data,
        .day[3] = pro_default_day_cool_data,
        .day[4] = pro_default_day_cool_data,
        .day[5] = pro_default_day_cool_data,
        .day[6] = pro_default_day_cool_data,
    },
    .pro_data_heat = {
        .day[0] = pro_default_day_heat_data,
        .day[1] = pro_default_day_heat_data,
        .day[2] = pro_default_day_heat_data,
        .day[3] = pro_default_day_heat_data,
        .day[4] = pro_default_day_heat_data,
        .day[5] = pro_default_day_heat_data,
        .day[6] = pro_default_day_heat_data,
    },
    .sleep_data = sleep_default_data,
    .sch_data = {
        .day[0] = sch_default_day_data,
        .day[1] = sch_default_day_data,
        .day[2] = sch_default_day_data,
        .day[3] = sch_default_day_data,
        .day[4] = sch_default_day_data,
        .day[5] = sch_default_day_data,
        .day[6] = sch_default_day_data,
    }
};

programme_data_t programme_data_get(void) 
{
    return programme_data_s;
}

void programme_data_set(programme_data_t param) 
{ 
    programme_data_s = param;
}

/*制冷/制热变成数据复位。1:制冷，2:制热*/
void programme_data_cool_reset(uint8_t cool_heat_type)
{
    programme_data_t programme_data = programme_data_get();
    if(cool_heat_type == 1)
    {
        if(Gettemperature_unit())
        {
            programme_data.pro_data_cool.day[0] = pro_default_day_cool_data_F;
            programme_data.pro_data_cool.day[1] = pro_default_day_cool_data_F;
            programme_data.pro_data_cool.day[2] = pro_default_day_cool_data_F;
            programme_data.pro_data_cool.day[3] = pro_default_day_cool_data_F;
            programme_data.pro_data_cool.day[4] = pro_default_day_cool_data_F;
            programme_data.pro_data_cool.day[5] = pro_default_day_cool_data_F;
            programme_data.pro_data_cool.day[6] = pro_default_day_cool_data_F;
        }
        else
        {
            programme_data.pro_data_cool.day[0] = pro_default_day_cool_data;
            programme_data.pro_data_cool.day[1] = pro_default_day_cool_data;
            programme_data.pro_data_cool.day[2] = pro_default_day_cool_data;
            programme_data.pro_data_cool.day[3] = pro_default_day_cool_data;
            programme_data.pro_data_cool.day[4] = pro_default_day_cool_data;
            programme_data.pro_data_cool.day[5] = pro_default_day_cool_data;
            programme_data.pro_data_cool.day[6] = pro_default_day_cool_data;
        }
    }
    else if(cool_heat_type == 2)
    {
        if(Gettemperature_unit())
        {
            programme_data.pro_data_heat.day[0] = pro_default_day_heat_data_F;
            programme_data.pro_data_heat.day[1] = pro_default_day_heat_data_F;
            programme_data.pro_data_heat.day[2] = pro_default_day_heat_data_F;
            programme_data.pro_data_heat.day[3] = pro_default_day_heat_data_F;
            programme_data.pro_data_heat.day[4] = pro_default_day_heat_data_F;
            programme_data.pro_data_heat.day[5] = pro_default_day_heat_data_F;
            programme_data.pro_data_heat.day[6] = pro_default_day_heat_data_F;
        }
        else
        {
            programme_data.pro_data_heat.day[0] = pro_default_day_heat_data;
            programme_data.pro_data_heat.day[1] = pro_default_day_heat_data;
            programme_data.pro_data_heat.day[2] = pro_default_day_heat_data;
            programme_data.pro_data_heat.day[3] = pro_default_day_heat_data;
            programme_data.pro_data_heat.day[4] = pro_default_day_heat_data;
            programme_data.pro_data_heat.day[5] = pro_default_day_heat_data;
            programme_data.pro_data_heat.day[6] = pro_default_day_heat_data;
        }
    }
    programme_data_set(programme_data);
    programme_state_reset();
} 

/*定时数据复位*/
void prog_schedule_data_reset(void)
{
    programme_data_t programme_data = programme_data_get();
    programme_data.sch_data.day[0] = sch_default_day_data;
    programme_data.sch_data.day[1] = sch_default_day_data;
    programme_data.sch_data.day[2] = sch_default_day_data;
    programme_data.sch_data.day[3] = sch_default_day_data;
    programme_data.sch_data.day[4] = sch_default_day_data;
    programme_data.sch_data.day[5] = sch_default_day_data;
    programme_data.sch_data.day[6] = sch_default_day_data;
    programme_data_set(programme_data);
}

/*programme_type: 0:sleep 2:programme 1:schedule*/
void programme_state_set(uint8_t programme_type, uint8_t state)
{
    programme_data_t programme_data = programme_data_get();
    switch(programme_type)
    {
      case 0:
        programme_data.programme_sleep = state;
        programme_data.programme_pro = 0;
        programme_data.programme_sch = 0;
        /*开睡眠需要记录开始时间，睡眠状态*/
        if(state)
        {
            sleep_time_count_set(1);
            programme_data.sleep_data.sleep_run_state = 0;
            programme_data.sleep_data.temp = GetSetPoint();
        }
        /*关睡眠需要睡眠状态，温度回退，*/
        else
        { 
            sleep_time_count_set(0);
            programme_data.sleep_data.sleep_run_state = 0;
        }
        break;

      case 1:
        prog_sch_state_reset();
        programme_data = programme_data_get();
        programme_data.programme_sleep = 0;
        programme_data.programme_pro = 0;
        programme_data.programme_sch = state;
        break;

      case 2:
        programme_state_reset();
        programme_data = programme_data_get();
        programme_data.programme_sleep = 0;
        programme_data.programme_pro = state;
        programme_data.programme_sch = 0;
        break;

      default:
        break;
    }
    programme_data_set(programme_data);
}

uint8_t programme_state_get(uint8_t programme_type)
{
    programme_data_t programme_data = programme_data_get();
    switch (programme_type)
    {
    case 0:
        return programme_data.programme_sleep;
        break;

    case 1:
        return programme_data.programme_sch;
        break;

    case 2:
        return programme_data.programme_pro;
        break;
    
    default:
        return 0;
        break;
    }
}


/*编程运行主函数/30S*/
void programme_task(void)
{
    rtc_time_cache_t rtc_time;
    uint16_t time_min = 0;
    pro_seg_data_t pro_seg_data_tmp = {0};
    programme_data_t programme_data_copy = {0};
    while(1)
    {
        system_get_rtc_time(&rtc_time);
        time_min = rtc_time.min + rtc_time.hour * 60;
        programme_data_copy = programme_data_get();

        if(sleep_time_count_get())
        {
            static uint16_t time_min_record = 0;
            if(time_min != time_min_record)
            {
                time_min_record = time_min;
                sleep_time_count_set(sleep_time_count_get() +1);
            }
        }
        for(uint8_t i = 0; i < 7; i++)
        { 
            if(i == (rtc_time.week -1))
            {
                if(programme_state_get(GetProgram_option()) && GetProgram_option() == 2)
                {
                    for(uint8_t j = 0; j < 6; j++)
                    {
                        /*制冷*/
                        if(GetWork_mode() == 1)
                        {
                            if(j < 5)
                            {
                                if(time_min >= programme_data_copy.pro_data_cool.day[i].seg[j].time && time_min < programme_data_copy.pro_data_cool.day[i].seg[j + 1].time)
                                {
                                    if(programme_data_copy.pro_data_cool.day[i].seg[j].state == 0)
                                    {
                                        programme_state_reset();
                                        programme_data_copy = programme_data_get();
                                        programme_data_copy.pro_data_cool.day[i].seg[j].state = 1;
                                        if(Gettemperature_unit() == 0)
                                        {
                                            SetSetPoint(((float)programme_data_copy.pro_data_cool.day[i].seg[j].temp)/10);
                                        }
                                        else
                                        {
                                            SetSetPoint(programme_data_copy.pro_data_cool.day[i].seg[j].temp);
                                        }
                                    }
                                }
                                if(time_min < programme_data_copy.pro_data_cool.day[i].seg[0].time)
                                {
                                    if(i == 0)
                                    {
                                        if(programme_data_copy.pro_data_cool.day[6].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_cool.day[6].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_cool.day[6].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_cool.day[6].seg[5].temp);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if(programme_data_copy.pro_data_cool.day[i -1].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_cool.day[i -1].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_cool.day[i -1].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_cool.day[i -1].seg[5].temp);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if(/*time_min < programme_data_copy.pro_data_cool.day[rtc_time.week].seg[0].time &&*/ time_min >= programme_data_copy.pro_data_cool.day[i].seg[5].time)
                                {
                                    if(programme_data_copy.pro_data_cool.day[i].seg[5].state == 0)
                                    {
                                        programme_state_reset();
                                        programme_data_copy = programme_data_get();
                                        programme_data_copy.pro_data_cool.day[i].seg[5].state = 1;
                                        if(Gettemperature_unit() == 0)
                                        {
                                            SetSetPoint(((float)programme_data_copy.pro_data_cool.day[i].seg[5].temp)/10);
                                        }
                                        else
                                        {
                                            SetSetPoint(programme_data_copy.pro_data_cool.day[i].seg[5].temp);
                                        }
                                    }
                                }
                                if(time_min < programme_data_copy.pro_data_cool.day[i].seg[0].time)
                                {
                                    if(i == 0)
                                    {
                                        if(programme_data_copy.pro_data_cool.day[6].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_cool.day[6].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_cool.day[6].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_cool.day[6].seg[5].temp);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if(programme_data_copy.pro_data_cool.day[i -1].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_cool.day[i -1].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_cool.day[i -1].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_cool.day[i -1].seg[5].temp);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        /*制热*/
                        else if(GetWork_mode() ==2)
                        {
                            if(j < 5)
                            {
                                if(time_min >= programme_data_copy.pro_data_heat.day[i].seg[j].time && time_min < programme_data_copy.pro_data_heat.day[i].seg[j + 1].time)
                                {
                                    if(programme_data_copy.pro_data_heat.day[i].seg[j].state == 0)
                                    {
                                        programme_state_reset();
                                        programme_data_copy = programme_data_get();
                                        programme_data_copy.pro_data_heat.day[i].seg[j].state = 1;
                                        if(Gettemperature_unit() == 0)
                                        {
                                            SetSetPoint(((float)programme_data_copy.pro_data_heat.day[i].seg[j].temp)/10);
                                        }
                                        else
                                        {
                                            SetSetPoint(programme_data_copy.pro_data_heat.day[i].seg[j].temp);
                                        }
                                    }
                                }

                                if(time_min < programme_data_copy.pro_data_heat.day[i].seg[0].time)
                                {
                                    if(i == 0)
                                    {
                                        if(programme_data_copy.pro_data_heat.day[6].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_heat.day[6].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_heat.day[6].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_heat.day[6].seg[5].temp);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if(programme_data_copy.pro_data_heat.day[i -1].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_heat.day[i -1].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_heat.day[i -1].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_heat.day[i -1].seg[5].temp);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if(/*time_min < programme_data_copy.pro_data_heat.day[i].seg[0].time &&*/ time_min >= programme_data_copy.pro_data_heat.day[i].seg[5].time)
                                {
                                   if(programme_data_copy.pro_data_heat.day[i].seg[5].state == 0)
                                    {
                                        programme_state_reset();
                                        programme_data_copy = programme_data_get();
                                        programme_data_copy.pro_data_heat.day[i].seg[5].state = 1;
                                        if(Gettemperature_unit() == 0)
                                        {
                                            SetSetPoint(((float)programme_data_copy.pro_data_heat.day[i].seg[5].temp)/10);
                                        }   
                                        else
                                        {
                                            SetSetPoint(programme_data_copy.pro_data_heat.day[i].seg[5].temp);
                                        }
                                    }
                                }

                                if(time_min < programme_data_copy.pro_data_heat.day[i].seg[0].time)
                                {
                                    if(i == 0)
                                    {
                                        if(programme_data_copy.pro_data_heat.day[6].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_heat.day[6].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_heat.day[6].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_heat.day[6].seg[5].temp);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if(programme_data_copy.pro_data_heat.day[i -1].seg[5].state == 0)
                                        {
                                            programme_state_reset();
                                            programme_data_copy = programme_data_get();
                                            programme_data_copy.pro_data_heat.day[i -1].seg[5].state = 1;
                                            if(Gettemperature_unit() == 0)
                                            {
                                                SetSetPoint(((float)programme_data_copy.pro_data_heat.day[i -1].seg[5].temp)/10);
                                            }
                                            else
                                            {
                                                SetSetPoint(programme_data_copy.pro_data_heat.day[i -1].seg[5].temp);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    programme_data_set(programme_data_copy);
                }
                /*睡眠*/
                else if(programme_state_get(GetProgram_option()) && GetProgram_option() == 0)
                {
                    if(sleep_time_count_get() >= programme_data_copy.sleep_data.sleep_hour *60)
                    {
                        /*关闭睡眠*/
                        if(programme_data_copy.sleep_data.sleep_run_state != 0) 
                        {
                            if(GetWork_mode() == 1)
                            {
                                if(programme_data_copy.temp_unit == 0)
                                {
                                    if(programme_data_copy.sleep_data.sleep_run_state == 2)
                                    {
                                        SetSetPoint(GetSetPoint() -1);    //温度
                                    }
                                    else if(programme_data_copy.sleep_data.sleep_run_state == 3)
                                    {
                                        SetSetPoint(GetSetPoint() -2);    //温度
                                    }
                                }
                                else if(programme_data_copy.temp_unit == 1)
                                {
                                    if(programme_data_copy.sleep_data.sleep_run_state == 2)
                                    {
                                        SetSetPoint(GetSetPoint() -2);    //温度
                                    }
                                    else if(programme_data_copy.sleep_data.sleep_run_state == 3)
                                    {
                                        SetSetPoint(GetSetPoint() -4);    //温度
                                    }
                                }
                            }
                            else if(GetWork_mode() == 2)
                            {
                                if(programme_data_copy.temp_unit == 0)
                                {
                                    if(programme_data_copy.sleep_data.sleep_run_state == 2)
                                    {
                                        SetSetPoint(GetSetPoint() +1);    //温度
                                    }
                                    else if(programme_data_copy.sleep_data.sleep_run_state == 3)
                                    {
                                        SetSetPoint(GetSetPoint() +2);    //温度
                                    }
                                }
                                else if(programme_data_copy.temp_unit == 1)
                                {
                                    if(programme_data_copy.sleep_data.sleep_run_state == 2)
                                    {
                                        SetSetPoint(GetSetPoint() +2);    //温度
                                    }
                                    else if(programme_data_copy.sleep_data.sleep_run_state == 3)
                                    {
                                        SetSetPoint(GetSetPoint() +4);    //温度
                                    }
                                }
                            }
                            programme_state_set(0, 0);
                            /*设置完后需重新拉取数据，避免数据覆盖*/
                            programme_data_copy = programme_data_get();
                            programme_data_copy.sleep_data.sleep_run_state = 0;
                        }
                    }
                    else if((sleep_time_count_get() >= 1) && (sleep_time_count_get() < 60))
                    {
                        /*开启睡眠*/
                        if(programme_data_copy.sleep_data.sleep_run_state == 0)
                        {
                            programme_data_copy.sleep_data.sleep_run_state = 1;
                        }
                    }
                    else if((sleep_time_count_get() >= 60) && (sleep_time_count_get() < 60*2))
                    {
                        if(programme_data_copy.sleep_data.sleep_run_state == 1)
                        {
                            programme_data_copy.sleep_data.sleep_run_state = 2;
                            if(GetWork_mode() == 1)
                            {
                                if(programme_data_copy.temp_unit == 0)
                                {
                                    SetSetPoint(GetSetPoint() +1);    //温度
                                }
                                else if(programme_data_copy.temp_unit == 1)
                                {
                                    SetSetPoint(GetSetPoint() +2);    //温度
                                }
                            }
                            else if(GetWork_mode() == 2)
                            {
                                if(programme_data_copy.temp_unit == 0)
                                {
                                    SetSetPoint(GetSetPoint() -1);    //温度
                                }
                                else if(programme_data_copy.temp_unit == 1)
                                {
                                    SetSetPoint(GetSetPoint() -2);    //温度
                                }
                            }
                        }
                    }
                    else if((sleep_time_count_get() >= 60*2) && (sleep_time_count_get() < 60*3))
                    {
                        if(programme_data_copy.sleep_data.sleep_run_state == 2)
                        {
                            programme_data_copy.sleep_data.sleep_run_state = 3;
                            if(GetWork_mode() == 1)
                            {
                                if(programme_data_copy.temp_unit == 0)
                                {
                                    SetSetPoint(GetSetPoint() +1);    //温度
                                }
                                else if(programme_data_copy.temp_unit == 1)
                                {
                                    SetSetPoint(GetSetPoint() +2);    //温度
                                }
                            }
                            else if(GetWork_mode() == 2)
                            {
                                if(programme_data_copy.temp_unit == 0)
                                {
                                    SetSetPoint(GetSetPoint() -1);    //温度
                                }
                                else if(programme_data_copy.temp_unit == 1)
                                {
                                    SetSetPoint(GetSetPoint() -2);    //温度
                                }
                            }
                        }
                    }
                    programme_data_set(programme_data_copy);
                }
                /*定时*/
                else if(programme_state_get(GetProgram_option()) && GetProgram_option() == 1)
                {
                    for(uint8_t k = 0; k < 4; k++)
                    {
                        if(time_min == programme_data_copy.sch_data.day[rtc_time.week -1].seg[k].time)
                        {
                            if(programme_data_copy.sch_data.day[rtc_time.week -1].seg[k].enable)
                            {
                                if(programme_data_copy.sch_data.day[i].seg[k].state == 0)
                                {
                                    prog_sch_state_reset();
                                    programme_data_copy = programme_data_get();
                                    programme_data_copy.sch_data.day[i].seg[k].state = 1;
                                    SetOnOffState(programme_data_copy.sch_data.day[i].seg[k].on_off);
                                }
                            }
                        }
                    }
                    programme_data_set(programme_data_copy);
                }
            }
        }
        vTaskDelay(5000);
    }
}

/*温度单位切换*/
void programme_data_C_F(void)
{
    programme_data_t programme_data = programme_data_get();
    /*摄氏度/华氏度切换*/
    if(programme_data.temp_unit != Gettemperature_unit())
    {
      programme_data.temp_unit = Gettemperature_unit();
      for(uint8_t i = 0; i < 7; i++)
      {
        for(uint8_t j = 0; j < 6; j++)
        {
          /*华氏度*/
          if(programme_data.temp_unit)
          {
            programme_data.pro_data_cool.day[i].seg[j].temp = (((float)programme_data.pro_data_cool.day[i].seg[j].temp)/10 *9 /5 + 32 + 0.5f);
            programme_data.pro_data_heat.day[i].seg[j].temp = (((float)programme_data.pro_data_heat.day[i].seg[j].temp)/10 *9 /5 + 32 + 0.5f);
          }
          /*摄氏度*/
          else
          {
            programme_data.pro_data_cool.day[i].seg[j].temp = (((float)programme_data.pro_data_cool.day[i].seg[j].temp) - 32) *5 /9 + 0.5f;
            programme_data.pro_data_heat.day[i].seg[j].temp = (((float)programme_data.pro_data_heat.day[i].seg[j].temp) - 32) *5 /9 + 0.5f;
            programme_data.pro_data_cool.day[i].seg[j].temp *= 10;
            programme_data.pro_data_heat.day[i].seg[j].temp *= 10;
          }
        }
      }
      programme_data_set(programme_data);
      programme_state_reset();
    }
}

/*编程状态复位，时间/同步设置/模式切换/开关机/编程数据设置*/
void programme_state_reset(void)
{
    if(programme_state_get(GetProgram_option()) && GetProgram_option() == 2)
    {
        programme_data_t programme_data = programme_data_get();
        for(uint8_t i = 0; i < 7; i++)
        {
            for(uint8_t j = 0; j < 6; j++)
            {
                programme_data.pro_data_cool.day[i].seg[j].state = 0;
                programme_data.pro_data_heat.day[i].seg[j].state = 0;
            }
        }
        programme_data_set(programme_data);
    }
}

/*时间/同步开关状态变化刷新定时状态*/
void prog_sch_state_reset(void)
{
    if(programme_state_get(GetProgram_option()) && GetProgram_option() == 1)
    {
        programme_data_t programme_data = programme_data_get();
        for(uint8_t i = 0; i < 7; i++)
        {
            for(uint8_t j = 0; j < 4; j++)
            {
                programme_data.sch_data.day[i].seg[j].state = 0;
            }
        }
        programme_data_set(programme_data);
    }
}

/*睡眠状态下温度/模式切换/时间/同步设置/风速切换/开关机，退出睡眠*/
void prog_sleep_state_reset(void)
{
    if(programme_state_get(GetProgram_option()) && GetProgram_option() == 0)
    {
        programme_state_set(0, 0);
    }
}

/*编程状态检查*/
void programme_state_check(void)
{
    static uint8_t pro_on_off_record = 0xff;
    static uint8_t pro_work_mode_record = 0xff;
    static uint8_t room_card_record = 0xff;

    /*防卡状态检查，房卡状态变更退出睡眠状态*/
    if(get_roomcard_detect() != room_card_record)
    {
        room_card_record = get_roomcard_detect();
        if((GetExternal_Input() == 2) && (get_roomcard_detect() == 1))
        {
            room_card_record = get_roomcard_detect();
            programme_state_reset();
            return;
        }
    }

    if(pro_on_off_record == 0xff)
    {
        pro_on_off_record = GetOnOffState();
    }

    if(pro_work_mode_record == 0xff)
    {
        pro_work_mode_record = GetWork_mode();
    }

    if((pro_work_mode_record != GetWork_mode())
    || (pro_on_off_record != GetOnOffState()))
    {
        programme_state_reset();
    }

    pro_on_off_record = GetOnOffState();
    pro_work_mode_record = GetWork_mode();
}

/*睡眠状态检查*/
void prog_sleep_state_check(void)
{
    static uint8_t on_off_record = 0xff;
    static uint8_t work_mode_record = 0xff;
    static uint8_t fan_speed_record = 0xff;

    static uint8_t room_card_record = 0xff;

    /*防卡状态检查，房卡状态变更退出睡眠状态*/
    if(get_roomcard_detect() != room_card_record)
    {
        room_card_record = get_roomcard_detect();
        if(GetExternal_Input() == 2)
        {
            room_card_record = get_roomcard_detect();
            prog_sleep_state_reset();
            return;
        }
    }

    programme_data_t programme_data = programme_data_get();

    /*上电首次同步*/
    if(on_off_record == 0xff)
    {
        on_off_record = GetOnOffState();
    }

    if(work_mode_record == 0xff)
    {
        work_mode_record = GetWork_mode();
    }

    if(fan_speed_record == 0xff)
    {
        fan_speed_record = GetFan_speed();
    }

    if((work_mode_record != GetWork_mode())
    || (fan_speed_record != GetFan_speed())
    || (on_off_record != GetOnOffState()))
    {
        prog_sleep_state_reset();
    }

    if(programme_data.sleep_data.temp != GetSetPoint())
    {
        if((programme_data.sleep_data.sleep_run_state == 0) 
        || (programme_data.sleep_data.sleep_run_state == 1))
        {
            if(programme_data.sleep_data.temp != GetSetPoint())
            {
                prog_sleep_state_reset();
            }
        }
        else if(programme_data.sleep_data.sleep_run_state == 2)
        {
            if(GetWork_mode() == 1)
            {
                if(Gettemperature_unit() == 0)
                {
                    if(programme_data.sleep_data.temp +1 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
                else
                {
                    if(programme_data.sleep_data.temp +2 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
            }
            else if(GetWork_mode() == 2)
            {
                if(Gettemperature_unit() == 0)
                {
                    if(programme_data.sleep_data.temp -1 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
                else
                {
                    if(programme_data.sleep_data.temp -2 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
            }
        }
        else if(programme_data.sleep_data.sleep_run_state == 3)
        {
            if(GetWork_mode() == 1)
            {
                if(Gettemperature_unit() == 0)
                {
                    if(programme_data.sleep_data.temp +2 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
                else
                {
                    if(programme_data.sleep_data.temp +4 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
            }
            else if(GetWork_mode() == 2)
            {
                if(Gettemperature_unit() == 0)
                {
                    if(programme_data.sleep_data.temp -2 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
                else
                {
                    if(programme_data.sleep_data.temp -4 != GetSetPoint())
                    {
                        prog_sleep_state_reset();
                    }
                }
            }
        }
    }

    on_off_record = GetOnOffState();
    work_mode_record = GetWork_mode();
    fan_speed_record = GetFan_speed();
}

/*编程温度和上下限检查*/
void programme_temp_check(void)
{
    if(GetProgram_option() == 2)
    {
        programme_data_t programme_data = programme_data_get();
        for(uint8_t i = 0; i < 7; i++)
        {
            for(uint8_t j = 0; j < 6; j++)
            {
                if(Gettemperature_unit() == 0)
                {
                    if(programme_data.pro_data_cool.day[i].seg[j].temp > GetSet_upper_temperature_limit()*10)
                    {
                        programme_data.pro_data_cool.day[i].seg[j].temp = GetSet_upper_temperature_limit()*10;
                    }
                    else if(programme_data.pro_data_cool.day[i].seg[j].temp < GetSet_lower_temperature_limit()*10)
                    {
                        programme_data.pro_data_cool.day[i].seg[j].temp = GetSet_lower_temperature_limit()*10;
                    }

                    if(programme_data.pro_data_heat.day[i].seg[j].temp > GetSet_upper_temperature_limit()*10)
                    {
                        programme_data.pro_data_heat.day[i].seg[j].temp = GetSet_upper_temperature_limit()*10;
                    }
                    else if(programme_data.pro_data_heat.day[i].seg[j].temp < GetSet_lower_temperature_limit()*10)
                    {
                        programme_data.pro_data_heat.day[i].seg[j].temp = GetSet_lower_temperature_limit()*10;
                    }
                }
                else if(Gettemperature_unit() == 1)
                {
                    if(programme_data.pro_data_cool.day[i].seg[j].temp > GetSet_upper_temperature_limit())
                    {
                        programme_data.pro_data_cool.day[i].seg[j].temp = GetSet_upper_temperature_limit();
                    }
                    else if(programme_data.pro_data_cool.day[i].seg[j].temp < GetSet_lower_temperature_limit())
                    {
                        programme_data.pro_data_cool.day[i].seg[j].temp = GetSet_lower_temperature_limit();
                    }

                    if(programme_data.pro_data_heat.day[i].seg[j].temp > GetSet_upper_temperature_limit())
                    {
                        programme_data.pro_data_heat.day[i].seg[j].temp = GetSet_upper_temperature_limit();
                    }
                    else if(programme_data.pro_data_heat.day[i].seg[j].temp < GetSet_lower_temperature_limit())
                    {
                        programme_data.pro_data_heat.day[i].seg[j].temp = GetSet_lower_temperature_limit();
                    }
                }
            }
        }
        programme_data_set(programme_data);
    }
}

void programme_flash_task(void)
{
    while(1)
    {
        programme_temp_check();
        programme_state_check();
        prog_sleep_state_check();
        programme_falsh_data_check_write();
        vTaskDelay(1000);
    }
}

void programme_init(void)
{ 
    programme_falsh_data_read();
    xTaskCreate(programme_task, "programme_task", 1024*8, NULL, PROGRAMME_PRIORITY, (TaskHandle_t *)&Programme_TaskHandle);
    xTaskCreate(programme_flash_task, "programme_flash_task", 1024*4, NULL, PROGRAMME_FLASH_PRIORITY, (TaskHandle_t *)&Programme_Flash_TaskHandle);
}


const char* flash_program_code_tag = "program_code_t";
/*编程7天*2 + 5个标志数据 +1睡眠 +7定时*/
const char *flash_program_code_key[7*2 + 5 + 1 + 7] =
{
    "temp_unit" , "program_sleep" , "program_pro" , "program_sch" , "flash_flag" , 
    "program_day_0" , "program_day_1" , "program_day_2" , "program_day_3" , "program_day_4", "program_day_5", "program_day_6",
    "program_day_7" , "program_day_8" , "program_day_9" , "program_day_10" , "program_day_11", "program_day_12", "program_day_13",
    "sleep_mode",
    "sch_day_0" , "sch_day_1" , "sch_day_2" , "sch_day_3" , "sch_day_4", "sch_day_5", "sch_day_6",
};

nvs_handle_t flash_program_code_handle;

/*此函数1S运行一次，3S延时存flash*/
void programme_falsh_data_check_write(void)
{
    static uint8_t init_num = 4;
    programme_data_t programme_data = programme_data_get();
    static programme_data_t read_flash_data;
    size_t length = 1;

    if(memcmp(&programme_data, &read_flash_data, sizeof(programme_data_get())) != 0)
    {
        init_num = 3;
        read_flash_data = programme_data;
    }

    if(init_num <= 3)
    {
        init_num--;
    }

    if(init_num)
    {
        return;
    }
    else
    {
        init_num = 4;
    }

    /*第一次上电，flash全部写入*/
    if(programme_data.flash_flag != 0x5A)
    {
        memset(&read_flash_data, 0xff, sizeof(programme_data_t));
        programme_data.flash_flag = 0x5A;
    }

    nvs_open(flash_program_code_tag, NVS_READWRITE, &flash_program_code_handle);
    for(uint8_t i = 0; i < 7*2 + 5 +1 +7; i++)
    {
        switch(i)
        {
            case 0:
                length = 1;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.temp_unit), &length);
                if(read_flash_data.temp_unit != programme_data.temp_unit)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.temp_unit), length);
                    nvs_commit(flash_program_code_handle);
                    nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.temp_unit), &length);
                }
                break;

            case 1:
                /*睡眠状态不用写*/
                break;
            
            case 2:
                length = 1;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.programme_pro), &length);
                if(read_flash_data.programme_pro != programme_data.programme_pro)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.programme_pro), length);
                    nvs_commit(flash_program_code_handle);
                    nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.programme_pro), &length);
                     printf("read_flash_data.programme_pro = %d\r\n", read_flash_data.programme_pro);
                }
                break;

            case 3:
                length = 1;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.programme_sch), &length);
                if(read_flash_data.programme_sch != programme_data.programme_sch)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.programme_sch), length);
                    nvs_commit(flash_program_code_handle);
                    nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.programme_sch), &length);
                }
                break;

            case 4:
                length = 4;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.flash_flag), &length);
                if(read_flash_data.flash_flag != 0x5A)
                {
                    programme_data.flash_flag = 0x5A;
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.flash_flag), length);
                    nvs_commit(flash_program_code_handle);
                    nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.flash_flag), &length);
                }
                break;
            
            case 5:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[0]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[0], &programme_data.pro_data_cool.day[0], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[0]), length);
                    nvs_commit(flash_program_code_handle);
                    nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[0]), &length);
                }
                break;

            case 6:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[1]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[1], &programme_data.pro_data_cool.day[1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[1]), length);
                    nvs_commit(flash_program_code_handle);
                    nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[1]), &length);
                }
                break;

            case 7:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[i-5]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[i-5], &programme_data.pro_data_cool.day[i-5], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[i-5]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;
            
            case 8:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[i-5]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[i-5], &programme_data.pro_data_cool.day[i-5], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[i-5]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 9:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[i-5]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[i-5], &programme_data.pro_data_cool.day[i-5], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[i-5]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 10:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[i-5]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[i-5], &programme_data.pro_data_cool.day[i-5], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[i-5]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;
            
            case 11:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[i-5]), &length);
                if(memcmp(&read_flash_data.pro_data_cool.day[i-5], &programme_data.pro_data_cool.day[i-5], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_cool.day[i-5]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 12:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 13:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;
            
            case 14:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 15:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 16:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;
            
            case 17:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 18:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[i-5-7]), &length);
                if(memcmp(&read_flash_data.pro_data_heat.day[i-5-7], &programme_data.pro_data_heat.day[i-5-7], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.pro_data_heat.day[i-5-7]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 19:
                length = sizeof(sleep_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sleep_data), &length);
                if(memcmp(&read_flash_data.sleep_data, &programme_data.sleep_data, length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sleep_data), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 20:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 21:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 22:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 23:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 24:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 25:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            case 26:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[i-5-7*2-1]), &length);
                if(memcmp(&read_flash_data.sch_data.day[i-5-7*2-1], &programme_data.sch_data.day[i-5-7*2-1], length) != 0)
                {
                    nvs_set_blob(flash_program_code_handle, flash_program_code_key[i], &(programme_data.sch_data.day[i-5-7*2-1]), length);
                    nvs_commit(flash_program_code_handle);
                }
                break;

            default:
                break;
        }
    }
    nvs_close(flash_program_code_handle);
}

void programme_falsh_data_read(void)
{
    programme_data_t read_flash_data;
    size_t length = 1;

    nvs_open(flash_program_code_tag, NVS_READWRITE, &flash_program_code_handle);
    for(uint8_t i = 0; i < 7*2 + 5 +1 +7; i++)
    {
        switch(i)
        {
            case 0:
                length = 1;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.temp_unit), &length);
                break;

            case 1:
                /*每次上电睡眠保持关*/
                read_flash_data.programme_sleep = 0;
                break;
            
            case 2:
                length = 1;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.programme_pro), &length);
                break;

            case 3:
                length = 1;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.programme_sch), &length);
                break;

            case 4:
                length = 4;
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.flash_flag), &length);
                break;
            
            case 5:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[0]), &length);
                break;

            case 6:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[1]), &length);
                break;

            case 7:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[2]), &length);
                break;
            
            case 8:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[3]), &length);
                break;

            case 9:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[4]), &length);
                break;

            case 10:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[5]), &length);
                break;
            
            case 11:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_cool.day[6]), &length);
                break;

            case 12:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[0]), &length);
                break;

            case 13:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[1]), &length);
                break;
            
            case 14:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[2]), &length);
                break;

            case 15:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[3]), &length);
                break;

            case 16:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[4]), &length);
                break;
            
            case 17:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[5]), &length);
                break;

            case 18:
                length = sizeof(pro_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.pro_data_heat.day[6]), &length);
                break;

            case 19:
                length = sizeof(sleep_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sleep_data), &length);
                break;

            case 20:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[0]), &length);
                break;

            case 21:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[1]), &length);
                break;

            case 22:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[2]), &length);
                break;

            case 23:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[3]), &length);
                break;

            case 24:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[4]), &length);
                break;

            case 25:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[5]), &length);
                break;

            case 26:
                length = sizeof(sch_day_data_t);
                nvs_get_blob(flash_program_code_handle, flash_program_code_key[i], &(read_flash_data.sch_data.day[6]), &length);
                break;

            default:
                break;
        }
    }
    nvs_close(flash_program_code_handle);

    if(read_flash_data.flash_flag != 0x5A)
    {
        return;
    }
    programme_data_set(read_flash_data);
    programme_state_reset();
    prog_sch_state_reset();
}