| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269 | #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)						{							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);							}							{								if (pInfo->up_alarm_low_limit != INT_MAX) {									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);								}															}							{								if (pInfo->up_warn_low_limit != INT_MAX) {									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);								}															}							{								if (pInfo->dw_alarm_high_limit != INT_MIN) {									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);								}															}							{								if (pInfo->dw_warn_high_limit != INT_MIN) {									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.cbegin();	//set<string> alarmDataSet;	for (int i = 0; it != CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.cend(); it++)	{		auto& pAlarmInfo = *it;		if (pAlarmInfo->ack_result != 0) continue;//未受理列表		/*if (alarmDataSet.size() == 10)			break;					string tag = (pAlarmInfo->mo + '.' + pAlarmInfo->mp + '.' + to_string(pAlarmInfo->no + 1) + "#").c_str();			string up, momp_name, up_name;			string momp = pAlarmInfo->mo + "." + pAlarmInfo->mp;			CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, up, momp_name);			alarmDataSet.insert((up + tag + to_string((int)pAlarmInfo->type)).c_str());*/						//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;}
 |