module_web.h 2.8 KB

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