MGDataHandler.cpp 39 KB

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