MGDataHandler.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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)
  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. auto obj = yyjson_mut_obj(doc);
  560. yyjson_mut_obj_add_str(doc, obj, "name", "up_alarm_low_limit");
  561. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_alarm_low_limit).c_str());
  562. yyjson_mut_arr_add_val(conf, obj);
  563. }
  564. {
  565. auto obj = yyjson_mut_obj(doc);
  566. yyjson_mut_obj_add_str(doc, obj, "name", "up_warn_low_limit");
  567. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_warn_low_limit).c_str());
  568. yyjson_mut_arr_add_val(conf, obj);
  569. }
  570. {
  571. auto obj = yyjson_mut_obj(doc);
  572. yyjson_mut_obj_add_str(doc, obj, "name", "dw_alarm_high_limit");
  573. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_alarm_high_limit).c_str());
  574. yyjson_mut_arr_add_val(conf, obj);
  575. }
  576. {
  577. auto obj = yyjson_mut_obj(doc);
  578. yyjson_mut_obj_add_str(doc, obj, "name", "dw_warn_high_limit");
  579. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_warn_high_limit).c_str());
  580. yyjson_mut_arr_add_val(conf, obj);
  581. }
  582. }
  583. else
  584. {
  585. }
  586. return 200;
  587. }
  588. }
  589. }
  590. }
  591. return 400;
  592. }
  593. else
  594. {
  595. return 500;
  596. }
  597. }
  598. int CMGDataHandler::HandleConfWrite(string tag, string type, string name, yyjson_val* conf, yyjson_mut_doc* doc, yyjson_mut_val* root)
  599. {
  600. //yyjson_mut_obj_add_str(doc, root, "cmd", "conf_write");
  601. time_t tt;
  602. time(&tt);
  603. int code = 500;
  604. if (type.compare("monitor.alarm.max_over_limit") == 0)
  605. {
  606. int nPos = tag.find('.');
  607. if (nPos != -1)
  608. {
  609. auto mo = tag.substr(0, nPos);
  610. int nPos2 = tag.find('.', nPos + 1);
  611. if (nPos2 != -1)
  612. {
  613. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  614. int nPos3 = tag.find('.', nPos2 + 1);
  615. if (nPos3 != -1)
  616. {
  617. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  618. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  619. {
  620. int nNo = atoi(no.c_str()) - 1;
  621. bool enable = false;
  622. int alarm = INT_MAX, warn = INT_MAX, f_alarm = INT_MAX, f_warn = INT_MAX;
  623. yyjson_val* it = yyjson_arr_get_first(conf);
  624. int n = yyjson_arr_size(conf);
  625. for (int i = 0; i < n; i++)
  626. {
  627. auto sz_name = yyjson_get_str(yyjson_obj_get(it, "name"));
  628. auto sz_val = yyjson_get_str(yyjson_obj_get(it, "val"));
  629. if (sz_name == 0 || sz_val == 0)
  630. continue;
  631. //解析赋值过程
  632. {
  633. string name = sz_name;
  634. string val = sz_val;
  635. if (name.compare("enable") == 0)
  636. {
  637. if (val.compare("false") == 0)
  638. {
  639. enable = false;
  640. }
  641. else
  642. {
  643. enable = true;
  644. }
  645. }
  646. else if (name.compare("lock_alarm_high_limit") == 0)
  647. {
  648. alarm = atoi(val.c_str());
  649. }
  650. else if (name.compare("lock_warn_high_limit") == 0)
  651. {
  652. warn = atoi(val.c_str());
  653. }
  654. else if (name.compare("d_alarm_high_limit") == 0)
  655. {
  656. alarm = atoi(val.c_str());
  657. }
  658. else if (name.compare("d_warn_high_limit") == 0)
  659. {
  660. warn = atoi(val.c_str());
  661. }
  662. else if (name.compare("keep_alarm_high_limit") == 0)
  663. {
  664. f_alarm = atoi(val.c_str());
  665. }
  666. else if (name.compare("keep_warn_high_limit") == 0)
  667. {
  668. f_warn = atoi(val.c_str());
  669. }
  670. else if (name.compare("f_alarm_high_limit") == 0)
  671. {
  672. f_alarm = atoi(val.c_str());
  673. }
  674. else if (name.compare("f_warn_high_limit") == 0)
  675. {
  676. f_warn = atoi(val.c_str());
  677. }
  678. }
  679. it = unsafe_yyjson_get_next(it);
  680. }
  681. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
  682. auto pInfo = (MAX_OVER_LIMIT_INFO*)pBase;
  683. if (pBase == nullptr) pInfo = new MAX_OVER_LIMIT_INFO;
  684. pInfo->enable = enable;
  685. pInfo->no = nNo;
  686. pInfo->type = eZL_ALARMTYPE::MAX_OVER_LIMIT;
  687. pInfo->alarm_high_limit = alarm;
  688. pInfo->warn_high_limit = warn;
  689. pInfo->f_alarm_high_limit = f_alarm;
  690. pInfo->f_warn_high_limit = f_warn;
  691. //记录
  692. {
  693. string station, mompname, moname, mpname, name1, name2, name3;
  694. CMonitorObjectMng::Instance()->GetStationNameByMomP(mo, mp, station, mompname);
  695. CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
  696. int nPos = mompname.find(".");
  697. if (nPos != -1)
  698. {
  699. moname = mompname.substr(0, nPos);
  700. mpname = mompname.substr(nPos + 1, mompname.length() - nPos - 1);
  701. }
  702. 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,
  703. fmt::format("用户[{}]配置了[{}][{}][{}]最大值超限报警", name, mompname, nNo == 0 ? name1 : nNo == 1 ? name2 : name3, enable ? "开" : "关"));
  704. }
  705. //生成字符串
  706. {
  707. auto conf_doc = yyjson_mut_doc_new(nullptr);
  708. auto conf_root = yyjson_val_mut_copy(conf_doc, conf);
  709. yyjson_mut_doc_set_root(conf_doc, conf_root);
  710. size_t ll;
  711. auto json = yyjson_mut_write(conf_doc, 0, &ll);
  712. assert(json);
  713. do
  714. {
  715. if (pBase == nullptr)
  716. {
  717. auto ret = CResistAlarmMng::Instance()->Insert(mo, mp, nNo, (int)eZL_ALARMTYPE::MAX_OVER_LIMIT, pInfo);
  718. assert(ret);
  719. if (ret == false)
  720. {
  721. delete pInfo;
  722. break;
  723. }
  724. //add
  725. CString sql;
  726. sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')",
  727. mo.c_str(), mp.c_str(), nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT, json, 0);
  728. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  729. {
  730. ASSERT(FALSE);
  731. CSimpleLog::Error("语句执行失败" + sql);
  732. break;
  733. }
  734. code = 200;
  735. }
  736. else
  737. {
  738. //update
  739. CString sql;
  740. sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
  741. json, mo.c_str(), mp.c_str(), nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
  742. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  743. {
  744. ASSERT(FALSE);
  745. CSimpleLog::Error("语句执行失败" + sql);
  746. break;
  747. }
  748. code = 200;
  749. }
  750. } while (false);
  751. if (json) free((void*)json);
  752. yyjson_mut_doc_free(conf_doc);
  753. }
  754. }
  755. }
  756. }
  757. }
  758. else code = 400;
  759. }
  760. else if (type.compare("monitor.resist.rename") == 0)
  761. {
  762. if (yyjson_arr_size(conf) != 3) return 400;
  763. int nPos = tag.find('.');
  764. if (nPos != -1)
  765. {
  766. auto mo = tag.substr(0, nPos);
  767. auto mp = tag.substr(nPos + 1, tag.length() - nPos - 1);
  768. //update
  769. auto name1 = yyjson_get_str(yyjson_arr_get(conf, 0));
  770. auto name2 = yyjson_get_str(yyjson_arr_get(conf, 1));
  771. auto name3 = yyjson_get_str(yyjson_arr_get(conf, 2));
  772. if (name1 == nullptr || name2 == nullptr || name3 == nullptr)
  773. return 400;
  774. CMonitorObjectMng::Instance()->SetNameByMoMp(tag, string(name1), string(name2), string(name3));
  775. //save
  776. CString sql;
  777. sql.Format("update rm_map set name1='%s',name2='%s',name3='%s' where mo = '%s' and mp = '%s';",
  778. name1, name2, name3, mo.c_str(), mp.c_str());
  779. auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
  780. if (false == ret)
  781. {
  782. CSimpleLog::Error("语句执行错误." + sql);
  783. return 500;
  784. }
  785. return 200;
  786. }
  787. return 400;
  788. }
  789. else if (type.compare("monitor.switch_direct.rename") == 0)
  790. {
  791. if (yyjson_arr_size(conf) != 2) return 400;
  792. int nPos = tag.find('.');
  793. if (nPos != -1)
  794. {
  795. auto mo = tag.substr(0, nPos);
  796. auto mp = tag.substr(nPos + 1, tag.length() - nPos - 1);
  797. //update
  798. auto direct1 = yyjson_get_str(yyjson_arr_get(conf, 0));
  799. auto direct2 = yyjson_get_str(yyjson_arr_get(conf, 1));
  800. if (direct1 == nullptr || direct2 == nullptr)
  801. return 400;
  802. CMonitorObjectMng::Instance()->SetDirectByMoMp(tag, string(direct1), string(direct2));
  803. //save
  804. CString sql;
  805. sql.Format("update rm_map set direct1='%s',direct2='%s' where mo = '%s' and mp = '%s';",
  806. direct1, direct2, mo.c_str(), mp.c_str());
  807. auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql);
  808. if (false == ret)
  809. {
  810. CSimpleLog::Error("语句执行错误." + sql);
  811. return 500;
  812. }
  813. return 200;
  814. }
  815. return 400;
  816. }
  817. else if (type.compare("monitor.alarm.friction_over_limit") == 0)
  818. {
  819. auto eType = eZL_ALARMTYPE::FRICTION_OVER_LIMIT;
  820. int nPos = tag.find('.');
  821. if (nPos != -1)
  822. {
  823. auto mo = tag.substr(0, nPos);
  824. int nPos2 = tag.find('.', nPos + 1);
  825. if (nPos2 != -1)
  826. {
  827. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  828. int nPos3 = tag.find('.', nPos2 + 1);
  829. if (nPos3 != -1)
  830. {
  831. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  832. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  833. {
  834. int nNo = atoi(no.c_str()) - 1;
  835. if (nNo != 2) return 400;
  836. bool enable = false;
  837. int up_alarm_low_limit = INT_MAX;
  838. int up_warn_low_limit = INT_MAX;
  839. int dw_alarm_high_limit = INT_MIN;
  840. int dw_warn_high_limit = INT_MIN;
  841. yyjson_val* it = yyjson_arr_get_first(conf);
  842. int n = yyjson_arr_size(conf);
  843. for (int i = 0; i < n; i++)
  844. {
  845. auto sz_name = yyjson_get_str(yyjson_obj_get(it, "name"));
  846. auto sz_val = yyjson_get_str(yyjson_obj_get(it, "val"));
  847. if (sz_name == 0 || sz_val == 0)
  848. continue;
  849. //解析赋值过程
  850. {
  851. string name = sz_name;
  852. string val = sz_val;
  853. if (name.compare("enable") == 0)
  854. {
  855. if (val.compare("false") == 0)
  856. {
  857. enable = false;
  858. }
  859. else
  860. {
  861. enable = true;
  862. }
  863. }
  864. else if (name.compare("up_alarm_low_limit") == 0)
  865. {
  866. up_alarm_low_limit = atoi(val.c_str());
  867. }
  868. else if (name.compare("up_warn_low_limit") == 0)
  869. {
  870. up_warn_low_limit = atoi(val.c_str());
  871. }
  872. else if (name.compare("dw_alarm_high_limit") == 0)
  873. {
  874. dw_alarm_high_limit = atoi(val.c_str());
  875. }
  876. else if (name.compare("dw_warn_high_limit") == 0)
  877. {
  878. dw_warn_high_limit = atoi(val.c_str());
  879. }
  880. }
  881. it = unsafe_yyjson_get_next(it);
  882. }
  883. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eType);
  884. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBase;
  885. if (pBase == nullptr) pInfo = new FRICTION_OVER_LIMIT_INFO;
  886. pInfo->enable = enable;
  887. pInfo->no = nNo;
  888. pInfo->type = eType;
  889. pInfo->up_alarm_low_limit = up_alarm_low_limit;
  890. pInfo->up_warn_low_limit = up_warn_low_limit;
  891. pInfo->dw_alarm_high_limit = dw_alarm_high_limit;
  892. pInfo->dw_warn_high_limit = dw_warn_high_limit;
  893. //记录
  894. {
  895. string station, mompname, moname, mpname, name1, name2, name3;
  896. CMonitorObjectMng::Instance()->GetStationNameByMomP(mo, mp, station, mompname);
  897. CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
  898. int nPos = mompname.find(".");
  899. if (nPos != -1)
  900. {
  901. moname = mompname.substr(0, nPos);
  902. mpname = mompname.substr(nPos + 1, mompname.length() - nPos - 1);
  903. }
  904. 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,
  905. fmt::format("用户[{}]配置了[{}][{}][{}]摩擦力超限报警", name, mompname, nNo == 0 ? name1 : nNo == 1 ? name2 : name3, enable ? "开" : "关"));
  906. }
  907. //生成字符串
  908. //生成字符串
  909. {
  910. auto conf_doc = yyjson_mut_doc_new(nullptr);
  911. auto conf_root = yyjson_val_mut_copy(conf_doc, conf);
  912. yyjson_mut_doc_set_root(conf_doc, conf_root);
  913. size_t ll;
  914. auto json = yyjson_mut_write(conf_doc, 0, &ll);
  915. assert(json);
  916. do
  917. {
  918. if (pBase == nullptr)
  919. {
  920. auto ret = CResistAlarmMng::Instance()->Insert(mo, mp, nNo, (int)eType, pInfo);
  921. assert(ret);
  922. if (ret == false)
  923. {
  924. delete pInfo;
  925. break;
  926. }
  927. //add
  928. CString sql;
  929. sql.Format("INSERT INTO [rm_alarm_set]([mo],[mp],[no],[type],[conf],[time]) VALUES ('%s','%s',%d,%d,'%s','%I64u')",
  930. mo.c_str(), mp.c_str(), nNo, eType, json, 0);
  931. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  932. {
  933. ASSERT(FALSE);
  934. CSimpleLog::Error("语句执行失败" + sql);
  935. break;
  936. }
  937. code = 200;
  938. }
  939. else
  940. {
  941. //update
  942. CString sql;
  943. sql.Format("update rm_alarm_set SET conf = '%s' WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
  944. json, mo.c_str(), mp.c_str(), nNo, eType);
  945. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  946. {
  947. ASSERT(FALSE);
  948. CSimpleLog::Error("语句执行失败" + sql);
  949. break;
  950. }
  951. code = 200;
  952. }
  953. } while (false);
  954. if (json) free((void*)json);
  955. yyjson_mut_doc_free(conf_doc);
  956. }
  957. }
  958. }
  959. }
  960. }
  961. else code = 400;
  962. }
  963. return code;
  964. }
  965. 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)
  966. {
  967. int npos = tag.rfind('.');
  968. string mo_mp = tag.substr(0, npos);
  969. string type = tag.substr(npos + 1);
  970. if (mo_mp.compare("undefined") == 0)
  971. {
  972. CSimpleLog::Error("[前端]收到未定义的请求");
  973. return 401;
  974. }
  975. if (type.compare("resist") == 0) //阻力数据
  976. {
  977. CTime ctStart, ctEnd;
  978. int pos = query_time.find('~');
  979. if (pos == -1) return 400;
  980. string start = query_time.substr(0, pos);
  981. string end = query_time.substr(pos + 1);
  982. try
  983. {
  984. int year, month, day, hour, minute, second;
  985. sscanf_s(start.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
  986. ctStart = CTime(year, month, day, hour, minute, second);
  987. sscanf_s(end.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
  988. ctEnd = CTime(year, month, day, hour, minute, second);
  989. }
  990. catch (CException*)
  991. {
  992. return 400;
  993. }
  994. string imei_idx;
  995. if (!CMonitorObjectMng::Instance()->MOMP2IMEI(mo_mp, imei_idx)) return 404;
  996. string imei, idx;
  997. if (!CMonitorObjectMng::Instance()->spiltByPoint(imei_idx, imei, idx)) return 400;
  998. int index = atoi(idx.c_str());
  999. auto pDevice = CDeviceMng::Instance()->Find(imei);
  1000. if (pDevice == nullptr) return 404;
  1001. pInfo->bWork = false;
  1002. if (pInfo->thread_hist)
  1003. {
  1004. pInfo->thread_hist->join();
  1005. delete pInfo->thread_hist;
  1006. pInfo->thread_hist = nullptr;
  1007. }
  1008. LPMGHISTORY_QUERY query_hist = new MG_HISTORY_QUERY;
  1009. query_hist->idx = index;
  1010. query_hist->imei = imei;
  1011. query_hist->mo_mp = mo_mp;
  1012. time_t tmStart = ctStart.GetTime();
  1013. query_hist->tmStart = tmStart * 1000;
  1014. query_hist->tmEnd = ctEnd.GetTime() * 1000;
  1015. query_hist->type = "resist";
  1016. query_hist->c = c;
  1017. query_hist->subsection = subsection;
  1018. pInfo->bWork = true;
  1019. time_t tmNow;
  1020. time(&tmNow);
  1021. //if (tmStart < g_stStart - 7200 || tmNow - tmStart > MAX_SAVE_TIME / 1000)
  1022. {
  1023. //超过当天,赋值当天 临时
  1024. //auto end = CTime(ctStart.GetYear(), ctStart.GetMonth(), ctStart.GetDay(), 23, 59, 59);
  1025. //改为限定24小时
  1026. auto end = ctStart + CTimeSpan(1, 0, 0, 0);
  1027. if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
  1028. pInfo->thread_hist = new thread(ThreadProcForQueryHistDB, query_hist);
  1029. }
  1030. //else
  1031. //{
  1032. // //实时也改为限定24小时
  1033. // auto end = ctStart + CTimeSpan(1, 0, 0, 0);
  1034. // if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
  1035. // pInfo->thread_hist = new thread(ThreadProcForQueryHist, query_hist);
  1036. //}
  1037. return 200;
  1038. }
  1039. return 400;
  1040. }
  1041. int CMGDataHandler::HandleAlarmAck(uint32_t alarm_id, string ack_name, yyjson_mut_doc* doc, yyjson_mut_val* root)
  1042. {
  1043. //yyjson_mut_obj_add_str(doc, root, "cmd", "alm_ack");
  1044. CTime ctNow = CTime::GetCurrentTime();
  1045. //删除内存数据
  1046. bool ret = CResistAlarmMng::Instance()->AckAlarm(alarm_id, ack_name, ctNow);
  1047. string ack_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
  1048. //更新数据库
  1049. {
  1050. CString sql;
  1051. sql.Format("UPDATE [rm_alarm] SET ack_result = 1, ack_name='%s', ack_time='%s' WHERE ID = %d;",
  1052. ack_name.c_str(), ack_time.c_str(), alarm_id);
  1053. ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1054. }
  1055. //记录
  1056. {
  1057. ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), ack_name, "", eRocord_Opt::RO_OPT,
  1058. fmt::format("用户[{}]确认id为[{}]的报警记录", ack_name, alarm_id));
  1059. }
  1060. if (ret)
  1061. {
  1062. yyjson_mut_obj_add_strcpy(doc, root, "ack_time", ack_time.c_str());
  1063. yyjson_mut_obj_add_strcpy(doc, root, "ack_name", ANSItoUTF8(ack_name).c_str());
  1064. return 200;
  1065. }
  1066. return 400;
  1067. }
  1068. int CMGDataHandler::HandleAlarmHandle(uint32_t alarm_id, string handle_name, string handle_info, yyjson_mut_doc* doc, yyjson_mut_val* root)
  1069. {
  1070. //yyjson_mut_obj_add_str(doc, root, "cmd", "alm_handle");
  1071. CTime ctNow = CTime::GetCurrentTime();
  1072. //删除内存数据
  1073. bool ret = CResistAlarmMng::Instance()->HandleAlarm(alarm_id);
  1074. string handle_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
  1075. //更新数据库
  1076. {
  1077. CString sql;
  1078. sql.Format("UPDATE [rm_alarm] SET handle_result = 1, handle_name='%s', handle_time='%s', handle_info='%s' WHERE ID = %d;",
  1079. handle_name.c_str(), handle_time.c_str(), handle_info.c_str(), alarm_id);
  1080. ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1081. }
  1082. //记录
  1083. {
  1084. ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), handle_name, "", eRocord_Opt::RO_OPT,
  1085. fmt::format("用户[{}]处理id为[{}]的报警记录", handle_name, alarm_id));
  1086. }
  1087. if (ret)
  1088. {
  1089. yyjson_mut_obj_add_strcpy(doc, root, "handle_time", handle_time.c_str());
  1090. yyjson_mut_obj_add_strcpy(doc, root, "handle_name", ANSItoUTF8(handle_name).c_str());
  1091. yyjson_mut_obj_add_strcpy(doc, root, "hanlde_info", ANSItoUTF8(handle_info).c_str());
  1092. return 200;
  1093. }
  1094. return 400;
  1095. }
  1096. int CMGDataHandler::SendUnAckAlarm(mg_connection* c)
  1097. {
  1098. lock_guard<mutex> lock(CResistAlarmMng::Instance()->m_mtxAlarm);
  1099. auto it = CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.crbegin();
  1100. for (int i = 0; i < 10 && it != CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.crend(); it++)
  1101. {
  1102. auto& pAlarmInfo = *it;
  1103. if (pAlarmInfo->ack_result != 0) continue;//未受理列表
  1104. //TODO 根据用户过滤
  1105. rapidjson::StringBuffer buffer;
  1106. auto ret = CResistAlarmMng::AlarmInfo2Pack(pAlarmInfo, buffer);
  1107. const char* output = buffer.GetString();
  1108. int len = buffer.GetLength();
  1109. auto send_len = mg_ws_send(c, output, len, WEBSOCKET_OP_TEXT);
  1110. if(c->fn_data) ((mg_per_session_data*)(c->fn_data))->send_size += send_len;
  1111. i++;
  1112. }
  1113. return 200;
  1114. }
  1115. bool CMGDataHandler::SendToAllClient(struct mg_connection* c, const char* ptr, size_t len)
  1116. {
  1117. if (g_bLog) CSimpleLog::Info(CString(ptr, len));
  1118. const auto& mgr = c->mgr;
  1119. for (auto it = mgr->conns; it; it = it->next)
  1120. {
  1121. if (it->is_listening == FALSE && it->is_websocket)
  1122. {
  1123. mg_ws_send(it, ptr, len, WEBSOCKET_OP_TEXT);
  1124. }
  1125. }
  1126. return true;
  1127. }
  1128. void CMGDataHandler::ThreadProcForQueryHist(LPMGHISTORY_QUERY history_query)
  1129. {
  1130. auto pDevice = CDeviceMng::Instance()->Find(history_query->imei);
  1131. if (pDevice == nullptr) return;
  1132. assert(pDevice);
  1133. auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
  1134. if (pInfo)
  1135. {
  1136. if (history_query->idx == 0)
  1137. 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);
  1138. else if (history_query->idx == 1)
  1139. 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);
  1140. else if (history_query->idx == 2)
  1141. 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);
  1142. }
  1143. delete history_query;
  1144. history_query = nullptr;
  1145. }
  1146. void CMGDataHandler::ThreadProcForQueryHistDB(LPMGHISTORY_QUERY history_query)
  1147. {
  1148. auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
  1149. if (pInfo)
  1150. {
  1151. pInfo->SendHistResistDBForEcharts(history_query);
  1152. }
  1153. delete history_query;
  1154. history_query = nullptr;
  1155. }