1
0

module_web.cpp 4.3 KB

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