#include "stdafx.h" #include "HttpPrcess.h" #include #include #include "MonitorObject.h" #include "AlarmDefine.h" #include #include "AppService.h" #include #include "Device.h" #include "ResistAlarm.h" //处理get请求总入口 void DealHttpGet(const httplib::Request& req, httplib::Response& res) { int code = 500; auto start = chrono::steady_clock::now(); auto token = req.get_header_value("token"); if (token.length() == 0 || token.empty()) token = req.get_header_value("Authorization"); char* json = nullptr; string body; size_t json_len = 0; SPDLOG_INFO("[HTTP][GET][START] {}:{} {}", req.remote_addr, req.remote_port, req.target); try { if (token.empty()) { code = 401; } if (req.path.find("option_svg") != -1) code = DealOptionSvgData(req, token, res); else if (req.path.find("get_svg") != -1) code = DealGetSvgData(req, token, res); else if (req.path.find("get_mp_stat") != -1) code = DealGetMpStat(req, token, res); else if (req.path.find("conf_read/mitie_lock") != -1) code = DealGetMitieLock(req, token, &json, &json_len); else if (req.path.find("conf_read/convert_resist") != -1) code = DealGetConvertResist(req, token, &json, &json_len); else if (req.path.find("resistance_curve") != -1) code = DealGetResistCurve(req, token, &json, &json_len); else if (req.path.find("resistance_report") != -1) code = DealGetResistReport(req, token, &json, &json_len); else if (req.path.find("move_info") != -1) code = DealGetMoveInfo(req, token, &json, &json_len); else if (req.path.find("operation_log") != -1) code = DealGetOptLog(req, token, &json, &json_len); else if (req.path.find("refer_curve") != -1) code = DealGetrefer_curve(req, token, &json, &json_len); else if (req.path.find("refer_option") != -1) code = DealGetrefer_option(req, token, &json, &json_len); else if (req.path.find("get_temp") != -1) code = DealGetTemp(req, token, &json, &json_len); else if (req.path.find("get_humitemp") != -1) code = DealGetTempHumi(req, token, &json, &json_len); else if (req.path.find("backend_type") != -1) { body = R"({"type":2})"; code = 200; } else code = 405; } catch (...) { code = 500; } if (json) res.set_content(json, "application/json"); if (!body.empty()) res.set_content(body.c_str(), "application/json"); auto cost = chrono::duration_cast(chrono::steady_clock::now() - start).count(); SPDLOG_INFO("[HTTP][GET][END] code:{} COST:{} {}:{} {} {}", code, cost, req.remote_addr, req.remote_port, req.target, json ? json : ""); res.set_header("cost", to_string(cost) + " ms"); res.status = code; if (json) free(json); } //处理post请求总入口 void DealHttpPost(const httplib::Request& req, httplib::Response& res) { int code = 500; auto start = chrono::steady_clock::now(); auto token = req.get_header_value("token"); if (token.length() == 0 || token.empty()) token = req.get_header_value("Authorization"); char* json = nullptr; size_t json_len = 0; SPDLOG_INFO("[HTTP][POST][START] {}:{} {} body:{}", req.remote_addr, req.remote_port, req.path, req.body); try { if (token.empty()) { code = 401; } if (req.path.find("post_svg") != -1) code = DealPostSvgData(req, token, res); else if (req.path.find("option_svg") != -1) code = DealOptionSvgData(req, token, res); else if (req.path.find("commit_record") != -1) code = DealPostCommitRecord(req.body.c_str(), req.body.length(), token, &json, &json_len); else if (req.path.find("get_svg") != -1) code = DealGetSvgData(req, token, res); //else if (req.path.find("post_soft") != -1) // code = DealPostBinData(req, token, res); else if (req.path.find("conf_write/convert_resist") != -1) code = DealPostConvertResist(req.body.c_str(), req.body.length(), token, &json, &json_len); else if (req.path.find("conf_write/mitie_lock") != -1) code = DealPostMitieLock(req.body.c_str(), req.body.length(), token, &json, &json_len); else if (req.path.find("refer_curve") != -1) code = DealPostrefer_curve(req.body.c_str(), req.body.length(), token, &json, &json_len); else code = 405; } catch (...) { code = 500; } if (json) res.set_content(json, "application/json"); auto cost = chrono::duration_cast(chrono::steady_clock::now() - start).count(); SPDLOG_INFO("[HTTP][POST][END] code:{} COST:{} {}:{} {} body:{} json:{}", code, cost, req.remote_addr, req.remote_port, req.path, req.body, json ? json : ""); res.set_header("cost", to_string(cost) + " ms"); res.status = code; if (json) free(json); } int DealPostSvgData(const httplib::Request& req, const string token, httplib::Response& res) { if (token.find("AAAAAAAA") == -1) return 403; auto id = req.get_header_value("id"); if (id.empty()) return 400;//参数错误 char szID[200] = { 0 }; utf82gbk(szID, 200, id.c_str(), id.length()); //处理中文 if (req.body.empty()) return 400; CString strPath = CSimpleLog::GetAppDir() + "svg\\" + szID + ".svg"; CFile file; if (file.Open(strPath, CFile::modeWrite | CFile::typeBinary | CFile::modeCreate)) { file.Write(req.body.c_str(), req.body.length()); file.Close(); return 200; } else { return 500; } return 405; } int DealGetSvgData(const httplib::Request& req, const string token, httplib::Response& res) { auto id = req.get_param_value("id"); if (id.empty()) return 400;//参数错误 char szID[200] = { 0 }; utf82gbk(szID, 200, id.c_str(), id.length()); //处理中文 CString strPath = CSimpleLog::GetAppDir() + "svg\\" + szID + ".svg"; auto bExist = CSimpleLog::PathFileExists(strPath); if (bExist == false) return 404; CFile file; if (file.Open(strPath, CFile::modeRead | CFile::typeBinary)) { int len = file.GetLength(); auto pData = new uint8_t[len]; file.Read(pData, len); file.Close(); res.set_content((char*)pData, len, "image/svg+xml"); } else { return 500; } return 200; } int DealOptionSvgData(const httplib::Request& req, const string token, httplib::Response& res) { auto id = req.get_param_value("id"); if (id.empty()) return 400;//参数错误 char szID[200] = { 0 }; utf82gbk(szID, 200, id.c_str(), id.length()); //处理中文 CString strPath = CSimpleLog::GetAppDir() + "svg\\" + szID + ".svg"; auto bExist = CSimpleLog::PathFileExists(strPath); string strResult; if (bExist) strResult = R"({"result":true})"; else strResult = R"({"result":false})"; res.set_content(strResult, "application/json"); return 200; } int DealGetMpStat(const httplib::Request& req, const string token, httplib::Response& res) { auto id = req.get_param_value("id");//杭南杭甬场 if (id.empty()) return 400;//参数错误 char station[200]; int id_len = utf82gbk(station, 200, id.c_str(), id.length()); auto pStation = CMonitorObjectMng::Instance()->GetTreeByID(string(station)); if (nullptr == pStation) return 404; if (pStation->type.compare("station") != 0) return 400; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_arr(doc); yyjson_mut_doc_set_root(doc, root); for (const auto& it : pStation->m_lstMo) { auto arr = yyjson_mut_arr(doc); yyjson_mut_arr_add_val(root, arr); yyjson_mut_arr_add_strcpy(doc, arr, it->id.c_str()); yyjson_mut_arr_add_strcpy(doc, arr, it->name.c_str()); CDevice* pDevice = CDeviceMng::Instance()->Find(it->id); if (pDevice) { string mo, mp; if (CMonitorObjectMng::spiltByPoint(it->id, mo, mp)) { SYSTEMTIME stAlarm = { 0 }; auto ty = CResistAlarmMng::Instance()->GetAlarmStat(mo, mp, stAlarm); if (ty == eZL_MP_STAT::MP_STAT_OFFLINE_GRAY) { if (pDevice->IsDeviceOnline()) yyjson_mut_arr_add_int(doc, arr, (int)eZL_MP_STAT::MP_STAT_NORMAL_GREEN); else yyjson_mut_arr_add_int(doc, arr, (int)eZL_MP_STAT::MP_STAT_OFFLINE_GRAY); } else { yyjson_mut_arr_add_int(doc, arr, (int)ty); sprintf_s(station, 200, "%04d-%02d-%02d %02d:%02d:%02d.%03d", stAlarm.wYear, stAlarm.wMonth, stAlarm.wDay, stAlarm.wHour, stAlarm.wMinute, stAlarm.wSecond, stAlarm.wMilliseconds); yyjson_mut_arr_add_strcpy(doc, arr, station); } } else { yyjson_mut_arr_add_int(doc, arr, (int)eZL_MP_STAT::MP_STAT_UNINSTALL_WHITE); } } else { yyjson_mut_arr_add_int(doc, arr, (int)eZL_MP_STAT::MP_STAT_UNINSTALL_WHITE); } } size_t len; auto json = yyjson_mut_write(doc, 0, &len); res.set_content(json, len, "application/json"); if (json) { free((void*)json); json = nullptr; } yyjson_mut_doc_free(doc); return 200; } int DealGetMitieLock(const httplib::Request& req, const string token, char** json, size_t* json_len) { string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); eSuoBiPosi posi = (eSuoBiPosi)atoi(req.get_param_value("posi").c_str()); if (posi == eSuoBiPosi::SB_ZERO) posi = eSuoBiPosi::SB_UNKNOWN; if (mo.length() == 0 || mp.length() == 0) return 400; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); SUOBI_OVER_LIMIT_INFO* pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, (uint8_t)posi, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); if (pInfo) { yyjson_mut_obj_add_bool(doc, root, "enable", pInfo->enable); if (pInfo->alarm_low_limit > INT_MIN) yyjson_mut_obj_add_int(doc, root, "alarm_low_limit", pInfo->alarm_low_limit); else yyjson_mut_obj_add_null(doc, root, "alarm_low_limit"); if (pInfo->warn_low_limit > INT_MIN) yyjson_mut_obj_add_int(doc, root, "warn_low_limit", pInfo->warn_low_limit); else yyjson_mut_obj_add_null(doc, root, "warn_low_limit"); if (pInfo->alarm_high_limit < INT_MAX) yyjson_mut_obj_add_int(doc, root, "alarm_high_limit", pInfo->alarm_high_limit); else yyjson_mut_obj_add_null(doc, root, "alarm_high_limit"); if (pInfo->warn_high_limit < INT_MAX) yyjson_mut_obj_add_int(doc, root, "warn_high_limit", pInfo->warn_high_limit); else yyjson_mut_obj_add_null(doc, root, "warn_high_limit"); } else { yyjson_mut_obj_add_bool(doc, root, "enable", false); yyjson_mut_obj_add_null(doc, root, "alarm_low_limit"); yyjson_mut_obj_add_null(doc, root, "warn_low_limit"); yyjson_mut_obj_add_null(doc, root, "alarm_high_limit"); yyjson_mut_obj_add_null(doc, root, "warn_high_limit"); } *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return 200; } int DealPostMitieLock(const char* body_ptr, const size_t body_len, const string token, char** json, size_t* json_len) { return 403; //关闭设置 int code = 400; auto req_doc = yyjson_read(body_ptr, body_len, 0); if (req_doc == nullptr) return 400; auto req_root = yyjson_doc_get_root(req_doc); do { auto mo = yyjson_get_str(yyjson_obj_get(req_root, "mo")); auto mp = yyjson_get_str(yyjson_obj_get(req_root, "mp")); eSuoBiPosi posi = (eSuoBiPosi)yyjson_get_int(yyjson_obj_get(req_root, "posi"));;//预留 if (posi == eSuoBiPosi::SB_ZERO) posi = eSuoBiPosi::SB_UNKNOWN; auto conf = yyjson_obj_get(req_root, "conf"); if (mo == nullptr || mp == nullptr || conf == nullptr) break; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); size_t len; char* str_conf = nullptr; //更新内存里 SUOBI_OVER_LIMIT_INFO* pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, (uint8_t)posi, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); CString sql; if (pInfo == nullptr) { SUOBI_OVER_LIMIT_INFO* pInfo = new SUOBI_OVER_LIMIT_INFO; pInfo->type = eZL_ALARMTYPE::SUOBI_LOCK_LIMIT; pInfo->no = (uint8_t)posi; pInfo->enable = yyjson_get_bool(yyjson_obj_get(conf, "enable")); yyjson_mut_obj_add_bool(doc, root, "enable", pInfo->enable); { auto val = yyjson_obj_get(conf, "alarm_low_limit"); if (val && yyjson_is_int(val)) pInfo->alarm_low_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "alarm_low_limit", pInfo->alarm_low_limit); } { auto val = yyjson_obj_get(conf, "warn_low_limit"); if (val && yyjson_is_int(val)) pInfo->warn_low_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "warn_low_limit", pInfo->warn_low_limit); } { auto val = yyjson_obj_get(conf, "alarm_high_limit"); if (val && yyjson_is_int(val)) pInfo->alarm_high_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "alarm_high_limit", pInfo->alarm_high_limit); } { auto val = yyjson_obj_get(conf, "warn_high_limit"); if (val && yyjson_is_int(val)) pInfo->warn_high_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "warn_high_limit", pInfo->warn_high_limit); } str_conf = yyjson_mut_write(doc, 0, &len); yyjson_mut_doc_free(doc); CResistAlarmMng::Instance()->Insert(mo, mp, (uint8_t)posi, (uint8_t)eZL_ALARMTYPE::SUOBI_LOCK_LIMIT, pInfo); time_t tt; time(&tt); sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d, %d,'%s','%I64u')", mo, mp, (uint8_t)posi, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT, str_conf, tt); } else { pInfo->enable = yyjson_get_bool(yyjson_obj_get(conf, "enable")); yyjson_mut_obj_add_bool(doc, root, "enable", pInfo->enable); { auto val = yyjson_obj_get(conf, "alarm_low_limit"); if (val && yyjson_is_int(val)) pInfo->alarm_low_limit = yyjson_get_int(val); else pInfo->alarm_low_limit = INT_MIN; yyjson_mut_obj_add_int(doc, root, "alarm_low_limit", pInfo->alarm_low_limit); } { auto val = yyjson_obj_get(conf, "warn_low_limit"); if (val && yyjson_is_int(val)) pInfo->warn_low_limit = yyjson_get_int(val); else pInfo->warn_low_limit = INT_MIN; yyjson_mut_obj_add_int(doc, root, "warn_low_limit", pInfo->warn_low_limit); } { auto val = yyjson_obj_get(conf, "alarm_high_limit"); if (val && yyjson_is_int(val)) pInfo->alarm_high_limit = yyjson_get_int(val); else pInfo->alarm_high_limit = INT_MAX; yyjson_mut_obj_add_int(doc, root, "alarm_high_limit", pInfo->alarm_high_limit); } { auto val = yyjson_obj_get(conf, "warn_high_limit"); if (val && yyjson_is_int(val)) pInfo->warn_high_limit = yyjson_get_int(val); else pInfo->warn_high_limit = INT_MAX; yyjson_mut_obj_add_int(doc, root, "warn_high_limit", pInfo->warn_high_limit); } str_conf = yyjson_mut_write(doc, 0, &len); yyjson_mut_doc_free(doc); sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d", str_conf, mo, mp, (uint8_t)posi, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); } if (str_conf) free(str_conf); if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE) { ASSERT(FALSE); CSimpleLog::Error("语句执行失败" + sql); code = 500; break; } code = 200; } while (false); yyjson_doc_free(req_doc); return code; } int DealGetConvertResist(const httplib::Request& req, const string token, char** json, size_t* json_len) { string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); if (mo.length() == 0 || mp.length() == 0) return 400; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); CONVERT_RESIST_OVER_LIMIT* pInfo = (CONVERT_RESIST_OVER_LIMIT*)CResistAlarmMng::Instance()->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT); if (pInfo) { yyjson_mut_obj_add_bool(doc, root, "enable", pInfo->enable); if (pInfo->dw_alarm_low_limit > INT_MIN) yyjson_mut_obj_add_int(doc, root, "dw_alarm_low_limit", pInfo->dw_alarm_low_limit); else yyjson_mut_obj_add_null(doc, root, "dw_alarm_low_limit"); if (pInfo->dw_warn_low_limit > INT_MIN) yyjson_mut_obj_add_int(doc, root, "dw_warn_low_limit", pInfo->dw_warn_low_limit); else yyjson_mut_obj_add_null(doc, root, "dw_warn_low_limit"); if (pInfo->up_alarm_high_limit < INT_MAX) yyjson_mut_obj_add_int(doc, root, "up_alarm_high_limit", pInfo->up_alarm_high_limit); else yyjson_mut_obj_add_null(doc, root, "up_alarm_high_limit"); if (pInfo->up_warn_high_limit < INT_MAX) yyjson_mut_obj_add_int(doc, root, "up_warn_high_limit", pInfo->up_warn_high_limit); else yyjson_mut_obj_add_null(doc, root, "up_warn_high_limit"); } else { yyjson_mut_obj_add_bool(doc, root, "enable", false); yyjson_mut_obj_add_null(doc, root, "dw_alarm_low_limit"); yyjson_mut_obj_add_null(doc, root, "dw_warn_low_limit"); yyjson_mut_obj_add_null(doc, root, "up_alarm_high_limit"); yyjson_mut_obj_add_null(doc, root, "up_warn_high_limit"); } *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return 200; } int DealPostConvertResist(const char* body_ptr, const size_t body_len, const string token, char** json, size_t* json_len) { return 403; int code = 400; auto req_doc = yyjson_read(body_ptr, body_len, 0); if (req_doc == nullptr) return 400; auto req_root = yyjson_doc_get_root(req_doc); do { auto mo = yyjson_get_str(yyjson_obj_get(req_root, "mo")); auto mp = yyjson_get_str(yyjson_obj_get(req_root, "mp")); auto conf = yyjson_obj_get(req_root, "conf"); if (mo == nullptr || mp == nullptr || conf == nullptr) break; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); size_t len; char* str_conf = nullptr; //更新内存里 CONVERT_RESIST_OVER_LIMIT* pInfo = (CONVERT_RESIST_OVER_LIMIT*)CResistAlarmMng::Instance()->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT); CString sql; if (pInfo == nullptr) { CONVERT_RESIST_OVER_LIMIT* pInfo = new CONVERT_RESIST_OVER_LIMIT; pInfo->type = eZL_ALARMTYPE::CONVERT_LIMIT; pInfo->no = 2; pInfo->enable = yyjson_get_bool(yyjson_obj_get(conf, "enable")); yyjson_mut_obj_add_bool(doc, root, "enable", pInfo->enable); { auto val = yyjson_obj_get(conf, "dw_alarm_low_limit"); if (val && yyjson_is_int(val)) pInfo->dw_alarm_low_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "dw_alarm_low_limit", pInfo->dw_alarm_low_limit); } { auto val = yyjson_obj_get(conf, "dw_warn_low_limit"); if (val && yyjson_is_int(val)) pInfo->dw_warn_low_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "dw_warn_low_limit", pInfo->dw_warn_low_limit); } { auto val = yyjson_obj_get(conf, "up_alarm_high_limit"); if (val && yyjson_is_int(val)) pInfo->up_alarm_high_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "up_alarm_high_limit", pInfo->up_alarm_high_limit); } { auto val = yyjson_obj_get(conf, "up_warn_high_limit"); if (val && yyjson_is_int(val)) pInfo->up_warn_high_limit = yyjson_get_int(val); yyjson_mut_obj_add_int(doc, root, "up_warn_high_limit", pInfo->up_warn_high_limit); } str_conf = yyjson_mut_write(doc, 0, &len); yyjson_mut_doc_free(doc); CResistAlarmMng::Instance()->Insert(mo, mp, 2, (uint8_t)eZL_ALARMTYPE::CONVERT_LIMIT, pInfo); sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')", mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT, str_conf, 0); } else { pInfo->enable = yyjson_get_bool(yyjson_obj_get(conf, "enable")); yyjson_mut_obj_add_bool(doc, root, "enable", pInfo->enable); { auto val = yyjson_obj_get(conf, "dw_alarm_low_limit"); if (val && yyjson_is_int(val)) pInfo->dw_alarm_low_limit = yyjson_get_int(val); else pInfo->dw_alarm_low_limit = INT_MIN; yyjson_mut_obj_add_int(doc, root, "dw_alarm_low_limit", pInfo->dw_alarm_low_limit); } { auto val = yyjson_obj_get(conf, "dw_warn_low_limit"); if (val && yyjson_is_int(val)) pInfo->dw_warn_low_limit = yyjson_get_int(val); else pInfo->dw_warn_low_limit = INT_MIN; yyjson_mut_obj_add_int(doc, root, "dw_warn_low_limit", pInfo->dw_warn_low_limit); } { auto val = yyjson_obj_get(conf, "up_alarm_high_limit"); if (val && yyjson_is_int(val)) pInfo->up_alarm_high_limit = yyjson_get_int(val); else pInfo->up_alarm_high_limit = INT_MAX; yyjson_mut_obj_add_int(doc, root, "up_alarm_high_limit", pInfo->up_alarm_high_limit); } { auto val = yyjson_obj_get(conf, "up_warn_high_limit"); if (val && yyjson_is_int(val)) pInfo->up_warn_high_limit = yyjson_get_int(val); else pInfo->up_warn_high_limit = INT_MAX; yyjson_mut_obj_add_int(doc, root, "up_warn_high_limit", pInfo->up_warn_high_limit); } str_conf = yyjson_mut_write(doc, 0, &len); yyjson_mut_doc_free(doc); sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d", str_conf, mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT); } if (str_conf) free(str_conf); if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE) { ASSERT(FALSE); CSimpleLog::Error("语句执行失败" + sql); code = 500; break; } code = 200; } while (false); yyjson_doc_free(req_doc); return code; } int DealPostCommitRecord(const char* body_ptr, const size_t body_len, const string token, char** json, size_t* json_len) { int code = 400; string msg; auto req_doc = yyjson_read(body_ptr, body_len, 0); if (req_doc == nullptr) return 400; auto req_root = yyjson_doc_get_root(req_doc); do { if (yyjson_is_arr(req_root) == false) break; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); size_t n = yyjson_arr_size(req_root); auto req_obj = yyjson_arr_get_first(req_root); for (size_t i = 0; i < n; i++) { auto module = yyjson_get_int(yyjson_obj_get(req_obj, "module")); auto dura = yyjson_get_int(yyjson_obj_get(req_obj, "dura")); auto analyze_type = yyjson_get_int(yyjson_obj_get(req_obj, "analyze_type")); auto station = yyjson_get_str(yyjson_obj_get(req_obj, "station")); auto station_name = yyjson_get_str(yyjson_obj_get(req_obj, "station_name")); auto mo = yyjson_get_str(yyjson_obj_get(req_obj, "mo")); auto mo_name = yyjson_get_str(yyjson_obj_get(req_obj, "mo_name")); auto mp = yyjson_get_str(yyjson_obj_get(req_obj, "mp")); auto mp_name = yyjson_get_str(yyjson_obj_get(req_obj, "mp_name")); auto start_time = yyjson_get_uint(yyjson_obj_get(req_obj, "start_time")); auto end_time = yyjson_get_uint(yyjson_obj_get(req_obj, "end_time")); time_t tt; time(&tt); auto name = yyjson_get_str(yyjson_obj_get(req_obj, "name")); auto username = yyjson_get_str(yyjson_obj_get(req_obj, "username")); if (module == 0 || dura == 0 || station == nullptr || station_name == nullptr || start_time == 0 || name == nullptr) break; auto strStation = UTF8toANSI(station); auto strStationName = UTF8toANSI(station_name); auto strName = UTF8toANSI(name); CString strDuraTime; dura = dura / 1000.0 + 0.5; if (dura / 60) strDuraTime.Format(" 时长:%d分%d秒", dura / 60, dura % 60); else if (dura) strDuraTime.Format(" 时长:%d秒", dura); CString sql = fmt::format("INSERT INTO [dbo].[rm_record]\ (module, dura, analyze_type, station\ , station_name, mo, mo_name, mp, mp_name, start_time\ , end_time, time, name,username,opt,mark) VALUES\ ({}, {}, {}, '{}', '{}'\ , '{}', '{}', '{}', '{}', {}\ , {}, {}, '{}', '{}',{}, '{}')", module, dura, analyze_type, strStation, strStationName, IS_NULL(mo), IS_NULL(mo_name), IS_NULL(mp), IS_NULL(mp_name), start_time, end_time, tt, strName, IS_NULL(username), 1, strDuraTime).c_str(); if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE) { ASSERT(FALSE); msg = "语句执行失败:" + sql; CSimpleLog::Error(msg.c_str()); code = 500; break; } else { code = 200; } code = ExecSqlForRecord((eRecord_Module)module, dura, analyze_type, strStation, strStationName, mo, mo_name, mp, mp_name, start_time, end_time, tt, strName, username, eRocord_Opt::RO_RECORD, string(strDuraTime)); req_obj = unsafe_yyjson_get_next(req_obj); } yyjson_mut_obj_add_strcpy(doc, root, "msg", ANSItoUTF8(msg).c_str()); yyjson_mut_obj_add_int(doc, root, "code", code); if (json) *json = yyjson_mut_write(doc, 0, json_len); } while (false); yyjson_doc_free(req_doc); return code; } int DealGetOptLog(const httplib::Request& req, const string token, char** json, size_t* json_len) { string station_name = req.get_param_value("station_name"); string time = req.get_param_value("time"); string end_time = req.get_param_value("end_time"); if (time.length() == 0 ) return 400; time_t uTime = atoll(time.c_str()); time_t uEndTime = atoll(end_time.c_str()); if (uEndTime == 0) uEndTime = uTime + 86400; COleDateTime odt(uTime); if (odt.GetStatus() == COleDateTime::invalid) return 400; string str_station_name = UTF8toANSI(station_name); int code = 400; string msg; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); auto yy_data = yyjson_mut_arr(doc); auto yy_name = yyjson_mut_arr(doc); auto yy_mo = yyjson_mut_arr(doc); auto yy_mp = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "data", yy_data); yyjson_mut_obj_add_val(doc, root, "name", yy_name); yyjson_mut_obj_add_val(doc, root, "mo_name", yy_mo); yyjson_mut_obj_add_val(doc, root, "mp_name", yy_mp); //query //查询数据库 std::map map_name; std::map map_mo_name; std::map map_mp_name; { //station,station_name, CString sql; if (str_station_name.length() == 0) sql = fmt::format("SELECT module,dura,analyze_type, mo, mo_name, mp, mp_name, start_time, end_time, [time], name, username, opt, mark,station_name\ FROM rm_record WHERE time between {} AND {}", uTime, uEndTime).c_str(); else sql = fmt::format("SELECT module,dura,analyze_type, mo, mo_name, mp, mp_name, start_time, end_time, [time], name, username, opt, mark,station_name\ FROM rm_record WHERE station_name = '{}' AND time between {} AND {}", str_station_name, uTime, uEndTime).c_str(); COdbcStatement stmt; if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) { uint8_t umodule, analyze_type; int16_t opt = 0; int dura; char /*station[100], char station_name[100], */mo[50], mo_name[50], mp[50], mp_name[50], name[100], username[50], mark[200], station_name[100]; uint64_t start_time, end_time, tt; int nCol = 1; stmt.BindTinyIntCol(nCol++, &umodule); stmt.BindIntCol(nCol++, &dura); stmt.BindTinyIntCol(nCol++, &analyze_type); //stmt.BindCharCol(nCol++, station, sizeof(station)); //stmt.BindCharCol(nCol++, station_name, sizeof(station_name)); stmt.BindCharCol(nCol++, mo, sizeof(mo)); stmt.BindCharCol(nCol++, mo_name, sizeof(mo_name)); stmt.BindCharCol(nCol++, mp, sizeof(mp)); stmt.BindCharCol(nCol++, mp_name, sizeof(mp_name)); stmt.BindBigIntCol(nCol++, (__int64*)&start_time); stmt.BindBigIntCol(nCol++, (__int64*)&end_time); stmt.BindBigIntCol(nCol++, (__int64*)&tt); stmt.BindCharCol(nCol++, name, sizeof(name)); stmt.BindCharCol(nCol++, username, sizeof(username)); stmt.BindSmallIntCol(nCol++, &opt); stmt.BindCharCol(nCol++, mark, sizeof(mark)); stmt.BindCharCol(nCol++, station_name, sizeof(station_name)); do { //ZeroMemory(station, 100); //ZeroMemory(station_name, 100); ZeroMemory(mo, 50); ZeroMemory(mo_name, 50); ZeroMemory(mp, 50); ZeroMemory(mp_name, 50); ZeroMemory(name, 100); ZeroMemory(username, 50); ZeroMemory(mark, 200); ZeroMemory(station_name, 100); if (stmt.FetchNext() != 0) break; map_name[name]++; map_mo_name[mo_name]++; map_mp_name[mp_name]++; auto o = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(yy_data, o); yyjson_mut_obj_add_int(doc, o, "module", umodule); yyjson_mut_obj_add_int(doc, o, "opt", opt); yyjson_mut_obj_add_int(doc, o, "analyze_type", analyze_type); yyjson_mut_obj_add_strcpy(doc, o, "mo_name", mo_name); yyjson_mut_obj_add_strcpy(doc, o, "mp_name", mp_name); yyjson_mut_obj_add_int(doc, o, "time", tt); yyjson_mut_obj_add_strcpy(doc, o, "name", ANSItoUTF8(name).c_str()); yyjson_mut_obj_add_strcpy(doc, o, "mark", ANSItoUTF8(mark).c_str()); yyjson_mut_obj_add_strcpy(doc, o, "station_name", ANSItoUTF8(station_name).c_str()); } while (TRUE); } stmt.Close(); } for (auto& it : map_name) yyjson_mut_arr_add_strcpy(doc, yy_name, ANSItoUTF8(it.first).c_str()); for (auto& it : map_mo_name) yyjson_mut_arr_add_strcpy(doc, yy_mo, it.first.c_str()); for (auto& it : map_mp_name) yyjson_mut_arr_add_strcpy(doc, yy_mp, it.first.c_str()); code = 200; yyjson_mut_obj_add_int(doc, root, "code", code); yyjson_mut_obj_add_strcpy(doc, root, "msg", ANSItoUTF8(msg).c_str()); if (json) { *json = yyjson_mut_write(doc, 0, json_len); /* *json = (char*)malloc(1024); strcpy_s(*json, 1024, R"({ "code": 200, "msg": "", "data": [{ "mo_name": "21#", "mp_name": "J1", "name": "张三", "opt":1, "time": 1693808336, "mark": "" }, { "mo_name": "22#", "mp_name": "J1", "name": "李四", "opt":1, "time": 1693808336, "mark": "" }], "name": ["张三", "李四", "王五"], "mo_name": ["21#", "22#", "23#"], "mp_name": ["J1", "J2", "J3"] } )"); */ } yyjson_mut_doc_free(doc); return code; } int ExecSqlForRecord(eRecord_Module module, int dura, uint8_t analyze_type, string& strStation, string& strStationName, const char* mo, const char* mo_name, const char* mp, const char* mp_name, uint64_t start_time, uint64_t end_time, time_t tt, string& strName, const char* username, eRocord_Opt opt, string& mark) { CString sql = fmt::format("INSERT INTO rm_record\ (module, dura, analyze_type, station\ , station_name, mo, mo_name, mp, mp_name, start_time\ , end_time, time, name,username,opt,mark) VALUES\ ({}, {}, {}, '{}', '{}'\ , '{}', '{}', '{}', '{}', {}\ , {}, {}, '{}', '{}',{}, '{}')", int(module), dura, analyze_type, strStation, strStationName, IS_NULL(mo), IS_NULL(mo_name), IS_NULL(mp), IS_NULL(mp_name), start_time, end_time, tt, strName, IS_NULL(username), int(opt), mark).c_str(); if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE) { ASSERT(FALSE); CSimpleLog::Error("语句执行失败:" + sql); return 500; } else { return 200; } } int DealGetrefer_option(const httplib::Request& req, const string token, char** json, size_t* json_len) { string station = req.get_param_value("station"); string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); string str_direct = req.get_param_value("direct"); if (station.length() == 0 || mo.length() == 0 || mp.length() == 0 || str_direct.length() == 0) return 400; auto direct = atoi(str_direct.c_str()); if (direct != 3 && direct != 4) return 400; string str_station = UTF8toANSI(station); if (CSimpleLog::PathFileExistsA(CSimpleLog::GetAppDir() + "refer\\" + fmt::format("{}.{}.{}.{}.json", str_station, mo, mp, direct).c_str())) return 200; else return 404; } //获取参考曲线 int DealGetrefer_curve(const httplib::Request& req, const string token, char** json, size_t* json_len) { string station = req.get_param_value("station"); string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); string str_direct = req.get_param_value("direct"); string str_show_time = req.get_param_value("show_time"); if (station.length() == 0 || mo.length() == 0 || mp.length() == 0 || str_direct.length() == 0 || str_show_time.length() == 0) return 400; auto direct = atoi(str_direct.c_str()); if (direct != 3 && direct != 4) return 400; int yy, mm, dd, hh, MM, SS, micromm; auto ret_num = sscanf_s(str_show_time.c_str(), "%d-%d-%d %d:%d:%d.%d", &yy, &mm, &dd, &hh, &MM, &SS, µmm); if (ret_num != 7) return 400; string str_station = UTF8toANSI(station); time_t show_time = CTime(yy, mm, dd, hh, MM, SS).GetTime() * 1000 + micromm; int code = 400; string msg; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); auto data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "data", data); yyjson_mut_obj_add_strcpy(doc, root, "station", station.c_str()); yyjson_mut_obj_add_strcpy(doc, root, "mo", mo.c_str()); yyjson_mut_obj_add_strcpy(doc, root, "mp", mp.c_str()); yyjson_mut_obj_add_int(doc, root, "direct", direct); code = ReadReferFile(str_station, mo, mp, direct, show_time, doc, data, msg); yyjson_mut_obj_add_int(doc, root, "code", code); yyjson_mut_obj_add_strcpy(doc, root, "msg", ANSItoUTF8(msg).c_str()); if (json) { *json = yyjson_mut_write(doc, 0, json_len); } yyjson_mut_doc_free(doc); return code; } int ReadReferFile(const string& station, const string& mo, const string& mp, uint8_t posi, time_t show_time, yyjson_mut_doc* mut_doc, yyjson_mut_val* arr, OUT string& msg) { string strJsonPath = fmt::format("{}refer\\{}.{}.{}.{}.json", CSimpleLog::GetAppDir(), station, mo, mp, posi); if (CSimpleLog::PathFileExistsA(strJsonPath.c_str()) == false) { msg = fmt::format("未设置参考曲线"); return 404; } yyjson_read_err err; auto doc = yyjson_read_file(strJsonPath.c_str(), strJsonPath.length(), nullptr, &err); if (doc == nullptr) { msg = err.msg; return 500; } auto root = yyjson_doc_get_root(doc); auto t = yyjson_obj_get(root, "t"); auto d0 = yyjson_obj_get(root, "d0"); auto d1 = yyjson_obj_get(root, "d1"); auto d2 = yyjson_obj_get(root, "d2"); if (yyjson_is_arr(t) == false || yyjson_is_arr(d0) == false || yyjson_is_arr(d1) == false || yyjson_is_arr(d2) == false) { msg = "参考曲线存储格式错误"; yyjson_doc_free(doc); doc = nullptr; return 500; } auto obj1 = yyjson_mut_obj(mut_doc); auto obj2 = yyjson_mut_obj(mut_doc); auto obj3 = yyjson_mut_obj(mut_doc); yyjson_mut_arr_add_val(arr, obj1); yyjson_mut_arr_add_val(arr, obj2); yyjson_mut_arr_add_val(arr, obj3); auto data1 = yyjson_mut_arr(mut_doc); auto data2 = yyjson_mut_arr(mut_doc); auto data3 = yyjson_mut_arr(mut_doc); yyjson_mut_obj_add_val(mut_doc, obj1, "data", data1); yyjson_mut_obj_add_val(mut_doc, obj2, "data", data2); yyjson_mut_obj_add_val(mut_doc, obj3, "data", data3); yyjson_mut_obj_add_val(mut_doc, obj1, "name", yyjson_val_mut_copy(mut_doc, yyjson_obj_get(root, "name0"))); yyjson_mut_obj_add_val(mut_doc, obj2, "name", yyjson_val_mut_copy(mut_doc, yyjson_obj_get(root, "name1"))); yyjson_mut_obj_add_val(mut_doc, obj3, "name", yyjson_val_mut_copy(mut_doc, yyjson_obj_get(root, "name2"))); int n = yyjson_arr_size(t); auto it_t = yyjson_arr_get_first(t); auto it_d0 = yyjson_arr_get_first(d0); auto it_d1 = yyjson_arr_get_first(d1); auto it_d2 = yyjson_arr_get_first(d2); for (int i = 0; i < n; i++) { //按双数组的形式添加 auto o1 = yyjson_mut_arr(mut_doc); yyjson_mut_arr_add_val(data1, o1); auto o2 = yyjson_mut_arr(mut_doc); yyjson_mut_arr_add_val(data2, o2); auto o3 = yyjson_mut_arr(mut_doc); yyjson_mut_arr_add_val(data3, o3); auto t = yyjson_get_int(it_t) + show_time; yyjson_mut_arr_add_int(mut_doc, o1, t); yyjson_mut_arr_add_int(mut_doc, o2, t); yyjson_mut_arr_add_int(mut_doc, o3, t); yyjson_mut_arr_add_int(mut_doc, o1, yyjson_get_int(it_d0)); yyjson_mut_arr_add_int(mut_doc, o2, yyjson_get_int(it_d1)); yyjson_mut_arr_add_int(mut_doc, o3, yyjson_get_int(it_d2)); it_t = unsafe_yyjson_get_next(it_t); it_d0 = unsafe_yyjson_get_next(it_d0); it_d1 = unsafe_yyjson_get_next(it_d1); it_d2 = unsafe_yyjson_get_next(it_d2); } yyjson_doc_free(doc); doc = nullptr; msg = "读取成功"; return 200; } int DealGetTemp(const httplib::Request& req, const string token, char** json, size_t* json_len) { string starttime = req.get_param_value("starttime"); string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); string endtime = req.get_param_value("endtime"); if (starttime.length() == 0 || mo.length() == 0 || endtime.length() == 0 || mp.length() == 0) return 400; int code = 500; static auto sz_temp = ANSItoUTF8("温度"); static auto sz_unit = ANSItoUTF8("℃"); auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); yyjson_mut_obj_add_str(doc, root, "name", sz_temp.c_str()); yyjson_mut_obj_add_str(doc, root, "ValueSuffix", sz_unit.c_str()); auto data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "data", data); do { int year, month, day; auto num = sscanf_s(starttime.c_str(), "%d-%d-%d ", &year, &month, &day); if (num != 3) break; CString sql = fmt::format(" SELECT TOP 100000 [acquisitiontime],[temperature] FROM rm_temphumidity_{}{:0>2} \ WHERE mo='{}' AND mp='{}' AND acquisitiontime >= '{}' AND acquisitiontime < '{}' ORDER BY acquisitiontime ", year, month, mo, mp, starttime, endtime).c_str(); COdbcStatement stmt; if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) break; TIMESTAMP_STRUCT ts; int temp; stmt.BindTimeStampCol(1, &ts); stmt.BindIntCol(2, &temp); while (true) { if (stmt.FetchNext() != 0) break; auto arr = yyjson_mut_arr(doc); yyjson_mut_arr_add_val(data, arr); yyjson_mut_arr_add_uint(doc, arr, CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).GetTime() * 1000 + ts.fraction); yyjson_mut_arr_add_strcpy(doc, arr, fmt::format("{:.1f}", temp / 100.0).c_str()); } code = 200; } while (false); *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return code; } int DealGetTempHumi(const httplib::Request& req, const string token, char** json, size_t* json_len) { string starttime = req.get_param_value("starttime"); string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); string endtime = req.get_param_value("endtime"); if (starttime.length() == 0 || mo.length() == 0 || endtime.length() == 0 || mp.length() == 0) return 400; int code = 500; static auto sz_temp = ANSItoUTF8("温度"); static auto sz_humi = ANSItoUTF8("湿度"); static auto sz_unit = ANSItoUTF8("℃"); auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); yyjson_mut_obj_add_int(doc, root, "total", 2); auto rows = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "rows", rows); auto temp_curve = yyjson_mut_obj(doc); auto humi_curve = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(rows, temp_curve); yyjson_mut_arr_add_val(rows, humi_curve); yyjson_mut_obj_add_int(doc, temp_curve, "yAxis", 0); yyjson_mut_obj_add_int(doc, humi_curve, "yAxis", 1); yyjson_mut_obj_add_str(doc, temp_curve, "name", sz_temp.c_str()); yyjson_mut_obj_add_str(doc, humi_curve, "name", sz_humi.c_str()); yyjson_mut_obj_add_str(doc, temp_curve, "ValueSuffix", sz_unit.c_str()); yyjson_mut_obj_add_str(doc, humi_curve, "ValueSuffix", "%"); auto temp_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, temp_curve, "data", temp_data); auto humi_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, humi_curve, "data", humi_data); do { int year, month, day; auto num = sscanf_s(starttime.c_str(), "%d-%d-%d ", &year, &month, &day); if (num != 3) break; CString sql = fmt::format(" SELECT TOP 100000 [acquisitiontime],[temperature],[humidity] FROM rm_temphumidity_{}{:0>2} \ WHERE mo='{}' AND mp='{}' AND acquisitiontime >= '{}' AND acquisitiontime < '{}' ORDER BY acquisitiontime ", year, month, mo, mp, starttime, endtime).c_str(); COdbcStatement stmt; if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) break; TIMESTAMP_STRUCT ts; int temp, humi; stmt.BindTimeStampCol(1, &ts); stmt.BindIntCol(2, &temp); stmt.BindIntCol(3, &humi); while (true) { if (stmt.FetchNext() != 0) break; uint64_t t = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).GetTime() * 1000 + ts.fraction; { auto arr = yyjson_mut_arr(doc); yyjson_mut_arr_add_val(temp_data, arr); yyjson_mut_arr_add_uint(doc, arr, t); yyjson_mut_arr_add_strcpy(doc, arr, fmt::format("{:.1f}", temp / 100.0).c_str()); } { auto arr = yyjson_mut_arr(doc); yyjson_mut_arr_add_val(humi_data, arr); yyjson_mut_arr_add_uint(doc, arr, t); yyjson_mut_arr_add_int(doc, arr, humi / 100); } } code = 200; } while (false); *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return code; } //设置参考曲线 int DealPostrefer_curve(const char* body_ptr, const size_t body_len, const string token, char** json, size_t* json_len) { return 403; //暂时屏蔽 /* int code = 400; string msg; auto req_doc = yyjson_read(body_ptr, body_len, 0); if (req_doc == nullptr) return 400; auto req_root = yyjson_doc_get_root(req_doc); auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); do { auto station = yyjson_get_str(yyjson_obj_get(req_root, "station")); auto mo = yyjson_get_str(yyjson_obj_get(req_root, "mo")); auto mp = yyjson_get_str(yyjson_obj_get(req_root, "mp")); auto mo_name = yyjson_get_str(yyjson_obj_get(req_root, "mo_name")); auto mp_name = yyjson_get_str(yyjson_obj_get(req_root, "mp_name")); auto direct = yyjson_get_int(yyjson_obj_get(req_root, "direct")); auto show_time = yyjson_get_str(yyjson_obj_get(req_root, "show_time")); auto full_name = yyjson_get_str(yyjson_obj_get(req_root, "full_name")); if (station == nullptr || show_time == 0 || mo == nullptr || mp == nullptr || (direct != 3 && direct != 4) || full_name == nullptr || mo_name == nullptr || mp_name == nullptr) { msg = "参数错误"; break; } auto strStation = UTF8toANSI(station); auto strName = UTF8toANSI(full_name); time_t tt; time(&tt); auto len = strlen(show_time); if (len == 0) //取消参考曲线 { auto strPath = CSimpleLog::GetAppDir() + "refer\\" + fmt::format("{}.{}.{}.{}.json", strStation, mo, mp, direct).c_str(); if (CSimpleLog::PathFileExistsA(strPath)) DeleteFile(strPath); ExecSqlForRecord(eRecord_Module::RM_REFER, 0, 0, strStation, strStation, mo, mo_name, mp, mp_name, 0, 0, tt, strName, "", eRocord_Opt::RO_CONFIG, fmt::format("用户[{}]取消了[{}.{}][{}]的参考曲线", strName, mo_name, mp_name, direct == 3 ? "定扳反" : "反扳定")); msg = "操作成功."; code = 200; break; } int yy, mm, dd, hh, MM, SS, micromm; auto ret_num = sscanf_s(show_time, "%d-%d-%d %d:%d:%d.%d", &yy, &mm, &dd, &hh, &MM, &SS, µmm); if (ret_num != 7) { msg = "show_time参数错误"; break; } CString sql = fmt::format("SELECT start_time,idx FROM rm_move_{:0>4}{:0>2} WHERE mo = '{}' and mp = '{}' and show_time = '{}'", yy, mm, mo, mp, show_time).c_str(); COdbcStatement stmt; if (CDBConnectPool::Instance()->DBQuery(stmt, sql) == FALSE) { ASSERT(FALSE); msg = "语句执行失败:" + sql; code = 500; break; } TIMESTAMP_STRUCT ts = { 0 }; uint8_t idx = -1; stmt.BindTimeStampCol(1, &ts); stmt.BindTinyIntCol(2, &idx); if (stmt.FetchNext() != 0) { msg = "查询转换记录失败"; code = 500; break; } stmt.Close(); string imei; int idx_2; if (CMonitorObjectMng::Instance()->MOMP2IMEI(string(mo), string(mp), imei, idx_2) == FALSE) { msg = "查询imei失败"; code = 500; break; } ASSERT(idx == idx_2); CTime ctStart(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); CTime ctEnd = ctStart + CTimeSpan(0, 0, 0, 30); sql = fmt::format(" SELECT acquisitiontime,data0,data1,data2 FROM rm_resistance_{:0>4}{:0>2}{:0>2}\ WHERE IMEI = '{}' AND acquisitiontime between '{}' and '{}' AND idx = {}", ts.year, ts.month, ts.day, imei, ctStart.Format("%Y-%m-%d %H:%M:%S"), ctEnd.Format("%Y-%m-%d %H:%M:%S"),idx ).c_str(); int sdata0, sdata1, sdata2; if (CDBConnectPool::Instance()->DBQuery(stmt, sql) == FALSE) { ASSERT(FALSE); msg = "语句执行失败:" + sql; code = 500; break; } int nCol = 1; stmt.BindTimeStampCol(nCol++, &ts); stmt.BindIntCol(nCol++, &sdata0); stmt.BindIntCol(nCol++, &sdata1); stmt.BindIntCol(nCol++, &sdata2); int no = 0; std::map data0, data1, data2; //time_t tmStart = ctStart.GetTime() * 1000; //存储数据时, 以起始时间为0 time_t tmShowTime = CTime(yy, mm, dd, hh, MM, SS).GetTime() * 1000 + micromm; do { if (stmt.FetchNext() != 0) break; no++; CTime ctTime; try { ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); } catch (...) { continue; } time_t tm = ctTime.GetTime() * 1000 + ts.fraction / 1000000 - tmShowTime; data0[tm] = sdata0; data1[tm] = sdata1; data2[tm] = sdata2; } while (true); stmt.Close(); if (no == 0) { msg = "数据为空"; code = 500; break; } //存储数据 code = WriteReferFile(strStation, mo, mp, direct, data0, data1, data2, msg); ExecSqlForRecord(eRecord_Module::RM_REFER, 0, 0, strStation, strStation, mo, mo_name, mp, mp_name, 0, 0, tt, strName, "", eRocord_Opt::RO_CONFIG, fmt::format("用户[{}]设置了[{}.{}][{}]的参考曲线", strName, mo_name, mp_name, direct == 3 ? "定扳反" : "反扳定")); } while (false); yyjson_mut_obj_add_strcpy(doc, root, "msg", ANSItoUTF8(msg).c_str()); yyjson_mut_obj_add_int(doc, root, "code", code); if (json) *json = yyjson_mut_write(doc, 0, json_len); yyjson_doc_free(req_doc); yyjson_mut_doc_free(doc); return code;*/ } int WriteReferFile(const string& station, const string& mo, const string& mp, uint8_t posi, const std::map& data0, const std::map& data1, const std::map& data2, OUT string& msg) { string strJsonPath = fmt::format("{}refer\\{}.{}.{}.{}.json", CSimpleLog::GetAppDir(), station, mo, mp, posi); string name1, name2, name3, out_name, in_name; CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3, out_name, in_name); auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); yyjson_mut_obj_add_strcpy(doc, root, "station", ANSItoUTF8(station).c_str()); yyjson_mut_obj_add_strcpy(doc, root, "mo", mo.c_str()); yyjson_mut_obj_add_strcpy(doc, root, "mp", mp.c_str()); yyjson_mut_obj_add_int(doc, root, "posi", posi); auto t = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "t", t); auto d0 = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "d0", d0); auto d1 = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "d1", d1); auto d2 = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "d2", d2); if (name1.find("定位") != -1) { yyjson_mut_obj_add_strcpy(doc, root, "name0", ANSItoUTF8("定位参考曲线").c_str()); yyjson_mut_obj_add_strcpy(doc, root, "name1", ANSItoUTF8("反位参考曲线").c_str()); yyjson_mut_obj_add_strcpy(doc, root, "name2", ANSItoUTF8("转换参考曲线").c_str()); } else { yyjson_mut_obj_add_strcpy(doc, root, "name0", ANSItoUTF8("反位参考曲线").c_str()); yyjson_mut_obj_add_strcpy(doc, root, "name1", ANSItoUTF8("定位参考曲线").c_str()); yyjson_mut_obj_add_strcpy(doc, root, "name2", ANSItoUTF8("转换参考曲线").c_str()); } for (auto& it : data0) { yyjson_mut_arr_add_int(doc, t, it.first); yyjson_mut_arr_add_int(doc, d0, it.second); } for (auto& it : data1) yyjson_mut_arr_add_int(doc, d1, it.second); for (auto& it : data2) yyjson_mut_arr_add_int(doc, d2, it.second); yyjson_write_err err; auto ret = yyjson_mut_write_file(strJsonPath.c_str(), doc, 0, nullptr, &err); if (ret == false) { msg = fmt::format("写入文件错误:{} 错误码:{}", strJsonPath, err.msg); CSimpleLog::Error(msg.c_str()); return 500; } msg = "处理成功."; return 200; } int DealGetResistCurve(const httplib::Request& req, const string token, char** json, size_t* json_len) { string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); string time = req.get_param_value("time"); string name = req.get_param_value("name"); if (mo.length() == 0 || mp.length() == 0 || time.length() == 0 || name.length() == 0) return 400; time_t uTime = atoll(time.c_str()); COleDateTime odt(uTime); if (odt.GetStatus() == COleDateTime::invalid) return 400; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); auto convert_resist = yyjson_mut_obj(doc); yyjson_mut_obj_add_val(doc, root, "convert_resist", convert_resist);//转换阻力 auto suobi_lock = yyjson_mut_obj(doc); yyjson_mut_obj_add_val(doc, root, "suobi_lock", suobi_lock);//锁闭力数据 yyjson_mut_obj_add_str(doc, root, "name", name.c_str()); yyjson_mut_obj_add_str(doc, root, "unit", "N"); yyjson_mut_obj_add_uint(doc, root, "time", uTime); //title yyjson_mut_obj_add_strcpy(doc, convert_resist, "title", ANSItoUTF8("转换阻力峰值统计图").c_str()); yyjson_mut_obj_add_strcpy(doc, suobi_lock, "title", ANSItoUTF8("定反位锁闭力值统计图").c_str()); //curve_data and line auto convert_curve_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, convert_resist, "curve_data", convert_curve_data); auto suobi_curve_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, suobi_lock, "curve_data", suobi_curve_data); auto convert_line = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, convert_resist, "line", convert_line); auto suobi_line = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, suobi_lock, "line", suobi_line); //curve_obj auto d_convert_obj = yyjson_mut_obj(doc); yyjson_mut_obj_add_strcpy(doc, d_convert_obj, "name", ANSItoUTF8("定扳反转换阻力峰值").c_str()); yyjson_mut_obj_add_str(doc, d_convert_obj, "color", "#a8ff78");// 苹果绿 yyjson_mut_arr_add_val(convert_curve_data, d_convert_obj); auto f_convert_obj = yyjson_mut_obj(doc); yyjson_mut_obj_add_strcpy(doc, f_convert_obj, "name", ANSItoUTF8("反扳定转换阻力峰值").c_str()); yyjson_mut_obj_add_str(doc, f_convert_obj, "color", "#FFE000");// 芒果黄 yyjson_mut_arr_add_val(convert_curve_data, f_convert_obj); auto d_suobi_obj = yyjson_mut_obj(doc); yyjson_mut_obj_add_strcpy(doc, d_suobi_obj, "name", ANSItoUTF8("定位锁闭力值").c_str()); yyjson_mut_obj_add_str(doc, d_suobi_obj, "color", "#a8ff78");// 苹果绿 yyjson_mut_arr_add_val(suobi_curve_data, d_suobi_obj); auto f_suobi_obj = yyjson_mut_obj(doc); yyjson_mut_obj_add_strcpy(doc, f_suobi_obj, "name", ANSItoUTF8("反位锁闭力值").c_str()); yyjson_mut_obj_add_str(doc, f_suobi_obj, "color", "#FFE000");// 芒果黄 yyjson_mut_arr_add_val(suobi_curve_data, f_suobi_obj); //curve_data auto d_convert_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, d_convert_obj, "data", d_convert_data); auto f_convert_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, f_convert_obj, "data", f_convert_data); auto d_suobi_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, d_suobi_obj, "data", d_suobi_data); auto f_suobi_data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, f_suobi_obj, "data", f_suobi_data); //query //查询数据库 填充 d_convert_data f_convert_data d_suobi_data f_suobi_data { string table_name = fmt::format("rm_move_{:0>4}{:0>2}", odt.GetYear(), odt.GetMonth()); CString sql = fmt::format("SELECT show_time,show_val,posi FROM {} WHERE mo = '{}' AND mp = '{}' AND show_time >= '{}' AND show_time <= '{}'", table_name, mo, mp, odt.Format("%Y-%m-%d 00:00:00"), odt.Format("%Y-%m-%d 23:59:59.999")).c_str(); COdbcStatement stmt; if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) { int nCol = 1; TIMESTAMP_STRUCT ts; int show_val; eDaoChaPosi posi; stmt.BindTimeStampCol(nCol++, &ts); stmt.BindIntCol(nCol++, &show_val); stmt.BindTinyIntCol(nCol++, (BYTE*)&posi); do { if (stmt.FetchNext() != 0) break; CTime ctTime; try { ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); } catch (...) { continue; } time_t tt = ctTime.GetTime() * 1000 + ts.fraction / 1000000; auto o = yyjson_mut_arr(doc); yyjson_mut_arr_add_int(doc, o, tt); yyjson_mut_arr_add_int(doc, o, show_val); switch (posi) { case eDaoChaPosi::DCP_FIX: yyjson_mut_arr_add_val(d_suobi_data, o); break; case eDaoChaPosi::DCP_INVERT: yyjson_mut_arr_add_val(f_suobi_data, o); break; case eDaoChaPosi::DCP_FIX2INVERT: yyjson_mut_arr_add_val(d_convert_data, o); break; case eDaoChaPosi::DCP_INVERT2FIX: yyjson_mut_arr_add_val(f_convert_data, o); break; default: ASSERT(FALSE); break; } } while (TRUE); } stmt.Close(); } //line { //转换阻力报警线 CONVERT_RESIST_OVER_LIMIT* pInfo = (CONVERT_RESIST_OVER_LIMIT*)CResistAlarmMng::Instance()->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT); if (pInfo && pInfo->enable) { if (pInfo->dw_warn_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(convert_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->dw_warn_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->up_warn_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(convert_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->up_warn_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->dw_alarm_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(convert_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->dw_alarm_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } if (pInfo->up_alarm_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(convert_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->up_alarm_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } } } { //锁闭力报警线 SUOBI_OVER_LIMIT_INFO* pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, -1, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); if (pInfo && pInfo->enable) { if (pInfo->warn_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->warn_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->warn_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->warn_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->alarm_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->alarm_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } if (pInfo->alarm_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->alarm_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } } } { //定位锁闭力报警线 SUOBI_OVER_LIMIT_INFO* pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_FIX, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); if (pInfo && pInfo->enable) { if (pInfo->warn_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("定位预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->warn_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->warn_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("定位预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->warn_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->alarm_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("定位告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->alarm_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } if (pInfo->alarm_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("定位告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->alarm_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } } } { //反位锁闭力报警线 SUOBI_OVER_LIMIT_INFO* pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_INVERT, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); if (pInfo && pInfo->enable) { if (pInfo->warn_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("反位预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->warn_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->warn_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("反位预警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->warn_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); } if (pInfo->alarm_low_limit > INT_MIN) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("反位告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->alarm_low_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } if (pInfo->alarm_high_limit < INT_MAX) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(suobi_line, obj); yyjson_mut_obj_add_strcpy(doc, obj, "name", ANSItoUTF8("反位告警线").c_str()); yyjson_mut_obj_add_int(doc, obj, "value", pInfo->alarm_high_limit); yyjson_mut_obj_add_str(doc, obj, "color", "Red"); } } } *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return 200; } int DealGetResistReport(const httplib::Request& req, const string token, char** json, size_t* json_len) { string station = req.get_param_value("station"); string time = req.get_param_value("time"); if (time.length() == 0 || station.length() == 0) return 400; time_t uTime = atoll(time.c_str()); COleDateTime odt(uTime); if (odt.GetStatus() == COleDateTime::invalid) return 400; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_arr(doc); yyjson_mut_doc_set_root(doc, root); { string strStation = UTF8toANSI(station); string table_name = fmt::format("rm_move_{:0>4}{:0>2}", odt.GetYear(), odt.GetMonth()); CString sql = fmt::format("SELECT A.*,B.name FROM (SELECT mo, mp, posi, count(*) as num, max(show_val) as l, min(show_val) as s, sum(show_val) as sum FROM {} WHERE mo in (SELECT id FROM rm_mo WHERE up = '{}') AND show_time >= '{}' AND show_time <= '{}' GROUP BY mo, mp, posi) AS A \ LEFT JOIN rm_mo AS B ON A.mo = B.id", table_name, strStation, odt.Format("%Y-%m-%d 00:00:00"), odt.Format("%Y-%m-%d 23:59:59.999")).c_str(); //SELECT A.*,B.name FROM (SELECT mo, mp, posi, count(*) as num, max(show_val) as l, min(show_val) as s FROM rm_move_202301 WHERE mo in (SELECT id FROM rm_mo WHERE up = '温州南站') AND show_time >= '2023-01-10 00:00:00' AND show_time <= '2023-01-10 23:59:59.999' GROUP BY mo, mp, posi) AS A LEFT JOIN rm_mo AS B ON A.mo = B.id COdbcStatement stmt; struct tagData { string mo_name; string mo; string mp; int d_convert_resist = INT_MAX; int d_convert_resist_max = INT_MAX; int d_convert_resist_min = INT_MAX; int d_convert_resist_avg = INT_MAX; uint8_t d_convert_resist_max_color = 0; //0 为正常 1 为预警 2 为报警 uint8_t d_convert_resist_min_color = 0; uint8_t d_convert_resist_avg_color = 0; int f_convert_resist = INT_MAX; int f_convert_resist_max = INT_MAX; int f_convert_resist_min = INT_MAX; int f_convert_resist_avg = INT_MAX; uint8_t f_convert_resist_max_color = 0; uint8_t f_convert_resist_min_color = 0; uint8_t f_convert_resist_avg_color = 0; int d_suobi_lock = INT_MAX; int d_suobi_lock_max = INT_MAX; int d_suobi_lock_min = INT_MAX; int d_suobi_lock_avg = INT_MAX; uint8_t d_suobi_lock_max_color = 0; uint8_t d_suobi_lock_min_color = 0; uint8_t d_suobi_lock_avg_color = 0; int f_suobi_lock = INT_MAX; int f_suobi_lock_max = INT_MAX; int f_suobi_lock_min = INT_MAX; int f_suobi_lock_avg = INT_MAX; uint8_t f_suobi_lock_max_color = 0; uint8_t f_suobi_lock_min_color = 0; uint8_t f_suobi_lock_avg_color = 0; }; std::map mapData; if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) { int nCol = 1; char mo[50], mp[50], mo_name[50]; int num; int max; int min; int64_t sum; eDaoChaPosi posi; stmt.BindCharCol(nCol++, mo, sizeof(mo)); stmt.BindCharCol(nCol++, mp, sizeof(mp)); stmt.BindTinyIntCol(nCol++, (BYTE*)&posi); stmt.BindIntCol(nCol++, &num); stmt.BindIntCol(nCol++, &max); stmt.BindIntCol(nCol++, &min); stmt.BindBigIntCol(nCol++, &sum); stmt.BindCharCol(nCol++, mo_name, sizeof(mo_name)); do { if (stmt.FetchNext() != 0) break; SUOBI_OVER_LIMIT_INFO* pInfo = nullptr; CONVERT_RESIST_OVER_LIMIT* pInfo2 = nullptr; auto& it = mapData[fmt::format("{}.{}", mo, mp)]; it.mo_name = mo_name; it.mo = mo; it.mp = mp; eSuoBiPosi idx; switch (posi) { case eDaoChaPosi::DCP_FIX: it.d_suobi_lock = num; it.d_suobi_lock_max = max; it.d_suobi_lock_min = min; if (num) it.d_suobi_lock_avg = sum / num; idx = eSuoBiPosi::SB_FIX; pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, (uint8_t)idx, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); if (pInfo && pInfo->enable) { if (max > pInfo->alarm_high_limit) it.d_suobi_lock_max_color = UINT8_ALARM; else if (max > pInfo->warn_high_limit) it.d_suobi_lock_max_color = UINT8_WARN; if (min < pInfo->alarm_low_limit) it.d_suobi_lock_min_color = UINT8_ALARM; else if (min < pInfo->warn_low_limit) it.d_suobi_lock_min_color = UINT8_WARN; if (num && (it.d_suobi_lock_avg > pInfo->alarm_high_limit || it.d_suobi_lock_avg < pInfo->alarm_low_limit)) it.d_suobi_lock_avg_color = UINT8_ALARM; else if (num && (it.d_suobi_lock_avg > pInfo->warn_high_limit || it.d_suobi_lock_avg < pInfo->warn_low_limit)) it.d_suobi_lock_avg_color = UINT8_WARN; } break; case eDaoChaPosi::DCP_INVERT: it.f_suobi_lock = num; it.f_suobi_lock_max = max; it.f_suobi_lock_min = min; if (num) it.f_suobi_lock_avg = sum / num; idx = eSuoBiPosi::SB_INVERT; pInfo = (SUOBI_OVER_LIMIT_INFO*)CResistAlarmMng::Instance()->Find(mo, mp, (uint8_t)idx, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT); if (pInfo && pInfo->enable) { if (max > pInfo->alarm_high_limit) it.f_suobi_lock_max_color = UINT8_ALARM; else if (max > pInfo->warn_high_limit) it.f_suobi_lock_max_color = UINT8_WARN; if (min < pInfo->alarm_low_limit) it.f_suobi_lock_min_color = UINT8_ALARM; else if (min < pInfo->warn_low_limit) it.f_suobi_lock_min_color = UINT8_WARN; if (num && (it.f_suobi_lock_avg > pInfo->alarm_high_limit || it.f_suobi_lock_avg < pInfo->alarm_low_limit)) it.f_suobi_lock_avg_color = UINT8_ALARM; else if (num && (it.f_suobi_lock_avg > pInfo->warn_high_limit || it.f_suobi_lock_avg < pInfo->warn_low_limit)) it.f_suobi_lock_avg_color = UINT8_WARN; } break; case eDaoChaPosi::DCP_FIX2INVERT: it.d_convert_resist = num; it.d_convert_resist_max = max; it.d_convert_resist_min = min; if (num) it.d_convert_resist_avg = sum / num; pInfo2 = (CONVERT_RESIST_OVER_LIMIT*)CResistAlarmMng::Instance()->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT); if (pInfo2 && pInfo2->enable) { string name1, name2, name3, out_name, in_name; CMonitorObjectMng::Instance()->GetNameByMoMp(fmt::format("{}.{}", mo, mp), name1, name2, name3, out_name, in_name); if (in_name.find("定扳反") == -1) //如果缩进不是定扳反, 那就是伸出 { if (max > pInfo2->dw_alarm_low_limit) it.d_convert_resist_max_color = UINT8_ALARM; else if (max > pInfo2->dw_warn_low_limit) it.d_convert_resist_max_color = UINT8_WARN; if (num && it.d_convert_resist_avg > pInfo2->dw_alarm_low_limit) it.d_convert_resist_avg_color = UINT8_ALARM; else if (num && it.d_convert_resist_avg > pInfo2->dw_warn_low_limit) it.d_convert_resist_avg_color = UINT8_WARN; } else { if (max > pInfo2->up_alarm_high_limit) it.d_convert_resist_max_color = UINT8_ALARM; else if (max > pInfo2->up_warn_high_limit) it.d_convert_resist_max_color = UINT8_WARN; if (num && it.d_convert_resist_avg > pInfo2->up_alarm_high_limit) it.d_convert_resist_avg_color = UINT8_ALARM; else if (num && it.d_convert_resist_avg > pInfo2->up_warn_high_limit) it.d_convert_resist_avg_color = UINT8_WARN; } } break; case eDaoChaPosi::DCP_INVERT2FIX: it.f_convert_resist = num; it.f_convert_resist_max = max; it.f_convert_resist_min = min; if (num) it.f_convert_resist_avg = sum / num; pInfo2 = (CONVERT_RESIST_OVER_LIMIT*)CResistAlarmMng::Instance()->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT); if (pInfo2 && pInfo2->enable) { string name1, name2, name3, out_name, in_name; CMonitorObjectMng::Instance()->GetNameByMoMp(fmt::format("{}.{}", mo, mp), name1, name2, name3, out_name, in_name); if (in_name.find("反扳定") != -1) //如果反扳定是缩进 { if (max > pInfo2->up_alarm_high_limit) it.f_convert_resist_max_color = UINT8_ALARM; else if (max > pInfo2->up_warn_high_limit) it.f_convert_resist_max_color = UINT8_WARN; if (num && it.f_convert_resist_avg > pInfo2->up_alarm_high_limit) it.f_convert_resist_avg_color = UINT8_ALARM; else if (num && it.f_convert_resist_avg > pInfo2->up_warn_high_limit) it.f_convert_resist_avg_color = UINT8_WARN; } else { if (max > pInfo2->dw_alarm_low_limit) it.f_convert_resist_max_color = UINT8_ALARM; else if (max > pInfo2->dw_warn_low_limit) it.f_convert_resist_max_color = UINT8_WARN; if (num && it.f_convert_resist_avg > pInfo2->dw_alarm_low_limit) it.f_convert_resist_avg_color = UINT8_ALARM; else if (num && it.f_convert_resist_avg > pInfo2->dw_warn_low_limit) it.f_convert_resist_avg_color = UINT8_WARN; } } break; default: ASSERT(FALSE); break; } } while (TRUE); } stmt.Close(); for (auto& it : mapData) { auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(root, obj); yyjson_mut_obj_add_strcpy(doc, obj, "mo_name", ANSItoUTF8(it.second.mo_name).c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "mo", it.second.mo.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "mp", it.second.mp.c_str()); if (it.second.d_convert_resist != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_convert_resist", it.second.d_convert_resist); else yyjson_mut_obj_add_str(doc, obj, "d_convert_resist", "-"); if (it.second.d_convert_resist_max != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_convert_resist_max", it.second.d_convert_resist_max); else yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_max", "-"); if (it.second.d_convert_resist_min != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_convert_resist_min", it.second.d_convert_resist_min); else yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_min", "-"); if (it.second.d_convert_resist_avg != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_convert_resist_avg", it.second.d_convert_resist_avg); else yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_avg", "-"); //颜色 if (it.second.d_convert_resist_max_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_max_color", "red"); else if (it.second.d_convert_resist_max_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_max_color", "orange"); if (it.second.d_convert_resist_min_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_min_color", "red"); else if (it.second.d_convert_resist_min_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_min_color", "orange"); if (it.second.d_convert_resist_avg_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_avg_color", "red"); else if (it.second.d_convert_resist_avg_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "d_convert_resist_avg_color", "orange"); if (it.second.f_convert_resist != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_convert_resist", it.second.f_convert_resist); else yyjson_mut_obj_add_str(doc, obj, "f_convert_resist", "-"); if (it.second.f_convert_resist_max != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_convert_resist_max", it.second.f_convert_resist_max); else yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_max", "-"); if (it.second.f_convert_resist_min != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_convert_resist_min", it.second.f_convert_resist_min); else yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_min", "-"); if (it.second.f_convert_resist_avg != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_convert_resist_avg", it.second.f_convert_resist_avg); else yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_avg", "-"); //颜色 if (it.second.f_convert_resist_max_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_max_color", "red"); else if (it.second.f_convert_resist_max_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_max_color", "orange"); if (it.second.f_convert_resist_min_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_min_color", "red"); else if (it.second.f_convert_resist_min_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_min_color", "orange"); if (it.second.f_convert_resist_avg_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_avg_color", "red"); else if (it.second.f_convert_resist_avg_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "f_convert_resist_avg_color", "orange"); if (it.second.d_suobi_lock != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_suobi_lock", it.second.d_suobi_lock); else yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock", "-"); if (it.second.d_suobi_lock_max != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_suobi_lock_max", it.second.d_suobi_lock_max); else yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_max", "-"); if (it.second.d_suobi_lock_min != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_suobi_lock_min", it.second.d_suobi_lock_min); else yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_min", "-"); if (it.second.d_suobi_lock_avg != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "d_suobi_lock_avg", it.second.d_suobi_lock_avg); else yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_avg", "-"); //颜色 if (it.second.d_suobi_lock_max_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_max_color", "red"); else if (it.second.d_suobi_lock_max_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_max_color", "orange"); if (it.second.d_suobi_lock_min_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_min_color", "red"); else if (it.second.d_suobi_lock_min_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_min_color", "orange"); if (it.second.d_suobi_lock_avg_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_avg_color", "red"); else if (it.second.d_suobi_lock_avg_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "d_suobi_lock_avg_color", "orange"); if (it.second.f_suobi_lock != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_suobi_lock", it.second.f_suobi_lock); else yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock", "-"); if (it.second.f_suobi_lock_max != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_suobi_lock_max", it.second.f_suobi_lock_max); else yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_max", "-"); if (it.second.f_suobi_lock_min!= INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_suobi_lock_min", it.second.f_suobi_lock_min); else yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_min", "-"); if (it.second.f_suobi_lock_avg != INT_MAX) yyjson_mut_obj_add_int(doc, obj, "f_suobi_lock_avg", it.second.f_suobi_lock_avg); else yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_avg", "-"); //颜色 if (it.second.f_suobi_lock_max_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_max_color", "red"); else if (it.second.f_suobi_lock_max_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_max_color", "orange"); if (it.second.f_suobi_lock_min_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_min_color", "red"); else if (it.second.f_suobi_lock_min_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_min_color", "orange"); if (it.second.f_suobi_lock_avg_color == UINT8_ALARM) yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_avg_color", "red"); else if (it.second.f_suobi_lock_avg_color == UINT8_WARN) yyjson_mut_obj_add_str(doc, obj, "f_suobi_lock_avg_color", "orange"); } } *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return 200; } int DealGetMoveInfo(const httplib::Request& req, const string token, char** json, size_t* json_len) { string mo = req.get_param_value("mo"); string mp = req.get_param_value("mp"); string start_time = req.get_param_value("start_time"); string end_time = req.get_param_value("end_time"); if (mo.length() == 0 || mp.length() == 0 || start_time.length() == 0 || end_time.length() == 0) return 400; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); auto data = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, root, "data", data); COleDateTime odtStart, odtEnd; do { auto ret_time = odtEnd.ParseDateTime(end_time.c_str()); if (ret_time == FALSE) break; ret_time = odtStart.ParseDateTime(start_time.c_str()); if (ret_time == FALSE) break; if (odtEnd <= odtStart) break; auto fix_object = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(data, fix_object); auto invert_object = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(data, invert_object); auto convert_object = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(data, convert_object); static string fix_name = ANSItoUTF8("定位测力曲线"); yyjson_mut_obj_add_str(doc, fix_object, "name", fix_name.c_str()); static string invert_name = ANSItoUTF8("反位测力曲线"); yyjson_mut_obj_add_str(doc, invert_object, "name", invert_name.c_str()); static string convert_name = ANSItoUTF8("转换阻力曲线"); yyjson_mut_obj_add_str(doc, convert_object, "name", convert_name.c_str()); static string lock_low = ANSItoUTF8("锁闭力过低"); auto fix_points = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, fix_object, "mark_points", fix_points); auto invert_points = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, invert_object, "mark_points", invert_points); auto convert_points = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, convert_object, "mark_points", convert_points); string table_name = fmt::format("rm_move_{:0>4}{:0>2}", odtStart.GetYear(), odtStart.GetMonth()); CString sql = fmt::format("SELECT show_time, curr_val, show_val, posi, mark FROM {} \ WHERE mo = '{}' AND mp = '{}' AND show_time >= '{}' AND show_time <= '{}' order by posi, show_time", table_name, mo, mp, start_time, end_time).c_str(); COdbcStatement stmt; if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) { TIMESTAMP_STRUCT ts; int curr_val; int show_val; eDaoChaPosi posi; char mark[200]; int nCol = 1; stmt.BindTimeStampCol(nCol++, &ts); stmt.BindIntCol(nCol++, &curr_val); stmt.BindIntCol(nCol++, &show_val); stmt.BindTinyIntCol(nCol++, (BYTE*)&posi); stmt.BindCharCol(nCol++, mark, sizeof(mark)); do { if (stmt.FetchNext() != 0) break; auto obj = yyjson_mut_obj(doc); switch (posi) { case eDaoChaPosi::DCP_FIX: { yyjson_mut_arr_add_val(fix_points, obj); auto coord = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, obj, "coord", coord); yyjson_mut_arr_add_int(doc, coord, CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).GetTime() * 1000 + ts.fraction / 1000000); yyjson_mut_arr_add_int(doc, coord, curr_val); yyjson_mut_obj_add_strcpy(doc, obj, "time", fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}.{:0>3}", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fraction / 1000000).c_str()); yyjson_mut_obj_add_int(doc, obj, "val", show_val); if (show_val < 500) { yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); yyjson_mut_obj_add_str(doc, obj, "label", lock_low.c_str()); } else { yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("锁闭力:{}", show_val)).c_str()); } } break; case eDaoChaPosi::DCP_INVERT: { yyjson_mut_arr_add_val(invert_points, obj); auto coord = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, obj, "coord", coord); yyjson_mut_arr_add_int(doc, coord, CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).GetTime() * 1000 + ts.fraction / 1000000); yyjson_mut_arr_add_int(doc, coord, curr_val); yyjson_mut_obj_add_strcpy(doc, obj, "time", fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}.{:0>3}", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fraction / 1000000).c_str()); yyjson_mut_obj_add_int(doc, obj, "val", show_val); if (show_val < 500) { yyjson_mut_obj_add_str(doc, obj, "color", "Orange"); yyjson_mut_obj_add_str(doc, obj, "label", lock_low.c_str()); } else { yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("锁闭力:{}", show_val)).c_str()); } } break; case eDaoChaPosi::DCP_FIX2INVERT: { yyjson_mut_arr_add_val(convert_points, obj); auto coord = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, obj, "coord", coord); yyjson_mut_arr_add_int(doc, coord, CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).GetTime() * 1000 + ts.fraction / 1000000); yyjson_mut_arr_add_int(doc, coord, curr_val); yyjson_mut_obj_add_strcpy(doc, obj, "time", fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}.{:0>3}", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fraction / 1000000).c_str()); yyjson_mut_obj_add_int(doc, obj, "val", show_val); if (curr_val < 0) yyjson_mut_obj_add_str(doc, obj, "position", "bottom"); yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("定扳反:{}", show_val)).c_str()); } break; case eDaoChaPosi::DCP_INVERT2FIX: { yyjson_mut_arr_add_val(convert_points, obj); auto coord = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, obj, "coord", coord); yyjson_mut_arr_add_int(doc, coord, CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).GetTime() * 1000 + ts.fraction / 1000000); yyjson_mut_arr_add_int(doc, coord, curr_val); yyjson_mut_obj_add_strcpy(doc, obj, "time", fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}.{:0>3}", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fraction / 1000000).c_str()); yyjson_mut_obj_add_int(doc, obj, "val", show_val); if (curr_val < 0) yyjson_mut_obj_add_str(doc, obj, "position", "bottom"); yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("反扳定:{}", show_val)).c_str()); } break; default: ASSERT(FALSE); break; } } while (TRUE); } stmt.Close(); }while (false); *json = yyjson_mut_write(doc, 0, json_len); yyjson_mut_doc_free(doc); return 200; }