MGWSServer.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. #include "stdafx.h"
  2. #include "AppService.h"
  3. #include "MGWSServer.h"
  4. #include "MGDataHandler.h"
  5. #include "MonitorObject.h"
  6. #include <gbk2utf8.h>
  7. #include "AlarmDefine.h"
  8. static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data) {
  9. time_t tmNow;
  10. time(&tmNow);
  11. if (ev == MG_EV_POLL)
  12. {
  13. //time_t tNow;
  14. //time(&tNow);
  15. //TRACE("%d\r\n", tNow);
  16. //if (c->next)
  17. // mg_ws_send(c->next, nullptr, 0, WEBSOCKET_OP_PING);
  18. if (c->fn_data)
  19. {
  20. auto pMsg = (mg_per_session_data*)c->fn_data;
  21. if (tmNow - pMsg->tmLastSendHeart > 30)
  22. {
  23. pMsg->tmLastSendHeart = tmNow;
  24. mg_ws_send(c, nullptr, 0, WEBSOCKET_OP_PING);
  25. }
  26. }
  27. }
  28. /*if (ev == MG_EV_OPEN) {
  29. c->is_hexdumping = 1;
  30. }*/
  31. else if (ev == MG_EV_HTTP_MSG) {
  32. struct mg_http_message* hm = (struct mg_http_message*)ev_data;
  33. mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to WS
  34. }
  35. else if (ev == MG_EV_WS_MSG) {
  36. // Got websocket frame. Received data is wm->data. Echo it back!
  37. struct mg_ws_message* wm = (struct mg_ws_message*)ev_data;
  38. //mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
  39. char* json = nullptr;
  40. auto pHandler = CAppService::Instance()->GetMgServer()->m_pDataHanlder;
  41. if (pHandler)
  42. {
  43. size_t len = pHandler->HandlerData(c, wm, &json);
  44. if (json)
  45. {
  46. char ip[50] = { 0 };
  47. SPDLOG_DEBUG("[WS]{}send data:{}", mg_straddr(&c->rem, ip, 50), json);
  48. auto send_len = mg_ws_send(c, json, len, WEBSOCKET_OP_TEXT);
  49. ((mg_per_session_data*)c->fn_data)->send_size += send_len;
  50. ((mg_per_session_data*)c->fn_data)->send_count++;
  51. free((void*)json);
  52. }
  53. }
  54. }
  55. else if (ev == MG_EV_WS_OPEN)
  56. {
  57. //websocket 链接时 为每个链接创建用户数据
  58. if (c->fn_data == nullptr)
  59. c->fn_data = new mg_per_session_data;
  60. }
  61. else if (ev == MG_EV_WS_CTL)
  62. {
  63. struct mg_ws_message* wm = (struct mg_ws_message*)ev_data;
  64. auto op = wm->flags & 15;
  65. if (op == WEBSOCKET_OP_CLOSE)
  66. {
  67. //关闭消息 释放内存数据
  68. if (c->fn_data)
  69. {
  70. delete (mg_per_session_data*)(c->fn_data);
  71. c->fn_data = nullptr;
  72. }
  73. }
  74. else if (op == WEBSOCKET_OP_PONG)
  75. {
  76. if (c->fn_data) ((mg_per_session_data*)c->fn_data)->tmLastRecvHeart = tmNow;
  77. }
  78. }
  79. (void)fn_data;
  80. }
  81. CMGWSServer::CMGWSServer()
  82. {
  83. }
  84. CMGWSServer::~CMGWSServer()
  85. {
  86. }
  87. BOOL CMGWSServer::Start(uint16_t port)//10086
  88. {
  89. Stop();
  90. m_bThreadWork = TRUE;
  91. m_pThread = new std::thread(CMGWSServer::ThreadProc, (DWORD_PTR)this, port);
  92. Sleep(100);
  93. if (m_bThreadWork == FALSE)
  94. return FALSE;
  95. m_pDataHanlder = new CMGDataHandler();
  96. return TRUE;
  97. }
  98. void CMGWSServer::Stop()
  99. {
  100. m_bThreadWork = FALSE;
  101. if (m_pThread)
  102. {
  103. m_pThread->join();
  104. delete m_pThread;
  105. m_pThread = nullptr;
  106. }
  107. if (m_pDataHanlder)
  108. {
  109. delete m_pDataHanlder;
  110. m_pDataHanlder = nullptr;
  111. }
  112. }
  113. void CMGWSServer::SendRealResistData(const string& mo_mp, const int num, const std::vector<int>& vctData0, const std::vector<int>& vctData1, const std::vector<int>& vctData2, const std::vector<bool>& vctResult, const CTime& atime)
  114. {
  115. if (m_mg_mgr.conns->is_listening && m_mg_mgr.conns->next == nullptr) return;
  116. if (mo_mp.length() == 0)return;
  117. bool bSub = false;
  118. for (auto it = m_mg_mgr.conns; it; it = it->next)
  119. {
  120. if (it->is_listening) continue;
  121. const auto& pConfInfo = (mg_per_session_data*)it->fn_data;
  122. if (pConfInfo) {
  123. //if (pConfInfo->token.find("BBBBBBBB") != -1)
  124. //{
  125. // bSub = true; break;
  126. //}
  127. if (pConfInfo->isLogin == false) continue;
  128. for (const auto& ik : pConfInfo->m_lstSubReal)
  129. {
  130. if (ik.compare(mo_mp) == 0)
  131. {
  132. bSub = true;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. if (bSub == false) return; //所有客户端都未订阅
  139. char* json = nullptr;
  140. auto len = GeneralResistData(mo_mp, num, 0, vctData0, vctData1, vctData2, vctResult, atime, &json);
  141. if (len == 0) return;
  142. char ip[50];
  143. for (auto it = m_mg_mgr.conns; it; it = it->next)
  144. {
  145. if (it->is_accepted == false) continue;
  146. const auto& pConfInfo = (mg_per_session_data*)it->fn_data;
  147. if (pConfInfo) {
  148. //if (pConfInfo->token.find("BBBBBBBB") != -1)
  149. //{
  150. // SPDLOG_DEBUG("[WS]{}:{}", mg_straddr(&it->rem, ip, 50), json);
  151. // mg_ws_send(it, json, len, WEBSOCKET_OP_TEXT);
  152. // continue;
  153. //}
  154. if (pConfInfo->isLogin == false) continue;
  155. for (const auto& ik : pConfInfo->m_lstSubReal)
  156. {
  157. if (ik.compare(mo_mp) == 0)
  158. {
  159. SPDLOG_DEBUG("[WS]{}:{}", mg_straddr(&it->rem, ip, 50), json);
  160. mg_ws_send(it, json, len, WEBSOCKET_OP_TEXT);
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. void CMGWSServer::SendHumiTemp(string mo_mp, const COleDateTime& dt, int humi, int temp)
  168. {
  169. return;//数据中心屏蔽
  170. if (m_mg_mgr.conns->is_listening && m_mg_mgr.conns->next == nullptr) return;
  171. if (mo_mp.length() == 0)return;
  172. bool bSub = false;
  173. for (auto it = m_mg_mgr.conns; it; it = it->next)
  174. {
  175. if (it->is_listening) continue;
  176. const auto& pConfInfo = (mg_per_session_data*)it->fn_data;
  177. if (pConfInfo) {
  178. if (pConfInfo->token.find("BBBBBBBB") != -1)
  179. {
  180. bSub = true; break;
  181. }
  182. }
  183. }
  184. //所有客户端都未订阅
  185. if (!bSub) return;
  186. auto doc = yyjson_mut_doc_new(nullptr);
  187. auto root = yyjson_mut_obj(doc);
  188. yyjson_mut_doc_set_root(doc, root);
  189. yyjson_mut_obj_add_str(doc, root, "cmd", "new_data_notify");
  190. yyjson_mut_obj_add_strcpy(doc, root, "time", dt.Format("%Y-%m-%d %H:%M:%S"));
  191. yyjson_mut_obj_add_int(doc, root, "humi", humi / 100);
  192. yyjson_mut_obj_add_int(doc, root, "temp", temp);
  193. yyjson_mut_obj_add_strcpy(doc, root, "tag", (mo_mp + ".humi_temp").c_str());
  194. size_t len;
  195. auto json = yyjson_mut_write(doc, 0, &len);
  196. if (json)
  197. {
  198. for (auto it = m_mg_mgr.conns; it; it = it->next)
  199. {
  200. if (it->is_accepted == false) continue;
  201. const auto& pConfInfo = (mg_per_session_data*)it->fn_data;
  202. if (pConfInfo) {
  203. if (pConfInfo->token.find("BBBBBBBB") != -1)
  204. {
  205. mg_ws_send(it, json, len, WEBSOCKET_OP_TEXT);
  206. }
  207. }
  208. }
  209. free((void*)json);
  210. }
  211. }
  212. void CMGWSServer::SendToAllClient(const char* ptr, size_t len)
  213. {
  214. SPDLOG_DEBUG("SendToAllClient:{}", ptr);
  215. const auto& mgr = m_mg_mgr;
  216. for (auto it = mgr.conns; it; it = it->next)
  217. {
  218. if (it->is_listening == FALSE && it->is_websocket)
  219. {
  220. mg_ws_send(it, ptr, len, WEBSOCKET_OP_TEXT);
  221. }
  222. }
  223. }
  224. void CMGWSServer::SendToClient(const char* ptr, const size_t len, const char* ip)
  225. {
  226. SPDLOG_DEBUG("Send Client {}:{}", ip, ptr);
  227. const auto& mgr = m_mg_mgr;
  228. mg_str ip_str = {ip, strlen(ip)};
  229. mg_addr ip_addr;
  230. mg_aton(ip_str, &ip_addr);
  231. for (auto it = mgr.conns; it; it = it->next)
  232. {
  233. if (it->is_listening == FALSE && it->is_websocket && it->rem.ip == ip_addr.ip)
  234. {
  235. mg_ws_send(it, ptr, len, WEBSOCKET_OP_TEXT);
  236. }
  237. }
  238. }
  239. void CMGWSServer::ThreadProc(DWORD_PTR wparam, uint16_t lparam)
  240. {
  241. CMGWSServer* pThis = (CMGWSServer*)wparam;
  242. if (!pThis->m_bThreadWork) return;
  243. mg_mgr_init(&pThis->m_mg_mgr);
  244. char url[128];
  245. sprintf_s(url, 128, "ws://0.0.0.0:%d", lparam);
  246. auto pConn = mg_http_listen(&pThis->m_mg_mgr, url, fn, nullptr);//用户自定义数据赋值为空
  247. if (pConn == nullptr || pConn->is_closing)
  248. SPDLOG_ERROR("开启Webscoket端口 {} 失败!", lparam);
  249. if (pConn) do { mg_mgr_poll(&pThis->m_mg_mgr, 100); } while (pThis->m_bThreadWork);
  250. mg_mgr_free(&pThis->m_mg_mgr);
  251. pThis->m_bThreadWork = FALSE;
  252. }
  253. int CMGWSServer::GeneralResistData(const string mo_mp, const int num, const int index, const std::vector<int>& vctData0, const std::vector<int>& vctData1, const std::vector<int>& vctData2, const std::vector<bool>& vctResult, const CTime& atime, char** json)
  254. {
  255. size_t json_len = 0;
  256. char sz_utf_first[100];
  257. char sz_utf_second[100];
  258. char sz_utf_three[100];
  259. string name1, name2, name3;
  260. CMonitorObjectMng::Instance()->GetNameByMoMp(mo_mp, name1, name2, name3);
  261. if (name1[0x00] == 0x00)
  262. gbk2utf8(sz_utf_first, 100, "1号测力曲线");
  263. else
  264. gbk2utf8(sz_utf_first, 100, name1.c_str());
  265. if (name2[0x00] == 0x00)
  266. gbk2utf8(sz_utf_second, 100, "2号测力曲线");
  267. else
  268. gbk2utf8(sz_utf_second, 100, name2.c_str());
  269. if (name3[0x00] == 0x00)
  270. gbk2utf8(sz_utf_three, 100, "转换阻力曲线");
  271. else
  272. gbk2utf8(sz_utf_three, 100, name3.c_str());
  273. int step = 1000 / num;
  274. uint64_t utime_t = atime.GetTime() * 1000;
  275. string up, momp_name;
  276. CMonitorObjectMng::Instance()->GetStationNameByMomP(mo_mp, up, momp_name);
  277. auto doc = yyjson_mut_doc_new(nullptr);
  278. auto root = yyjson_mut_obj(doc);
  279. yyjson_mut_doc_set_root(doc, root);
  280. auto data = yyjson_mut_arr(doc);
  281. yyjson_mut_obj_add_val(doc, root, "data", data);
  282. auto curve_1 = yyjson_mut_obj(doc);
  283. auto curve_2 = yyjson_mut_obj(doc);
  284. auto curve_3 = yyjson_mut_obj(doc);
  285. yyjson_mut_arr_add_val(data, curve_1);
  286. yyjson_mut_arr_add_val(data, curve_2);
  287. yyjson_mut_arr_add_val(data, curve_3);
  288. yyjson_mut_obj_add_str(doc, root, "cmd", "new_data_notify");
  289. yyjson_mut_obj_add_strcpy(doc, root, "tag", (mo_mp + ".resist").c_str());
  290. yyjson_mut_obj_add_str(doc, root, "data_fmt", "curve");
  291. yyjson_mut_obj_add_str(doc, root, "unit", "N");
  292. yyjson_mut_obj_add_strcpy(doc, root, "up", ANSItoUTF8(up).c_str());
  293. yyjson_mut_obj_add_strcpy(doc, root, "tag_name", ANSItoUTF8(momp_name).c_str());
  294. //1号测力点
  295. yyjson_mut_obj_add_str(doc, curve_1, "name", sz_utf_first);
  296. {
  297. auto arr = yyjson_mut_arr(doc);
  298. yyjson_mut_obj_add_val(doc, curve_1, "points", arr);
  299. for (int i = 0; i < num; i++)
  300. {
  301. if (vctResult[i] == false) continue;
  302. if (vctData0[i] == INVALID_RESIST) continue;
  303. auto item = yyjson_mut_arr(doc);
  304. yyjson_mut_arr_add_val(arr, item);
  305. yyjson_mut_arr_add_uint(doc, item, utime_t + step * i);
  306. yyjson_mut_arr_add_int(doc, item, vctData0[i]);
  307. }
  308. }
  309. //2号测力点
  310. yyjson_mut_obj_add_str(doc, curve_2, "name", sz_utf_second);
  311. {
  312. auto arr = yyjson_mut_arr(doc);
  313. yyjson_mut_obj_add_val(doc, curve_2, "points", arr);
  314. for (int i = 0; i < num; i++)
  315. {
  316. if (vctResult[i] == false) continue;
  317. if (vctData1[i] == INVALID_RESIST) continue;
  318. auto item = yyjson_mut_arr(doc);
  319. yyjson_mut_arr_add_val(arr, item);
  320. yyjson_mut_arr_add_uint(doc, item, utime_t + step * i);
  321. yyjson_mut_arr_add_int(doc, item, vctData1[i]);
  322. }
  323. }
  324. //3号测力点
  325. yyjson_mut_obj_add_str(doc, curve_3, "name", sz_utf_three);
  326. {
  327. auto arr = yyjson_mut_arr(doc);
  328. yyjson_mut_obj_add_val(doc, curve_3, "points", arr);
  329. for (int i = 0; i < num; i++)
  330. {
  331. if (vctResult[i] == false) continue;
  332. if (vctData2[i] == false) continue;
  333. auto item = yyjson_mut_arr(doc);
  334. yyjson_mut_arr_add_val(arr, item);
  335. yyjson_mut_arr_add_uint(doc, item, utime_t + step * i);
  336. yyjson_mut_arr_add_int(doc, item, vctData2[i]);
  337. ++i;
  338. }
  339. }
  340. *json = yyjson_mut_write(doc, 0, &json_len);
  341. yyjson_mut_doc_free(doc);
  342. return json_len;
  343. }
  344. BOOL CMGWSServer::IsClientSubReal(const string& momp)
  345. {
  346. if (m_mg_mgr.conns == nullptr) return false;
  347. if (m_mg_mgr.conns->is_listening && m_mg_mgr.conns->next == nullptr) return false;
  348. bool bSub = false;
  349. for (auto it = m_mg_mgr.conns; it; it = it->next)
  350. {
  351. if (it->is_listening) continue;
  352. const auto& pConfInfo = (mg_per_session_data*)it->fn_data;
  353. if (pConfInfo) {
  354. if (pConfInfo->isLogin == false) continue;
  355. for (const auto& ik : pConfInfo->m_lstSubReal)
  356. {
  357. if (ik.compare(momp) == 0)
  358. {
  359. bSub = true;
  360. break;
  361. }
  362. }
  363. }
  364. }
  365. return bSub;
  366. }
  367. BOOL CMGWSServer::SendClientSubReal(const string& momp, const char* json, const size_t len)
  368. {
  369. char ip[50];
  370. for (auto it = m_mg_mgr.conns; it; it = it->next)
  371. {
  372. if (it->is_accepted == false) continue;
  373. const auto& pConfInfo = (mg_per_session_data*)it->fn_data;
  374. if (pConfInfo) {
  375. if (pConfInfo->isLogin == false) continue;
  376. for (const auto& ik : pConfInfo->m_lstSubReal)
  377. {
  378. if (ik.compare(momp) == 0)
  379. {
  380. if (g_bLog) SPDLOG_DEBUG(mg_straddr(&it->rem, ip, 50) + CString(json, len));
  381. mg_ws_send(it, json, len, WEBSOCKET_OP_TEXT);
  382. break;
  383. }
  384. }
  385. }
  386. }
  387. return true;
  388. }
  389. int mg_per_session_data::SendHistResistForEcharts(struct mg_connection* c, string mo_mp, time_t start, time_t end, uint32_t subsection, std::map<time_t, int>& data0, std::map<time_t, int>& data1, std::map<time_t, int>& data2)
  390. {
  391. char sz_utf_first[100];
  392. char sz_utf_second[100];
  393. char sz_utf_three[100];
  394. string name1, name2, name3;
  395. CMonitorObjectMng::Instance()->GetNameByMoMp(mo_mp, name1, name2, name3);
  396. if (name1[0x00] == 0x00)
  397. gbk2utf8(sz_utf_first, 100, "1号测力曲线");
  398. else
  399. gbk2utf8(sz_utf_first, 100, name1.c_str());
  400. if (name2[0x00] == 0x00)
  401. gbk2utf8(sz_utf_second, 100, "2号测力曲线");
  402. else
  403. gbk2utf8(sz_utf_second, 100, name2.c_str());
  404. if (name3[0x00] == 0x00)
  405. gbk2utf8(sz_utf_three, 100, "转换阻力曲线");
  406. else
  407. gbk2utf8(sz_utf_three, 100, name3.c_str());
  408. int offset = 0; uint32_t limit = subsection;
  409. auto it_data0 = data0.begin();
  410. auto it_data1 = data1.begin();
  411. auto it_data2 = data2.begin();
  412. bBlock = false;
  413. do
  414. {
  415. static int n = 600;
  416. for (int i = 0; i < n; i++)
  417. {
  418. if (bWork == false) break;
  419. if (bBlock) this_thread::sleep_for(chrono::milliseconds(100));
  420. else break;
  421. }
  422. #ifndef _DEBUG
  423. if (bBlock) ////5秒未返回确认包,结束
  424. break;;
  425. #endif // _DEBUG
  426. auto cost_start = chrono::steady_clock::now();
  427. //10000 一次
  428. auto doc = yyjson_mut_doc_new(nullptr);
  429. auto root = yyjson_mut_obj(doc);
  430. yyjson_mut_doc_set_root(doc, root);
  431. auto data = yyjson_mut_arr(doc);
  432. yyjson_mut_obj_add_val(doc, root, "data", data);
  433. auto curve_1 = yyjson_mut_obj(doc);
  434. auto curve_2 = yyjson_mut_obj(doc);
  435. auto curve_3 = yyjson_mut_obj(doc);
  436. yyjson_mut_arr_add_val(data, curve_1);
  437. yyjson_mut_arr_add_val(data, curve_2);
  438. yyjson_mut_arr_add_val(data, curve_3);
  439. yyjson_mut_obj_add_str(doc, root, "cmd", "query_hist");
  440. yyjson_mut_obj_add_strcpy(doc, root, "tag", (mo_mp + ".resist").c_str());
  441. yyjson_mut_obj_add_str(doc, root, "data_fmt", "curve");
  442. yyjson_mut_obj_add_str(doc, root, "unit", "N");
  443. yyjson_mut_obj_add_str(doc, root, "time", "");
  444. //1号测力点
  445. yyjson_mut_obj_add_str(doc, curve_1, "name", sz_utf_first);
  446. {
  447. auto arr = yyjson_mut_arr(doc);
  448. yyjson_mut_obj_add_val(doc, curve_1, "points", arr);
  449. auto mark_points = yyjson_mut_arr(doc);
  450. yyjson_mut_obj_add_val(doc, curve_1, "mark_points", mark_points);
  451. for (int i = 0; i < limit && it_data0 != data0.end(); ++it_data0)
  452. {
  453. if (it_data0->first < start)
  454. continue;
  455. if (it_data0->first >= end)
  456. break;
  457. if (it_data0->second == INVALID_RESIST)
  458. continue;
  459. auto item = yyjson_mut_arr(doc);
  460. yyjson_mut_arr_add_val(arr, item);
  461. yyjson_mut_arr_add_uint(doc, item, it_data0->first);
  462. yyjson_mut_arr_add_int(doc, item, it_data0->second);
  463. ++i;
  464. }
  465. }
  466. //2号测力点
  467. yyjson_mut_obj_add_str(doc, curve_2, "name", sz_utf_second);
  468. {
  469. auto arr = yyjson_mut_arr(doc);
  470. yyjson_mut_obj_add_val(doc, curve_2, "points", arr);
  471. auto mark_points = yyjson_mut_arr(doc);
  472. yyjson_mut_obj_add_val(doc, curve_1, "mark_points", mark_points);
  473. for (int i = 0; i < limit && it_data1 != data1.end(); ++it_data1)
  474. {
  475. if (it_data1->first < start)
  476. continue;
  477. if (it_data1->first >= end)
  478. break;
  479. if (it_data1->second == INVALID_RESIST)
  480. continue;
  481. auto item = yyjson_mut_arr(doc);
  482. yyjson_mut_arr_add_val(arr, item);
  483. yyjson_mut_arr_add_uint(doc, item, it_data1->first);
  484. yyjson_mut_arr_add_int(doc, item, it_data1->second);
  485. ++i;
  486. }
  487. }
  488. //3号测力点
  489. yyjson_mut_obj_add_str(doc, curve_3, "name", sz_utf_three);
  490. {
  491. auto arr = yyjson_mut_arr(doc);
  492. yyjson_mut_obj_add_val(doc, curve_3, "points", arr);
  493. auto mark_points = yyjson_mut_arr(doc);
  494. yyjson_mut_obj_add_val(doc, curve_1, "mark_points", mark_points);
  495. for (int i = 0; i < limit && it_data2 != data2.end(); ++it_data2)
  496. {
  497. if (it_data2->first < start)
  498. continue;
  499. if (it_data2->first >= end)
  500. break;
  501. if (it_data2->second == INVALID_RESIST)
  502. continue;
  503. auto item = yyjson_mut_arr(doc);
  504. yyjson_mut_arr_add_val(arr, item);
  505. yyjson_mut_arr_add_uint(doc, item, it_data2->first);
  506. yyjson_mut_arr_add_int(doc, item, it_data2->second);
  507. ++i;
  508. }
  509. }
  510. auto cost_end = chrono::steady_clock::now();
  511. auto cost_dif = chrono::duration_cast<chrono::milliseconds>(cost_end - cost_start).count();
  512. yyjson_mut_obj_add_strcpy(doc, root, "cost", (to_string(cost_dif) + "ms").c_str());
  513. if (bWork == false) break;
  514. size_t ll;
  515. auto json = yyjson_mut_write(doc, 0, &ll);
  516. if (json && c->is_closing == false)
  517. {
  518. mg_ws_send(c, json, ll, WEBSOCKET_OP_TEXT);
  519. free((void*)json);
  520. }
  521. yyjson_mut_doc_free(doc);
  522. bBlock = true;
  523. if (bWork == false) break;
  524. } while (!((it_data0 == data0.end() || it_data0->first >= end) && (it_data1 == data1.end() || it_data1->first >= end) && (it_data2 == data2.end() || it_data2->first >= end)));
  525. return 0;
  526. }
  527. //#ifndef _DEBUG
  528. #define SHOW_DATA
  529. //#endif // _DEBUG
  530. int mg_per_session_data::SendHistResistDBForEcharts(LPMGHISTORY_QUERY history_query)
  531. {
  532. static string str_utf_fix = ANSItoUTF8("定位测力曲线");
  533. static string str_utf_invert = ANSItoUTF8("反位测力曲线");
  534. static string str_utf_convert = ANSItoUTF8("转换阻力曲线");
  535. int offset = 0; uint32_t limit = history_query->subsection; //每次取出10000
  536. CTime ctStart(history_query->tmStart / 1000);
  537. CTime ctEnd(history_query->tmEnd / 1000);
  538. char tablename[50];
  539. char tablenameTom[50];
  540. SYSTEMTIME stStart;
  541. ctStart.GetAsSystemTime(stStart);
  542. sprintf_s(tablename, 50, "rm_resistance_%04d%02d%02d", stStart.wYear, stStart.wMonth, stStart.wDay);
  543. SYSTEMTIME stEnd;
  544. ctEnd.GetAsSystemTime(stEnd);
  545. sprintf_s(tablenameTom, 50, "rm_resistance_%04d%02d%02d", stEnd.wYear, stEnd.wMonth, stEnd.wDay);
  546. string strStartTime = ctStart.Format("%Y-%m-%d %H:%M:%S");
  547. string strEndTime = ctEnd.Format("%Y-%m-%d %H:%M:%S");
  548. bBlock = false;
  549. CString sql;
  550. do
  551. {
  552. static int n = 600;
  553. for (int i = 0; i < n; i++)
  554. {
  555. if (bWork == false) break;
  556. if (bBlock) this_thread::sleep_for(chrono::milliseconds(100));
  557. else break;
  558. }
  559. #ifndef _DEBUG
  560. if (bBlock) ////5秒未返回确认包,结束
  561. break;;
  562. #endif // _DEBUG
  563. auto cost_start = chrono::steady_clock::now();
  564. if (stStart.wDay == stEnd.wDay)
  565. {
  566. sql.Format("SELECT [acquisitiontime],[data0],[data1],[data2] "\
  567. "FROM %s WHERE momp = '%s' AND acquisitiontime >= '%s' and acquisitiontime < '%s' "\
  568. "ORDER BY acquisitiontime ASC OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
  569. tablename, history_query->mo_mp.c_str(), strStartTime.c_str(), strEndTime.c_str(), offset, limit);
  570. }
  571. else
  572. {
  573. sql.Format("SELECT [acquisitiontime],[data0],[data1],[data2] "\
  574. "FROM %s WHERE momp = '%s' AND acquisitiontime >= '%s' and acquisitiontime < '%s' "\
  575. "UNION ALL "\
  576. "SELECT [acquisitiontime],[data0],[data1],[data2] "\
  577. "FROM %s WHERE momp = '%s' AND acquisitiontime >= '%s' and acquisitiontime < '%s' "\
  578. "ORDER BY acquisitiontime ASC OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
  579. tablename, history_query->mo_mp.c_str(), strStartTime.c_str(), strEndTime.c_str(),
  580. tablenameTom, history_query->mo_mp.c_str(), strStartTime.c_str(), strEndTime.c_str(),
  581. offset, limit);
  582. }
  583. TRACE("%s\r\n", sql);
  584. int no = 0;
  585. TIMESTAMP_STRUCT ts;
  586. int sdata0, sdata1, sdata2;
  587. COdbcStatement stmt;
  588. if (CDBConnectPool::Instance()->DBQuery(stmt, sql) == FALSE)
  589. {
  590. SPDLOG_ERROR("查询语句出错:{}", sql);
  591. break;
  592. }
  593. int nCol = 1;
  594. stmt.BindTimeStampCol(nCol++, &ts);
  595. stmt.BindIntCol(nCol++, &sdata0);
  596. stmt.BindIntCol(nCol++, &sdata1);
  597. stmt.BindIntCol(nCol++, &sdata2);
  598. std::map<time_t, int> data0, data1, data2;
  599. do
  600. {
  601. if (stmt.FetchNext() != 0)
  602. break;
  603. no++;
  604. CTime ctTime;
  605. try
  606. {
  607. ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  608. }
  609. catch (...)
  610. {
  611. continue;
  612. }
  613. time_t tm = ctTime.GetTime() * 1000 + ts.fraction / 1000000;
  614. data0[tm] = sdata0;
  615. data1[tm] = sdata1;
  616. data2[tm] = sdata2;
  617. } while (true);
  618. //扳动阈值判断 放到HTTP里
  619. //std::map<time_t, int64_t> maxlock0, maxlock1, maxForce;
  620. //list<CONVERT_RESIST> maxResist;
  621. //GetMaxResist(data2, maxResist);
  622. //GetMaxLockNew(data0, maxResist, maxlock0);
  623. //GetMaxLockNew(data1, maxResist, maxlock1);
  624. ////CalcFixOrInvert(maxResist, maxlock0, maxlock1, name1, name2);
  625. //GetMaxForce(data2, maxForce);
  626. //10000 一次
  627. auto doc = yyjson_mut_doc_new(nullptr);
  628. auto root = yyjson_mut_obj(doc);
  629. yyjson_mut_doc_set_root(doc, root);
  630. auto data = yyjson_mut_arr(doc);
  631. yyjson_mut_obj_add_val(doc, root, "data", data);
  632. auto curve_1 = yyjson_mut_obj(doc);
  633. auto curve_2 = yyjson_mut_obj(doc);
  634. auto curve_3 = yyjson_mut_obj(doc);
  635. yyjson_mut_arr_add_val(data, curve_1);
  636. yyjson_mut_arr_add_val(data, curve_2);
  637. yyjson_mut_arr_add_val(data, curve_3);
  638. yyjson_mut_obj_add_str(doc, root, "cmd", "query_hist");
  639. yyjson_mut_obj_add_strcpy(doc, root, "tag", (history_query->mo_mp + ".resist").c_str());
  640. yyjson_mut_obj_add_str(doc, root, "data_fmt", "curve");
  641. yyjson_mut_obj_add_str(doc, root, "unit", "N");
  642. yyjson_mut_obj_add_str(doc, root, "time", "");
  643. //1号测力点
  644. yyjson_mut_obj_add_str(doc, curve_1, "name", str_utf_fix.c_str());
  645. {
  646. auto arr = yyjson_mut_arr(doc);
  647. yyjson_mut_obj_add_val(doc, curve_1, "points", arr);
  648. #ifdef SHOW_DATA
  649. for (const auto& it : data0)
  650. {
  651. if (INVALID_RESIST == it.second)
  652. continue;
  653. auto item = yyjson_mut_arr(doc);
  654. yyjson_mut_arr_add_val(arr, item);
  655. yyjson_mut_arr_add_uint(doc, item, it.first);
  656. yyjson_mut_arr_add_int(doc, item, it.second);
  657. }
  658. #endif // SHOW_DATA
  659. // auto mark_points = yyjson_mut_arr(doc);
  660. // yyjson_mut_obj_add_val(doc, curve_1, "mark_points", mark_points);
  661. // for (const auto& it : maxlock0)
  662. // {
  663. // auto obj = yyjson_mut_obj(doc);
  664. // yyjson_mut_arr_add_val(mark_points, obj);
  665. // auto val = (it.second >> 32);
  666. // if (val > 500)
  667. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("锁闭力:{}", val)).c_str());
  668. // else
  669. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8("锁闭力过低").c_str());
  670. // yyjson_mut_obj_add_int(doc, obj, "val", val);
  671. // auto arr = yyjson_mut_arr(doc);
  672. // yyjson_mut_obj_add_val(doc, obj, "coord", arr);
  673. //#ifdef SHOW_DATA
  674. // yyjson_mut_arr_add_uint(doc, arr, it.first);
  675. //#else
  676. // yyjson_mut_arr_add_strcpy(doc, arr, CTime(it.first / 1000).Format("%Y-%m-%d %H:%M:%S"));
  677. //#endif // _DEBUG
  678. // yyjson_mut_arr_add_int(doc, arr, (int)it.second);
  679. // }
  680. }
  681. //2号测力点
  682. yyjson_mut_obj_add_str(doc, curve_2, "name", str_utf_invert.c_str());
  683. {
  684. auto arr = yyjson_mut_arr(doc);
  685. yyjson_mut_obj_add_val(doc, curve_2, "points", arr);
  686. #ifdef SHOW_DATA
  687. for (const auto& it : data1)
  688. {
  689. if (INVALID_RESIST == it.second)
  690. continue;
  691. auto item = yyjson_mut_arr(doc);
  692. yyjson_mut_arr_add_val(arr, item);
  693. yyjson_mut_arr_add_uint(doc, item, it.first);
  694. yyjson_mut_arr_add_int(doc, item, it.second);
  695. }
  696. #endif
  697. // auto mark_points = yyjson_mut_arr(doc);
  698. // yyjson_mut_obj_add_val(doc, curve_2, "mark_points", mark_points);
  699. // for (const auto& it : maxlock1)
  700. // {
  701. // auto obj = yyjson_mut_obj(doc);
  702. // yyjson_mut_arr_add_val(mark_points, obj);
  703. // auto val = (it.second >> 32);
  704. // if (val > 500)
  705. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("锁闭力:{}", val)).c_str());
  706. // else
  707. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8("锁闭力过低").c_str());
  708. // auto arr = yyjson_mut_arr(doc);
  709. // yyjson_mut_obj_add_val(doc, obj, "coord", arr);
  710. // yyjson_mut_obj_add_int(doc, obj, "val", val);
  711. //#ifdef SHOW_DATA
  712. // yyjson_mut_arr_add_uint(doc, arr, it.first);
  713. //#else
  714. // yyjson_mut_arr_add_strcpy(doc, arr, CTime(it.first / 1000).Format("%Y-%m-%d %H:%M:%S"));
  715. //#endif // _DEBUG
  716. // yyjson_mut_arr_add_int(doc, arr, (int)it.second);
  717. // }
  718. }
  719. //3号测力点
  720. yyjson_mut_obj_add_str(doc, curve_3, "name", str_utf_convert.c_str());
  721. {
  722. auto arr = yyjson_mut_arr(doc);
  723. yyjson_mut_obj_add_val(doc, curve_3, "points", arr);
  724. #ifdef SHOW_DATA
  725. for (const auto& it : data2)
  726. {
  727. if (INVALID_RESIST == it.second)
  728. continue;
  729. auto item = yyjson_mut_arr(doc);
  730. yyjson_mut_arr_add_val(arr, item);
  731. yyjson_mut_arr_add_uint(doc, item, it.first);
  732. yyjson_mut_arr_add_int(doc, item, it.second);
  733. }
  734. #endif
  735. // auto mark_points = yyjson_mut_arr(doc);
  736. // yyjson_mut_obj_add_val(doc, curve_3, "mark_points", mark_points);
  737. // for (const auto& it : maxResist)
  738. // {
  739. // auto obj = yyjson_mut_obj(doc);
  740. // yyjson_mut_arr_add_val(mark_points, obj);
  741. // if (it.bUpOrDown == 1 && in_name.length() > 0)
  742. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("{}:{}", in_name, it.fluctuation_val)).c_str());
  743. // else if (it.bUpOrDown == 2 && out_name.length() > 0)
  744. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("{}:{}", out_name, it.fluctuation_val)).c_str());
  745. // else
  746. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("最大转换阻力值:{}", it.fluctuation_val)).c_str());
  747. //
  748. // if (it.bUpOrDown == 2) yyjson_mut_obj_add_str(doc, obj, "position", "bottom"); //负数增加显示位置
  749. // auto arr = yyjson_mut_arr(doc);
  750. // yyjson_mut_obj_add_val(doc, obj, "coord", arr);
  751. //#ifdef SHOW_DATA
  752. // yyjson_mut_arr_add_uint(doc, arr, it.time);
  753. //#else
  754. // yyjson_mut_arr_add_strcpy(doc, arr, CTime(it.time / 1000).Format("%Y-%m-%d %H:%M:%S"));
  755. //#endif // _DEBUG
  756. // yyjson_mut_arr_add_int(doc, arr, it.val);
  757. // }
  758. // for (const auto& it : maxForce)
  759. // {
  760. // auto obj = yyjson_mut_obj(doc);
  761. // yyjson_mut_arr_add_val(mark_points, obj);
  762. // yyjson_mut_obj_add_strcpy(doc, obj, "label", ANSItoUTF8(fmt::format("摩擦力值:{}", (it.second >> 32))).c_str());
  763. // auto arr = yyjson_mut_arr(doc);
  764. // yyjson_mut_obj_add_val(doc, obj, "coord", arr);
  765. // yyjson_mut_arr_add_uint(doc, arr, it.first);
  766. // yyjson_mut_arr_add_int(doc, arr, (int)it.second);
  767. // }
  768. }
  769. auto cost_end = chrono::steady_clock::now();
  770. auto cost_dif = chrono::duration_cast<chrono::milliseconds>(cost_end - cost_start).count();
  771. yyjson_mut_obj_add_strcpy(doc, root, "cost", (to_string(cost_dif) + "ms").c_str());
  772. if (bWork)
  773. {
  774. size_t ll;
  775. auto json = yyjson_mut_write(doc, 0, &ll);
  776. char ip[50];
  777. if (json && history_query->c->is_closing == false) {
  778. SPDLOG_DEBUG("[WS]send data {}:{}", mg_straddr(&history_query->c->rem, ip, 50), json);
  779. mg_ws_send(history_query->c, json, ll, WEBSOCKET_OP_TEXT);
  780. free((void*)json);
  781. }
  782. }
  783. yyjson_mut_doc_free(doc);
  784. //maxlock0.clear();
  785. //maxlock1.clear();
  786. //maxForce.clear();
  787. //maxResist.clear();
  788. //len 335214
  789. bBlock = true;
  790. if (bWork == false) break;
  791. offset += no;
  792. if (no != limit)
  793. {
  794. break;
  795. }
  796. } while (true);
  797. return 0;
  798. }
  799. //{ "cmd": "login", "token": "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"}
  800. //{ "cmd": "query_hist", "tag": "TLDZ24.J3.resist", "time": "2022-10-14 03:42:00~2022-10-14 03:46:30", "subsection": 5000}
  801. void mg_per_session_data::GetMaxResist(std::map<time_t, int>& data, list<CONVERT_RESIST>& out, const string& mo, const string& mp)
  802. {
  803. std::map<time_t, tagSecondStatInfo> mapSecondStatInfo;
  804. ConvertMiroToSecond(data, mapSecondStatInfo);
  805. if (mapSecondStatInfo.size() == 0) return;
  806. GetMaxResistNew(mapSecondStatInfo, out, mo, mp);
  807. }
  808. void mg_per_session_data::GetMaxResist(std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo, list<CONVERT_RESIST>& out)
  809. {
  810. int steady_val = INT_MIN;
  811. time_t move_start_t = 0; //扳动开始时间
  812. int move_start_val = INT_MIN; //扳动起始值
  813. int move_max_val = INT_MIN; //扳动峰值
  814. time_t move_max_t = 0; //扳动峰值的时间
  815. int move_min_val = INT_MAX; //扳动谷值
  816. time_t move_min_t = 0; //扳动谷值的时间
  817. time_t last_t = mapSecondStatInfo.begin()->first;
  818. time_t last_last_t;
  819. int last_end_val = mapSecondStatInfo.begin()->second.end_val;
  820. int last_last_end_val; //上一秒的最后一个值
  821. for (auto& it : mapSecondStatInfo)
  822. {
  823. auto str = CTime(it.first).Format("%Y-%m-%d %H:%M:%S") + '.' + to_string(it.first % 1000).c_str();
  824. TRACE("%s\r\n", str);
  825. last_last_t = last_t;//更新前赋值
  826. last_t = it.first;
  827. last_last_end_val = last_end_val;
  828. last_end_val = it.second.end_val;
  829. if (it.second.dif_val <= 150) //稳定 大于500N 为波动
  830. {
  831. if (move_start_t != 0) // 波动结束
  832. {
  833. CONVERT_RESIST info;
  834. info.tmStart = move_start_t;
  835. info.tmEnd = it.first;
  836. int fluctuation_t = it.first - move_start_t;//波动时间
  837. bool bMax = false;
  838. auto iMax = abs(move_max_val - steady_val);
  839. auto iMin = abs(move_min_val - steady_val);
  840. if (iMax > iMin)
  841. {
  842. bMax = true;
  843. info.fluctuation_val = iMax;//最大值减去前面的稳定平均值
  844. }
  845. else info.fluctuation_val = iMin;
  846. if (fluctuation_t >= 4 && fluctuation_t <= 8 && info.fluctuation_val > 450)
  847. {
  848. //扳动
  849. if (bMax)
  850. {
  851. //输出记录
  852. info.time = move_max_t;
  853. info.bUpOrDown = 1;
  854. info.val = move_max_val;
  855. out.emplace_back(info);
  856. //out[move_max_t] = (((int64_t)move_max_val - steady_val) << 32) + move_max_val;
  857. SPDLOG_INFO("[转换阻力求值]符合特征 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最大值:{} 波动值:{}",
  858. CTime(move_start_t).Format("%Y-%m-%d %H:%M:%S"), CTime(it.first).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  859. CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_max_t % 1000).c_str(), move_max_val, it.second.dif_val
  860. );
  861. }
  862. else
  863. {
  864. info.time = move_min_t;
  865. info.bUpOrDown = 2;
  866. info.val = move_min_val;
  867. out.emplace_back(info);
  868. //out[move_min_t] = (((int64_t)move_min_val - steady_val) << 32) + move_min_val;//输出记录
  869. SPDLOG_INFO("[转换阻力求值]符合特征 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最小值:{} 波动值:{}",
  870. CTime(move_start_t).Format("%Y-%m-%d %H:%M:%S"), CTime(it.first).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  871. CTime(move_min_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_min_t % 1000).c_str(), move_min_val, it.second.dif_val
  872. );
  873. }
  874. }
  875. else
  876. {
  877. if (bMax)
  878. SPDLOG_INFO("[转换阻力求值]不符合特征 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最大值:{} 波动值:{} 不符合特征",
  879. CTime(move_start_t).Format("%Y-%m-%d %H:%M:%S"), CTime(it.first).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  880. CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_max_t % 1000).c_str(), move_max_val, it.second.dif_val
  881. );
  882. else
  883. SPDLOG_INFO("[转换阻力求值]不符合特征 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最小值:{} 波动值:{} 不符合特征",
  884. CTime(move_start_t).Format("%Y-%m-%d %H:%M:%S"), CTime(it.first).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  885. CTime(move_min_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_min_t % 1000).c_str(), move_min_val, it.second.dif_val
  886. );
  887. }
  888. //重置
  889. move_start_t = 0;
  890. move_max_val = INT_MIN;
  891. move_max_t = 0;
  892. move_start_val = INT_MIN;
  893. move_min_val = INT_MAX; //扳动谷值
  894. move_min_t = 0; //扳动谷值的时间
  895. }
  896. //steady_val = it.second.sum_val / it.second.cout; //稳定值为平均值
  897. steady_val = it.second.end_val; //2022.10.22换成最后一个值
  898. }
  899. else if (steady_val == INT_MIN) //起始点就是波动
  900. {
  901. continue;
  902. }
  903. else if (it.second.dif_val > 400)
  904. {
  905. if (move_start_t == 0)
  906. {
  907. //开始波动
  908. if (abs(last_last_end_val - it.second.first_val) < 100) //如果前面的最后一个值 和 当前最开始的值 小于 100 . 以当前这个为准 2022.10.22
  909. {
  910. move_start_t = last_t; //取当前 it.first
  911. steady_val = it.second.first_val;
  912. SPDLOG_INFO("[转换阻力求值]检测到开始波动:{} 上一个稳定时间:{} 起始值:{}", CTime(it.first).Format("%Y-%m-%d %H:%M:%S"),
  913. CTime(last_t).Format("%Y-%m-%d %H:%M:%S"), steady_val);
  914. }
  915. else
  916. {
  917. move_start_t = last_last_t; //开始波动取上一个
  918. steady_val = last_last_end_val;
  919. SPDLOG_INFO("[转换阻力求值]检测到开始波动:{} 上一个稳定时间:{} 起始值:{}", CTime(it.first).Format("%Y-%m-%d %H:%M:%S"),
  920. CTime(last_last_t).Format("%Y-%m-%d %H:%M:%S"), steady_val);
  921. }
  922. }
  923. //波动 取出最大值
  924. if (it.second.max_val > move_max_val)
  925. {
  926. move_max_t = it.second.max_time;
  927. move_max_val = it.second.max_val;
  928. }
  929. if (it.second.min_val < move_min_val)
  930. {
  931. move_min_t = it.second.min_time;
  932. move_min_val = it.second.min_val;
  933. }
  934. }
  935. }
  936. }
  937. void mg_per_session_data::GetMaxResistNew(std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo, list<CONVERT_RESIST>& out, const string& mo, const string& mp)
  938. {
  939. if (mapSecondStatInfo.size() == 0) return;
  940. time_t move_start_t = 0; //扳动开始时间
  941. int move_start_val = INT_MIN; //扳动起始值
  942. int move_max_val = INT_MIN; //扳动峰值
  943. time_t move_max_t = 0; //扳动峰值的时间
  944. int move_min_val = INT_MAX; //扳动谷值
  945. time_t move_min_t = 0; //扳动谷值的时间
  946. time_t last_t = mapSecondStatInfo.begin()->first;
  947. time_t last_last_t;
  948. int last_end_val = mapSecondStatInfo.begin()->second.end_val;
  949. int last_last_end_val; //上一秒的最后一个值
  950. list<tagSecondStatInfo> lstStatInfo;
  951. auto it = mapSecondStatInfo.cbegin();
  952. lstStatInfo.emplace_back(it->second);
  953. time_t tLastIt = it->first;
  954. BOOL bForContinue = TRUE;;
  955. do
  956. {
  957. it++;
  958. if (it != mapSecondStatInfo.cend())
  959. {
  960. if (it->first - tLastIt < 2) //单位秒
  961. {
  962. lstStatInfo.emplace_back(it->second);
  963. tLastIt = it->first;
  964. continue;
  965. }
  966. else
  967. tLastIt = it->first;
  968. }
  969. else
  970. bForContinue = FALSE;//遍历结束
  971. do
  972. {
  973. if (lstStatInfo.size() < 7) //扳动不小于7秒
  974. break;
  975. //处理逻辑
  976. int steady_val = INT_MIN;
  977. list<tagSecondStatInfo>::iterator it_start = lstStatInfo.end();
  978. list<tagSecondStatInfo>::reverse_iterator it_end = lstStatInfo.rend();
  979. for (auto i = lstStatInfo.begin(); i != lstStatInfo.end(); i++)
  980. {
  981. if (i->dif_val >= 100) //大于100N算波动
  982. {
  983. if (steady_val == INT_MIN)
  984. steady_val = i->first_val;
  985. it_start = i;
  986. break;
  987. }
  988. else
  989. steady_val = i->end_val;
  990. }
  991. for (auto i = lstStatInfo.rbegin(); i != lstStatInfo.rend(); i++)
  992. {
  993. if (i->dif_val >= 100) //大于100N算波动
  994. {
  995. it_end = i;
  996. break;
  997. }
  998. }
  999. if (it_start == lstStatInfo.end() || it_end == lstStatInfo.rend())
  1000. break;
  1001. if (it_end == lstStatInfo.rbegin())
  1002. {
  1003. //结束还在波动 不进行判断
  1004. SPDLOG_INFO("[转换阻力判断][{}:{}] 结束还在波动 不进行判断: {}", mo, mp,
  1005. CTime(it_end->max_time / 1000).Format("%Y-%m-%d %H:%M:%S"));
  1006. break;
  1007. }
  1008. CONVERT_RESIST info;
  1009. info.tmStart = it_start->max_time / 1000;
  1010. info.tmEnd = it_end->max_time / 1000 + 1;
  1011. //取出转换值
  1012. move_max_val = it_start->max_val;
  1013. move_min_val = it_start->min_val;
  1014. move_max_t = move_min_t = it_start->max_time;
  1015. for (auto i = it_start;;)
  1016. {
  1017. if (i->max_val > move_max_val)
  1018. {
  1019. move_max_val = i->max_val;
  1020. move_max_t = i->max_time;
  1021. }
  1022. if (i->min_val < move_min_val)
  1023. {
  1024. move_min_val = i->min_val;
  1025. move_min_t = i->min_time;
  1026. }
  1027. if (i->max_time > it_end->max_time)
  1028. {
  1029. break;
  1030. }
  1031. i++;
  1032. }
  1033. bool bMax = false;
  1034. auto iMax = abs(move_max_val - steady_val);
  1035. auto iMin = abs(move_min_val - steady_val);
  1036. if (iMax > iMin)
  1037. {
  1038. bMax = true;
  1039. info.fluctuation_val = iMax;//最大值减去前面的稳定平均值
  1040. }
  1041. else info.fluctuation_val = iMin;
  1042. auto fluctuation_t = it_end->max_time / 1000 - it_start->max_time / 1000 + 1; //波动时长
  1043. if (fluctuation_t >= 3 && fluctuation_t <= 9 && info.fluctuation_val > 450)
  1044. {
  1045. //扳动
  1046. if (bMax)
  1047. {
  1048. //输出记录
  1049. info.time = move_max_t;
  1050. info.bUpOrDown = 1;
  1051. info.val = move_max_val;
  1052. out.emplace_back(info);
  1053. //out[move_max_t] = (((int64_t)move_max_val - steady_val) << 32) + move_max_val;
  1054. if (g_bLog)
  1055. SPDLOG_INFO("[转换阻力求值][{}:{}]符合特征 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最大值:{}", mo, mp,
  1056. CTime(info.tmStart).Format("%Y-%m-%d %H:%M:%S"), CTime(info.tmEnd).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  1057. CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_max_t % 1000).c_str(), move_max_val
  1058. );
  1059. }
  1060. else
  1061. {
  1062. info.time = move_min_t;
  1063. info.bUpOrDown = 2;
  1064. info.val = move_min_val;
  1065. out.emplace_back(info);
  1066. //out[move_min_t] = (((int64_t)move_min_val - steady_val) << 32) + move_min_val;//输出记录
  1067. if (g_bLog)
  1068. SPDLOG_INFO("[转换阻力求值][{}:{}]符合特征 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最小值:{}", mo, mp,
  1069. CTime(info.tmStart).Format("%Y-%m-%d %H:%M:%S"), CTime(info.tmEnd).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  1070. CTime(move_min_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_min_t % 1000).c_str(), move_min_val
  1071. );
  1072. }
  1073. }
  1074. else
  1075. {
  1076. if (bMax)
  1077. SPDLOG_INFO("[转换阻力求值][{}:{}]不符合3秒~9秒 大于450N 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最大值:{} 不符合特征", mo, mp,
  1078. CTime(info.tmStart).Format("%Y-%m-%d %H:%M:%S"), CTime(info.tmEnd).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  1079. CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_max_t % 1000).c_str(), move_max_val
  1080. );
  1081. else
  1082. SPDLOG_INFO("[转换阻力求值][{}:{}]不符合3秒~9秒 大于450N 波动开始:{} 结束:{} 波动时长:{} 转换阻力值大小:{} 时间:{} 最小值:{} 不符合特征", mo, mp,
  1083. CTime(info.tmStart).Format("%Y-%m-%d %H:%M:%S"), CTime(info.tmEnd).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t, info.fluctuation_val,
  1084. CTime(move_min_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_min_t % 1000).c_str(), move_min_val
  1085. );
  1086. }
  1087. TRACE("%s %d \r\n", CTime(it_end->max_time / 1000).Format("%Y-%m-%d %H:%M:%S"), fluctuation_t);
  1088. } while (false);
  1089. if (lstStatInfo.size()) lstStatInfo.clear();
  1090. ////遍历结束
  1091. //if (it == mapSecondStatInfo.cend())
  1092. // break;
  1093. } while (bForContinue);
  1094. }
  1095. void mg_per_session_data::GetMaxLockNew(const std::map<time_t, int>& data, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out)
  1096. {
  1097. std::map<time_t, tagSecondStatInfo> mapSecondStatInfo;
  1098. ConvertMiroToSecond(data, mapSecondStatInfo);
  1099. if (mapSecondStatInfo.size() == 0) return;
  1100. GetMaxLockNew(mapSecondStatInfo, refer, out);
  1101. }
  1102. /*
  1103. void mg_per_session_data::GetMaxLock(std::map<time_t, tagSecondStatInfo> mapSecondStatInfo, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out)
  1104. {
  1105. int steady_val = INT_MIN;
  1106. time_t move_start_t = 0; //扳动开始时间 最后一个稳态时间
  1107. time_t move_start_move_t = 0; //扳动开始时间 即波动时间
  1108. int move_start_val = INT_MIN; //扳动起始值
  1109. int move_max_val = INT_MIN; //扳动峰值
  1110. time_t move_max_t = 0; //扳动峰值的时间
  1111. int move_min_val = INT_MAX; //扳动谷值 只参与计算
  1112. time_t move_min_t = 0; //扳动谷值的时间
  1113. time_t last_t = mapSecondStatInfo.begin()->first;
  1114. time_t last_last_t;
  1115. for (auto& it : mapSecondStatInfo)
  1116. {
  1117. auto str = CTime(it.first).Format("%Y-%m-%d %H:%M:%S") + '.' + to_string(it.first % 1000).c_str();
  1118. //TRACE("%s\r\n", str);
  1119. last_last_t = last_t;//更新前赋值
  1120. last_t = it.first;
  1121. if (it.second.dif_val <= 300) //稳定 大于300N 为波动
  1122. {
  1123. if (move_start_t != 0) // 波动结束
  1124. {
  1125. int fluctuation_t = it.first - move_start_t;//波动时间
  1126. int fluctuation_val = move_max_val - steady_val; //最大值减去前面的稳定平均值
  1127. int nSecondDif = INT_MAX;
  1128. bool bCalcDif = CalcSecondDif(refer, move_start_move_t, nSecondDif);
  1129. if (fluctuation_t >= 1 && fluctuation_t <= 7 && fluctuation_val > 30 && bCalcDif && nSecondDif >= 1) //小于0 不是密贴 2023.2.10 2秒改为1秒
  1130. {
  1131. //扳动
  1132. out[move_max_t] = (((int64_t)fluctuation_val) << 32) + move_max_val;//输出记录
  1133. //SPDLOG_INFO("[锁闭力求值]符合特征 波动时长:{} 锁闭力大小:{} 时间:{} 最大值:{}", fluctuation_t, fluctuation_val,
  1134. // CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S") + to_string(move_max_t % 1000).c_str(), move_max_val);
  1135. SPDLOG_INFO("[锁闭力求值]符合特征 波动时长:{} 锁闭力大小:{} 时间:{} 最大值:{} nSecondDif:{} 开始波动时间:{}", fluctuation_t, fluctuation_val,
  1136. CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_max_t % 1000).c_str(), move_max_val, nSecondDif, CTime(move_start_move_t).Format("%Y-%m-%d %H:%M:%S")).c_str());
  1137. }
  1138. else
  1139. {
  1140. if (fluctuation_val < 0)
  1141. {
  1142. fluctuation_val = move_min_val - steady_val;
  1143. SPDLOG_INFO("[锁闭力求值]不符合特征 波动时长:{} 锁闭力大小:{} 时间:{} 最小值:{} bCalcDif:{} nSecondDif:{} 开始波动时间:{} 不符合特征", fluctuation_t, fluctuation_val,
  1144. CTime(move_min_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_min_t % 1000).c_str(), move_min_val, bCalcDif, nSecondDif, CTime(move_start_move_t).Format("%Y-%m-%d %H:%M:%S")).c_str());
  1145. }
  1146. else
  1147. {
  1148. //SPDLOG_INFO("[锁闭力求值]不符合特征 波动时长:{} 锁闭力大小:{} 时间:{} 最大值:{}", fluctuation_t, fluctuation_val,
  1149. // CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S") + to_string(move_max_t % 1000).c_str(), move_max_val);
  1150. SPDLOG_INFO("[锁闭力求值]不符合特征 波动时长:{} 锁闭力大小:{} 时间:{} 最大值:{} bCalcDif:{} nSecondDif:{} 开始波动时间:{} 不符合特征", fluctuation_t, fluctuation_val,
  1151. CTime(move_max_t / 1000).Format("%Y-%m-%d %H:%M:%S.") + to_string(move_max_t % 1000).c_str(), move_max_val, bCalcDif, nSecondDif, CTime(move_start_move_t).Format("%Y-%m-%d %H:%M:%S")).c_str());
  1152. }
  1153. }
  1154. //重置
  1155. move_start_t = 0;
  1156. move_start_move_t = 0;
  1157. move_max_val = INT_MIN;
  1158. move_max_t = 0;
  1159. move_start_val = INT_MIN;
  1160. move_min_val = INT_MAX; //扳动谷值
  1161. move_min_t = 0; //扳动谷值的时间
  1162. }
  1163. steady_val = it.second.sum_val / it.second.cout; //稳定值为平均值
  1164. }
  1165. else if (steady_val == INT_MIN) //起始点就是波动
  1166. {
  1167. continue;
  1168. }
  1169. else if (it.second.dif_val > 300)
  1170. {
  1171. if (move_start_t == 0)
  1172. {
  1173. move_start_t = last_last_t; //开始波动取上一个
  1174. move_start_move_t = it.first;
  1175. //SPDLOG_INFO("检测到开始波动:{} 上一个稳定时间:{}", CTime(it.first / 1000).Format("%Y-%m-%d %H:%M:%S"), CTime(last_last_t / 1000).Format("%Y-%m-%d %H:%M:%S"));
  1176. SPDLOG_INFO("[锁闭力求值]检测到开始波动:{} 上一个稳定时间:{}", CTime(it.first).Format("%Y-%m-%d %H:%M:%S"),
  1177. CTime(last_last_t).Format("%Y-%m-%d %H:%M:%S")).c_str());
  1178. }
  1179. //波动 取出最大值
  1180. if (it.second.max_val > move_max_val)
  1181. {
  1182. move_max_t = it.second.max_time;
  1183. move_max_val = it.second.max_val;
  1184. }
  1185. if (it.second.min_val < move_min_val)
  1186. {
  1187. move_min_t = it.second.min_time;
  1188. move_min_val = it.second.min_val;
  1189. }
  1190. }
  1191. }
  1192. }
  1193. */
  1194. void mg_per_session_data::GetMaxLockNew(std::map<time_t, tagSecondStatInfo> mapSecondStatInfo, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out)
  1195. {
  1196. for (auto& it : refer)
  1197. {
  1198. auto it_start = mapSecondStatInfo.find(it.tmStart);
  1199. auto it_end = mapSecondStatInfo.find(it.tmEnd);
  1200. if (it_start == mapSecondStatInfo.end() || it_end == mapSecondStatInfo.end())
  1201. continue;
  1202. auto fluctuation_val = it_end->second.first_val - it_start->second.end_val;
  1203. if (fluctuation_val < 30)
  1204. continue;
  1205. int move_max_val = INT_MIN; //扳动峰值
  1206. time_t move_max_t = 0; //扳动峰值的时间
  1207. for (auto i = it_start; i->first < it_end->first; i++)
  1208. {
  1209. if (move_max_val < i->second.max_val)
  1210. {
  1211. move_max_val = i->second.max_val;
  1212. move_max_t = i->second.max_time;
  1213. }
  1214. }
  1215. out[move_max_t] = (((int64_t)fluctuation_val) << 32) + move_max_val;//输出记录
  1216. }
  1217. }
  1218. void mg_per_session_data::GetMaxForce(std::map<time_t, int>& data2, std::map<time_t, int64_t>& out)
  1219. {
  1220. }
  1221. void mg_per_session_data::CalcFixOrInvert(list<CONVERT_RESIST>& out, std::map<time_t, int64_t>& in1, std::map<time_t, int64_t>& in2, string name1, string name2)
  1222. {
  1223. if (name1.find("定") == -1 && name2.find("定") == -1) return;
  1224. if (in1.size() == 0 && in2.size() == 0) return;
  1225. for (auto& it : out)
  1226. {
  1227. uint8_t bFirstOrSecond = 0;
  1228. int time_dif = INT_MAX;
  1229. for (const auto& ik : in1)
  1230. {
  1231. auto dif = abs(it.time - ik.first);
  1232. if (dif < 8000 && dif < time_dif)
  1233. {
  1234. time_dif = dif;
  1235. bFirstOrSecond = 1;
  1236. }
  1237. }
  1238. for (const auto& ik : in2)
  1239. {
  1240. auto dif = abs(it.time - ik.first);
  1241. if (dif < 8000 && dif < time_dif)
  1242. {
  1243. time_dif = dif;
  1244. bFirstOrSecond = 2;
  1245. }
  1246. }
  1247. if (bFirstOrSecond == 1)
  1248. {
  1249. if (name1.find("定") != -1) it.bFixOrInvert = 1;
  1250. else if (name1.find("反") != -1) it.bFixOrInvert = 2;
  1251. }
  1252. else if (bFirstOrSecond == 2)
  1253. {
  1254. if (name2.find("定") != -1) it.bFixOrInvert = 1;
  1255. else if (name2.find("反") != -1) it.bFixOrInvert = 2;
  1256. }
  1257. }
  1258. }