| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806 | 
							- #include "stdafx.h"
 
- #include "AppService.h"
 
- #include <gbk2utf8.h>
 
- #include "MGDataHandler.h"
 
- #include "ODBC/DBConnectPool.h"
 
- #include "Simplelog.h"
 
- #include "MonitorObject.h"
 
- #include "Device.h"
 
- #include "HttpPrcess.h"
 
- #include "ResistAlarm.h"
 
- extern time_t g_stStart;
 
- static const char* http_status_code_str(int status_code) {
 
- 	switch (status_code) {
 
- 	case 100: return "Continue";
 
- 	case 201: return "Created";
 
- 	case 202: return "Accepted";
 
- 	case 204: return "No Content";
 
- 	case 206: return "Partial Content";
 
- 	case 301: return "Moved Permanently";
 
- 	case 302: return "Found";
 
- 	case 304: return "Not Modified";
 
- 	case 400: return "Bad Request";
 
- 	case 401: return "Unauthorized";
 
- 	case 403: return "Forbidden";
 
- 	case 404: return "Not Found";
 
- 	case 418: return "I'm a teapot";
 
- 	case 500: return "Internal Server Error";
 
- 	case 501: return "Not Implemented";
 
- 	default: return "OK";
 
- 	}
 
- }
 
- CMGDataHandler::CMGDataHandler()
 
- {
 
- }
 
- CMGDataHandler::~CMGDataHandler()
 
- {
 
- }
 
- BOOL CMGDataHandler::HandlerData(const char* ptr, size_t len, char** json)
 
- {
 
- 	
 
- 	return FALSE;
 
- }
 
- size_t CMGDataHandler::HandlerData(struct mg_connection* c, struct mg_ws_message* wm, char** json)
 
- {
 
- 	size_t len = 0;
 
- 	auto doc = yyjson_read(wm->data.ptr, wm->data.len, 0);
 
- 	if (doc == nullptr) return len;
 
- 	auto root = yyjson_doc_get_root(doc);
 
- 	auto res_doc = yyjson_mut_doc_new(nullptr);
 
- 	auto res_root = yyjson_mut_obj(res_doc);
 
- 	yyjson_mut_doc_set_root(res_doc, res_root);
 
- 	auto pConfInfo = (mg_per_session_data*)(c->fn_data);
 
- 	int code = 501; //未实现
 
- 	do 
 
- 	{
 
- 		if (FALSE == yyjson_is_obj(root))
 
- 		{
 
- 			code = 400;
 
- 			break;
 
- 		}
 
- 		auto sz_cmd = yyjson_get_str(yyjson_obj_get(root, "cmd"));
 
- 		if (sz_cmd == 0)
 
- 			break;
 
- 		string cmd = sz_cmd;
 
- 		if (g_bLog)
 
- 		{
 
- 			char ip[50];
 
- 			auto gbk = UTF8toANSI(string(wm->data.ptr, wm->data.len));
 
- 			SPDLOG_INFO(mg_straddr(&c->rem, ip, 50) + gbk);
 
- 		}
 
- 		if (cmd.compare("heartbeat.ping") == 0)//心跳包
 
- 		{
 
- 			yyjson_mut_obj_add_str(res_doc, res_root, "cmd", "heartbeat.pong");
 
- 			SYSTEMTIME tm;
 
- 			GetLocalTime(&tm);
 
- 			char time[50];
 
- 			sprintf_s(time, 50, "%04d-%02d-%02d %02d:%02d:%02d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
 
- 			yyjson_mut_obj_add_strcpy(res_doc, res_root, "time", time);
 
- 			code = 200;
 
- 			break;
 
- 		}
 
- 		yyjson_mut_obj_add_strcpy(res_doc, res_root, "cmd", cmd.c_str()); //回包带命令
 
- 		if (cmd.compare("sub_notify") == 0)//实时订阅包
 
- 		{
 
- 			auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
 
- 			if (sz_tag == 0)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			string tag = sz_tag;
 
- 			int npos = tag.rfind('.');
 
- 			if (npos == -1)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			string momp = tag.substr(0, npos);
 
- 			string type = tag.substr(npos + 1);
 
- 			if (type.compare("resist") == 0)
 
- 			{
 
- 				if (pConfInfo->isLogin == FALSE)
 
- 				{
 
- 					code = 401;
 
- 					break;
 
- 				}
 
- 				//if (!CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei_idx))
 
- 				//{
 
- 				//	code = 404;
 
- 				//	break;
 
- 				//}
 
- 				pConfInfo->m_lstSubReal.push_back(momp);
 
- 				if (CAppService::Instance()->GetHandle()) 
 
- 					CAppService::Instance()->GetHandle()->SendSubNotify(momp);  //通知
 
- 				yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag.c_str());
 
- 				code = 200;
 
- 				//string up, momp_name;
 
- 				//CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, up, momp_name);
 
- 				break;
 
- 			}
 
- 			code = 400;
 
- 		}
 
- 		else if (cmd.compare("login") == 0) //登录包
 
- 		{
 
- 			auto token = yyjson_get_str(yyjson_obj_get(root, "token"));
 
- 			if (token == 0)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			if (strcmp(token, "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") == 0)
 
- 			{
 
- 				pConfInfo->token = token;
 
- 				pConfInfo->username = "system";
 
- 				pConfInfo->name = "系统管理员";
 
- 				pConfInfo->node = "100000";
 
- 				pConfInfo->node_name = "国铁集团";
 
- 				pConfInfo->isLogin = true;
 
- 				code = 200;
 
- 				SendUnAckAlarm(c);
 
- 				break;
 
- 			}
 
- 			CString sql;
 
- 			sql.Format("SELECT username,a.name,node,b.name FROM rm_user a LEFT JOIN rm_mo b ON A.node = B.id  WHERE token = '%s';", token);
 
- 			COdbcStatement stmt;
 
- 			if (FALSE == CDBConnectPool::Instance()->DBQuery(stmt, sql))
 
- 			{
 
- 				SPDLOG_ERROR("[前端]查询语句出错:{}", sql);
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			char username[50];
 
- 			char name[50];
 
- 			char node[50];
 
- 			char node_name[50];
 
- 			int nCol = 1;
 
- 			stmt.BindCharCol(nCol++, username, sizeof(username));
 
- 			stmt.BindCharCol(nCol++, name, sizeof(name));
 
- 			stmt.BindCharCol(nCol++, node, sizeof(node));
 
- 			stmt.BindCharCol(nCol++, node_name, sizeof(node_name));
 
- 			if (stmt.FetchNext() != 0)
 
- 			{
 
- 				code = 401;
 
- 				break;
 
- 			}
 
- 			
 
- 			pConfInfo->token = token;
 
- 			pConfInfo->username = username;
 
- 			pConfInfo->name = name;
 
- 			pConfInfo->node = node;
 
- 			pConfInfo->node_name = node_name;
 
- 			pConfInfo->isLogin = true;
 
- 			
 
- 			SendUnAckAlarm(c);
 
- 			code = 200;
 
- 			break;
 
- 		}
 
- 		else if (cmd.compare("unsub_notify") == 0)
 
- 		{
 
- 			auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
 
- 			if (sz_tag == 0)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			string tag = sz_tag;
 
- 			int npos = tag.rfind('.');
 
- 			if (npos == -1)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			string momp = tag.substr(0, npos);
 
- 			string type = tag.substr(npos + 1);
 
- 			if (type.compare("resist") == 0)
 
- 			{
 
- 				if (pConfInfo->isLogin == FALSE)
 
- 				{
 
- 					code = 401;
 
- 					break;
 
- 				}
 
- 				//if (!CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei_idx))
 
- 				//{
 
- 				//	code = 404;
 
- 				//	break;
 
- 				//}
 
- 				for (auto it = pConfInfo->m_lstSubReal.begin(); it != pConfInfo->m_lstSubReal.end();)
 
- 				{
 
- 					if (it->compare(momp) == 0)
 
- 					{
 
- 						it = pConfInfo->m_lstSubReal.erase(it);
 
- 						code = 200;
 
- 						continue;
 
- 					}
 
- 					it++;
 
- 				}
 
- 				yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag.c_str());
 
- 				code = 200;
 
- 				break;
 
- 			}
 
- 			code = 400;
 
- 		}
 
- 		else if (cmd.compare("query_alm_unack") == 0)
 
- 		{
 
- 			if (pConfInfo->isLogin == false)
 
- 			{
 
- 				code = 401; break;
 
- 			}
 
- 			code = SendUnAckAlarm(c);
 
- 		}
 
- 		else if (cmd.compare("query_hist") == 0){
 
- 			auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
 
- 			yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
 
- 			auto time = yyjson_get_str(yyjson_obj_get(root, "time"));
 
- 			if (tag == 0 || time == 0) { code = 400; break; }
 
- 			if (pConfInfo->isLogin == false) { code = 401; break; }
 
- 			auto yy_sub = yyjson_obj_get(root, "subsection");
 
- 			uint32_t subsection = 5000;
 
- 			if (yy_sub) subsection = yyjson_get_uint(yy_sub);
 
- 			code = HandleQueryHist(tag, time, subsection, c, pConfInfo, res_doc, res_root);
 
- 		}
 
- 		else if (cmd.compare("query_hist_confirm") == 0) //确认历史数据
 
- 		{
 
- 			auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
 
- 			if (sz_tag == 0)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			string tag = sz_tag;
 
- 			int npos = tag.rfind('.');
 
- 			if (npos == -1)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			string momp = tag.substr(0, npos);
 
- 			string type = tag.substr(npos + 1);
 
- 			if (type.compare("resist") == 0)
 
- 			{
 
- 				pConfInfo->bBlock = FALSE;
 
- 				code = 200;
 
- 				//不需要回包
 
- 				yyjson_mut_doc_free(res_doc);
 
- 				res_doc = nullptr;
 
- 				break;
 
- 			}
 
- 			code = 400;
 
- 		}
 
- 		else if (cmd.compare("conf_read") == 0) {
 
- 			auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
 
- 			yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
 
- 			auto type = yyjson_get_str(yyjson_obj_get(root, "type"));
 
- 			yyjson_mut_obj_add_strcpy(res_doc, res_root, "type", type);
 
- 			if (type && strcmp(type, "log") == 0)
 
- 			{
 
- 				yyjson_mut_obj_add_bool(res_doc, res_root, "val", g_bLog);
 
- 				code = 200;
 
- 				break;
 
- 			}
 
- 			if (pConfInfo->isLogin == false) { code = 401; break; }
 
- 			if (tag == 0 || type == 0)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			code = HandleConfRead(tag, type, res_doc, res_root);
 
- 		}
 
- 		else if (cmd.compare("alm_ack") == 0) 
 
- 		{
 
- 			uint32_t alarm_id = yyjson_get_uint(yyjson_obj_get(root, "alarm_id"));
 
- 			yyjson_mut_obj_add_uint(res_doc, res_root, "alarm_id", alarm_id);
 
- 			if (pConfInfo->isLogin == false) { code = 401; break; }
 
- 			if (alarm_id == 0) { code = 400; break; }
 
- 			code = HandleAlarmAck(alarm_id, pConfInfo->name, res_doc, res_root);
 
- 			//回包改为群发
 
- 			if (code == 200)
 
- 			{
 
- 				yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
 
- 				auto c_json = yyjson_mut_write(res_doc, 0, &len);
 
- 				yyjson_mut_doc_free(res_doc);
 
- 				res_doc = nullptr;
 
- 				SendToAllClient(c, c_json, len);
 
- 				free((void*)c_json);
 
- 			}
 
- 		}
 
- 		else if (cmd.compare("alm_handle") == 0)
 
- 		{ 
 
- 			uint32_t alarm_id = yyjson_get_uint(yyjson_obj_get(root, "alarm_id"));
 
- 			yyjson_mut_obj_add_uint(res_doc, res_root, "alarm_id", alarm_id);
 
- 			auto hanlde_info = yyjson_get_str(yyjson_obj_get(root, "hanlde_info"));
 
- 			string strHandleInfo;
 
- 			if (hanlde_info) strHandleInfo = UTF8toANSI(hanlde_info);
 
- 			if (pConfInfo->isLogin == false) { code = 401; break; }
 
- 			if (alarm_id == 0) { code = 400; break; }
 
- 			code = HandleAlarmHandle(alarm_id, pConfInfo->name, strHandleInfo, res_doc, res_root);
 
- 			//回包改为群发
 
- 			if (code == 200)
 
- 			{
 
- 				yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
 
- 				auto c_json = yyjson_mut_write(res_doc, 0, &len);
 
- 				yyjson_mut_doc_free(res_doc);
 
- 				res_doc = nullptr;
 
- 				SendToAllClient(c, c_json, len);
 
- 				free((void*)c_json);
 
- 			}
 
- 		}
 
- 		else if (cmd.compare("test_alarm") == 0) //测试报警
 
- 		{
 
- 			yyjson_mut_doc_free(res_doc);
 
- 			res_doc = nullptr;
 
- 			{
 
- 				auto new_doc = yyjson_mut_doc_new(nullptr);
 
- 				auto new_root = yyjson_val_mut_copy(new_doc, root);
 
- 				yyjson_mut_doc_set_root(new_doc, new_root);
 
- 				yyjson_mut_obj_remove_str(new_root, "cmd");
 
- 				yyjson_mut_obj_add_str(new_doc, new_root, "cmd", "new_alarm");
 
- 				auto c_json = yyjson_mut_write(new_doc, 0, &len);
 
- 				yyjson_mut_doc_free(new_doc);
 
- 				new_doc = nullptr;
 
- 				SendToAllClient(c, c_json, len);
 
- 				free((void*)c_json);
 
- 			}
 
- 		}
 
- 		else if (cmd.compare("sync") == 0)
 
- 		{
 
- 			if (pConfInfo->token.compare("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") != 0)
 
- 			{
 
- 				code = 403;
 
- 				break;
 
- 			}
 
- 			auto ret = CMonitorObjectMng::Instance()->LoadMonitorTree();
 
- 			if (ret) ret = CMonitorObjectMng::Instance()->LoadHistoryData();
 
- 			if (ret) code = 200;
 
- 			else code = 500;
 
- 		}
 
- 	} while (FALSE);
 
- 	if (res_doc)
 
- 	{
 
- 		yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
 
- 		yyjson_mut_obj_add_strcpy(res_doc, res_root, "msg", http_status_code_str(code));
 
- 		if (json) *json = yyjson_mut_write(res_doc, 0, &len);
 
- 		yyjson_mut_doc_free(res_doc);
 
- 	}
 
- 	yyjson_doc_free(doc);
 
- 	return len;
 
- }
 
- int CMGDataHandler::HandleConfRead(string tag, string type, yyjson_mut_doc* doc, yyjson_mut_val* root)
 
- {
 
- 	auto conf = yyjson_mut_arr(doc);
 
- 	yyjson_mut_obj_add_val(doc, root, "conf", conf);
 
- 	if (type.compare("monitor.alarm.max_over_limit") == 0)  //最大值超限
 
- 	{
 
- 		int nPos = tag.find('.');
 
- 		if (nPos != -1)
 
- 		{
 
- 			auto mo = tag.substr(0, nPos);
 
- 			int nPos2 = tag.find('.', nPos + 1);
 
- 			if (nPos2 != -1)
 
- 			{
 
- 				auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
 
- 				int nPos3 = tag.find('.', nPos2 + 1);
 
- 				if (nPos3 != -1)
 
- 				{
 
- 					auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
 
- 					if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
 
- 					{
 
- 						int nNo = atoi(no.c_str()) - 1;
 
- 						auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
 
- 						if (pBase)
 
- 						{
 
- 							string name1, name2, name3;
 
- 							CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
 
- 							assert(pBase->type == eZL_ALARMTYPE::MAX_OVER_LIMIT);
 
- 							auto pInfo = (MAX_OVER_LIMIT_INFO*)pBase;
 
- 							{
 
- 								auto object = yyjson_mut_obj(doc);
 
- 								yyjson_mut_arr_add_val(conf, object);
 
- 								yyjson_mut_obj_add_str(doc, object, "name", "enable");
 
- 								yyjson_mut_obj_add_str(doc, object, "val", pInfo->enable ? "true" : "false");
 
- 							}
 
- 							if (pInfo->alarm_high_limit != SHORT_MAX)
 
- 							{
 
- 								auto object = yyjson_mut_obj(doc);
 
- 								yyjson_mut_arr_add_val(conf, object);
 
- 								if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "d_alarm_high_limit");
 
- 								else
 
- 								{
 
- 									yyjson_mut_obj_add_str(doc, object, "name", "lock_alarm_high_limit");
 
- 									if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
 
- 									else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
 
- 								}
 
- 								yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->alarm_high_limit).c_str());
 
- 							}
 
- 							if (pInfo->warn_high_limit != SHORT_MAX)
 
- 							{
 
- 								auto object = yyjson_mut_obj(doc);
 
- 								yyjson_mut_arr_add_val(conf, object);
 
- 								if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "d_warn_high_limit");
 
- 								else
 
- 								{
 
- 									yyjson_mut_obj_add_str(doc, object, "name", "lock_warn_high_limit");
 
- 									if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
 
- 									else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
 
- 								}
 
- 								yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->warn_high_limit).c_str());
 
- 							}
 
- 							if (pInfo->f_alarm_high_limit != SHORT_MAX)
 
- 							{
 
- 								auto object = yyjson_mut_obj(doc);
 
- 								yyjson_mut_arr_add_val(conf, object);
 
- 								if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "f_alarm_high_limit");
 
- 								else
 
- 								{
 
- 									yyjson_mut_obj_add_str(doc, object, "name", "keep_alarm_high_limit");
 
- 									if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
 
- 									else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
 
- 								}
 
- 								yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_alarm_high_limit).c_str());
 
- 							}
 
- 							if (pInfo->f_warn_high_limit != SHORT_MAX)
 
- 							{
 
- 								auto object = yyjson_mut_obj(doc);
 
- 								yyjson_mut_arr_add_val(conf, object);
 
- 								if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "f_warn_high_limit");
 
- 								else
 
- 								{
 
- 									yyjson_mut_obj_add_str(doc, object, "name", "keep_warn_high_limit");
 
- 									if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
 
- 									else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
 
- 								}
 
- 								yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_warn_high_limit).c_str());
 
- 							}
 
- 						}
 
- 						else
 
- 						{
 
- 						}
 
- 						return 200;
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		return 400;
 
- 	}
 
- 	else if (type.compare("monitor.resist.rename") == 0) //设置曲线别名
 
- 	{
 
- 		string name1, name2, name3;
 
- 		CMonitorObjectMng::Instance()->GetNameByMoMp(tag, name1, name2, name3);
 
- 		char utf[200];
 
- 		if (name1.length())
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(name1).c_str());
 
- 		else
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("1号测力曲线").c_str());
 
- 		if (name2.length())
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(name2).c_str());
 
- 		else
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("2号测力曲线").c_str());
 
- 		if (name3.length())
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(name3).c_str());
 
- 		else
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("转换阻力曲线").c_str());
 
- 		return 200;
 
- 	}
 
- 	else if (type.compare("monitor.switch_direct.rename") == 0)
 
- 	{
 
- 		string direct1, direct2;
 
- 		CMonitorObjectMng::Instance()->GetDirectByMoMp(tag, direct1, direct2);
 
- 		yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(direct1).c_str());
 
- 		yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(direct2).c_str());
 
- 		return 200;
 
- 	}
 
- 	else if (type.compare("monitor.alarm.friction_over_limit") == 0) {
 
- 		int nPos = tag.find('.');
 
- 		if (nPos != -1)
 
- 		{
 
- 			auto mo = tag.substr(0, nPos);
 
- 			int nPos2 = tag.find('.', nPos + 1);
 
- 			if (nPos2 != -1)
 
- 			{
 
- 				auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
 
- 				int nPos3 = tag.find('.', nPos2 + 1);
 
- 				if (nPos3 != -1)
 
- 				{
 
- 					auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
 
- 					if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
 
- 					{
 
- 						int nNo = atoi(no.c_str()) - 1;
 
- 						if (nNo != 2) return 400;
 
- 						auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::FRICTION_OVER_LIMIT);
 
- 						if (pBase)
 
- 						{
 
- 							assert(pBase->type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT);
 
- 							auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBase;
 
- 							//换成 yyjson
 
- 							{
 
- 								auto obj = yyjson_mut_obj(doc);
 
- 								yyjson_mut_obj_add_str(doc, obj, "name", "enable");
 
- 								yyjson_mut_obj_add_str(doc, obj, "val", pInfo->enable ? "true" : "false");
 
- 								yyjson_mut_arr_add_val(conf, obj);
 
- 							}
 
- 							{
 
- 								auto obj = yyjson_mut_obj(doc);
 
- 								yyjson_mut_obj_add_str(doc, obj, "name", "up_alarm_low_limit");
 
- 								yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_alarm_low_limit).c_str());
 
- 								yyjson_mut_arr_add_val(conf, obj);
 
- 							}
 
- 							{
 
- 								auto obj = yyjson_mut_obj(doc);
 
- 								yyjson_mut_obj_add_str(doc, obj, "name", "up_warn_low_limit");
 
- 								yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_warn_low_limit).c_str());
 
- 								yyjson_mut_arr_add_val(conf, obj);
 
- 							}
 
- 							{
 
- 								auto obj = yyjson_mut_obj(doc);
 
- 								yyjson_mut_obj_add_str(doc, obj, "name", "dw_alarm_high_limit");
 
- 								yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_alarm_high_limit).c_str());
 
- 								yyjson_mut_arr_add_val(conf, obj);
 
- 							}
 
- 							{
 
- 								auto obj = yyjson_mut_obj(doc);
 
- 								yyjson_mut_obj_add_str(doc, obj, "name", "dw_warn_high_limit");
 
- 								yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_warn_high_limit).c_str());
 
- 								yyjson_mut_arr_add_val(conf, obj);
 
- 							}
 
- 						}
 
- 						else
 
- 						{
 
- 						}
 
- 						return 200;
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		return 400;
 
- 	}
 
- 	else
 
- 	{
 
- 		return 500;
 
- 	}
 
- }
 
- int CMGDataHandler::HandleQueryHist(string tag, string query_time, uint32_t subsection, struct mg_connection* c, mg_per_session_data* pInfo, yyjson_mut_doc* doc, yyjson_mut_val* root)
 
- {
 
- 	int npos = tag.rfind('.');
 
- 	string mo_mp = tag.substr(0, npos);
 
- 	string type = tag.substr(npos + 1);
 
- 	if (mo_mp.compare("undefined") == 0)
 
- 	{
 
- 		SPDLOG_ERROR("[前端]收到未定义的请求");
 
- 		return 401;
 
- 	}
 
- 	if (type.compare("resist") == 0) //阻力数据
 
- 	{
 
- 		CTime ctStart, ctEnd;
 
- 		int pos = query_time.find('~');
 
- 		if (pos == -1) return 400;
 
- 		string start = query_time.substr(0, pos);
 
- 		string end = query_time.substr(pos + 1);
 
- 		try
 
- 		{
 
- 			int year, month, day, hour, minute, second;
 
- 			auto num = sscanf_s(start.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
 
- 			if (num != 6) throw "time error.";
 
- 			ctStart = CTime(year, month, day, hour, minute, second);
 
- 			num = sscanf_s(end.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
 
- 			if (num != 6) throw "time error.";
 
- 			ctEnd = CTime(year, month, day, hour, minute, second);
 
- 		}
 
- 		catch (CException*)
 
- 		{
 
- 			return 400;
 
- 		}
 
- 		pInfo->bWork = false;
 
- 		if (pInfo->thread_hist)
 
- 		{
 
- 			pInfo->thread_hist->join();
 
- 			delete pInfo->thread_hist;
 
- 			pInfo->thread_hist = nullptr;
 
- 		}
 
- 		LPMGHISTORY_QUERY query_hist = new MG_HISTORY_QUERY;
 
- 		query_hist->mo_mp = mo_mp;
 
- 		time_t tmStart = ctStart.GetTime();
 
- 		query_hist->tmStart = tmStart * 1000;
 
- 		query_hist->tmEnd = ctEnd.GetTime() * 1000;
 
- 		query_hist->type = "resist";
 
- 		query_hist->c = c;
 
- 		query_hist->subsection = subsection;
 
- 		pInfo->bWork = true;
 
- 		time_t tmNow;
 
- 		time(&tmNow);
 
- 		//if (tmStart < g_stStart - 7200 || tmNow - tmStart > MAX_SAVE_TIME / 1000)
 
- 		{
 
- 			//超过当天,赋值当天 临时
 
- 			//auto end = CTime(ctStart.GetYear(), ctStart.GetMonth(), ctStart.GetDay(), 23, 59, 59);
 
- 			//改为限定24小时
 
- 			auto end = ctStart + CTimeSpan(1, 0, 0, 0);
 
- 			if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
 
- 			pInfo->thread_hist = new thread(ThreadProcForQueryHistDB, query_hist);
 
- 		}
 
- 		//else
 
- 		//{
 
- 		//	//实时也改为限定24小时
 
- 		//	auto end = ctStart + CTimeSpan(1, 0, 0, 0);
 
- 		//	if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
 
- 		//	pInfo->thread_hist = new thread(ThreadProcForQueryHist, query_hist);
 
- 		//}
 
- 		return 200;
 
- 	}
 
- 	return 400;
 
- }
 
- int CMGDataHandler::HandleAlarmAck(uint32_t alarm_id, string ack_name, yyjson_mut_doc* doc, yyjson_mut_val* root)
 
- {
 
- 	//TODO
 
- 	/*
 
- 	CTime ctNow = CTime::GetCurrentTime();
 
- 	//删除内存数据
 
- 	bool ret = CResistAlarmMng::Instance()->AckAlarm(alarm_id, ack_name, ctNow);
 
- 	string ack_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
 
- 	//更新数据库
 
- 	{
 
- 		CString sql;
 
- 		sql.Format("UPDATE [rm_alarm] SET ack_result = 1, ack_name='%s', ack_time='%s' WHERE ID = %d;",
 
- 			ack_name.c_str(), ack_time.c_str(), alarm_id);
 
- 		ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
 
- 	}
 
- 	//记录
 
- 	{
 
- 		ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), ack_name, "", eRocord_Opt::RO_OPT,
 
- 			fmt::format("用户[{}]确认id为[{}]的报警记录", ack_name, alarm_id));
 
- 	}
 
- 	if (ret)
 
- 	{
 
- 		yyjson_mut_obj_add_strcpy(doc, root, "ack_time", ack_time.c_str());
 
- 		yyjson_mut_obj_add_strcpy(doc, root, "ack_name", ANSItoUTF8(ack_name).c_str());
 
- 		return 200;
 
- 	}
 
- 	*/
 
- 	return 400;
 
- }
 
- int CMGDataHandler::HandleAlarmHandle(uint32_t alarm_id, string handle_name, string handle_info, yyjson_mut_doc* doc, yyjson_mut_val* root)
 
- {
 
- 	/*
 
- 	//yyjson_mut_obj_add_str(doc, root, "cmd", "alm_handle");
 
- 	CTime ctNow = CTime::GetCurrentTime();
 
- 	//删除内存数据
 
- 	bool ret = CResistAlarmMng::Instance()->HandleAlarm(alarm_id);
 
- 	string handle_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
 
- 	//更新数据库
 
- 	{
 
- 		CString sql;
 
- 		sql.Format("UPDATE [rm_alarm] SET handle_result = 1, handle_name='%s', handle_time='%s', handle_info='%s' WHERE ID = %d;",
 
- 			handle_name.c_str(), handle_time.c_str(), handle_info.c_str(), alarm_id);
 
- 		ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
 
- 	}
 
- 	//记录
 
- 	{
 
- 		ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), handle_name, "", eRocord_Opt::RO_OPT,
 
- 			fmt::format("用户[{}]处理id为[{}]的报警记录", handle_name, alarm_id));
 
- 	}
 
- 	if (ret)
 
- 	{
 
- 		yyjson_mut_obj_add_strcpy(doc, root, "handle_time", handle_time.c_str());
 
- 		yyjson_mut_obj_add_strcpy(doc, root, "handle_name", ANSItoUTF8(handle_name).c_str());
 
- 		yyjson_mut_obj_add_strcpy(doc, root, "hanlde_info", ANSItoUTF8(handle_info).c_str());
 
- 		return 200;
 
- 	}*/
 
- 	return 400;
 
- }
 
- int CMGDataHandler::SendUnAckAlarm(mg_connection* c)
 
- {
 
- 	//lock_guard<mutex> lock(CResistAlarmMng::Instance()->m_mtxAlarm);
 
- 	//auto it = CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.crbegin();
 
- 	//for (int i = 0; i < 10 && it != CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.crend(); i++, it++)
 
- 	//{
 
- 	//	auto& pAlarmInfo = *it;
 
- 	//	//TODO 根据用户过滤
 
- 	//	rapidjson::StringBuffer buffer;
 
- 	//	auto ret = CResistAlarmMng::AlarmInfo2Pack(pAlarmInfo, buffer);
 
- 	//	const char* output = buffer.GetString();
 
- 	//	int len = buffer.GetLength();
 
- 	//	auto send_len = mg_ws_send(c, output, len, WEBSOCKET_OP_TEXT);
 
- 	//	if(c->fn_data) ((mg_per_session_data*)(c->fn_data))->send_size += send_len;
 
- 	//}
 
- 	return 200;
 
- }
 
- bool CMGDataHandler::SendToAllClient(struct mg_connection* c, const char* ptr, size_t len)
 
- {
 
- 	SPDLOG_DEBUG("[WS]Send Data : {}", ptr);
 
- 	const auto& mgr = c->mgr;
 
- 	for (auto it = mgr->conns; it; it = it->next)
 
- 	{
 
- 		if (it->is_listening == FALSE && it->is_websocket)
 
- 		{
 
- 			char buf[40] = { 0 };
 
- 			SPDLOG_DEBUG("[WS]{}", mg_straddr(it->rem, buf, sizeof(buf)));
 
- 			mg_ws_send(it, ptr, len, WEBSOCKET_OP_TEXT);
 
- 		}
 
- 	}
 
- 	return true;
 
- }
 
- //void CMGDataHandler::ThreadProcForQueryHist(LPMGHISTORY_QUERY history_query)
 
- //{
 
- //	auto pDevice = CDeviceMng::Instance()->Find(history_query->mo_mp);
 
- //	if (pDevice == nullptr) return;
 
- //	assert(pDevice);
 
- //
 
- //	auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
 
- //	if (pInfo)
 
- //	{
 
- //		if (history_query->idx == 0)
 
- //			pInfo->SendHistResistForEcharts(history_query->c, history_query->mo_mp, history_query->tmStart, history_query->tmEnd, history_query->subsection, pDevice->map_resist_idx00, pDevice->map_resist_idx01, pDevice->map_resist_idx02);
 
- //		else if (history_query->idx == 1)
 
- //			pInfo->SendHistResistForEcharts(history_query->c, history_query->mo_mp, history_query->tmStart, history_query->tmEnd, history_query->subsection, pDevice->map_resist_idx10, pDevice->map_resist_idx11, pDevice->map_resist_idx12);
 
- //		else if (history_query->idx == 2)
 
- //			pInfo->SendHistResistForEcharts(history_query->c, history_query->mo_mp, history_query->tmStart, history_query->tmEnd, history_query->subsection, pDevice->map_resist_idx20, pDevice->map_resist_idx21, pDevice->map_resist_idx22);
 
- //
 
- //	}
 
- //
 
- //	delete history_query;
 
- //	history_query = nullptr;
 
- //}
 
- void CMGDataHandler::ThreadProcForQueryHistDB(LPMGHISTORY_QUERY history_query)
 
- {
 
- 	auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
 
- 	if (pInfo)
 
- 	{
 
- 		pInfo->SendHistResistDBForEcharts(history_query);
 
- 	}
 
- 	delete history_query;
 
- 	history_query = nullptr;
 
- }
 
 
  |