module_web.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef MODULE_WEB_H
  2. #define MODULE_WEB_H
  3. /**
  4. * @brief 与web通信的接口类,以及json工具类:tool_json
  5. * @author 戴月腾
  6. * @date 2018-08-25
  7. */
  8. #include <memory>
  9. #include <string>
  10. #include"module_const.h"
  11. #include "rapidjson/prettywriter.h"
  12. #include "rapidjson/document.h"
  13. #include "websocket/wsClientMgr.h"
  14. class module_web : public i_thread, public singleton_base<module_web>
  15. {
  16. private:
  17. friend class singleton_base<module_web>;
  18. module_web()
  19. {
  20. }
  21. public:
  22. /**
  23. * @brief 用来处理WEB发送的请求(web请求回调函数)
  24. * @param ID
  25. * @param name
  26. * @param data
  27. * @param need_ack
  28. * @param ack_resp
  29. */
  30. static void accept( int ID, std::string const& name,
  31. sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );
  32. /**
  33. * @brief 注册web请求回调函数
  34. * @param MsgFuncList
  35. */
  36. void init(config_file& config, std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE>& MsgFuncList)
  37. {
  38. sleep_ms = std::stoi(config.get("service.interval_send_json_alarm","10000"));
  39. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_META_DATA_CHANGED, &module_web::accept ) );
  40. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_REQUEST_ALL_DATA, &module_web::accept ) );
  41. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_DEAL_HELP, &module_web::accept ) );
  42. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CALL_CARD_REQUEST, &module_web::accept ) );
  43. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST, &module_web::accept ) );
  44. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CLEAR_CARD, &module_web::accept ) );//手动升井
  45. }
  46. /// web前端有用户登录时,反馈给web所有信息
  47. void response_login();
  48. private:
  49. /**
  50. * @brief 线程函数
  51. */
  52. void run();
  53. ///获取所有的告警事件
  54. void get_all_events(std::vector<std::shared_ptr<ya_event>>& arr)
  55. {
  56. auto _map = event_list::instance()->m_map;
  57. auto it_map = _map.begin();
  58. for(;it_map!=_map.end();++it_map)
  59. {
  60. arr.push_back(it_map->second);
  61. }
  62. }
  63. ///在全局列表中删除已经处理或结束了的告警
  64. void delete_end(std::vector<std::shared_ptr<ya_event>>& arr)
  65. {
  66. std::vector<uint64_t> todelete;
  67. auto arr_iter = arr.begin();
  68. for(;arr_iter!=arr.end();++arr_iter)
  69. {
  70. if((*arr_iter)->is_end())//删除掉已经处理的
  71. {
  72. todelete.push_back((*arr_iter)->get_list_id());
  73. }
  74. }
  75. if(todelete.size())
  76. {
  77. event_list::instance()->remove(todelete);
  78. }
  79. }
  80. };
  81. #endif // MODULE_WEB_H