diff --git a/othersoft/TemperatureControler/project/CMakeLists.txt b/othersoft/TemperatureControler/project/CMakeLists.txt index c7bcccb..8083fd0 100644 --- a/othersoft/TemperatureControler/project/CMakeLists.txt +++ b/othersoft/TemperatureControler/project/CMakeLists.txt @@ -10,6 +10,7 @@ include_directories("../src/json") include_directories("../src/TimeWorker") include_directories("../src/Temperature") include_directories("../src/controler") +include_directories("../src/sunfunction") set(CMAKE_MOC_VERBOSE TRUE) add_executable(TemperatureControler tempmain.cpp @@ -18,6 +19,7 @@ add_executable(TemperatureControler ../src/Temperature/TemperaTureWoker.cpp ../src/Temperature/TG_Manager.cpp ../src/Temperature/TemperatureRegulator.cpp + ../src/sunfunction/sunupanddown.cpp ) diff --git a/othersoft/TemperatureControler/project/Constant_Temperature_Config.json b/othersoft/TemperatureControler/project/Constant_Temperature_Config.json index 14cfe2c..32137e1 100644 --- a/othersoft/TemperatureControler/project/Constant_Temperature_Config.json +++ b/othersoft/TemperatureControler/project/Constant_Temperature_Config.json @@ -9,23 +9,29 @@ "BaudRate": 115200, "Pin": { "TG": { - "Pin_H": 7, - "Pin_L": 10 + "Pin_H": 2, + "Pin_L": 3 }, "Fan": { - "Pin": 11 + "Pin": 10 }, "HOT": { - "Pin": 12 + "Pin": 7 } }, "Constant_Temperature": { - "MaxTemp": 35, - "MinTemp": 30 + "CoolBegin_Temp_Insight": 20, + "CoolBegin_Diff_Temp": 5, + "HeatingBegin_Temp_Insight": 0, + "IsCoolInDaylight": false }, - "Temperature": { - "index_x": 0, - "index_y": 0 + "Location": { + "latitude": 0, + "longitude": 0 + }, + "IndexInfo": { + "Insight": [{"x":7,"y":1},{"x":8,"y":1}], + "Outside": [{"x":1,"y":1},{"x":2,"y":1}] } } diff --git a/othersoft/TemperatureControler/src/Temperature/TG_Manager.cpp b/othersoft/TemperatureControler/src/Temperature/TG_Manager.cpp index db9f418..e067918 100644 --- a/othersoft/TemperatureControler/src/Temperature/TG_Manager.cpp +++ b/othersoft/TemperatureControler/src/Temperature/TG_Manager.cpp @@ -10,6 +10,7 @@ void TG_Manager::SetPin(int pin1_H, int pin1_L, int pin2_H, int pin2_L) this->pin1_L = pin1_L; this->pin2_H = pin2_H; this->pin2_L = pin2_L; + InitManger(); } bool TG_Manager::SetTuiGan(int IS_High) diff --git a/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.cpp b/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.cpp index 4192fdb..86e51ae 100644 --- a/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.cpp +++ b/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.cpp @@ -4,33 +4,40 @@ #include "TemperatureRegulator.h" #include "logout.h" +#include "sunupanddown.h" TemperatureRegulator *g_tempretureRegulator; TemperatureRegulator::TemperatureRegulator(){ g_tempretureRegulator=this; } /* - "PortName": "ttyS2", +"PortName": "ttyS2", "BaudRate": 115200, "Pin": { "TG": { - "Pin_H": 7, - "Pin_L": 10 + "Pin_H": 2, + "Pin_L": 3 }, "Fan": { - "Pin": 11 + "Pin": 10 }, "HOT": { - "Pin": 12 + "Pin": 7 } }, "Constant_Temperature": { - "MaxTemp": 30, - "MinTemp": 0 + "CoolBegin_Temp_Insight": 20, + "CoolBegin_Diff_Temp": 5, +"HeatingBegin_Temp_Insight": 0, + "IsCoolInDaylight": false }, -"Temperature": { - "index_x": 0, - "index_y": 0, + "Location": { + "latitude": 0, + "longitude": 0 + }, + "IndexInfo": { + "Insight": [{"x":7,"y":1},{"x":8,"y":1}], + "Outside": [{"x":1,"y":1},{"x":2,"y":1}] } **/ @@ -73,23 +80,53 @@ void TemperatureRegulator::setconfig(json jconfig) { m_TG_Manager=new TG_Manager(); m_TG_Manager->SetPin(pin1_H,pin1_L,pin2_H,pin2_L); - if (jconfig.contains("Constant_Temperature")) { - if (jconfig["Constant_Temperature"].contains("MaxTemp")) { - M_Targert_Max_Tempreature=jconfig["Constant_Temperature"]["MaxTemp"]; + if (jconfig["Constant_Temperature"].contains("CoolBegin_Temp_Insight")) { + m_CoolBegin_Temp_Insight=jconfig["Constant_Temperature"]["CoolBegin_Temp_Insight"]; } - if (jconfig["Constant_Temperature"].contains("MinTemp")) { - M_Targert_Min_Tempreature=jconfig["Constant_Temperature"]["MinTemp"]; + if (jconfig["Constant_Temperature"].contains("CoolBegin_Diff_Temp")) { + m_CoolBegin_Diff_Temp=jconfig["Constant_Temperature"]["CoolBegin_Diff_Temp"]; + } + if (jconfig["Constant_Temperature"].contains("HeatingBegin_Temp_Insight")) { + m_HeatingBegin_Temp_Insight=jconfig["Constant_Temperature"]["HeatingBegin_Temp_Insight"]; + } + if (jconfig["Constant_Temperature"].contains("IsCoolInDaylight")) { + m_IsCoolInDaylight=jconfig["Constant_Temperature"]["IsCoolInDaylight"].get(); } } - if (jconfig.contains("Temperature")) { - if (jconfig["Temperature"].contains("index_x")) { - m_target_index=jconfig["Temperature"]["index_x"]; + + + + if (jconfig.contains("Location")) { + if (jconfig["Location"].contains("latitude")) { + m_Latitude=jconfig["Location"]["latitude"]; } - if (jconfig["Temperature"].contains("index_y")) { - m_target_indey=jconfig["Temperature"]["index_y"]; + if (jconfig["Location"].contains("longitude")) { + m_Longitude=jconfig["Location"]["longitude"]; } } + if (jconfig.contains("IndexInfo")) { + if (jconfig["IndexInfo"].contains("Insight")) { + for (int i = 0; i < jconfig["IndexInfo"]["Insight"].size(); ++i) { + int x=jconfig["IndexInfo"]["Insight"][i]["x"]; + int y=jconfig["IndexInfo"]["Insight"][i]["y"]; + addTargetIndex(x,y,INSIGHT_TEMPERATURE); + } + } + if (jconfig["IndexInfo"].contains("Outside")) { + for (int i = 0; i < jconfig["IndexInfo"]["Outside"].size(); ++i) { + int x=jconfig["IndexInfo"]["Outside"][i]["x"]; + int y=jconfig["IndexInfo"]["Outside"][i]["y"]; + addTargetIndex(x,y,OUTSIGHT_TEMPERATURE); + } + } + } + + + + + + isinit=true; @@ -102,16 +139,198 @@ void TemperatureRegulator::setconfig(json jconfig) { void TemperatureRegulator::SetTartGetX_Y(int x,int y){ m_target_index=x; m_target_indey=y; + } + +void TemperatureRegulator::addTargetIndex(int x, int y,int Data_type) { + //判断数据有效性 + if (x<0||y<0) { + logout("TemperatureRegulator","addTargetIndex error little than 0",6); + return; + } + if (x>7||y>7) { + logout("TemperatureRegulator","addTargetIndex error Big",6); + return; + } + switch (Data_type) { + case INSIGHT_TEMPERATURE: { + m_target_index_of_insight.push_back({x-1,y-1}); + break; + } + case OUTSIGHT_TEMPERATURE: { + m_target_index_of_outsight.push_back({x-1,y-1}); + break; + } + default: { + break; + } + } +} + +float TemperatureRegulator::GetMax_of_Vector(int Data_type) { + switch (Data_type) { + case INSIGHT_TEMPERATURE: { + float max=0; + for (int i = 0; i < m_target_index_of_insight.size(); ++i) { + int x=m_target_index_of_insight[i].x; + int y=m_target_index_of_insight[i].y; + if (m_TemperaTureWoker->m_Tempreaturegroups[y].tempera[x]>max) { + max=m_TemperaTureWoker->m_Tempreaturegroups[y].tempera[x]; + } + } + return max; + } + case OUTSIGHT_TEMPERATURE: { + float max=0; + for (int i = 0; i < m_target_index_of_outsight.size(); ++i) { + int x=m_target_index_of_outsight[i].x; + int y=m_target_index_of_outsight[i].y; + if (m_TemperaTureWoker->m_Tempreaturegroups[y].tempera[x]>max) { + max=m_TemperaTureWoker->m_Tempreaturegroups[y].tempera[x]; + } + } + return max; + } + default: { + return 0; + } + } +} + +float TemperatureRegulator::GetMin_of_Vector(int Data_type) { + switch (Data_type) { + case INSIGHT_TEMPERATURE: { + + float min=100; + for (int i = 0; i < m_target_index_of_insight.size(); ++i) { + int x=m_target_index_of_insight[i].x; + int y=m_target_index_of_insight[i].y; + if (m_TemperaTureWoker->m_Tempreaturegroups[y].tempera[x]m_Tempreaturegroups[y].tempera[x]; + } + } + return min; + } + case OUTSIGHT_TEMPERATURE: { + float min=100; + for (int i = 0; i < m_target_index_of_outsight.size(); ++i) { + int x=m_target_index_of_outsight[i].x; + int y=m_target_index_of_outsight[i].y; + if (m_TemperaTureWoker->m_Tempreaturegroups[y].tempera[x]m_Tempreaturegroups[y].tempera[x]; + } + } + return min; + } + default: { + return 0; + } + } +} + void TemperatureRegulator::init(){ +} + +void TemperatureRegulator::PanduanCooling() { + } void TemperatureRegulator::loop(){ if (!isinit) return; m_TemperaTureWoker->ReadTempreature(); + // todo 用savedata函数记录temperature + QDateTime currentTime=QDateTime::currentDateTime(); + int year=currentTime.date().year(); + int month=currentTime.date().month(); + int day=currentTime.date().day(); + int nowhour=currentTime.time().hour(); + int nowminute=currentTime.time().minute(); + long nowminuteofday=nowhour*60+nowminute; + SunInfo sunInfo=calculateSunriseSunset(year,month,day,m_Latitude,m_Longitude); + float sunrise_minte=sunInfo.sunrise_h*60+sunInfo.sunrise_m; + float sunset_minte=sunInfo.sunset_h*60+sunInfo.sunset_m; + //如果时白天(前后个加15分钟误差) + if (nowminuteofday>sunrise_minte+15&&nowminuteofdaym_CoolBegin_Diff_Temp) { + // 如果当前insight温度大于设定起始值 + if (maxinsighttemp>m_CoolBegin_Temp_Insight) { + m_TG_Manager->Cooling(); + logout("Temp","current max insight temp is "+QString::number(maxinsighttemp),6); + } else { + m_TG_Manager->Stop(); + logout("Temp","current max insight temp is "+QString::number(maxinsighttemp),6); + } + } else { + m_TG_Manager->Stop(); + logout("Temp","current diff is "+QString::number(diff),6); + } + } + + + + + + //如果 开始加热温度高于开始降温温度 说明参数设置有问题 推出 + if (m_CoolBegin_Temp_InsightHeating(); + logout("Temp","current min insight temp is "+QString::number(mininsighttemp),6); + } + else + { + m_TG_Manager->Stop(); + logout("Temp","current min insight temp is "+QString::number(mininsighttemp),6); + } + + + } + else //夜晚 + { + //夜间只降温 且只要外部温度大于内部温度即降温 + //第一步 获取内部最大温度 + float maxinsighttemp=GetMax_of_Vector(INSIGHT_TEMPERATURE); + //第二步 获取外部最小温度 + float minoutsighttemp=GetMin_of_Vector(OUTSIGHT_TEMPERATURE); + // 如果外部温度小于内部温度 + if (maxinsighttemp>minoutsighttemp) + { + m_TG_Manager->Cooling(); + logout("Temp","current max insight temp is "+QString::number(maxinsighttemp),6); + } + else + { + m_TG_Manager->Stop(); + logout("Temp","current max insight temp is "+QString::number(maxinsighttemp),6); + } + } + + + + return; + + float tempreture=m_TemperaTureWoker->m_Tempreaturegroups[m_target_index].tempera[m_target_indey]; //printf("current tempreture is %f\n",tempreture); logout("Temp","current tempreture is "+QString::number(tempreture),6); @@ -135,3 +354,5 @@ void TemperatureRegulator::loop(){ bool TemperatureRegulator::looptask() { g_tempretureRegulator->loop(); } + + diff --git a/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.h b/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.h index 55ae63a..bd746dd 100644 --- a/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.h +++ b/othersoft/TemperatureControler/src/Temperature/TemperatureRegulator.h @@ -7,16 +7,57 @@ #include "TG_Manager.h" #include "TemperaTureWoker.h" #include "QObject" - +#include "vector" +#define INSIGHT_TEMPERATURE 0 +#define OUTSIGHT_TEMPERATURE 1 +/* +* "PortName": "ttyS2", + "BaudRate": 115200, + "Pin": { + "TG": { + "Pin_H": 2, + "Pin_L": 3 + }, + "Fan": { + "Pin": 10 + }, + "HOT": { + "Pin": 7 + } + }, + "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}] + } + **/ +struct indexofdata +{ + int x; + int y; +}; class TemperatureRegulator :public QObject { Q_OBJECT public: TemperatureRegulator(); void setconfig(json j); void SetTartGetX_Y(int x,int y); + void addTargetIndex(int x,int y,int Data_type=INSIGHT_TEMPERATURE); + float GetMax_of_Vector(int Data_type=INSIGHT_TEMPERATURE); + float GetMin_of_Vector(int Data_type=INSIGHT_TEMPERATURE); void init(); void loop(); static bool looptask(); + void PanduanCooling(); @@ -25,9 +66,17 @@ private: TG_Manager *m_TG_Manager; float M_Targert_Max_Tempreature=35; float M_Targert_Min_Tempreature=25; + float m_CoolBegin_Temp_Insight=20; + float m_CoolBegin_Diff_Temp=5; + float m_HeatingBegin_Temp_Insight=0; + bool m_IsCoolInDaylight=false; int m_target_index=0; + std::vector m_target_index_of_insight; + std::vector m_target_index_of_outsight; int m_target_indey=0; bool isinit=false; + float m_Latitude=39.9; + float m_Longitude=116.4; }; diff --git a/othersoft/TemperatureControler/src/json/logout.cpp b/othersoft/TemperatureControler/src/json/logout.cpp index 9ca3b41..379ad08 100644 --- a/othersoft/TemperatureControler/src/json/logout.cpp +++ b/othersoft/TemperatureControler/src/json/logout.cpp @@ -114,4 +114,30 @@ void writelogtofile(QString sender,QString qstr) fprintf(file,"%s %s: %s\n",currentTime.toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str(),sender.toStdString().c_str(),qstr.toStdString().c_str()); fflush(file); +} + +void saveData(QString sender,QString qstr) +{ + QDateTime currentTime = QDateTime::currentDateTime(); + QString datestr=currentTime.toString("yyyy_MM_dd"); + if (lastdatestr!=datestr) + { + if (file!=NULL) + { + fclose(file); + file=NULL; + } + lastdatestr=datestr; + } + if (file==NULL) + { + QString path="/home/iris/TempData/"+currentTime.toString("yyyy_MM_dd")+".csv"; + system("mkdir -p /home/iris/TempData"); + file=fopen(path.toStdString().c_str(),"a+"); + fprintf(file,QString::fromLocal8Bit("鏃ユ湡, 鏁版嵁绫诲瀷, 鏃ユ湡~n").toStdString().c_str()); + } + + + fprintf(file,"%s,%s,%s\n",currentTime.toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str(),sender.toStdString().c_str(),qstr.toStdString().c_str()); + fflush(file); } \ No newline at end of file diff --git a/othersoft/TemperatureControler/src/json/logout.h b/othersoft/TemperatureControler/src/json/logout.h index 86bebd4..8f565cd 100644 --- a/othersoft/TemperatureControler/src/json/logout.h +++ b/othersoft/TemperatureControler/src/json/logout.h @@ -20,12 +20,30 @@ void logout(T str) #endif } +template + +void saveDataToFile(QString typeofdata,T str) { + const std::string red("\033[0;31m"); + const std::string green("\033[0;32m"); + const std::string yellow("\033[0;33m"); + const std::string blue("\033[0;34m"); + const std::string magenta("\033[0;35m"); + const std::string cyan("\033[0;36m"); + const std::string reset("\033[0m"); + + QString qstr11=QString(str); + QDateTime currentTime = QDateTime::currentDateTime(); + std::cout< void logout(QString sender,T str,int level = 5) { - - const std::string red("\033[0;31m"); const std::string green("\033[0;32m"); const std::string yellow("\033[0;33m"); @@ -35,6 +53,8 @@ void logout(QString sender,T str,int level = 5) const std::string reset("\033[0m"); + + QString qstr11=QString(str); // Log(qstr,3); QDateTime currentTime = QDateTime::currentDateTime(); diff --git a/othersoft/TemperatureControler/src/sunfunction/sunupanddown.cpp b/othersoft/TemperatureControler/src/sunfunction/sunupanddown.cpp new file mode 100644 index 0000000..7fa198a --- /dev/null +++ b/othersoft/TemperatureControler/src/sunfunction/sunupanddown.cpp @@ -0,0 +1,99 @@ +// +// Created by xin on 25-5-8. +// + +#include "sunupanddown.h" +#include +#include +#include +using namespace std; +bool isLeapYear(int year) { + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); +} +int dayOfYear(int year, int month, int day) { + int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (isLeapYear(year)) { + days[2] = 29; + } + int sum = day; + for (int i = 1; i < month; ++i) { + sum += days[i]; + } + return sum; +} +void calculateSunriseSunset(int year, int month, int day, double latitude, double longitude, + double &sunrise, double &sunset, bool &hasSunlight) { + int n = dayOfYear(year, month, day); + + // 计算太阳赤纬(单位:度) + double delta_deg = 23.44 * sin(2 * M_PI * (284.0 + n) / 365.0); + double delta_rad = delta_deg * M_PI / 180.0; + + // 日出日落时太阳高度角(考虑大气折射) + double h0_deg = -0.833; + double h0_rad = h0_deg * M_PI / 180.0; + + // 纬度转换为弧度 + double phi_rad = latitude * M_PI / 180.0; + double sin_phi = sin(phi_rad); + double cos_phi = cos(phi_rad); + double sin_delta = sin(delta_rad); + double cos_delta = cos(delta_rad); + + // 计算时角的余弦值 + double numerator = sin(h0_rad) - sin_phi * sin_delta; + double denominator = cos_phi * cos_delta; + + if (denominator == 0) { + hasSunlight = false; + return; + } + + double cos_omega = numerator / denominator; + + if (cos_omega < -1.0 || cos_omega > 1.0) { + // 极昼或极夜 + hasSunlight = false; + return; + } + + // 计算时角并转换为小时 + double omega_rad = acos(cos_omega); + double omega_deg = omega_rad * 180.0 / M_PI; + double omega_hours = omega_deg / 15.0; + + // 地方时日出日落时间 + double sunrise_local = 12.0 - omega_hours; + double sunset_local = 12.0 + omega_hours; + + // 计算时区修正 + double timezone_h = round(longitude / 15.0); + double timezone_lon = timezone_h * 15.0; + double time_diff = (longitude - timezone_lon) / 15.0; // 小时 + + // 转换为标准时间 + sunrise = sunrise_local - time_diff; + sunset = sunset_local - time_diff; + + // 处理时间溢出 + sunrise = fmod(sunrise + 24.0, 24.0); + sunset = fmod(sunset + 24.0, 24.0); + + hasSunlight = true; +} + +SunInfo calculateSunriseSunset(int year, int month, int day, double latitude, double longitude){ + SunInfo sunInfo; + double sunrise, sunset; + bool hasSunlight; + + calculateSunriseSunset(year, month, day, latitude, longitude, sunrise, sunset, hasSunlight); + + sunInfo.sunrise_h = floor(sunrise); + sunInfo.sunrise_m = (sunrise - sunInfo.sunrise_h) * 60; + sunInfo.sunset_h = floor(sunset); + sunInfo.sunset_m = (sunset - sunInfo.sunset_h) * 60; + sunInfo.hasSunlight = hasSunlight; + + return sunInfo; +} \ No newline at end of file diff --git a/othersoft/TemperatureControler/src/sunfunction/sunupanddown.h b/othersoft/TemperatureControler/src/sunfunction/sunupanddown.h new file mode 100644 index 0000000..08b0e82 --- /dev/null +++ b/othersoft/TemperatureControler/src/sunfunction/sunupanddown.h @@ -0,0 +1,22 @@ +// +// Created by xin on 25-5-8. +// + +#ifndef SUNUPANDDOWN_H +#define SUNUPANDDOWN_H +struct SunInfo { + double sunrise_h; // 日出时间(小时) + double sunrise_m; // 日出时间(分钟) + double sunset_h; // 日落时间(小时) + double sunset_m; // 日落时间(分钟) + bool hasSunlight; // 是否有阳光 +}; + +SunInfo calculateSunriseSunset(int year, int month, int day, double latitude, double longitude); + +void calculateSunriseSunset(int year, int month, int day, double latitude, double longitude, + double &sunrise, double &sunset, bool &hasSunlight); + + + +#endif //SUNUPANDDOWN_H