module_web.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_REQUEST_ALL_DATA, &module_web::accept ) );
  40. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_DEAL_HELP, &module_web::accept ) );
  41. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CALL_CARD_REQUEST, &module_web::accept ) );
  42. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST, &module_web::accept ) );
  43. MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CLEAR_CARD, &module_web::accept ) );//手动升井
  44. }
  45. /// web前端有用户登录时,反馈给web所有信息
  46. void response_login();
  47. private:
  48. /**
  49. * @brief 线程函数
  50. */
  51. void run();
  52. ///获取所有的告警事件
  53. void get_all_events(std::vector<std::shared_ptr<ya_event>>& arr)
  54. {
  55. auto _map = event_list::instance()->m_map;
  56. auto it_map = _map.begin();
  57. for(;it_map!=_map.end();++it_map)
  58. {
  59. arr.push_back(it_map->second);
  60. }
  61. }
  62. ///在全局列表中删除已经处理或结束了的告警
  63. void delete_end(std::vector<std::shared_ptr<ya_event>>& arr)
  64. {
  65. std::vector<uint64_t> todelete;
  66. auto arr_iter = arr.begin();
  67. for(;arr_iter!=arr.end();++arr_iter)
  68. {
  69. if((*arr_iter)->is_end())//删除掉已经处理的
  70. {
  71. todelete.push_back((*arr_iter)->get_list_id());
  72. }
  73. }
  74. if(todelete.size())
  75. {
  76. event_list::instance()->remove(todelete);
  77. }
  78. }
  79. };
  80. #endif // MODULE_WEB_H