//
// Created by xin on 24-12-11.
//
#include <iostream>
#include <TemperatureRegulator.h>
#include <TimeWorker.h>
#include <QtCore/QCoreApplication>
#include <qthread.h>
#include <fstream>
#include "logout.h"


int main(int argc, char* argv[])
{
    QCoreApplication aaaaa(argc, argv);
    std::cout<<"Hello World"<<std::flush;


    json j;
    std::ifstream i("/home/data/Setting/Constant_Temperature_Config.json");
    //如果配置文件不存在则创建一个默认配置文件
    if (!i.is_open()) {
        logout("main","config file not exist,create default config file",10);
        std::ofstream o("/home/data/Setting/Constant_Temperature_Config.json");
        json jdefault={
            {"TimeConfig",
                {
                    {"StartTime","00:00:00"},
                    {"EndTime","23:59:00"},
                    {"IntervalTime",10}
                }
            },
            {"TemperatureConfig",
                {
                    {"PortName","ttyS2"},
                    {"BaudRate",115200},
                    {"Pin",
                        {
                            {"COOLER", {{"Pin",24}}},
                            {"Fan", {{"Pin_H",3},{"Pin_L",2}}},
                            {"HOT", {{"Pin",19}}},
                            {"WINDOWS", {{"Pin_H",10},{"Pin_L",7},{"WaitTime",60}}}
                        }
                    },
                    {"Constant_Temperature",
                        {
                            {"CoolBegin_Temp_Insight",20},
                            {"CoolBegin_Diff_Temp",5},
                            {"HeatingBegin_Temp_Insight",0},
                            {"IsCoolInDaylight",false}
                        }
                    },
                    {"Location",
                        {
                            {"latitude",0},
                            {"longitude",0}
                        }
                    },
                    {"IndexInfo",
                        {
                            {"Insight",{ {{"x",7},{"y",1}}, {{"x",8},{"y",1}} }},
                            {"Outside",{ {{"x",1},{"y",1}}, {{"x",2},{"y",1}} }}
                        }
                    }
                }
            }
        };
        o << std::setw(4) << jdefault << std::endl;
        o.close();
        logout("main","default config file created,please modify the config file and restart the program",10);
        return 0;
        }







    logout("main","prepare to read config data,if error please check file /home/data/Setting/Constant_Temperature_Config.json",10);
    i>>j;
    logout("main","config data read finish",10);
    json timeconfig=j["TimeConfig"];
    json tempconfig=j["TemperatureConfig"];
    TemperatureRegulator *temperature_regulator=new TemperatureRegulator();
    temperature_regulator->setconfig(tempconfig);
    TimeWorker *timeWorker=new TimeWorker();

    timeWorker->ReadConfig(timeconfig);


    timeWorker->SetTask(TemperatureRegulator::looptask);
    timeWorker->Start();






    return aaaaa.exec();
}