MGDataHandler.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. #include "stdafx.h"
  2. #include "AppService.h"
  3. #include <gbk2utf8.h>
  4. #include "MGDataHandler.h"
  5. #include "ODBC/DBConnectPool.h"
  6. #include "Simplelog.h"
  7. #include "MonitorObject.h"
  8. #include "ResistAlarm.h"
  9. #include "Device.h"
  10. #include "HttpPrcess.h"
  11. extern time_t g_stStart;
  12. static const char* http_status_code_str(int status_code) {
  13. switch (status_code) {
  14. case 100: return "Continue";
  15. case 201: return "Created";
  16. case 202: return "Accepted";
  17. case 204: return "No Content";
  18. case 206: return "Partial Content";
  19. case 301: return "Moved Permanently";
  20. case 302: return "Found";
  21. case 304: return "Not Modified";
  22. case 400: return "Bad Request";
  23. case 401: return "Unauthorized";
  24. case 403: return "Forbidden";
  25. case 404: return "Not Found";
  26. case 418: return "I'm a teapot";
  27. case 500: return "Internal Server Error";
  28. case 501: return "Not Implemented";
  29. case 503: return "Service Unavailable";
  30. default: return "OK";
  31. }
  32. }
  33. CMGDataHandler::CMGDataHandler()
  34. {
  35. }
  36. CMGDataHandler::~CMGDataHandler()
  37. {
  38. }
  39. BOOL CMGDataHandler::HandlerData(const char* ptr, size_t len, char** json)
  40. {
  41. return FALSE;
  42. }
  43. size_t CMGDataHandler::HandlerData(struct mg_connection* c, struct mg_ws_message* wm, char** json)
  44. {
  45. size_t len = 0;
  46. auto doc = yyjson_read(wm->data.ptr, wm->data.len, 0);
  47. if (doc == nullptr) return len;
  48. auto root = yyjson_doc_get_root(doc);
  49. auto res_doc = yyjson_mut_doc_new(nullptr);
  50. auto res_root = yyjson_mut_obj(res_doc);
  51. yyjson_mut_doc_set_root(res_doc, res_root);
  52. auto pConfInfo = (mg_per_session_data*)(c->fn_data);
  53. int code = 501; //未实现
  54. do
  55. {
  56. if (FALSE == yyjson_is_obj(root))
  57. {
  58. code = 400;
  59. break;
  60. }
  61. auto sz_cmd = yyjson_get_str(yyjson_obj_get(root, "cmd"));
  62. if (sz_cmd == 0)
  63. break;
  64. string cmd = sz_cmd;
  65. if (g_bLog)
  66. {
  67. char ip[50];
  68. auto gbk = UTF8toANSI(string(wm->data.ptr, wm->data.len));
  69. CSimpleLog::Info((mg_straddr(&c->rem, ip, 50) + gbk).c_str());
  70. }
  71. if (cmd.compare("heartbeat.ping") == 0)//心跳包
  72. {
  73. yyjson_mut_obj_add_str(res_doc, res_root, "cmd", "heartbeat.pong");
  74. SYSTEMTIME tm;
  75. GetLocalTime(&tm);
  76. char time[50];
  77. sprintf_s(time, 50, "%04d-%02d-%02d %02d:%02d:%02d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
  78. yyjson_mut_obj_add_strcpy(res_doc, res_root, "time", time);
  79. code = 200;
  80. break;
  81. }
  82. yyjson_mut_obj_add_strcpy(res_doc, res_root, "cmd", cmd.c_str()); //回包带命令
  83. if (cmd.compare("sub_notify") == 0)//实时订阅包
  84. {
  85. auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  86. if (sz_tag == 0)
  87. {
  88. code = 400;
  89. break;
  90. }
  91. string tag = sz_tag;
  92. int npos = tag.rfind('.');
  93. if (npos == -1)
  94. {
  95. code = 400;
  96. break;
  97. }
  98. string momp = tag.substr(0, npos);
  99. string type = tag.substr(npos + 1);
  100. if (type.compare("resist") == 0)
  101. {
  102. if (pConfInfo->isLogin == FALSE)
  103. {
  104. code = 401;
  105. break;
  106. }
  107. //if (!CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei_idx))
  108. //{
  109. // code = 404;
  110. // break;
  111. //}
  112. pConfInfo->m_lstSubReal.push_back(momp);
  113. string imei; int idx;
  114. CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei, idx);
  115. CAppService::Instance()->GetHandle()->SendMsgToDevice(imei.c_str());
  116. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag.c_str());
  117. code = 200;
  118. //string up, momp_name;
  119. //CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, up, momp_name);
  120. break;
  121. }
  122. code = 400;
  123. }
  124. else if (cmd.compare("login") == 0) //登录包
  125. {
  126. auto token = yyjson_get_str(yyjson_obj_get(root, "token"));
  127. if (token == 0)
  128. {
  129. code = 400;
  130. break;
  131. }
  132. if (strcmp(token, "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") == 0)
  133. {
  134. pConfInfo->token = token;
  135. pConfInfo->username = "system";
  136. pConfInfo->name = "系统管理员";
  137. pConfInfo->node = "100000";
  138. pConfInfo->node_name = "国铁集团";
  139. pConfInfo->isLogin = true;
  140. code = 200;
  141. SendUnAckAlarm(c);
  142. break;
  143. }
  144. CString sql;
  145. 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);
  146. COdbcStatement stmt;
  147. if (FALSE == CDBConnectPool::Instance()->DBQuery(stmt, sql))
  148. {
  149. CSimpleLog::Error("[前端]查询语句出错:" + sql);
  150. code = 400;
  151. break;
  152. }
  153. char username[50];
  154. char name[50];
  155. char node[50];
  156. char node_name[50];
  157. int nCol = 1;
  158. stmt.BindCharCol(nCol++, username, sizeof(username));
  159. stmt.BindCharCol(nCol++, name, sizeof(name));
  160. stmt.BindCharCol(nCol++, node, sizeof(node));
  161. stmt.BindCharCol(nCol++, node_name, sizeof(node_name));
  162. if (stmt.FetchNext() != 0)
  163. {
  164. code = 401;
  165. break;
  166. }
  167. pConfInfo->token = token;
  168. pConfInfo->username = username;
  169. pConfInfo->name = name;
  170. pConfInfo->node = node;
  171. pConfInfo->node_name = node_name;
  172. pConfInfo->isLogin = true;
  173. SendUnAckAlarm(c);
  174. code = 200;
  175. break;
  176. }
  177. else if (cmd.compare("unsub_notify") == 0)
  178. {
  179. auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  180. if (sz_tag == 0)
  181. {
  182. code = 400;
  183. break;
  184. }
  185. string tag = sz_tag;
  186. int npos = tag.rfind('.');
  187. if (npos == -1)
  188. {
  189. code = 400;
  190. break;
  191. }
  192. string momp = tag.substr(0, npos);
  193. string type = tag.substr(npos + 1);
  194. if (type.compare("resist") == 0)
  195. {
  196. if (pConfInfo->isLogin == FALSE)
  197. {
  198. code = 401;
  199. break;
  200. }
  201. //if (!CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei_idx))
  202. //{
  203. // code = 404;
  204. // break;
  205. //}
  206. for (auto it = pConfInfo->m_lstSubReal.begin(); it != pConfInfo->m_lstSubReal.end();)
  207. {
  208. if (it->compare(momp) == 0)
  209. {
  210. it = pConfInfo->m_lstSubReal.erase(it);
  211. code = 200;
  212. continue;
  213. }
  214. it++;
  215. }
  216. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag.c_str());
  217. code = 200;
  218. break;
  219. }
  220. code = 400;
  221. }
  222. else if (cmd.compare("query_alm_unack") == 0)
  223. {
  224. if (pConfInfo->isLogin == false)
  225. {
  226. code = 401; break;
  227. }
  228. code = SendUnAckAlarm(c);
  229. }
  230. else if (cmd.compare("query_hist") == 0){
  231. auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  232. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
  233. auto time = yyjson_get_str(yyjson_obj_get(root, "time"));
  234. if (tag == 0 || time == 0) { code = 400; break; }
  235. if (pConfInfo->isLogin == false) { code = 401; break; }
  236. auto yy_sub = yyjson_obj_get(root, "subsection");
  237. uint32_t subsection = 5000;
  238. if (yy_sub) subsection = yyjson_get_uint(yy_sub);
  239. code = HandleQueryHist(tag, time, subsection, c, pConfInfo, res_doc, res_root);
  240. }
  241. else if (cmd.compare("query_hist_confirm") == 0) //确认历史数据
  242. {
  243. auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  244. if (sz_tag == 0)
  245. {
  246. code = 400;
  247. break;
  248. }
  249. string tag = sz_tag;
  250. int npos = tag.rfind('.');
  251. if (npos == -1)
  252. {
  253. code = 400;
  254. break;
  255. }
  256. string momp = tag.substr(0, npos);
  257. string type = tag.substr(npos + 1);
  258. if (type.compare("resist") == 0)
  259. {
  260. pConfInfo->bBlock = FALSE;
  261. code = 200;
  262. //不需要回包
  263. yyjson_mut_doc_free(res_doc);
  264. res_doc = nullptr;
  265. break;
  266. }
  267. code = 400;
  268. }
  269. else if (cmd.compare("conf_read") == 0){
  270. auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  271. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
  272. auto type = yyjson_get_str(yyjson_obj_get(root, "type"));
  273. yyjson_mut_obj_add_strcpy(res_doc, res_root, "type", type);
  274. if (type && strcmp(type, "log") == 0)
  275. {
  276. yyjson_mut_obj_add_bool(res_doc, res_root, "val", g_bLog);
  277. code = 200;
  278. break;
  279. }
  280. if (pConfInfo->isLogin == false) { code = 401; break; }
  281. if (tag == 0 || type == 0)
  282. {
  283. code = 400;
  284. break;
  285. }
  286. if (g_lockSync.TryReadLock() == false)
  287. code = 503;
  288. else
  289. {
  290. code = HandleConfRead(tag, type, res_doc, res_root);
  291. g_lockSync.Unlock();
  292. }
  293. }
  294. else if (cmd.compare("conf_write") == 0){
  295. auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  296. auto type = yyjson_get_str(yyjson_obj_get(root, "type"));
  297. yyjson_mut_obj_add_strcpy(res_doc, res_root, "type", type);
  298. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
  299. auto conf = yyjson_obj_get(root, "conf");
  300. if (type && strcmp(type, "log") == 0) //设置日志
  301. {
  302. g_bLog = yyjson_get_bool(yyjson_obj_get(root, "val"));
  303. CSimpleLog::Info(g_bLog ? "打开日志" : "关闭日志");
  304. code = 200;
  305. break;
  306. }
  307. if (pConfInfo->isLogin == false) { code = 401; break; }
  308. if (tag == 0 || type == 0 || conf == nullptr || yyjson_is_arr(conf) == false)
  309. {
  310. code = 400;
  311. break;
  312. }
  313. if (g_lockSync.TryWriteLock() == false)
  314. code = 503;
  315. else
  316. {
  317. code = HandleConfWrite(tag, type, pConfInfo->name, conf, res_doc, res_root);
  318. g_lockSync.Unlock();
  319. }
  320. }
  321. else if (cmd.compare("alm_ack") == 0)
  322. {
  323. uint32_t alarm_id = yyjson_get_uint(yyjson_obj_get(root, "alarm_id"));
  324. yyjson_mut_obj_add_uint(res_doc, res_root, "alarm_id", alarm_id);
  325. if (pConfInfo->isLogin == false) { code = 401; break; }
  326. if (alarm_id == 0) { code = 400; break; }
  327. code = HandleAlarmAck(alarm_id, pConfInfo->name, res_doc, res_root);
  328. //回包改为群发
  329. if (code == 200)
  330. {
  331. yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
  332. auto c_json = yyjson_mut_write(res_doc, 0, &len);
  333. yyjson_mut_doc_free(res_doc);
  334. res_doc = nullptr;
  335. SendToAllClient(c, c_json, len);
  336. free((void*)c_json);
  337. }
  338. }
  339. else if (cmd.compare("alm_handle") == 0)
  340. {
  341. uint32_t alarm_id = yyjson_get_uint(yyjson_obj_get(root, "alarm_id"));
  342. yyjson_mut_obj_add_uint(res_doc, res_root, "alarm_id", alarm_id);
  343. auto hanlde_info = yyjson_get_str(yyjson_obj_get(root, "hanlde_info"));
  344. string strHandleInfo;
  345. if (hanlde_info) strHandleInfo = UTF8toANSI(hanlde_info);
  346. if (pConfInfo->isLogin == false) { code = 401; break; }
  347. if (alarm_id == 0) { code = 400; break; }
  348. code = HandleAlarmHandle(alarm_id, pConfInfo->name, strHandleInfo, res_doc, res_root);
  349. //回包改为群发
  350. if (code == 200)
  351. {
  352. yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
  353. auto c_json = yyjson_mut_write(res_doc, 0, &len);
  354. yyjson_mut_doc_free(res_doc);
  355. res_doc = nullptr;
  356. SendToAllClient(c, c_json, len);
  357. free((void*)c_json);
  358. }
  359. }
  360. else if (cmd.compare("test_alarm") == 0) //测试报警
  361. {
  362. yyjson_mut_doc_free(res_doc);
  363. res_doc = nullptr;
  364. {
  365. auto new_doc = yyjson_mut_doc_new(nullptr);
  366. auto new_root = yyjson_val_mut_copy(new_doc, root);
  367. yyjson_mut_doc_set_root(new_doc, new_root);
  368. yyjson_mut_obj_remove_str(new_root, "cmd");
  369. yyjson_mut_obj_add_str(new_doc, new_root, "cmd", "new_alarm");
  370. auto c_json = yyjson_mut_write(new_doc, 0, &len);
  371. yyjson_mut_doc_free(new_doc);
  372. new_doc = nullptr;
  373. SendToAllClient(c, c_json, len);
  374. free((void*)c_json);
  375. }
  376. }
  377. else if (cmd.compare("sync") == 0)
  378. {
  379. if (pConfInfo->token.compare("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") != 0)
  380. {
  381. code = 403;
  382. break;
  383. }
  384. if (g_lockSync.TryWriteLock() == false)
  385. {
  386. code = 503;
  387. }
  388. else
  389. {
  390. auto ret = CMonitorObjectMng::Instance()->LoadMonitorTree();
  391. //if (ret) ret = CMonitorObjectMng::Instance()->LoadHistoryData();
  392. //if (ret) ret = CResistAlarmMng::Instance()->LoadAlarmSet(); 报警配置数据
  393. if (ret) code = 200;
  394. else code = 500;
  395. g_lockSync.Unlock();
  396. }
  397. }
  398. } while (FALSE);
  399. if (res_doc)
  400. {
  401. yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
  402. yyjson_mut_obj_add_strcpy(res_doc, res_root, "msg", http_status_code_str(code));
  403. if (json) *json = yyjson_mut_write(res_doc, 0, &len);
  404. yyjson_mut_doc_free(res_doc);
  405. }
  406. yyjson_doc_free(doc);
  407. return len;
  408. }
  409. int CMGDataHandler::HandleConfRead(string tag, string type, yyjson_mut_doc* doc, yyjson_mut_val* root)
  410. {
  411. auto conf = yyjson_mut_arr(doc);
  412. yyjson_mut_obj_add_val(doc, root, "conf", conf);
  413. if (type.compare("monitor.alarm.max_over_limit") == 0) //最大值超限
  414. {
  415. int nPos = tag.find('.');
  416. if (nPos != -1)
  417. {
  418. auto mo = tag.substr(0, nPos);
  419. int nPos2 = tag.find('.', nPos + 1);
  420. if (nPos2 != -1)
  421. {
  422. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  423. int nPos3 = tag.find('.', nPos2 + 1);
  424. if (nPos3 != -1)
  425. {
  426. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  427. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  428. {
  429. int nNo = atoi(no.c_str()) - 1;
  430. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
  431. if (pBase)
  432. {
  433. string name1, name2, name3;
  434. CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
  435. assert(pBase->type == eZL_ALARMTYPE::MAX_OVER_LIMIT);
  436. auto pInfo = (MAX_OVER_LIMIT_INFO*)pBase;
  437. {
  438. auto object = yyjson_mut_obj(doc);
  439. yyjson_mut_arr_add_val(conf, object);
  440. yyjson_mut_obj_add_str(doc, object, "name", "enable");
  441. yyjson_mut_obj_add_str(doc, object, "val", pInfo->enable ? "true" : "false");
  442. }
  443. if (pInfo->alarm_high_limit != INT_MAX)
  444. {
  445. auto object = yyjson_mut_obj(doc);
  446. yyjson_mut_arr_add_val(conf, object);
  447. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "d_alarm_high_limit");
  448. else
  449. {
  450. yyjson_mut_obj_add_str(doc, object, "name", "lock_alarm_high_limit");
  451. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  452. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  453. }
  454. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->alarm_high_limit).c_str());
  455. }
  456. if (pInfo->warn_high_limit != INT_MAX)
  457. {
  458. auto object = yyjson_mut_obj(doc);
  459. yyjson_mut_arr_add_val(conf, object);
  460. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "d_warn_high_limit");
  461. else
  462. {
  463. yyjson_mut_obj_add_str(doc, object, "name", "lock_warn_high_limit");
  464. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  465. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  466. }
  467. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->warn_high_limit).c_str());
  468. }
  469. if (pInfo->f_alarm_high_limit != INT_MAX)
  470. {
  471. auto object = yyjson_mut_obj(doc);
  472. yyjson_mut_arr_add_val(conf, object);
  473. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "f_alarm_high_limit");
  474. else
  475. {
  476. yyjson_mut_obj_add_str(doc, object, "name", "keep_alarm_high_limit");
  477. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  478. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  479. }
  480. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_alarm_high_limit).c_str());
  481. }
  482. if (pInfo->f_warn_high_limit != INT_MAX)
  483. {
  484. auto object = yyjson_mut_obj(doc);
  485. yyjson_mut_arr_add_val(conf, object);
  486. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "f_warn_high_limit");
  487. else
  488. {
  489. yyjson_mut_obj_add_str(doc, object, "name", "keep_warn_high_limit");
  490. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  491. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  492. }
  493. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_warn_high_limit).c_str());
  494. }
  495. }
  496. else
  497. {
  498. }
  499. return 200;
  500. }
  501. }
  502. }
  503. }
  504. return 400;
  505. }
  506. else if (type.compare("monitor.resist.rename") == 0) //设置曲线别名
  507. {
  508. auto pInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(tag);
  509. if (pInfo == nullptr) return 404;
  510. if (pInfo->name1.length())
  511. yyjson_mut_arr_add_strcpy(doc, conf, pInfo->name1utf.c_str());
  512. else
  513. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("1号测力曲线").c_str());
  514. if (pInfo->name2.length())
  515. yyjson_mut_arr_add_strcpy(doc, conf, pInfo->name2utf.c_str());
  516. else
  517. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("2号测力曲线").c_str());
  518. yyjson_mut_arr_add_strcpy(doc, conf, pInfo->name3utf.c_str());
  519. return 200;
  520. }
  521. else if (type.compare("monitor.switch_direct.rename") == 0)
  522. {
  523. auto pInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(tag);
  524. if (pInfo == nullptr) return 404;
  525. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(pInfo->out_name).c_str());
  526. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(pInfo->in_name).c_str());
  527. return 200;
  528. }
  529. else if (type.compare("monitor.alarm.friction_over_limit") == 0) {
  530. int nPos = tag.find('.');
  531. if (nPos != -1)
  532. {
  533. auto mo = tag.substr(0, nPos);
  534. int nPos2 = tag.find('.', nPos + 1);
  535. if (nPos2 != -1)
  536. {
  537. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  538. int nPos3 = tag.find('.', nPos2 + 1);
  539. if (nPos3 != -1)
  540. {
  541. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  542. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  543. {
  544. int nNo = atoi(no.c_str()) - 1;
  545. if (nNo != 2) return 400;
  546. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::FRICTION_OVER_LIMIT);
  547. if (pBase && pBase->enable)
  548. {
  549. assert(pBase->type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT);
  550. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBase;
  551. //换成 yyjson
  552. {
  553. auto obj = yyjson_mut_obj(doc);
  554. yyjson_mut_obj_add_str(doc, obj, "name", "enable");
  555. yyjson_mut_obj_add_str(doc, obj, "val", pInfo->enable ? "true" : "false");
  556. yyjson_mut_arr_add_val(conf, obj);
  557. }
  558. {
  559. if (pInfo->up_alarm_low_limit != INT_MAX) {
  560. auto obj = yyjson_mut_obj(doc);
  561. yyjson_mut_obj_add_str(doc, obj, "name", "up_alarm_low_limit");
  562. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_alarm_low_limit).c_str());
  563. yyjson_mut_arr_add_val(conf, obj);
  564. }
  565. }
  566. {
  567. if (pInfo->up_warn_low_limit != INT_MAX) {
  568. auto obj = yyjson_mut_obj(doc);
  569. yyjson_mut_obj_add_str(doc, obj, "name", "up_warn_low_limit");
  570. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_warn_low_limit).c_str());
  571. yyjson_mut_arr_add_val(conf, obj);
  572. }
  573. }
  574. {
  575. if (pInfo->dw_alarm_high_limit != INT_MIN) {
  576. auto obj = yyjson_mut_obj(doc);
  577. yyjson_mut_obj_add_str(doc, obj, "name", "dw_alarm_high_limit");
  578. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_alarm_high_limit).c_str());
  579. yyjson_mut_arr_add_val(conf, obj);
  580. }
  581. }
  582. {
  583. if (pInfo->dw_warn_high_limit != INT_MIN) {
  584. auto obj = yyjson_mut_obj(doc);
  585. yyjson_mut_obj_add_str(doc, obj, "name", "dw_warn_high_limit");
  586. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_warn_high_limit).c_str());
  587. yyjson_mut_arr_add_val(conf, obj);
  588. }
  589. }
  590. }
  591. else
  592. {
  593. }
  594. return 200;
  595. }
  596. }
  597. }
  598. }
  599. return 400;
  600. }
  601. else
  602. {
  603. return 500;
  604. }
  605. }
  606. int CMGDataHandler::HandleConfWrite(string tag, string type, string name, yyjson_val* conf, yyjson_mut_doc* doc, yyjson_mut_val* root)
  607. {
  608. //yyjson_mut_obj_add_str(doc, root, "cmd", "conf_write");
  609. time_t tt;
  610. time(&tt);
  611. int code = 500;
  612. if (type.compare("monitor.alarm.max_over_limit") == 0)
  613. {
  614. int nPos = tag.find('.');
  615. if (nPos != -1)
  616. {
  617. auto mo = tag.substr(0, nPos);
  618. int nPos2 = tag.find('.', nPos + 1);
  619. if (nPos2 != -1)
  620. {
  621. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  622. int nPos3 = tag.find('.', nPos2 + 1);
  623. if (nPos3 != -1)
  624. {
  625. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  626. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  627. {
  628. int nNo = atoi(no.c_str()) - 1;
  629. bool enable = false;
  630. int alarm = INT_MAX, warn = INT_MAX, f_alarm = INT_MAX, f_warn = INT_MAX;
  631. yyjson_val* it = yyjson_arr_get_first(conf);
  632. int n = yyjson_arr_size(conf);
  633. for (int i = 0; i < n; i++)
  634. {
  635. auto sz_name = yyjson_get_str(yyjson_obj_get(it, "name"));
  636. auto sz_val = yyjson_get_str(yyjson_obj_get(it, "val"));
  637. if (sz_name == 0 || sz_val == 0)
  638. continue;
  639. //解析赋值过程
  640. {
  641. string name = sz_name;
  642. string val = sz_val;
  643. if (name.compare("enable") == 0)
  644. {
  645. if (val.compare("false") == 0)
  646. {
  647. enable = false;
  648. }
  649. else
  650. {
  651. enable = true;
  652. }
  653. }
  654. else if (name.compare("lock_alarm_high_limit") == 0)
  655. {
  656. alarm = atoi(val.c_str());
  657. }
  658. else if (name.compare("lock_warn_high_limit") == 0)
  659. {
  660. warn = atoi(val.c_str());
  661. }
  662. else if (name.compare("d_alarm_high_limit") == 0)
  663. {
  664. alarm = atoi(val.c_str());
  665. }
  666. else if (name.compare("d_warn_high_limit") == 0)
  667. {
  668. warn = atoi(val.c_str());
  669. }
  670. else if (name.compare("keep_alarm_high_limit") == 0)
  671. {
  672. f_alarm = atoi(val.c_str());
  673. }
  674. else if (name.compare("keep_warn_high_limit") == 0)
  675. {
  676. f_warn = atoi(val.c_str());
  677. }
  678. else if (name.compare("f_alarm_high_limit") == 0)
  679. {
  680. f_alarm = atoi(val.c_str());
  681. }
  682. else if (name.compare("f_warn_high_limit") == 0)
  683. {
  684. f_warn = atoi(val.c_str());
  685. }
  686. }
  687. it = unsafe_yyjson_get_next(it);
  688. }
  689. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
  690. auto pInfo = (MAX_OVER_LIMIT_INFO*)pBase;
  691. if (pBase == nullptr) pInfo = new MAX_OVER_LIMIT_INFO;
  692. pInfo->enable = enable;
  693. pInfo->no = nNo;
  694. pInfo->type = eZL_ALARMTYPE::MAX_OVER_LIMIT;
  695. pInfo->alarm_high_limit = alarm;
  696. pInfo->warn_high_limit = warn;
  697. pInfo->f_alarm_high_limit = f_alarm;
  698. pInfo->f_warn_high_limit = f_warn;
  699. //记录
  700. {
  701. string station, mompname, moname, mpname, name1, name2, name3;
  702. CMonitorObjectMng::Instance()->GetStationNameByMomP(mo, mp, station, mompname);
  703. CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
  704. int nPos = mompname.find(".");
  705. if (nPos != -1)
  706. {
  707. moname = mompname.substr(0, nPos);
  708. mpname = mompname.substr(nPos + 1, mompname.length() - nPos - 1);
  709. }
  710. 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,
  711. fmt::format("用户[{}]配置了[{}][{}][{}]最大值超限报警", name, mompname, nNo == 0 ? name1 : nNo == 1 ? name2 : name3, enable ? "开" : "关"));
  712. }
  713. //生成字符串
  714. {
  715. auto conf_doc = yyjson_mut_doc_new(nullptr);
  716. auto conf_root = yyjson_val_mut_copy(conf_doc, conf);
  717. yyjson_mut_doc_set_root(conf_doc, conf_root);
  718. size_t ll;
  719. auto json = yyjson_mut_write(conf_doc, 0, &ll);
  720. assert(json);
  721. do
  722. {
  723. if (pBase == nullptr)
  724. {
  725. auto ret = CResistAlarmMng::Instance()->Insert(mo, mp, nNo, (int)eZL_ALARMTYPE::MAX_OVER_LIMIT, pInfo);
  726. assert(ret);
  727. if (ret == false)
  728. {
  729. delete pInfo;
  730. break;
  731. }
  732. //add
  733. CString sql;
  734. sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')",
  735. mo.c_str(), mp.c_str(), nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT, json, 0);
  736. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  737. {
  738. ASSERT(FALSE);
  739. CSimpleLog::Error("语句执行失败" + sql);
  740. break;
  741. }
  742. code = 200;
  743. }
  744. else
  745. {
  746. //update
  747. CString sql;
  748. sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
  749. json, mo.c_str(), mp.c_str(), nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
  750. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  751. {
  752. ASSERT(FALSE);
  753. CSimpleLog::Error("语句执行失败" + sql);
  754. break;
  755. }
  756. code = 200;
  757. }
  758. } while (false);
  759. if (json) free((void*)json);
  760. yyjson_mut_doc_free(conf_doc);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. else code = 400;
  767. }
  768. else if (type.compare("monitor.resist.rename") == 0)
  769. {
  770. if (yyjson_arr_size(conf) != 3) return 400;
  771. int nPos = tag.find('.');
  772. if (nPos != -1)
  773. {
  774. auto mo = tag.substr(0, nPos);
  775. auto mp = tag.substr(nPos + 1, tag.length() - nPos - 1);
  776. //update
  777. auto name1 = yyjson_get_str(yyjson_arr_get(conf, 0));
  778. auto name2 = yyjson_get_str(yyjson_arr_get(conf, 1));
  779. auto name3 = yyjson_get_str(yyjson_arr_get(conf, 2));
  780. if (name1 == nullptr || name2 == nullptr || name3 == nullptr)
  781. return 400;
  782. CMonitorObjectMng::Instance()->SetNameByMoMp(tag, string(name1), string(name2), string(name3));
  783. //save
  784. CString sql;
  785. sql.Format("update rm_map set name1='%s',name2='%s',name3='%s' where mo = '%s' and mp = '%s';",
  786. name1, name2, name3, mo.c_str(), mp.c_str());
  787. auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
  788. if (false == ret)
  789. {
  790. CSimpleLog::Error("语句执行错误." + sql);
  791. return 500;
  792. }
  793. return 200;
  794. }
  795. return 400;
  796. }
  797. else if (type.compare("monitor.switch_direct.rename") == 0)
  798. {
  799. if (yyjson_arr_size(conf) != 2) return 400;
  800. int nPos = tag.find('.');
  801. if (nPos != -1)
  802. {
  803. auto mo = tag.substr(0, nPos);
  804. auto mp = tag.substr(nPos + 1, tag.length() - nPos - 1);
  805. //update
  806. auto direct1 = yyjson_get_str(yyjson_arr_get(conf, 0));
  807. auto direct2 = yyjson_get_str(yyjson_arr_get(conf, 1));
  808. if (direct1 == nullptr || direct2 == nullptr)
  809. return 400;
  810. CMonitorObjectMng::Instance()->SetDirectByMoMp(tag, string(direct1), string(direct2));
  811. //save
  812. CString sql;
  813. sql.Format("update rm_map set direct1='%s',direct2='%s' where mo = '%s' and mp = '%s';",
  814. direct1, direct2, mo.c_str(), mp.c_str());
  815. auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
  816. if (false == ret)
  817. {
  818. CSimpleLog::Error("语句执行错误." + sql);
  819. return 500;
  820. }
  821. return 200;
  822. }
  823. return 400;
  824. }
  825. else if (type.compare("monitor.alarm.friction_over_limit") == 0)
  826. {
  827. auto eType = eZL_ALARMTYPE::FRICTION_OVER_LIMIT;
  828. int nPos = tag.find('.');
  829. if (nPos != -1)
  830. {
  831. auto mo = tag.substr(0, nPos);
  832. int nPos2 = tag.find('.', nPos + 1);
  833. if (nPos2 != -1)
  834. {
  835. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  836. int nPos3 = tag.find('.', nPos2 + 1);
  837. if (nPos3 != -1)
  838. {
  839. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  840. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  841. {
  842. int nNo = atoi(no.c_str()) - 1;
  843. if (nNo != 2) return 400;
  844. bool enable = false;
  845. int up_alarm_low_limit = INT_MAX;
  846. int up_warn_low_limit = INT_MAX;
  847. int dw_alarm_high_limit = INT_MIN;
  848. int dw_warn_high_limit = INT_MIN;
  849. yyjson_val* it = yyjson_arr_get_first(conf);
  850. int n = yyjson_arr_size(conf);
  851. for (int i = 0; i < n; i++)
  852. {
  853. auto sz_name = yyjson_get_str(yyjson_obj_get(it, "name"));
  854. auto sz_val = yyjson_get_str(yyjson_obj_get(it, "val"));
  855. if (sz_name == 0 || sz_val == 0)
  856. continue;
  857. //解析赋值过程
  858. {
  859. string name = sz_name;
  860. string val = sz_val;
  861. if (name.compare("enable") == 0)
  862. {
  863. if (val.compare("false") == 0)
  864. {
  865. enable = false;
  866. }
  867. else
  868. {
  869. enable = true;
  870. }
  871. }
  872. else if (name.compare("up_alarm_low_limit") == 0)
  873. {
  874. up_alarm_low_limit = atoi(val.c_str());
  875. }
  876. else if (name.compare("up_warn_low_limit") == 0)
  877. {
  878. up_warn_low_limit = atoi(val.c_str());
  879. }
  880. else if (name.compare("dw_alarm_high_limit") == 0)
  881. {
  882. dw_alarm_high_limit = atoi(val.c_str());
  883. }
  884. else if (name.compare("dw_warn_high_limit") == 0)
  885. {
  886. dw_warn_high_limit = atoi(val.c_str());
  887. }
  888. }
  889. it = unsafe_yyjson_get_next(it);
  890. }
  891. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eType);
  892. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBase;
  893. if (pBase == nullptr) pInfo = new FRICTION_OVER_LIMIT_INFO;
  894. pInfo->enable = enable;
  895. pInfo->no = nNo;
  896. pInfo->type = eType;
  897. pInfo->up_alarm_low_limit = up_alarm_low_limit;
  898. pInfo->up_warn_low_limit = up_warn_low_limit;
  899. pInfo->dw_alarm_high_limit = dw_alarm_high_limit;
  900. pInfo->dw_warn_high_limit = dw_warn_high_limit;
  901. //记录
  902. {
  903. string station, mompname, moname, mpname, name1, name2, name3;
  904. CMonitorObjectMng::Instance()->GetStationNameByMomP(mo, mp, station, mompname);
  905. CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
  906. int nPos = mompname.find(".");
  907. if (nPos != -1)
  908. {
  909. moname = mompname.substr(0, nPos);
  910. mpname = mompname.substr(nPos + 1, mompname.length() - nPos - 1);
  911. }
  912. 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,
  913. fmt::format("用户[{}]配置了[{}][{}][{}]摩擦力超限报警", name, mompname, nNo == 0 ? name1 : nNo == 1 ? name2 : name3, enable ? "开" : "关"));
  914. }
  915. //生成字符串
  916. //生成字符串
  917. {
  918. auto conf_doc = yyjson_mut_doc_new(nullptr);
  919. auto conf_root = yyjson_val_mut_copy(conf_doc, conf);
  920. yyjson_mut_doc_set_root(conf_doc, conf_root);
  921. size_t ll;
  922. auto json = yyjson_mut_write(conf_doc, 0, &ll);
  923. assert(json);
  924. do
  925. {
  926. if (pBase == nullptr)
  927. {
  928. auto ret = CResistAlarmMng::Instance()->Insert(mo, mp, nNo, (int)eType, pInfo);
  929. assert(ret);
  930. if (ret == false)
  931. {
  932. delete pInfo;
  933. break;
  934. }
  935. //add
  936. CString sql;
  937. sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')",
  938. mo.c_str(), mp.c_str(), nNo, eType, json, 0);
  939. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  940. {
  941. ASSERT(FALSE);
  942. CSimpleLog::Error("语句执行失败" + sql);
  943. break;
  944. }
  945. code = 200;
  946. }
  947. else
  948. {
  949. //update
  950. CString sql;
  951. sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
  952. json, mo.c_str(), mp.c_str(), nNo, eType);
  953. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  954. {
  955. ASSERT(FALSE);
  956. CSimpleLog::Error("语句执行失败" + sql);
  957. break;
  958. }
  959. code = 200;
  960. }
  961. } while (false);
  962. if (json) free((void*)json);
  963. yyjson_mut_doc_free(conf_doc);
  964. }
  965. }
  966. }
  967. }
  968. }
  969. else code = 400;
  970. }
  971. return code;
  972. }
  973. 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)
  974. {
  975. int npos = tag.rfind('.');
  976. string mo_mp = tag.substr(0, npos);
  977. string type = tag.substr(npos + 1);
  978. if (mo_mp.compare("undefined") == 0)
  979. {
  980. CSimpleLog::Error("[前端]收到未定义的请求");
  981. return 401;
  982. }
  983. if (type.compare("resist") == 0) //阻力数据
  984. {
  985. CTime ctStart, ctEnd;
  986. int pos = query_time.find('~');
  987. if (pos == -1) return 400;
  988. string start = query_time.substr(0, pos);
  989. string end = query_time.substr(pos + 1);
  990. try
  991. {
  992. int year, month, day, hour, minute, second;
  993. sscanf_s(start.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
  994. ctStart = CTime(year, month, day, hour, minute, second);
  995. sscanf_s(end.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
  996. ctEnd = CTime(year, month, day, hour, minute, second);
  997. }
  998. catch (CException*)
  999. {
  1000. return 400;
  1001. }
  1002. string imei_idx;
  1003. if (!CMonitorObjectMng::Instance()->MOMP2IMEI(mo_mp, imei_idx)) return 404;
  1004. string imei, idx;
  1005. if (!CMonitorObjectMng::Instance()->spiltByPoint(imei_idx, imei, idx)) return 400;
  1006. int index = atoi(idx.c_str());
  1007. auto pDevice = CDeviceMng::Instance()->Find(imei);
  1008. if (pDevice == nullptr) return 404;
  1009. pInfo->bWork = false;
  1010. if (pInfo->thread_hist)
  1011. {
  1012. pInfo->thread_hist->join();
  1013. delete pInfo->thread_hist;
  1014. pInfo->thread_hist = nullptr;
  1015. }
  1016. LPMGHISTORY_QUERY query_hist = new MG_HISTORY_QUERY;
  1017. query_hist->idx = index;
  1018. query_hist->imei = imei;
  1019. query_hist->mo_mp = mo_mp;
  1020. time_t tmStart = ctStart.GetTime();
  1021. query_hist->tmStart = tmStart * 1000;
  1022. query_hist->tmEnd = ctEnd.GetTime() * 1000;
  1023. query_hist->type = "resist";
  1024. query_hist->c = c;
  1025. query_hist->subsection = subsection;
  1026. pInfo->bWork = true;
  1027. time_t tmNow;
  1028. time(&tmNow);
  1029. //if (tmStart < g_stStart - 7200 || tmNow - tmStart > MAX_SAVE_TIME / 1000)
  1030. {
  1031. //超过当天,赋值当天 临时
  1032. //auto end = CTime(ctStart.GetYear(), ctStart.GetMonth(), ctStart.GetDay(), 23, 59, 59);
  1033. //改为限定24小时
  1034. auto end = ctStart + CTimeSpan(1, 0, 0, 0);
  1035. if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
  1036. pInfo->thread_hist = new thread(ThreadProcForQueryHistDB, query_hist);
  1037. }
  1038. //else
  1039. //{
  1040. // //实时也改为限定24小时
  1041. // auto end = ctStart + CTimeSpan(1, 0, 0, 0);
  1042. // if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
  1043. // pInfo->thread_hist = new thread(ThreadProcForQueryHist, query_hist);
  1044. //}
  1045. return 200;
  1046. }
  1047. return 400;
  1048. }
  1049. int CMGDataHandler::HandleAlarmAck(uint32_t alarm_id, string ack_name, yyjson_mut_doc* doc, yyjson_mut_val* root)
  1050. {
  1051. //yyjson_mut_obj_add_str(doc, root, "cmd", "alm_ack");
  1052. CTime ctNow = CTime::GetCurrentTime();
  1053. //删除内存数据
  1054. bool ret = CResistAlarmMng::Instance()->AckAlarm(alarm_id, ack_name, ctNow);
  1055. string ack_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
  1056. //更新数据库
  1057. {
  1058. CString sql;
  1059. sql.Format("UPDATE t1 SET t1.ack_result = 1, t1.ack_name = '%s',t1.ack_time = '%s' \
  1060. from rm_alarm t1 where t1.ack_result = 0 and EXISTS(SELECT 1 from rm_alarm t2 \
  1061. WHERE t2.ID = % d and t1.mo = t2.mo and t1.mp = t2.mp and t1.no = t2.no and t1.type \
  1062. = t2.type);",ack_name.c_str(), ack_time.c_str(), alarm_id);
  1063. ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1064. }
  1065. //记录
  1066. {
  1067. ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), ack_name, "", eRocord_Opt::RO_OPT,
  1068. fmt::format("用户[{}]确认id为[{}]的报警记录", ack_name, alarm_id));
  1069. }
  1070. if (ret)
  1071. {
  1072. yyjson_mut_obj_add_strcpy(doc, root, "ack_time", ack_time.c_str());
  1073. yyjson_mut_obj_add_strcpy(doc, root, "ack_name", ANSItoUTF8(ack_name).c_str());
  1074. return 200;
  1075. }
  1076. return 400;
  1077. }
  1078. int CMGDataHandler::HandleAlarmHandle(uint32_t alarm_id, string handle_name, string handle_info, yyjson_mut_doc* doc, yyjson_mut_val* root)
  1079. {
  1080. //yyjson_mut_obj_add_str(doc, root, "cmd", "alm_handle");
  1081. CTime ctNow = CTime::GetCurrentTime();
  1082. //删除内存数据
  1083. bool ret = CResistAlarmMng::Instance()->HandleAlarm(alarm_id);
  1084. string handle_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
  1085. //更新数据库
  1086. {
  1087. CString sql;
  1088. sql.Format("UPDATE [rm_alarm] SET handle_result = 1, handle_name='%s', handle_time='%s', handle_info='%s' WHERE ID = %d;",
  1089. handle_name.c_str(), handle_time.c_str(), handle_info.c_str(), alarm_id);
  1090. ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1091. }
  1092. //记录
  1093. {
  1094. ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), handle_name, "", eRocord_Opt::RO_OPT,
  1095. fmt::format("用户[{}]处理id为[{}]的报警记录", handle_name, alarm_id));
  1096. }
  1097. if (ret)
  1098. {
  1099. yyjson_mut_obj_add_strcpy(doc, root, "handle_time", handle_time.c_str());
  1100. yyjson_mut_obj_add_strcpy(doc, root, "handle_name", ANSItoUTF8(handle_name).c_str());
  1101. yyjson_mut_obj_add_strcpy(doc, root, "hanlde_info", ANSItoUTF8(handle_info).c_str());
  1102. return 200;
  1103. }
  1104. return 400;
  1105. }
  1106. int CMGDataHandler::SendUnAckAlarm(mg_connection* c)
  1107. {
  1108. lock_guard<mutex> lock(CResistAlarmMng::Instance()->m_mtxAlarm);
  1109. auto it = CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.cbegin();
  1110. //set<string> alarmDataSet;
  1111. for (int i = 0; it != CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.cend(); it++)
  1112. {
  1113. auto& pAlarmInfo = *it;
  1114. if (pAlarmInfo->ack_result != 0) continue;//未受理列表
  1115. /*if (alarmDataSet.size() == 10)
  1116. break;
  1117. string tag = (pAlarmInfo->mo + '.' + pAlarmInfo->mp + '.' + to_string(pAlarmInfo->no + 1) + "#").c_str();
  1118. string up, momp_name, up_name;
  1119. string momp = pAlarmInfo->mo + "." + pAlarmInfo->mp;
  1120. CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, up, momp_name);
  1121. alarmDataSet.insert((up + tag + to_string((int)pAlarmInfo->type)).c_str());*/
  1122. //TODO 根据用户过滤
  1123. rapidjson::StringBuffer buffer;
  1124. auto ret = CResistAlarmMng::AlarmInfo2Pack(pAlarmInfo, buffer);
  1125. const char* output = buffer.GetString();
  1126. int len = buffer.GetLength();
  1127. auto send_len = mg_ws_send(c, output, len, WEBSOCKET_OP_TEXT);
  1128. if(c->fn_data) ((mg_per_session_data*)(c->fn_data))->send_size += send_len;
  1129. i++;
  1130. }
  1131. return 200;
  1132. }
  1133. bool CMGDataHandler::SendToAllClient(struct mg_connection* c, const char* ptr, size_t len)
  1134. {
  1135. if (g_bLog) CSimpleLog::Info(CString(ptr, len));
  1136. const auto& mgr = c->mgr;
  1137. for (auto it = mgr->conns; it; it = it->next)
  1138. {
  1139. if (it->is_listening == FALSE && it->is_websocket)
  1140. {
  1141. mg_ws_send(it, ptr, len, WEBSOCKET_OP_TEXT);
  1142. }
  1143. }
  1144. return true;
  1145. }
  1146. void CMGDataHandler::ThreadProcForQueryHist(LPMGHISTORY_QUERY history_query)
  1147. {
  1148. auto pDevice = CDeviceMng::Instance()->Find(history_query->imei);
  1149. if (pDevice == nullptr) return;
  1150. assert(pDevice);
  1151. auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
  1152. if (pInfo)
  1153. {
  1154. if (history_query->idx == 0)
  1155. 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);
  1156. else if (history_query->idx == 1)
  1157. 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);
  1158. else if (history_query->idx == 2)
  1159. 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);
  1160. }
  1161. delete history_query;
  1162. history_query = nullptr;
  1163. }
  1164. void CMGDataHandler::ThreadProcForQueryHistDB(LPMGHISTORY_QUERY history_query)
  1165. {
  1166. auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
  1167. if (pInfo)
  1168. {
  1169. pInfo->SendHistResistDBForEcharts(history_query);
  1170. }
  1171. delete history_query;
  1172. history_query = nullptr;
  1173. }