ResistAlarm.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #pragma once
  2. #include <map>
  3. #include <thread>
  4. #include <string>
  5. #include <list>
  6. #include "AlarmDefine.h"
  7. #include <rapidjson/stringbuffer.h>
  8. #include <mutex>
  9. class CResistAlarm
  10. {
  11. CResistAlarm();
  12. ~CResistAlarm();
  13. friend class CResistAlarmMng;
  14. public:
  15. };
  16. typedef struct tagBaseInfo
  17. {
  18. char momp[20] = { 0 };
  19. uint8_t no = 255; //0, 1, 2 //通道号
  20. eZL_ALARMTYPE type = (eZL_ALARMTYPE)0x00; //报警类型
  21. bool enable = false; //报警开关
  22. time_t tmLastCheckTime = 0;
  23. }BASE_INFO;
  24. typedef struct tagMax_Over_Limit : public tagBaseInfo
  25. {
  26. int alarm_high_limit = INT_MAX; //锁闭力 定向反
  27. int warn_high_limit = INT_MAX; //锁闭力 反向定
  28. //short d_alarm_high_limit;
  29. //short d_warn_high_limit;
  30. int f_alarm_high_limit = INT_MAX; //保持力 反向定
  31. int f_warn_high_limit = INT_MAX; //保持力 反向定
  32. }MAX_OVER_LIMIT_INFO;
  33. typedef struct tagFriction_Over_Limit : public tagBaseInfo
  34. {
  35. int up_alarm_low_limit = INT_MAX; //上超低限报警值(暂定名称) 整数
  36. int up_warn_low_limit = INT_MAX; //上超低限预警值 整数
  37. int dw_alarm_high_limit = INT_MIN; //下超高限报警值 整数
  38. int dw_warn_high_limit = INT_MIN; //下超高限预警值 整数
  39. }FRICTION_OVER_LIMIT_INFO;
  40. typedef struct tagSuobi_Over_Limit : public tagBaseInfo
  41. {
  42. int alarm_low_limit = INT_MIN; //最小告警值
  43. int warn_low_limit = INT_MIN; //最小预警值
  44. int alarm_high_limit = INT_MAX; //最大告警值
  45. int warn_high_limit = INT_MAX; //最大预警值
  46. }SUOBI_OVER_LIMIT_INFO;
  47. typedef struct tagConvertResist_Over_Limit : public tagBaseInfo
  48. {
  49. int dw_alarm_low_limit = INT_MIN; //定板反报警值
  50. int dw_warn_low_limit = INT_MIN; //定板反预警值
  51. int up_alarm_high_limit = INT_MAX; //反板定报警值
  52. int up_warn_high_limit = INT_MAX; //反板定预警值
  53. }CONVERT_RESIST_OVER_LIMIT;
  54. typedef struct tagRetension_Force_Drop : public tagBaseInfo
  55. {
  56. int dw_alarm_low_drop = INT_MIN; //最低预警值
  57. int alarm_low_percent = INT_MIN; //下降百分比报警值
  58. int alarm_high_percent = INT_MIN; //上升百分比报警值
  59. bool is_page_dyna = false; //是否页面动态设置,默认否
  60. time_t tLastCalc = 0; //上一次计算时间(不参与配置) //是不是可以用 tmLastCheckTime
  61. tagRetension_Force_Drop()
  62. {
  63. type = eZL_ALARMTYPE::RETENSION_FORCE;
  64. enable = false;
  65. }
  66. }RETENSION_FORCE_DROP;
  67. //有string 不能拷贝
  68. typedef struct tagAlarmInfo
  69. {
  70. uint32_t id; //报警唯一ID
  71. string mo; //监控对象
  72. string mp; //监控点
  73. uint8_t no; //通道
  74. eZL_ALARMTYPE type;//报警类型
  75. uint8_t level; //0 预警 1 报警
  76. SYSTEMTIME time; //报警时间
  77. string desc; //报警描述
  78. string suggest; //建议
  79. int val = 0;
  80. uint8_t ack_result = 0; //是否受理
  81. string ack_name; //受理人
  82. CTime ack_time; //受理时间
  83. string event_id; //事件ID
  84. string rel_id; //第一次报警id
  85. eDaoChaPosi posi = eDaoChaPosi::DCP_UNKNOWN; //方向
  86. eLowHigh loworhigh = eLowHigh::LH_UNKNOWN;
  87. int refer_val = 0; //报警参数值
  88. SYSTEMTIME recoveryTime = { 0 };
  89. uint32_t zzjno = 0;
  90. uint8_t sunroof = 0; //是否天窗
  91. }ALARM_INFO;
  92. //获取报警名称
  93. inline string GetAlarmName(eZL_ALARMTYPE id)
  94. {
  95. switch (id)
  96. {
  97. case eZL_ALARMTYPE::MAX_OVER_LIMIT:
  98. return "最大值超限";
  99. case eZL_ALARMTYPE::FRICTION_OVER_LIMIT:
  100. return "摩擦力超限";
  101. case eZL_ALARMTYPE::EQUIP_OFFLINE:
  102. return "设备离线";
  103. case eZL_ALARMTYPE::SENSOR_ABNORMAL:
  104. return "传感器异常";
  105. case eZL_ALARMTYPE::CONVERT_LIMIT:
  106. return "转换阻力超限";
  107. case eZL_ALARMTYPE::SUOBI_LOCK_LIMIT:
  108. return "锁闭力超限";
  109. case eZL_ALARMTYPE::RETENSION_FORCE:
  110. return "保持力";
  111. default:
  112. return "未知";
  113. }
  114. }
  115. typedef struct eCalcMoveInfo
  116. {
  117. string imei;
  118. uint8_t idx;
  119. string mo;
  120. string mp;
  121. eCalcMoveInfo(string a, uint8_t b, string c, string d)
  122. {
  123. imei = a;
  124. idx = b;
  125. mo = c;
  126. mp = d;
  127. }
  128. }CALC_MOVE_INFO;
  129. BOOL ConmpareValue(std::map<time_t, int>::const_iterator i, std::map<time_t, int>::const_iterator j, int refer_value, int& hight_num, int& low_num);
  130. class CResistAlarmMng
  131. {
  132. CResistAlarmMng();
  133. ~CResistAlarmMng();
  134. friend class CLWSServer;
  135. friend class CMGDataHandler;
  136. public:
  137. static CResistAlarmMng* Instance() { return &obj; }
  138. BOOL Start(CString strIniPath);
  139. void Stop();
  140. //BASE_INFO* Find(string momp, uint8_t no, uint8_t type);
  141. BASE_INFO* Find(string mo, string mp, uint8_t no, eZL_ALARMTYPE type);
  142. bool Insert(string mo, string mp, uint8_t no, uint8_t type, BASE_INFO* info);
  143. //bool Insert(const string& momp, uint8_t no, uint8_t type, BASE_INFO* info);
  144. //bool ConfirmAlarm(string mo, string mp, uint8_t no, uint8_t type, const SYSTEMTIME& st);
  145. //受理报警
  146. bool AckAlarm(int alarm_id, string& name, CTime& time);
  147. //处理报警
  148. bool HandleAlarm(int alarm_id);
  149. //外部调用产生报警
  150. void GeneralAlarm(string mo, string mp, uint8_t no, eZL_ALARMTYPE type, uint8_t level, SYSTEMTIME& tAlarm);
  151. //外部调用恢复报警
  152. void RecoverAlarm(const string mo, const string mp, const uint8_t no, const eZL_ALARMTYPE type, const uint8_t level, const SYSTEMTIME& tAlarm);
  153. static bool AlarmInfo2Pack(const ALARM_INFO* pAlarmInfo, rapidjson::StringBuffer& strBuf, bool bGb2312 = false);
  154. eZL_MP_STAT GetAlarmStat(string& mo, string& mp, SYSTEMTIME& stAlarm);
  155. inline void InsertCalcMoveInfo(string imei, uint8_t idx, string mo, string mp)
  156. {
  157. m_lstCalcMoveInfo.emplace_back(imei, idx, mo, mp);
  158. }
  159. //序列化
  160. uint32_t GeneralAlarmSet(uint8_t** pack, int* len);
  161. uint32_t GeneralUnAck(uint8_t** pack, int* len);
  162. static uint32_t GeneralNewAlarmData(ALARM_INFO *pAlarmInfo, uint8_t** pack, int* len);
  163. private:
  164. static CResistAlarmMng obj;
  165. static void InsertToDBByMove(string mo, string mp, time_t show_time, time_t start_time, time_t end_time,
  166. int curr_val, int show_val, uint8_t idx, uint8_t posi, uint8_t type, string mark);
  167. static void JudgeAlarm(CResistAlarmMng* pService, SUOBI_OVER_LIMIT_INFO* pSuobiOverInfo,
  168. time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, uint32_t zzjno);
  169. static void JudgeAlarm(CResistAlarmMng* pService, CONVERT_RESIST_OVER_LIMIT* pConvertResistOverLimitInfo,
  170. time_t show_time, int show_val, eDaoChaPosi posi, string mo, string mp, uint32_t zzjno);
  171. //不调用
  172. static void JudgeAlarm2(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pRetensionForceDropInfo,
  173. time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, uint32_t zzjno);
  174. //不调用
  175. static void JudgeAlarm3(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pRetensionForceDropInfo,
  176. time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, uint32_t zzjno);
  177. //保持力判断报警,且保存数据
  178. static void JudgeAlarm(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pAlarmSet,
  179. time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, void* pMoMpInfo, int detectToday, eDaoChaPosi posiLast, BOOL isRedAlarm, BOOL alarmStatus);
  180. //摩檫力报警判断
  181. static void JudgeAlarm(CResistAlarmMng* pService, FRICTION_OVER_LIMIT_INFO* pAlarmSet,
  182. time_t show_time, int show_val, eDaoChaPosi posi, eUpOrDownInfo eUpOrDown, void* pMoMpInfo);
  183. static BOOL JudgePass(string mo, string mp);
  184. //报警规则检测线程
  185. static void ThreadProcAlarmSet(DWORD_PTR,CString strIniPath);
  186. //设备报警检测线程
  187. static void ThreadProcDevice(DWORD_PTR);
  188. //扳动线程检测线程
  189. static void ThreadProcMove(DWORD_PTR);
  190. //搬动线程外部数据线程
  191. static void ThreadProcMoveFromDB(DWORD_PTR);
  192. BOOL LoadAlarmSet();
  193. BOOL LoadAlarmID();
  194. static BOOL LoadMoveData(string momp, time_t* showTime, int* showVal, int posi);
  195. static BOOL isBetweenFiveAndFiveTen();
  196. static time_t SystemTimeToTimeT(const SYSTEMTIME& st);
  197. static BOOL CompareValuePass(std::map<time_t, int>* pData, time_t tsStartTime, time_t tsEndTime, int& hight_num, int& low_num, int threshold, int& iMaxDif, time_t& tMaxTime, int& nMaxValue);
  198. static string getTimeString();
  199. BOOL LoadUnAck();
  200. //报警ID
  201. int m_nAlarmID = 0;
  202. time_t m_lastDetectTime = 0;
  203. time_t m_lastDetectMove = 0;
  204. time_t m_lastDetectDrop = 0;
  205. bool m_bWork = false;
  206. //报警规则检测线程
  207. std::thread *m_pThreadAlarmSet = nullptr;
  208. //设备报警检测线程
  209. std::thread* m_pThreadDevice = nullptr;
  210. //扳动线程检测
  211. std::thread* m_pThreadMove = nullptr;
  212. //计算阻力值和锁闭力值
  213. std::list<CALC_MOVE_INFO> m_lstCalcMoveInfo;
  214. //报警参数 mo.mp
  215. std::map<std::string, BASE_INFO*> m_alarm_set;
  216. //未确认报警
  217. std::mutex m_mtxAlarm;
  218. std::list<ALARM_INFO*> m_lstUnConfirmAlarm; //未受理报警和未恢复集合
  219. };