module_web.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "module_web.h"
  2. #include"module_call_help.h"
  3. #include"module_call.h"
  4. #include"area_business_person_attendance.h"
  5. #include"module_meta_date_changed.h"
  6. #include"common_tool.h"
  7. #include"event.h"
  8. #include"log.h"
  9. void module_web::accept( int ID, std::string const& name,
  10. sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
  11. {
  12. if(data->get_flag() != sio::message::flag_object)
  13. {
  14. log_error("web发来的数据不是对象");
  15. return;
  16. }
  17. std::string cmd = "";
  18. if(!tool_map::try_get_value(cmd, JSON_ROOT_KEY_CMD, data) || cmd.empty())
  19. {
  20. log_error("web发来的数据cmd字段为空");
  21. return;
  22. }
  23. log_info("web发来的数据 cmd=%s", cmd.c_str());
  24. if(JSON_CMD_VALUE_CLEAR_CARD == cmd)//手动升井
  25. {
  26. area_business_person_attendance::handle_up_mine(data);
  27. }
  28. else if (JSON_CMD_VALUE_REQUEST_ALL_DATA == cmd)//web登录请求所有信息
  29. {
  30. module_web::instance()->response_login();
  31. }else if(JSON_CMD_REQ_ALL_PERSON_ON_CAR == cmd)
  32. {
  33. //人上车数据
  34. }
  35. else
  36. {
  37. sio::message::ptr data_value;
  38. if(!tool_map::try_get_value(data_value, JSON_ROOT_KEY_DATA, data))
  39. {
  40. log_error("web发来的 %s 数据data字段格式不对, 不是map",cmd.c_str());
  41. return;
  42. }
  43. if(JSON_CMD_VALUE_META_DATA_CHANGED == cmd)///基础数据
  44. {
  45. module_meta_date_changed::instance()->accept(data_value);
  46. }
  47. else if(JSON_CMD_VALUE_DEAL_HELP == cmd) // 处理呼救信息
  48. {
  49. module_call_help::instance()->accept_web_deal_help(data_value);
  50. }
  51. else if(JSON_CMD_VALUE_CALL_CARD_REQUEST == cmd)//呼叫
  52. {
  53. module_call::instance()->accept_call(data_value);
  54. }
  55. else if(JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST == cmd)//取消呼叫
  56. {
  57. module_call::instance()->accept_cancel(data_value);
  58. }
  59. }
  60. }
  61. void module_web::response_login()
  62. {
  63. rapidjson::Document doc(rapidjson::kObjectType);
  64. auto& allocator = doc.GetAllocator();
  65. rapidjson::Value nodes(rapidjson::kArrayType);
  66. //所有的呼叫信息
  67. std::string str=module_call::instance()->accept_login();
  68. if(!str.empty())
  69. {
  70. tool_json::push_back(nodes, str, allocator);
  71. }
  72. //所有告警
  73. std::vector<std::shared_ptr<ya_event>> arr;
  74. _get_all_events(arr);
  75. if(!arr.empty())
  76. {
  77. tool_json::push_back(nodes, event_list::evs_to_json(arr), allocator);
  78. }
  79. if(nodes.Size()>0)
  80. {
  81. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_RESPONSE_ALL_DATA, allocator);
  82. doc.AddMember(JSON_ROOT_KEY_VERSION, INTERFACE_VERSION, allocator);
  83. doc.AddMember(JSON_ROOT_KEY_DATA, nodes, allocator);
  84. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  85. }
  86. }
  87. void module_web::run()
  88. {
  89. std::vector<std::shared_ptr<ya_event>> arr;
  90. _get_all_events(arr,true);
  91. if(!arr.empty())//发送给web端
  92. {
  93. _delete_end(arr);
  94. std::string tmp = event_list::evs_to_json(arr);
  95. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tmp);
  96. // log_info("发送给web的告警json:%s", tmp.c_str());
  97. }
  98. std::string help = module_call_help::get_json_help();
  99. if(!help.empty())
  100. {
  101. swsClientMgr.send(JSON_CMD_VALUE_PUSH, help);
  102. }
  103. }
  104. void module_web::_get_all_events(std::vector<std::shared_ptr<ya_event>>& arr,bool f)
  105. {
  106. auto _map = event_list::instance()->m_map;
  107. auto it_map = _map.begin();
  108. for(;it_map!=_map.end();++it_map)
  109. {
  110. if(f && it_map->second->m_is_sent && !it_map->second->is_end())continue;
  111. else it_map->second->m_is_sent=true;
  112. arr.push_back(it_map->second);
  113. }
  114. }
  115. ///在全局列表中删除已经处理或结束了的告警
  116. void module_web::_delete_end(std::vector<std::shared_ptr<ya_event>>& arr)
  117. {
  118. std::vector<uint64_t> todelete;
  119. auto arr_iter = arr.begin();
  120. for(;arr_iter!=arr.end();++arr_iter)
  121. if((*arr_iter)->is_end())//删除掉已经处理的
  122. todelete.push_back((*arr_iter)->get_list_id());
  123. if(!todelete.empty())
  124. event_list::instance()->remove(todelete);
  125. }