MGWSServer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 imei;
  23. string mo_mp;
  24. short idx = 0;
  25. string type;
  26. }MG_HISTORY_QUERY, * LPMGHISTORY_QUERY;
  27. typedef struct tagConvertResist
  28. {
  29. time_t time = 0; //报警时间
  30. int val = 0; //报警时阻力值
  31. int fluctuation_val = 0; //报警值;
  32. eUpOrDownInfo bUpOrDown = eUpOrDownInfo::UOD_UNKNOWN; //1. up 缩进 2. down 伸出
  33. uint8_t bFixOrInvert = 0; //1. fix 2. invert
  34. time_t tmStart = 0; //开始波动的时间 单位秒
  35. time_t tmEnd = 0; //结束波动时间 单位秒
  36. }CONVERT_RESIST;
  37. typedef struct tagFrictionResist
  38. {
  39. int show_val = 0; //稳态值
  40. time_t show_time = 0; //稳态时间
  41. int peak_val = 0; //峰值
  42. time_t peak_time = 0; //峰值时间
  43. eUpOrDownInfo bUpOrDown = eUpOrDownInfo::UOD_UNKNOWN; //1. up 缩进 2. down 伸出
  44. time_t tmStart = 0; //开始波动的时间 单位秒
  45. time_t tmEnd = 0; //结束波动时间 单位秒
  46. string t;
  47. }FRICTION_RESIST;
  48. //过车信息
  49. typedef struct tagPassResist
  50. {
  51. time_t show_time = 0;//报警时间
  52. int val = 0; //报警时阻力值
  53. int vv = 0; //过车晃动量
  54. time_t tmStart = 0; //开始过车的时间 单位秒
  55. time_t tmEnd = 0; //结束过车时间 单位秒
  56. eDaoChaPosi posi = eDaoChaPosi::DCP_UNKNOWN; //判断定位还是反位过车
  57. }PASS_RESIST;
  58. struct mg_per_session_data
  59. {
  60. mg_per_session_data()
  61. {
  62. }
  63. ~mg_per_session_data()
  64. {
  65. bWork = false;
  66. bBlock = false;
  67. if (thread_hist)
  68. {
  69. thread_hist->join();
  70. delete thread_hist;
  71. thread_hist = nullptr;
  72. }
  73. }
  74. uint32_t send_count = 0; //发送
  75. uint32_t send_fail_count = 0;
  76. uint32_t recv_count = 0; //接收
  77. uint64_t send_size = 0;
  78. uint64_t recv_size = 0;
  79. time_t tmConnect = 0; //链接时间
  80. time_t tmLastRecvHeart = 0; //最后心跳时间
  81. time_t tmLastSendHeart = 0;
  82. bool isLogin = false; //是否登录
  83. string token; //登录的token
  84. string username; //登录的用户名
  85. string name;
  86. string node; //管辖ID
  87. string node_name; //管辖名称
  88. std::list<std::string> m_lstSubReal; //订阅实时数据列表 mo,mp
  89. std::thread* thread_hist = nullptr; //历史数据发送线程
  90. bool bWork = false;
  91. bool bBlock = false; //历史数据包阻塞
  92. 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);
  93. int SendHistResistDBForEcharts(LPMGHISTORY_QUERY);
  94. //最大转换阻力值 时间 高32值:计算值 低32位:阻力值
  95. static void GetMaxResist(std::map<time_t, int>& data2, std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo, list<CONVERT_RESIST>& out, const string& mo, const string& mp);
  96. static void GetMaxResist(std::map<time_t, tagSecondStatInfo>& data, list<CONVERT_RESIST>& out);
  97. static void GetMaxResistNew(std::map<time_t, tagSecondStatInfo>& data, list<CONVERT_RESIST>& out, const string& mo, const string& mp);
  98. //密贴锁闭力值
  99. static void GetMaxLockNew(const std::map<time_t, int>& data, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out, std::map<time_t, int64_t>& retensionforce);
  100. static void GetMaxLockNew(const std::map<time_t, tagSecondStatInfo>& data, const list<CONVERT_RESIST>& refer, std::map<time_t, int64_t>& out, std::map<time_t, int64_t>& retensionforce);
  101. //摩擦力值
  102. void GetMaxForce(std::map<time_t, int>& data2, std::map<time_t, int64_t>& out);
  103. //通过1号和2号来计算定扳反 和 反扳定
  104. void CalcFixOrInvert(list<CONVERT_RESIST>& out, std::map<time_t, int64_t>& in1, std::map<time_t, int64_t>& in2, string name1, string name2);
  105. //遍历求得最接近时间的秒数
  106. inline static bool CalcSecondDif(const list<CONVERT_RESIST>& out, const time_t t, OUT int& dif)
  107. {
  108. int lastVal = 8;
  109. bool bRet = false;
  110. for (const auto& it : out)
  111. {
  112. auto d = t - it.tmStart;
  113. if (abs(d) < lastVal)
  114. {
  115. dif = d;
  116. lastVal = abs(d);
  117. bRet = true;
  118. }
  119. }
  120. return bRet;
  121. }
  122. //毫秒记录 转换成 秒统计
  123. inline static void ConvertMiroToSecond(const std::map<time_t, int>& data, std::map<time_t, tagSecondStatInfo>& out)
  124. {
  125. uint32_t uLast = 0;
  126. std::map<time_t, tagSecondStatInfo>::iterator ik;
  127. for (auto& it : data)
  128. {
  129. uint32_t tt = it.first / 1000;
  130. if (uLast == tt)
  131. {
  132. }
  133. else
  134. {
  135. if (out[tt].first_val == INT_MIN) out[tt].first_val = it.second; //存储第一个值
  136. ik = out.find(tt);
  137. }
  138. if (it.second > ik->second.max_val)
  139. {
  140. ik->second.max_val = it.second;
  141. ik->second.max_time = it.first;
  142. }
  143. if (it.second < ik->second.min_val)
  144. {
  145. ik->second.min_val = it.second;
  146. ik->second.min_time = it.first;
  147. }
  148. ik->second.dif_val = abs(ik->second.max_val - ik->second.min_val);
  149. ik->second.sum_val += it.second;
  150. ik->second.cout++;
  151. ik->second.end_val = it.second;
  152. }
  153. }
  154. static void GetPass(std::map<time_t, tagSecondStatInfo>& pStatInfo, std::map<time_t, int>* pData, const string& mo, const string& mp, OUT list<PASS_RESIST>* lstResist);
  155. static void GetPassNew(std::map<time_t, tagSecondStatInfo>& pStatInfo0, std::map<time_t, int>* pData0,
  156. std::map<time_t, tagSecondStatInfo>& pStatInfo1, std::map<time_t, int>* pData1,
  157. const string& mo, const string& mp, OUT list<PASS_RESIST>* lstResist);
  158. static void GetPassNewUnit(std::map<time_t, tagSecondStatInfo>* pStatInfo0, std::map<time_t, int>* pData0,
  159. std::map<time_t, tagSecondStatInfo>* pStatInfo1, std::map<time_t, int>* pData1,
  160. const string& mo, const string& mp, OUT list<PASS_RESIST>* lstResist);
  161. };
  162. __interface IMGWSHandler
  163. {
  164. virtual BOOL HandlerData(const char* ptr, size_t len, char** json) = 0;
  165. virtual size_t HandlerData(struct mg_connection* c, struct mg_ws_message* wm, char** json) = 0;
  166. };
  167. class CMGWSServer
  168. {
  169. public:
  170. CMGWSServer();
  171. ~CMGWSServer();
  172. public:
  173. BOOL Start(uint16_t port);
  174. void Stop();
  175. //协议处理
  176. IMGWSHandler* m_pDataHanlder = nullptr;
  177. //发送部分
  178. void SendRealResistData(const string& imei_idx, const int num, const std::vector<int>& vctData0,
  179. const std::vector<int>& vctData1, const std::vector<int>& vctData2, const std::vector<bool>& vctResult, const CTime& atime);
  180. //推送温湿度数据
  181. void SendHumiTemp(string mo_mp, const COleDateTime& dt, int humi, int temp);
  182. void SendToAllClient(const char* json, size_t len);
  183. void SendToClient(const char* json, const size_t len, const char* ip);
  184. private:
  185. struct mg_mgr m_mg_mgr = { 0 }; //Event management structure
  186. std::thread* m_pThread = nullptr;
  187. BOOL m_bThreadWork = FALSE;
  188. static void ThreadProc(DWORD_PTR, uint16_t);
  189. int GeneralResistData(const string mo_mp, const int num, const int index, const std::vector<int>& vctData0,
  190. const std::vector<int>& vctData1, const std::vector<int>& vctData2, const std::vector<bool>& vctResult, const CTime& atime, char** json);
  191. };