MGWSServer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma once
  2. #include <mongoose.h>
  3. #include <thread>
  4. #include <string>
  5. #include <list>
  6. #include <yyjson.h>
  7. #include <map>
  8. #include <vector>
  9. #include "AlarmDefine.h"
  10. enum class VAL_STATUS : uint8_t
  11. {
  12. VAL_STEADY = 0x00, //稳定
  13. VAL_MOVE = 0x01, //波动
  14. VAL_UNKONW = 0x04, //未知
  15. };
  16. typedef struct tagMgHist_Query
  17. {
  18. uint32_t subsection = 5000;
  19. struct mg_connection* c = nullptr;
  20. time_t tmStart = 0;
  21. time_t tmEnd = 0;
  22. string mo_mp;
  23. string type;
  24. }MG_HISTORY_QUERY, * LPMGHISTORY_QUERY;
  25. typedef struct tagConvertResist
  26. {
  27. time_t time = 0; //报警时间
  28. int val = 0; //报警时阻力值
  29. int fluctuation_val = 0; //报警值;
  30. uint8_t bUpOrDown = 0; //1. up 缩进 2. down 伸出
  31. uint8_t bFixOrInvert = 0; //1. fix 2. invert
  32. time_t tmStart = 0; //开始波动的时间 单位秒
  33. time_t tmEnd = 0; //结束波动时间 单位秒
  34. }CONVERT_RESIST;
  35. struct mg_per_session_data
  36. {
  37. mg_per_session_data()
  38. {
  39. }
  40. ~mg_per_session_data()
  41. {
  42. bWork = false;
  43. bBlock = false;
  44. if (thread_hist)
  45. {
  46. thread_hist->join();
  47. delete thread_hist;
  48. thread_hist = nullptr;
  49. }
  50. }
  51. uint32_t send_count = 0; //发送
  52. uint32_t send_fail_count = 0;
  53. uint32_t recv_count = 0; //接收
  54. uint64_t send_size = 0;
  55. uint64_t recv_size = 0;
  56. time_t tmConnect = 0; //链接时间
  57. time_t tmLastRecvHeart = 0; //最后心跳时间
  58. time_t tmLastSendHeart = 0;
  59. bool isLogin = false; //是否登录
  60. string token; //登录的token
  61. string username; //登录的用户名
  62. string name;
  63. string node; //管辖ID
  64. string node_name; //管辖名称
  65. std::list<std::string> m_lstSubReal; //订阅实时数据列表 mo,mp
  66. std::thread* thread_hist = nullptr; //历史数据发送线程
  67. bool bWork = false;
  68. bool bBlock = false; //历史数据包阻塞
  69. int 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);
  70. int SendHistResistDBForEcharts(LPMGHISTORY_QUERY);
  71. //最大转换阻力值 时间 高32值:计算值 低32位:阻力值
  72. static void GetMaxResist(std::map<time_t, int>& data2, list<CONVERT_RESIST>& out, const string& mo, const string& mp);
  73. static void GetMaxResist(std::map<time_t, tagSecondStatInfo>& data, list<CONVERT_RESIST>& out);
  74. static void GetMaxResistNew(std::map<time_t, tagSecondStatInfo>& data, list<CONVERT_RESIST>& out, const string& mo, const string& mp);
  75. //密贴锁闭力值
  76. static void GetMaxLockNew(const std::map<time_t, int>& data, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out);
  77. static void GetMaxLockNew(std::map<time_t, tagSecondStatInfo> data, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out);
  78. //摩擦力值
  79. void GetMaxForce(std::map<time_t, int>& data2, std::map<time_t, int64_t>& out);
  80. //通过1号和2号来计算定扳反 和 反扳定
  81. void CalcFixOrInvert(list<CONVERT_RESIST>& out, std::map<time_t, int64_t>& in1, std::map<time_t, int64_t>& in2, string name1, string name2);
  82. //遍历求得最接近时间的秒数
  83. inline static bool CalcSecondDif(const list<CONVERT_RESIST>& out, const time_t t, OUT int& dif)
  84. {
  85. int lastVal = 8;
  86. bool bRet = false;
  87. for (const auto& it : out)
  88. {
  89. auto d = t - it.tmStart;
  90. if (abs(d) < lastVal)
  91. {
  92. dif = d;
  93. lastVal = abs(d);
  94. bRet = true;
  95. }
  96. }
  97. return bRet;
  98. }
  99. //毫秒记录 转换成 秒统计
  100. inline static void ConvertMiroToSecond(const std::map<time_t, int>& data, std::map<time_t, tagSecondStatInfo>& out)
  101. {
  102. uint32_t uLast = 0;
  103. std::map<time_t, tagSecondStatInfo>::iterator ik;
  104. for (auto& it : data)
  105. {
  106. uint32_t tt = it.first / 1000;
  107. if (uLast == tt)
  108. {
  109. }
  110. else
  111. {
  112. if (out[tt].first_val == INT_MIN) out[tt].first_val = it.second; //存储第一个值
  113. ik = out.find(tt);
  114. }
  115. if (it.second > ik->second.max_val)
  116. {
  117. ik->second.max_val = it.second;
  118. ik->second.max_time = it.first;
  119. }
  120. if (it.second < ik->second.min_val)
  121. {
  122. ik->second.min_val = it.second;
  123. ik->second.min_time = it.first;
  124. }
  125. ik->second.dif_val = abs(ik->second.max_val - ik->second.min_val);
  126. ik->second.sum_val += it.second;
  127. ik->second.cout++;
  128. ik->second.end_val = it.second;
  129. }
  130. }
  131. };
  132. __interface IMGWSHandler
  133. {
  134. virtual BOOL HandlerData(const char* ptr, size_t len, char** json) = 0;
  135. virtual size_t HandlerData(struct mg_connection* c, struct mg_ws_message* wm, char** json) = 0;
  136. };
  137. class CMGWSServer
  138. {
  139. public:
  140. CMGWSServer();
  141. ~CMGWSServer();
  142. public:
  143. BOOL Start(uint16_t port);
  144. void Stop();
  145. //协议处理
  146. IMGWSHandler* m_pDataHanlder = nullptr;
  147. //发送部分
  148. void SendRealResistData(const string& imei_idx, const int num, const std::vector<int>& vctData0,
  149. const std::vector<int>& vctData1, const std::vector<int>& vctData2, const std::vector<bool>& vctResult, const CTime& atime);
  150. //推送温湿度数据
  151. void SendHumiTemp(string mo_mp, const COleDateTime& dt, int humi, int temp);
  152. void SendToAllClient(const char* json, size_t len);
  153. void SendToClient(const char* json, const size_t len, const char* ip);
  154. BOOL IsClientSubReal(const string& momp);
  155. BOOL SendClientSubReal(const string& momp, const char* json, const size_t len);
  156. private:
  157. struct mg_mgr m_mg_mgr = { 0 }; //Event management structure
  158. std::thread* m_pThread = nullptr;
  159. BOOL m_bThreadWork = FALSE;
  160. static void ThreadProc(DWORD_PTR, uint16_t);
  161. int GeneralResistData(const string mo_mp, const int num, const int index, const std::vector<int>& vctData0,
  162. const std::vector<int>& vctData1, const std::vector<int>& vctData2, const std::vector<bool>& vctResult, const CTime& atime, char** json);
  163. };