MGDataHandler.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 "Device.h"
  9. #include "HttpPrcess.h"
  10. #include "ResistAlarm.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. default: return "OK";
  30. }
  31. }
  32. CMGDataHandler::CMGDataHandler()
  33. {
  34. }
  35. CMGDataHandler::~CMGDataHandler()
  36. {
  37. }
  38. BOOL CMGDataHandler::HandlerData(const char* ptr, size_t len, char** json)
  39. {
  40. return FALSE;
  41. }
  42. size_t CMGDataHandler::HandlerData(struct mg_connection* c, struct mg_ws_message* wm, char** json)
  43. {
  44. size_t len = 0;
  45. auto doc = yyjson_read(wm->data.ptr, wm->data.len, 0);
  46. if (doc == nullptr) return len;
  47. auto root = yyjson_doc_get_root(doc);
  48. auto res_doc = yyjson_mut_doc_new(nullptr);
  49. auto res_root = yyjson_mut_obj(res_doc);
  50. yyjson_mut_doc_set_root(res_doc, res_root);
  51. auto pConfInfo = (mg_per_session_data*)(c->fn_data);
  52. int code = 501; //未实现
  53. do
  54. {
  55. if (FALSE == yyjson_is_obj(root))
  56. {
  57. code = 400;
  58. break;
  59. }
  60. auto sz_cmd = yyjson_get_str(yyjson_obj_get(root, "cmd"));
  61. if (sz_cmd == 0)
  62. break;
  63. string cmd = sz_cmd;
  64. if (g_bLog)
  65. {
  66. char ip[50];
  67. auto gbk = UTF8toANSI(string(wm->data.ptr, wm->data.len));
  68. SPDLOG_INFO(mg_straddr(&c->rem, ip, 50) + gbk);
  69. }
  70. if (cmd.compare("heartbeat.ping") == 0)//心跳包
  71. {
  72. yyjson_mut_obj_add_str(res_doc, res_root, "cmd", "heartbeat.pong");
  73. SYSTEMTIME tm;
  74. GetLocalTime(&tm);
  75. char time[50];
  76. sprintf_s(time, 50, "%04d-%02d-%02d %02d:%02d:%02d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
  77. yyjson_mut_obj_add_strcpy(res_doc, res_root, "time", time);
  78. code = 200;
  79. break;
  80. }
  81. yyjson_mut_obj_add_strcpy(res_doc, res_root, "cmd", cmd.c_str()); //回包带命令
  82. if (cmd.compare("sub_notify") == 0)//实时订阅包
  83. {
  84. auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  85. if (sz_tag == 0)
  86. {
  87. code = 400;
  88. break;
  89. }
  90. string tag = sz_tag;
  91. int npos = tag.rfind('.');
  92. if (npos == -1)
  93. {
  94. code = 400;
  95. break;
  96. }
  97. string momp = tag.substr(0, npos);
  98. string type = tag.substr(npos + 1);
  99. if (type.compare("resist") == 0)
  100. {
  101. if (pConfInfo->isLogin == FALSE)
  102. {
  103. code = 401;
  104. break;
  105. }
  106. //if (!CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei_idx))
  107. //{
  108. // code = 404;
  109. // break;
  110. //}
  111. pConfInfo->m_lstSubReal.push_back(momp);
  112. if (CAppService::Instance()->GetHandle())
  113. CAppService::Instance()->GetHandle()->SendSubNotify(momp); //通知
  114. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag.c_str());
  115. code = 200;
  116. //string up, momp_name;
  117. //CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, up, momp_name);
  118. break;
  119. }
  120. code = 400;
  121. }
  122. else if (cmd.compare("login") == 0) //登录包
  123. {
  124. auto token = yyjson_get_str(yyjson_obj_get(root, "token"));
  125. if (token == 0)
  126. {
  127. code = 400;
  128. break;
  129. }
  130. if (strcmp(token, "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") == 0)
  131. {
  132. pConfInfo->token = token;
  133. pConfInfo->username = "system";
  134. pConfInfo->name = "系统管理员";
  135. pConfInfo->node = "100000";
  136. pConfInfo->node_name = "国铁集团";
  137. pConfInfo->isLogin = true;
  138. code = 200;
  139. SendUnAckAlarm(c);
  140. break;
  141. }
  142. CString sql;
  143. 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);
  144. COdbcStatement stmt;
  145. if (FALSE == CDBConnectPool::Instance()->DBQuery(stmt, sql))
  146. {
  147. SPDLOG_ERROR("[前端]查询语句出错:{}", sql);
  148. code = 400;
  149. break;
  150. }
  151. char username[50];
  152. char name[50];
  153. char node[50];
  154. char node_name[50];
  155. int nCol = 1;
  156. stmt.BindCharCol(nCol++, username, sizeof(username));
  157. stmt.BindCharCol(nCol++, name, sizeof(name));
  158. stmt.BindCharCol(nCol++, node, sizeof(node));
  159. stmt.BindCharCol(nCol++, node_name, sizeof(node_name));
  160. if (stmt.FetchNext() != 0)
  161. {
  162. code = 401;
  163. break;
  164. }
  165. pConfInfo->token = token;
  166. pConfInfo->username = username;
  167. pConfInfo->name = name;
  168. pConfInfo->node = node;
  169. pConfInfo->node_name = node_name;
  170. pConfInfo->isLogin = true;
  171. SendUnAckAlarm(c);
  172. code = 200;
  173. break;
  174. }
  175. else if (cmd.compare("unsub_notify") == 0)
  176. {
  177. auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  178. if (sz_tag == 0)
  179. {
  180. code = 400;
  181. break;
  182. }
  183. string tag = sz_tag;
  184. int npos = tag.rfind('.');
  185. if (npos == -1)
  186. {
  187. code = 400;
  188. break;
  189. }
  190. string momp = tag.substr(0, npos);
  191. string type = tag.substr(npos + 1);
  192. if (type.compare("resist") == 0)
  193. {
  194. if (pConfInfo->isLogin == FALSE)
  195. {
  196. code = 401;
  197. break;
  198. }
  199. //if (!CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei_idx))
  200. //{
  201. // code = 404;
  202. // break;
  203. //}
  204. for (auto it = pConfInfo->m_lstSubReal.begin(); it != pConfInfo->m_lstSubReal.end();)
  205. {
  206. if (it->compare(momp) == 0)
  207. {
  208. it = pConfInfo->m_lstSubReal.erase(it);
  209. code = 200;
  210. continue;
  211. }
  212. it++;
  213. }
  214. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag.c_str());
  215. code = 200;
  216. break;
  217. }
  218. code = 400;
  219. }
  220. else if (cmd.compare("query_alm_unack") == 0)
  221. {
  222. if (pConfInfo->isLogin == false)
  223. {
  224. code = 401; break;
  225. }
  226. code = SendUnAckAlarm(c);
  227. }
  228. else if (cmd.compare("query_hist") == 0){
  229. auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  230. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
  231. auto time = yyjson_get_str(yyjson_obj_get(root, "time"));
  232. if (tag == 0 || time == 0) { code = 400; break; }
  233. if (pConfInfo->isLogin == false) { code = 401; break; }
  234. auto yy_sub = yyjson_obj_get(root, "subsection");
  235. uint32_t subsection = 5000;
  236. if (yy_sub) subsection = yyjson_get_uint(yy_sub);
  237. code = HandleQueryHist(tag, time, subsection, c, pConfInfo, res_doc, res_root);
  238. }
  239. else if (cmd.compare("query_hist_confirm") == 0) //确认历史数据
  240. {
  241. auto sz_tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  242. if (sz_tag == 0)
  243. {
  244. code = 400;
  245. break;
  246. }
  247. string tag = sz_tag;
  248. int npos = tag.rfind('.');
  249. if (npos == -1)
  250. {
  251. code = 400;
  252. break;
  253. }
  254. string momp = tag.substr(0, npos);
  255. string type = tag.substr(npos + 1);
  256. if (type.compare("resist") == 0)
  257. {
  258. pConfInfo->bBlock = FALSE;
  259. code = 200;
  260. //不需要回包
  261. yyjson_mut_doc_free(res_doc);
  262. res_doc = nullptr;
  263. break;
  264. }
  265. code = 400;
  266. }
  267. else if (cmd.compare("conf_read") == 0) {
  268. auto tag = yyjson_get_str(yyjson_obj_get(root, "tag"));
  269. yyjson_mut_obj_add_strcpy(res_doc, res_root, "tag", tag);
  270. auto type = yyjson_get_str(yyjson_obj_get(root, "type"));
  271. yyjson_mut_obj_add_strcpy(res_doc, res_root, "type", type);
  272. if (type && strcmp(type, "log") == 0)
  273. {
  274. yyjson_mut_obj_add_bool(res_doc, res_root, "val", g_bLog);
  275. code = 200;
  276. break;
  277. }
  278. if (pConfInfo->isLogin == false) { code = 401; break; }
  279. if (tag == 0 || type == 0)
  280. {
  281. code = 400;
  282. break;
  283. }
  284. code = HandleConfRead(tag, type, res_doc, res_root);
  285. }
  286. else if (cmd.compare("alm_ack") == 0)
  287. {
  288. uint32_t alarm_id = yyjson_get_uint(yyjson_obj_get(root, "alarm_id"));
  289. yyjson_mut_obj_add_uint(res_doc, res_root, "alarm_id", alarm_id);
  290. if (pConfInfo->isLogin == false) { code = 401; break; }
  291. if (alarm_id == 0) { code = 400; break; }
  292. code = HandleAlarmAck(alarm_id, pConfInfo->name, res_doc, res_root);
  293. //回包改为群发
  294. if (code == 200)
  295. {
  296. yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
  297. auto c_json = yyjson_mut_write(res_doc, 0, &len);
  298. yyjson_mut_doc_free(res_doc);
  299. res_doc = nullptr;
  300. SendToAllClient(c, c_json, len);
  301. free((void*)c_json);
  302. }
  303. }
  304. else if (cmd.compare("alm_handle") == 0)
  305. {
  306. uint32_t alarm_id = yyjson_get_uint(yyjson_obj_get(root, "alarm_id"));
  307. yyjson_mut_obj_add_uint(res_doc, res_root, "alarm_id", alarm_id);
  308. auto hanlde_info = yyjson_get_str(yyjson_obj_get(root, "hanlde_info"));
  309. string strHandleInfo;
  310. if (hanlde_info) strHandleInfo = UTF8toANSI(hanlde_info);
  311. if (pConfInfo->isLogin == false) { code = 401; break; }
  312. if (alarm_id == 0) { code = 400; break; }
  313. code = HandleAlarmHandle(alarm_id, pConfInfo->name, strHandleInfo, res_doc, res_root);
  314. //回包改为群发
  315. if (code == 200)
  316. {
  317. yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
  318. auto c_json = yyjson_mut_write(res_doc, 0, &len);
  319. yyjson_mut_doc_free(res_doc);
  320. res_doc = nullptr;
  321. SendToAllClient(c, c_json, len);
  322. free((void*)c_json);
  323. }
  324. }
  325. else if (cmd.compare("test_alarm") == 0) //测试报警
  326. {
  327. yyjson_mut_doc_free(res_doc);
  328. res_doc = nullptr;
  329. {
  330. auto new_doc = yyjson_mut_doc_new(nullptr);
  331. auto new_root = yyjson_val_mut_copy(new_doc, root);
  332. yyjson_mut_doc_set_root(new_doc, new_root);
  333. yyjson_mut_obj_remove_str(new_root, "cmd");
  334. yyjson_mut_obj_add_str(new_doc, new_root, "cmd", "new_alarm");
  335. auto c_json = yyjson_mut_write(new_doc, 0, &len);
  336. yyjson_mut_doc_free(new_doc);
  337. new_doc = nullptr;
  338. SendToAllClient(c, c_json, len);
  339. free((void*)c_json);
  340. }
  341. }
  342. else if (cmd.compare("sync") == 0)
  343. {
  344. if (pConfInfo->token.compare("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") != 0)
  345. {
  346. code = 403;
  347. break;
  348. }
  349. auto ret = CMonitorObjectMng::Instance()->LoadMonitorTree();
  350. if (ret) ret = CMonitorObjectMng::Instance()->LoadHistoryData();
  351. if (ret) code = 200;
  352. else code = 500;
  353. }
  354. } while (FALSE);
  355. if (res_doc)
  356. {
  357. yyjson_mut_obj_add_int(res_doc, res_root, "code", code);
  358. yyjson_mut_obj_add_strcpy(res_doc, res_root, "msg", http_status_code_str(code));
  359. if (json) *json = yyjson_mut_write(res_doc, 0, &len);
  360. yyjson_mut_doc_free(res_doc);
  361. }
  362. yyjson_doc_free(doc);
  363. return len;
  364. }
  365. int CMGDataHandler::HandleConfRead(string tag, string type, yyjson_mut_doc* doc, yyjson_mut_val* root)
  366. {
  367. auto conf = yyjson_mut_arr(doc);
  368. yyjson_mut_obj_add_val(doc, root, "conf", conf);
  369. if (type.compare("monitor.alarm.max_over_limit") == 0) //最大值超限
  370. {
  371. int nPos = tag.find('.');
  372. if (nPos != -1)
  373. {
  374. auto mo = tag.substr(0, nPos);
  375. int nPos2 = tag.find('.', nPos + 1);
  376. if (nPos2 != -1)
  377. {
  378. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  379. int nPos3 = tag.find('.', nPos2 + 1);
  380. if (nPos3 != -1)
  381. {
  382. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  383. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  384. {
  385. int nNo = atoi(no.c_str()) - 1;
  386. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::MAX_OVER_LIMIT);
  387. if (pBase)
  388. {
  389. string name1, name2, name3;
  390. CMonitorObjectMng::Instance()->GetNameByMoMp(mo + "." + mp, name1, name2, name3);
  391. assert(pBase->type == eZL_ALARMTYPE::MAX_OVER_LIMIT);
  392. auto pInfo = (MAX_OVER_LIMIT_INFO*)pBase;
  393. {
  394. auto object = yyjson_mut_obj(doc);
  395. yyjson_mut_arr_add_val(conf, object);
  396. yyjson_mut_obj_add_str(doc, object, "name", "enable");
  397. yyjson_mut_obj_add_str(doc, object, "val", pInfo->enable ? "true" : "false");
  398. }
  399. if (pInfo->alarm_high_limit != SHORT_MAX)
  400. {
  401. auto object = yyjson_mut_obj(doc);
  402. yyjson_mut_arr_add_val(conf, object);
  403. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "d_alarm_high_limit");
  404. else
  405. {
  406. yyjson_mut_obj_add_str(doc, object, "name", "lock_alarm_high_limit");
  407. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  408. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  409. }
  410. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->alarm_high_limit).c_str());
  411. }
  412. if (pInfo->warn_high_limit != SHORT_MAX)
  413. {
  414. auto object = yyjson_mut_obj(doc);
  415. yyjson_mut_arr_add_val(conf, object);
  416. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "d_warn_high_limit");
  417. else
  418. {
  419. yyjson_mut_obj_add_str(doc, object, "name", "lock_warn_high_limit");
  420. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  421. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  422. }
  423. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->warn_high_limit).c_str());
  424. }
  425. if (pInfo->f_alarm_high_limit != SHORT_MAX)
  426. {
  427. auto object = yyjson_mut_obj(doc);
  428. yyjson_mut_arr_add_val(conf, object);
  429. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "f_alarm_high_limit");
  430. else
  431. {
  432. yyjson_mut_obj_add_str(doc, object, "name", "keep_alarm_high_limit");
  433. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  434. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  435. }
  436. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_alarm_high_limit).c_str());
  437. }
  438. if (pInfo->f_warn_high_limit != SHORT_MAX)
  439. {
  440. auto object = yyjson_mut_obj(doc);
  441. yyjson_mut_arr_add_val(conf, object);
  442. if (nNo == 2) yyjson_mut_obj_add_str(doc, object, "name", "f_warn_high_limit");
  443. else
  444. {
  445. yyjson_mut_obj_add_str(doc, object, "name", "keep_warn_high_limit");
  446. if (nNo == 0) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name1).c_str());
  447. else if (nNo == 1) yyjson_mut_obj_add_strcpy(doc, object, "label", ANSItoUTF8(name2).c_str());
  448. }
  449. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_warn_high_limit).c_str());
  450. }
  451. }
  452. else
  453. {
  454. }
  455. return 200;
  456. }
  457. }
  458. }
  459. }
  460. return 400;
  461. }
  462. else if (type.compare("monitor.resist.rename") == 0) //设置曲线别名
  463. {
  464. string name1, name2, name3;
  465. CMonitorObjectMng::Instance()->GetNameByMoMp(tag, name1, name2, name3);
  466. char utf[200];
  467. if (name1.length())
  468. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(name1).c_str());
  469. else
  470. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("1号测力曲线").c_str());
  471. if (name2.length())
  472. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(name2).c_str());
  473. else
  474. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("2号测力曲线").c_str());
  475. if (name3.length())
  476. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(name3).c_str());
  477. else
  478. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8("转换阻力曲线").c_str());
  479. return 200;
  480. }
  481. else if (type.compare("monitor.switch_direct.rename") == 0)
  482. {
  483. string direct1, direct2;
  484. CMonitorObjectMng::Instance()->GetDirectByMoMp(tag, direct1, direct2);
  485. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(direct1).c_str());
  486. yyjson_mut_arr_add_strcpy(doc, conf, ANSItoUTF8(direct2).c_str());
  487. return 200;
  488. }
  489. else if (type.compare("monitor.alarm.friction_over_limit") == 0) {
  490. int nPos = tag.find('.');
  491. if (nPos != -1)
  492. {
  493. auto mo = tag.substr(0, nPos);
  494. int nPos2 = tag.find('.', nPos + 1);
  495. if (nPos2 != -1)
  496. {
  497. auto mp = tag.substr(nPos + 1, nPos2 - nPos - 1);
  498. int nPos3 = tag.find('.', nPos2 + 1);
  499. if (nPos3 != -1)
  500. {
  501. auto no = tag.substr(nPos2 + 1, nPos3 - nPos2 - 1);
  502. if (mo.length() > 0 && mp.length() > 0 && no.length() > 0)
  503. {
  504. int nNo = atoi(no.c_str()) - 1;
  505. if (nNo != 2) return 400;
  506. auto pBase = CResistAlarmMng::Instance()->Find(mo, mp, nNo, eZL_ALARMTYPE::FRICTION_OVER_LIMIT);
  507. if (pBase)
  508. {
  509. assert(pBase->type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT);
  510. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBase;
  511. //换成 yyjson
  512. {
  513. auto obj = yyjson_mut_obj(doc);
  514. yyjson_mut_obj_add_str(doc, obj, "name", "enable");
  515. yyjson_mut_obj_add_str(doc, obj, "val", pInfo->enable ? "true" : "false");
  516. yyjson_mut_arr_add_val(conf, obj);
  517. }
  518. {
  519. auto obj = yyjson_mut_obj(doc);
  520. yyjson_mut_obj_add_str(doc, obj, "name", "up_alarm_low_limit");
  521. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_alarm_low_limit).c_str());
  522. yyjson_mut_arr_add_val(conf, obj);
  523. }
  524. {
  525. auto obj = yyjson_mut_obj(doc);
  526. yyjson_mut_obj_add_str(doc, obj, "name", "up_warn_low_limit");
  527. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->up_warn_low_limit).c_str());
  528. yyjson_mut_arr_add_val(conf, obj);
  529. }
  530. {
  531. auto obj = yyjson_mut_obj(doc);
  532. yyjson_mut_obj_add_str(doc, obj, "name", "dw_alarm_high_limit");
  533. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_alarm_high_limit).c_str());
  534. yyjson_mut_arr_add_val(conf, obj);
  535. }
  536. {
  537. auto obj = yyjson_mut_obj(doc);
  538. yyjson_mut_obj_add_str(doc, obj, "name", "dw_warn_high_limit");
  539. yyjson_mut_obj_add_strcpy(doc, obj, "val", to_string(pInfo->dw_warn_high_limit).c_str());
  540. yyjson_mut_arr_add_val(conf, obj);
  541. }
  542. }
  543. else
  544. {
  545. }
  546. return 200;
  547. }
  548. }
  549. }
  550. }
  551. return 400;
  552. }
  553. else
  554. {
  555. return 500;
  556. }
  557. }
  558. 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)
  559. {
  560. int npos = tag.rfind('.');
  561. string mo_mp = tag.substr(0, npos);
  562. string type = tag.substr(npos + 1);
  563. if (mo_mp.compare("undefined") == 0)
  564. {
  565. SPDLOG_ERROR("[前端]收到未定义的请求");
  566. return 401;
  567. }
  568. if (type.compare("resist") == 0) //阻力数据
  569. {
  570. CTime ctStart, ctEnd;
  571. int pos = query_time.find('~');
  572. if (pos == -1) return 400;
  573. string start = query_time.substr(0, pos);
  574. string end = query_time.substr(pos + 1);
  575. try
  576. {
  577. int year, month, day, hour, minute, second;
  578. auto num = sscanf_s(start.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
  579. if (num != 6) throw "time error.";
  580. ctStart = CTime(year, month, day, hour, minute, second);
  581. num = sscanf_s(end.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
  582. if (num != 6) throw "time error.";
  583. ctEnd = CTime(year, month, day, hour, minute, second);
  584. }
  585. catch (CException*)
  586. {
  587. return 400;
  588. }
  589. pInfo->bWork = false;
  590. if (pInfo->thread_hist)
  591. {
  592. pInfo->thread_hist->join();
  593. delete pInfo->thread_hist;
  594. pInfo->thread_hist = nullptr;
  595. }
  596. LPMGHISTORY_QUERY query_hist = new MG_HISTORY_QUERY;
  597. query_hist->mo_mp = mo_mp;
  598. time_t tmStart = ctStart.GetTime();
  599. query_hist->tmStart = tmStart * 1000;
  600. query_hist->tmEnd = ctEnd.GetTime() * 1000;
  601. query_hist->type = "resist";
  602. query_hist->c = c;
  603. query_hist->subsection = subsection;
  604. pInfo->bWork = true;
  605. time_t tmNow;
  606. time(&tmNow);
  607. //if (tmStart < g_stStart - 7200 || tmNow - tmStart > MAX_SAVE_TIME / 1000)
  608. {
  609. //超过当天,赋值当天 临时
  610. //auto end = CTime(ctStart.GetYear(), ctStart.GetMonth(), ctStart.GetDay(), 23, 59, 59);
  611. //改为限定24小时
  612. auto end = ctStart + CTimeSpan(1, 0, 0, 0);
  613. if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
  614. pInfo->thread_hist = new thread(ThreadProcForQueryHistDB, query_hist);
  615. }
  616. //else
  617. //{
  618. // //实时也改为限定24小时
  619. // auto end = ctStart + CTimeSpan(1, 0, 0, 0);
  620. // if (ctEnd > end) query_hist->tmEnd = end.GetTime() * 1000;
  621. // pInfo->thread_hist = new thread(ThreadProcForQueryHist, query_hist);
  622. //}
  623. return 200;
  624. }
  625. return 400;
  626. }
  627. int CMGDataHandler::HandleAlarmAck(uint32_t alarm_id, string ack_name, yyjson_mut_doc* doc, yyjson_mut_val* root)
  628. {
  629. //TODO
  630. /*
  631. CTime ctNow = CTime::GetCurrentTime();
  632. //删除内存数据
  633. bool ret = CResistAlarmMng::Instance()->AckAlarm(alarm_id, ack_name, ctNow);
  634. string ack_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
  635. //更新数据库
  636. {
  637. CString sql;
  638. sql.Format("UPDATE [rm_alarm] SET ack_result = 1, ack_name='%s', ack_time='%s' WHERE ID = %d;",
  639. ack_name.c_str(), ack_time.c_str(), alarm_id);
  640. ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
  641. }
  642. //记录
  643. {
  644. ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), ack_name, "", eRocord_Opt::RO_OPT,
  645. fmt::format("用户[{}]确认id为[{}]的报警记录", ack_name, alarm_id));
  646. }
  647. if (ret)
  648. {
  649. yyjson_mut_obj_add_strcpy(doc, root, "ack_time", ack_time.c_str());
  650. yyjson_mut_obj_add_strcpy(doc, root, "ack_name", ANSItoUTF8(ack_name).c_str());
  651. return 200;
  652. }
  653. */
  654. return 400;
  655. }
  656. int CMGDataHandler::HandleAlarmHandle(uint32_t alarm_id, string handle_name, string handle_info, yyjson_mut_doc* doc, yyjson_mut_val* root)
  657. {
  658. /*
  659. //yyjson_mut_obj_add_str(doc, root, "cmd", "alm_handle");
  660. CTime ctNow = CTime::GetCurrentTime();
  661. //删除内存数据
  662. bool ret = CResistAlarmMng::Instance()->HandleAlarm(alarm_id);
  663. string handle_time = ctNow.Format("%Y-%m-%d %H:%M:%S");
  664. //更新数据库
  665. {
  666. CString sql;
  667. sql.Format("UPDATE [rm_alarm] SET handle_result = 1, handle_name='%s', handle_time='%s', handle_info='%s' WHERE ID = %d;",
  668. handle_name.c_str(), handle_time.c_str(), handle_info.c_str(), alarm_id);
  669. ret |= CDBConnectPool::Instance()->DBExecuteSQL(sql);
  670. }
  671. //记录
  672. {
  673. ExecSqlForRecord(eRecord_Module::RM_ALARM, 0, 0, string(), string(), "", "", "", "", 0, 0, ctNow.GetTime(), handle_name, "", eRocord_Opt::RO_OPT,
  674. fmt::format("用户[{}]处理id为[{}]的报警记录", handle_name, alarm_id));
  675. }
  676. if (ret)
  677. {
  678. yyjson_mut_obj_add_strcpy(doc, root, "handle_time", handle_time.c_str());
  679. yyjson_mut_obj_add_strcpy(doc, root, "handle_name", ANSItoUTF8(handle_name).c_str());
  680. yyjson_mut_obj_add_strcpy(doc, root, "hanlde_info", ANSItoUTF8(handle_info).c_str());
  681. return 200;
  682. }*/
  683. return 400;
  684. }
  685. int CMGDataHandler::SendUnAckAlarm(mg_connection* c)
  686. {
  687. //lock_guard<mutex> lock(CResistAlarmMng::Instance()->m_mtxAlarm);
  688. //auto it = CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.crbegin();
  689. //for (int i = 0; i < 10 && it != CResistAlarmMng::Instance()->m_lstUnConfirmAlarm.crend(); i++, it++)
  690. //{
  691. // auto& pAlarmInfo = *it;
  692. // //TODO 根据用户过滤
  693. // rapidjson::StringBuffer buffer;
  694. // auto ret = CResistAlarmMng::AlarmInfo2Pack(pAlarmInfo, buffer);
  695. // const char* output = buffer.GetString();
  696. // int len = buffer.GetLength();
  697. // auto send_len = mg_ws_send(c, output, len, WEBSOCKET_OP_TEXT);
  698. // if(c->fn_data) ((mg_per_session_data*)(c->fn_data))->send_size += send_len;
  699. //}
  700. return 200;
  701. }
  702. bool CMGDataHandler::SendToAllClient(struct mg_connection* c, const char* ptr, size_t len)
  703. {
  704. SPDLOG_DEBUG("[WS]Send Data : {}", ptr);
  705. const auto& mgr = c->mgr;
  706. for (auto it = mgr->conns; it; it = it->next)
  707. {
  708. if (it->is_listening == FALSE && it->is_websocket)
  709. {
  710. char buf[40] = { 0 };
  711. SPDLOG_DEBUG("[WS]{}", mg_straddr(it->rem, buf, sizeof(buf)));
  712. mg_ws_send(it, ptr, len, WEBSOCKET_OP_TEXT);
  713. }
  714. }
  715. return true;
  716. }
  717. //void CMGDataHandler::ThreadProcForQueryHist(LPMGHISTORY_QUERY history_query)
  718. //{
  719. // auto pDevice = CDeviceMng::Instance()->Find(history_query->mo_mp);
  720. // if (pDevice == nullptr) return;
  721. // assert(pDevice);
  722. //
  723. // auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
  724. // if (pInfo)
  725. // {
  726. // if (history_query->idx == 0)
  727. // 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);
  728. // else if (history_query->idx == 1)
  729. // 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);
  730. // else if (history_query->idx == 2)
  731. // 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);
  732. //
  733. // }
  734. //
  735. // delete history_query;
  736. // history_query = nullptr;
  737. //}
  738. void CMGDataHandler::ThreadProcForQueryHistDB(LPMGHISTORY_QUERY history_query)
  739. {
  740. auto pInfo = (mg_per_session_data*)history_query->c->fn_data;
  741. if (pInfo)
  742. {
  743. pInfo->SendHistResistDBForEcharts(history_query);
  744. }
  745. delete history_query;
  746. history_query = nullptr;
  747. }