#pragma once #include #include #include #include #include #include #include #include "AlarmDefine.h" enum class VAL_STATUS : uint8_t { VAL_STEADY = 0x00, //稳定 VAL_MOVE = 0x01, //波动 VAL_UNKONW = 0x04, //未知 }; typedef struct tagMgHist_Query { uint32_t subsection = 5000; struct mg_connection* c = nullptr; time_t tmStart = 0; time_t tmEnd = 0; string mo_mp; string type; }MG_HISTORY_QUERY, * LPMGHISTORY_QUERY; typedef struct tagConvertResist { time_t time = 0; //报警时间 int val = 0; //报警时阻力值 int fluctuation_val = 0; //报警值; uint8_t bUpOrDown = 0; //1. up 缩进 2. down 伸出 uint8_t bFixOrInvert = 0; //1. fix 2. invert time_t tmStart = 0; //开始波动的时间 单位秒 time_t tmEnd = 0; //结束波动时间 单位秒 }CONVERT_RESIST; struct mg_per_session_data { mg_per_session_data() { } ~mg_per_session_data() { bWork = false; bBlock = false; if (thread_hist) { thread_hist->join(); delete thread_hist; thread_hist = nullptr; } } uint32_t send_count = 0; //发送 uint32_t send_fail_count = 0; uint32_t recv_count = 0; //接收 uint64_t send_size = 0; uint64_t recv_size = 0; time_t tmConnect = 0; //链接时间 time_t tmLastRecvHeart = 0; //最后心跳时间 time_t tmLastSendHeart = 0; bool isLogin = false; //是否登录 string token; //登录的token string username; //登录的用户名 string name; string node; //管辖ID string node_name; //管辖名称 std::list m_lstSubReal; //订阅实时数据列表 mo,mp std::thread* thread_hist = nullptr; //历史数据发送线程 bool bWork = false; bool bBlock = false; //历史数据包阻塞 int SendHistResistForEcharts(struct mg_connection* c, string mo_mp, time_t start, time_t end, uint32_t subsection, std::map& data0, std::map& data1, std::map& data2); int SendHistResistDBForEcharts(LPMGHISTORY_QUERY); //最大转换阻力值 时间 高32值:计算值 低32位:阻力值 static void GetMaxResist(std::map& data2, list& out, const string& mo, const string& mp); static void GetMaxResist(std::map& data, list& out); static void GetMaxResistNew(std::map& data, list& out, const string& mo, const string& mp); //密贴锁闭力值 static void GetMaxLockNew(const std::map& data, const list& refer, std::map& out); static void GetMaxLockNew(std::map data, const list& refer, std::map& out); //摩擦力值 void GetMaxForce(std::map& data2, std::map& out); //通过1号和2号来计算定扳反 和 反扳定 void CalcFixOrInvert(list& out, std::map& in1, std::map& in2, string name1, string name2); //遍历求得最接近时间的秒数 inline static bool CalcSecondDif(const list& out, const time_t t, OUT int& dif) { int lastVal = 8; bool bRet = false; for (const auto& it : out) { auto d = t - it.tmStart; if (abs(d) < lastVal) { dif = d; lastVal = abs(d); bRet = true; } } return bRet; } //毫秒记录 转换成 秒统计 inline static void ConvertMiroToSecond(const std::map& data, std::map& out) { uint32_t uLast = 0; std::map::iterator ik; for (auto& it : data) { uint32_t tt = it.first / 1000; if (uLast == tt) { } else { if (out[tt].first_val == INT_MIN) out[tt].first_val = it.second; //存储第一个值 ik = out.find(tt); } if (it.second > ik->second.max_val) { ik->second.max_val = it.second; ik->second.max_time = it.first; } if (it.second < ik->second.min_val) { ik->second.min_val = it.second; ik->second.min_time = it.first; } ik->second.dif_val = abs(ik->second.max_val - ik->second.min_val); ik->second.sum_val += it.second; ik->second.cout++; ik->second.end_val = it.second; } } }; __interface IMGWSHandler { virtual BOOL HandlerData(const char* ptr, size_t len, char** json) = 0; virtual size_t HandlerData(struct mg_connection* c, struct mg_ws_message* wm, char** json) = 0; }; class CMGWSServer { public: CMGWSServer(); ~CMGWSServer(); public: BOOL Start(uint16_t port); void Stop(); //协议处理 IMGWSHandler* m_pDataHanlder = nullptr; //发送部分 void SendRealResistData(const string& imei_idx, const int num, const std::vector& vctData0, const std::vector& vctData1, const std::vector& vctData2, const std::vector& vctResult, const CTime& atime); //推送温湿度数据 void SendHumiTemp(string mo_mp, const COleDateTime& dt, int humi, int temp); void SendToAllClient(const char* json, size_t len); void SendToClient(const char* json, const size_t len, const char* ip); BOOL IsClientSubReal(const string& momp); BOOL SendClientSubReal(const string& momp, const char* json, const size_t len); private: struct mg_mgr m_mg_mgr = { 0 }; //Event management structure std::thread* m_pThread = nullptr; BOOL m_bThreadWork = FALSE; static void ThreadProc(DWORD_PTR, uint16_t); int GeneralResistData(const string mo_mp, const int num, const int index, const std::vector& vctData0, const std::vector& vctData1, const std::vector& vctData2, const std::vector& vctResult, const CTime& atime, char** json); };