| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 | 
							- #include "stdafx.h"
 
- #include "AppService.h"
 
- #include <gbk2utf8.h>
 
- #include "MGDataHandler.h"
 
- #include "ODBC/DBConnectPool.h"
 
- #include "Simplelog.h"
 
- #include "MonitorObject.h"
 
- #include "ResistAlarm.h"
 
- #include "Device.h"
 
- #include "HttpPrcess.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";
 
- 	case 503: return "Service Unavailable";
 
- 	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));
 
- 			CSimpleLog::Info((mg_straddr(&c->rem, ip, 50) + gbk).c_str());
 
- 		}
 
- 		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);
 
- 				string imei; int idx;
 
- 				CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei, idx);
 
- 				CAppService::Instance()->GetHandle()->SendMsgToDevice(imei.c_str());
 
- 				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))
 
- 			{
 
- 				CSimpleLog::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;
 
- 			}
 
- 			if (g_lockSync.TryReadLock() == false)
 
- 				code = 503;
 
- 			else
 
- 			{
 
- 				code = HandleConfRead(tag, type, res_doc, res_root);
 
- 				g_lockSync.Unlock();
 
- 			}
 
- 		}
 
- 		else if (cmd.compare("conf_write") == 0){
 
- 			auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
 
- 			auto type = yyjson_get_str(yyjson_obj_get(root, "type"));
 
- 			yyjson_mut_obj_add_strcpy(res_doc, res_root, "type", type);
 
- 			yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
 
- 			auto conf = yyjson_obj_get(root, "conf");
 
- 			if (type && strcmp(type, "log") == 0) //设置日志
 
- 			{
 
- 				g_bLog = yyjson_get_bool(yyjson_obj_get(root, "val"));
 
- 				CSimpleLog::Info(g_bLog ? "打开日志" : "关闭日志");
 
- 				code = 200;
 
- 				break;
 
- 			}
 
- 			if (pConfInfo->isLogin == false) { code = 401; break; }
 
- 			if (tag == 0 || type == 0 || conf == nullptr || yyjson_is_arr(conf) == false)
 
- 			{
 
- 				code = 400;
 
- 				break;
 
- 			}
 
- 			if (g_lockSync.TryWriteLock() == false)
 
- 				code = 503;
 
- 			else
 
- 			{
 
- 				code = HandleConfWrite(tag, type, pConfInfo->name, conf, res_doc, res_root);
 
- 				g_lockSync.Unlock();
 
- 			}
 
- 		}
 
- 		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;
 
- 			}
 
- 			if (g_lockSync.TryWriteLock() == false)
 
- 			{
 
- 				code = 503;
 
- 			}
 
- 			else
 
- 			{
 
- 				auto ret = CMonitorObjectMng::Instance()->LoadMonitorTree();
 
- 				//if (ret) ret = CMonitorObjectMng::Instance()->LoadHistoryData();
 
- 				//if (ret) ret = CResistAlarmMng::Instance()->LoadAlarmSet(); 报警配置数据
 
- 				if (ret) code = 200;
 
- 				else code = 500;
 
- 				g_lockSync.Unlock();
 
- 			}
 
- 		}
 
- 	} 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 && pBase->enable)
 
- 						{
 
- 							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 != INT_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 != INT_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 != INT_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 != INT_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) //设置曲线别名
 
- 	{
 
- 		auto pInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(tag);
 
- 		if (pInfo == nullptr) return 404;
 
- 		if (pInfo->name1.length())
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, pInfo->name1utf.c_str());
 
- 		else
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("1号测力曲线").c_str());
 
- 		if (pInfo->name2.length())
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, pInfo->name2utf.c_str());
 
- 		else
 
- 			yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("2号测力曲线").c_str());
 
- 		yyjson_mut_arr_add_strcpy(doc, conf, pInfo->name3utf.c_str());
 
- 		return 200;
 
- 	}
 
- 	else if (type.compare("monitor.switch_direct.rename") == 0)
 
- 	{
 
- 		auto pInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(tag);
 
- 		if (pInfo == nullptr) return 404;
 
- 		yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(pInfo->out_name).c_str());
 
- 		yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(pInfo->in_name).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 && pBase->enable)
 
- 						{
 
- 							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::HandleConfWrite(string tag, string type, string name, yyjson_val* conf, yyjson_mut_doc* doc, yyjson_mut_val* root)
 
- {
 
- 	//yyjson_mut_obj_add_str(doc, root, "cmd", "conf_write");
 
- 	time_t tt;
 
- 	time(&tt);
 
- 	int code = 500;
 
- 	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;
 
- 						bool enable = false;
 
- 						int alarm = INT_MAX, warn = INT_MAX, f_alarm = INT_MAX, f_warn = INT_MAX;
 
- 						yyjson_val* it = yyjson_arr_get_first(conf);
 
- 						int n = yyjson_arr_size(conf);
 
- 						for (int i = 0; i < n; i++)
 
- 						{
 
- 							auto sz_name = yyjson_get_str(yyjson_obj_get(it, "name"));
 
- 							auto sz_val = yyjson_get_str(yyjson_obj_get(it, "val"));
 
- 							if (sz_name == 0 || sz_val == 0)
 
- 								continue;
 
- 							//解析赋值过程
 
- 							{
 
- 								string name = sz_name;
 
- 								string val = sz_val;
 
- 								if (name.compare("enable") == 0)
 
- 								{
 
- 									if (val.compare("false") == 0)
 
- 									{
 
- 										enable = false;
 
- 									}
 
- 									else
 
- 									{
 
- 										enable = true;
 
- 									}
 
- 								}
 
- 								else if (name.compare("lock_alarm_high_limit") == 0)
 
- 								{
 
- 									alarm = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("lock_warn_high_limit") == 0)
 
- 								{
 
- 									warn = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("d_alarm_high_limit") == 0)
 
- 								{
 
- 									alarm = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("d_warn_high_limit") == 0)
 
- 								{
 
- 									warn = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("keep_alarm_high_limit") == 0)
 
- 								{
 
- 									f_alarm = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("keep_warn_high_limit") == 0)
 
- 								{
 
- 									f_warn = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("f_alarm_high_limit") == 0)
 
- 								{
 
- 									f_alarm = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("f_warn_high_limit") == 0)
 
- 								{
 
- 									f_warn = atoi(val.c_str());
 
- 								}
 
- 							}
 
- 							
 
- 							it = unsafe_yyjson_get_next(it);
 
- 						}
 
- 						auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
 
- 						auto pInfo = (MAX_OVER_LIMIT_INFO*)pBase;
 
- 						if (pBase == nullptr) pInfo = new MAX_OVER_LIMIT_INFO;
 
- 						pInfo->enable = enable;
 
- 						pInfo->no = nNo;
 
- 						pInfo->type = eZL_ALARMTYPE::MAX_OVER_LIMIT;
 
- 						pInfo->alarm_high_limit = alarm;
 
- 						pInfo->warn_high_limit = warn;
 
- 						pInfo->f_alarm_high_limit = f_alarm;
 
- 						pInfo->f_warn_high_limit = f_warn;
 
- 						//记录
 
- 						{
 
- 							string station, mompname, moname, mpname, name1, name2, name3;
 
- 							CMonitorObjectMng::Instance()->GetStationNameByMomP(mo, mp, station, mompname);
 
- 							CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
 
- 							int nPos = mompname.find(".");
 
- 							if (nPos != -1)
 
- 							{
 
- 								moname = mompname.substr(0, nPos);
 
- 								mpname = mompname.substr(nPos + 1, mompname.length() - nPos - 1);
 
- 							}
 
- 							ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, station, station, mo.c_str(), moname.c_str(), mp.c_str(), mpname.c_str(), 0, 0, tt, name, "", eRocord_Opt::RO_CONFIG,
 
- 								fmt::format("用户[{}]配置了[{}][{}][{}]最大值超限报警", name, mompname, nNo == 0 ? name1 : nNo == 1 ? name2 : name3, enable ? "开" : "关"));
 
- 						}
 
- 						//生成字符串
 
- 						{
 
- 							auto conf_doc = yyjson_mut_doc_new(nullptr);
 
- 							auto conf_root = yyjson_val_mut_copy(conf_doc, conf);
 
- 							yyjson_mut_doc_set_root(conf_doc, conf_root);
 
- 							size_t ll;
 
- 							auto json = yyjson_mut_write(conf_doc, 0, &ll);
 
- 							assert(json);
 
- 							do
 
- 							{
 
- 								if (pBase == nullptr)
 
- 								{
 
- 									auto ret = CResistAlarmMng::Instance()->Insert(mo, mp, nNo, (int)eZL_ALARMTYPE::MAX_OVER_LIMIT, pInfo);
 
- 									assert(ret);
 
- 									if (ret == false)
 
- 									{
 
- 										delete pInfo;
 
- 										break;
 
- 									}
 
- 									//add
 
- 									CString sql;
 
- 									sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')",
 
- 										mo.c_str(), mp.c_str(), nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT, json, 0);
 
- 									if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
 
- 									{
 
- 										ASSERT(FALSE);
 
- 										CSimpleLog::Error("语句执行失败" + sql);
 
- 										break;
 
- 									}
 
- 									code = 200;
 
- 								}
 
- 								else
 
- 								{
 
- 									//update
 
- 									CString sql;
 
- 									sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
 
- 										json, mo.c_str(), mp.c_str(), nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
 
- 									if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
 
- 									{
 
- 										ASSERT(FALSE);
 
- 										CSimpleLog::Error("语句执行失败" + sql);
 
- 										break;
 
- 									}
 
- 									code = 200;
 
- 								}
 
- 							} while (false);
 
- 							if (json) free((void*)json);
 
- 							yyjson_mut_doc_free(conf_doc);
 
- 						}
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		else code = 400;
 
- 	}
 
- 	else if (type.compare("monitor.resist.rename") == 0)
 
- 	{
 
- 		if (yyjson_arr_size(conf) != 3) return 400;
 
- 		int nPos = tag.find('.');
 
- 		if (nPos != -1)
 
- 		{
 
- 			auto mo = tag.substr(0, nPos);
 
- 			auto mp = tag.substr(nPos + 1, tag.length() - nPos - 1);
 
- 			//update 
 
- 			auto name1 = yyjson_get_str(yyjson_arr_get(conf, 0));
 
- 			auto name2 = yyjson_get_str(yyjson_arr_get(conf, 1));
 
- 			auto name3 = yyjson_get_str(yyjson_arr_get(conf, 2));
 
- 			if (name1 == nullptr || name2 == nullptr || name3 == nullptr)
 
- 				return 400;
 
- 			CMonitorObjectMng::Instance()->SetNameByMoMp(tag, string(name1), string(name2), string(name3));
 
- 			//save
 
- 			CString sql;
 
- 			sql.Format("update rm_map set name1='%s',name2='%s',name3='%s' where mo = '%s' and mp = '%s';",
 
- 				name1, name2, name3, mo.c_str(), mp.c_str());
 
- 			auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
 
- 			if (false == ret)
 
- 			{
 
- 				CSimpleLog::Error("语句执行错误." + sql);
 
- 				return 500;
 
- 			}
 
- 			return 200;
 
- 		}
 
- 		return 400;
 
- 	}
 
- 	else if (type.compare("monitor.switch_direct.rename") == 0)
 
- 	{
 
- 		if (yyjson_arr_size(conf) != 2) return 400;
 
- 		int nPos = tag.find('.');
 
- 		if (nPos != -1)
 
- 		{
 
- 			auto mo = tag.substr(0, nPos);
 
- 			auto mp = tag.substr(nPos + 1, tag.length() - nPos - 1);
 
- 			//update 
 
- 			auto direct1 = yyjson_get_str(yyjson_arr_get(conf, 0));
 
- 			auto direct2 = yyjson_get_str(yyjson_arr_get(conf, 1));
 
- 			if (direct1 == nullptr || direct2 == nullptr)
 
- 				return 400;
 
- 			CMonitorObjectMng::Instance()->SetDirectByMoMp(tag, string(direct1), string(direct2));
 
- 			//save
 
- 			CString sql;
 
- 			sql.Format("update rm_map set direct1='%s',direct2='%s' where mo = '%s' and mp = '%s';",
 
- 				direct1, direct2, mo.c_str(), mp.c_str());
 
- 			auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
 
- 			if (false == ret)
 
- 			{
 
- 				CSimpleLog::Error("语句执行错误." + sql);
 
- 				return 500;
 
- 			}
 
- 			return 200;
 
- 		}
 
- 		return 400;
 
- 	}
 
- 	else if (type.compare("monitor.alarm.friction_over_limit") == 0)
 
- 	{
 
- 		auto eType = eZL_ALARMTYPE::FRICTION_OVER_LIMIT;
 
- 		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;
 
- 						bool enable = false;
 
- 						int up_alarm_low_limit = INT_MAX;
 
- 						int up_warn_low_limit = INT_MAX;
 
- 						int dw_alarm_high_limit = INT_MIN;
 
- 						int dw_warn_high_limit = INT_MIN;
 
- 						yyjson_val* it = yyjson_arr_get_first(conf);
 
- 						int n = yyjson_arr_size(conf);
 
- 						for (int i = 0; i < n; i++)
 
- 						{
 
- 							auto sz_name = yyjson_get_str(yyjson_obj_get(it, "name"));
 
- 							auto sz_val = yyjson_get_str(yyjson_obj_get(it, "val"));
 
- 							if (sz_name == 0 || sz_val == 0)
 
- 								continue;
 
- 							//解析赋值过程
 
- 							{
 
- 								string name = sz_name;
 
- 								string val = sz_val;
 
- 								if (name.compare("enable") == 0)
 
- 								{
 
- 									if (val.compare("false") == 0)
 
- 									{
 
- 										enable = false;
 
- 									}
 
- 									else
 
- 									{
 
- 										enable = true;
 
- 									}
 
- 								}
 
- 								else if (name.compare("up_alarm_low_limit") == 0)
 
- 								{
 
- 									up_alarm_low_limit = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("up_warn_low_limit") == 0)
 
- 								{
 
- 									up_warn_low_limit = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("dw_alarm_high_limit") == 0)
 
- 								{
 
- 									dw_alarm_high_limit = atoi(val.c_str());
 
- 								}
 
- 								else if (name.compare("dw_warn_high_limit") == 0)
 
- 								{
 
- 									dw_warn_high_limit = atoi(val.c_str());
 
- 								}
 
- 							}
 
- 							it = unsafe_yyjson_get_next(it);
 
- 						}
 
- 						auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eType);
 
- 						auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBase;
 
- 						if (pBase == nullptr) pInfo = new FRICTION_OVER_LIMIT_INFO;
 
- 						pInfo->enable = enable;
 
- 						pInfo->no = nNo;
 
- 						pInfo->type = eType;
 
- 						pInfo->up_alarm_low_limit = up_alarm_low_limit;
 
- 						pInfo->up_warn_low_limit = up_warn_low_limit;
 
- 						pInfo->dw_alarm_high_limit = dw_alarm_high_limit;
 
- 						pInfo->dw_warn_high_limit = dw_warn_high_limit;
 
- 						//记录
 
- 						{
 
- 							string station, mompname, moname, mpname, name1, name2, name3;
 
- 							CMonitorObjectMng::Instance()->GetStationNameByMomP(mo, mp, station, mompname);
 
- 							CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
 
- 							int nPos = mompname.find(".");
 
- 							if (nPos != -1)
 
- 							{
 
- 								moname = mompname.substr(0, nPos);
 
- 								mpname = mompname.substr(nPos + 1, mompname.length() - nPos - 1);
 
- 							}
 
- 							ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, station, station, mo.c_str(), moname.c_str(), mp.c_str(), mpname.c_str(), 0, 0, tt, name, "", eRocord_Opt::RO_CONFIG,
 
- 								fmt::format("用户[{}]配置了[{}][{}][{}]摩擦力超限报警", name, mompname, nNo == 0 ? name1 : nNo == 1 ? name2 : name3, enable ? "开" : "关"));
 
- 						}
 
- 						//生成字符串
 
- 							//生成字符串
 
- 						{
 
- 							auto conf_doc = yyjson_mut_doc_new(nullptr);
 
- 							auto conf_root = yyjson_val_mut_copy(conf_doc, conf);
 
- 							yyjson_mut_doc_set_root(conf_doc, conf_root);
 
- 							size_t ll;
 
- 							auto json = yyjson_mut_write(conf_doc, 0, &ll);
 
- 							assert(json);
 
- 							do
 
- 							{
 
- 								if (pBase == nullptr)
 
- 								{
 
- 									auto ret = CResistAlarmMng::Instance()->Insert(mo, mp, nNo, (int)eType, pInfo);
 
- 									assert(ret);
 
- 									if (ret == false)
 
- 									{
 
- 										delete pInfo;
 
- 										break;
 
- 									}
 
- 									//add
 
- 									CString sql;
 
- 									sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')",
 
- 										mo.c_str(), mp.c_str(), nNo, eType, json, 0);
 
- 									if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
 
- 									{
 
- 										ASSERT(FALSE);
 
- 										CSimpleLog::Error("语句执行失败" + sql);
 
- 										break;
 
- 									}
 
- 									code = 200;
 
- 								}
 
- 								else
 
- 								{
 
- 									//update
 
- 									CString sql;
 
- 									sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
 
- 										json, mo.c_str(), mp.c_str(), nNo, eType);
 
- 									if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
 
- 									{
 
- 										ASSERT(FALSE);
 
- 										CSimpleLog::Error("语句执行失败" + sql);
 
- 										break;
 
- 									}
 
- 									code = 200;
 
- 								}
 
- 							} while (false);
 
- 							if (json) free((void*)json);
 
- 							yyjson_mut_doc_free(conf_doc);
 
- 						}
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		else code = 400;
 
- 	}
 
- 	return code;
 
- }
 
- 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)
 
- 	{
 
- 		CSimpleLog::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;
 
- 			sscanf_s(start.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
 
- 			ctStart = CTime(year, month, day, hour, minute, second);
 
- 			sscanf_s(end.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
 
- 			ctEnd = CTime(year, month, day, hour, minute, second);
 
- 		}
 
- 		catch (CException*)
 
- 		{
 
- 			return 400;
 
- 		}
 
- 		string imei_idx;
 
- 		if (!CMonitorObjectMng::Instance()->MOMP2IMEI(mo_mp, imei_idx)) return 404;
 
- 		string imei, idx;
 
- 		if (!CMonitorObjectMng::Instance()->spiltByPoint(imei_idx, imei, idx)) return 400;
 
- 		int index = atoi(idx.c_str());
 
- 		auto pDevice = CDeviceMng::Instance()->Find(imei);
 
- 		if (pDevice == nullptr) return 404;
 
- 		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->idx = index;
 
- 		query_hist->imei = imei;
 
- 		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)
 
- {
 
- 	//yyjson_mut_obj_add_str(doc, root, "cmd", "alm_ack");
 
- 	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  t1 SET t1.ack_result = 1, t1.ack_name = '%s',t1.ack_time = '%s' \
 
- 			from rm_alarm  t1 where t1.ack_result = 0 and EXISTS(SELECT  1 from  rm_alarm t2 \
 
-            WHERE t2.ID = % d and t1.mo = t2.mo and t1.mp = t2.mp and t1.no = t2.no and t1.type \
 
-            = t2.type);",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(); it++)
 
- 	{
 
- 		auto& pAlarmInfo = *it;
 
- 		if (pAlarmInfo->ack_result != 0) continue;//未受理列表
 
- 		//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;
 
- 		i++;
 
- 	}
 
- 	return 200;
 
- }
 
- bool CMGDataHandler::SendToAllClient(struct mg_connection* c, const char* ptr, size_t len)
 
- {
 
- 	if (g_bLog) CSimpleLog::Info(CString(ptr, len));
 
- 	const auto& mgr = c->mgr;
 
- 	for (auto it = mgr->conns; it; it = it->next)
 
- 	{
 
- 		if (it->is_listening == FALSE && it->is_websocket)
 
- 		{
 
- 			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->imei);
 
- 	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;
 
- }
 
 
  |