module_call_help.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "module_call_help.h"
  2. #include "event.h"
  3. #include "common_tool.h"
  4. #include "websocket/constdef.h"
  5. #include "log.h"
  6. #include "db/db_tool.h"
  7. #include "tool_time.h"
  8. /**
  9. * @brief //1.如果已经有呼救告警
  10. //2.web取消告警。status_help && status_help_cancel 置为status_help_dealed
  11. //3.如果底层已经没有呼救则,取消后,不会再次进行呼救。因为status_help已经不会置为status_error。
  12. //4.如果底层还在呼救。这时候不管status_help是何种状态,极大的可能是error.status_help_cancel的存在都会取消。
  13. //并且还要保证等待线程把json传送到前端
  14. */
  15. void module_call_help::rev_from_card_help(std::shared_ptr<card_location_base> card_ptr)
  16. {
  17. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  18. event_tool::instance()->handle_event(OT_CARD, ET_CARD_HELP, id, 0, 0, true);
  19. if(card_ptr)card_ptr->set_event_flag(ET_CARD_HELP);
  20. }
  21. /**
  22. * @brief 接收到web端发来的处理呼救请求
  23. * @param data_map
  24. */
  25. void module_call_help::accept_web_deal_help(sio::message::ptr const& data)
  26. {
  27. int64_t ev_id = 0;
  28. std::string s_card_id;
  29. if(!tool_map::try_get_value(s_card_id, JSON_KEY_ID, data)
  30. ||!tool_map::try_get_value(ev_id, JSON_KEY_EVENT_EVENT_ID, data))
  31. {
  32. log_error("处理呼救,web发来的数据 card_id 或 ev_id格式不对");
  33. return;
  34. }
  35. uint32_t id = tool_other::id64_to_id(s_card_id);
  36. int type = tool_other::id64_to_type(s_card_id);
  37. auto card_ptr = card_list::instance()->get(tool_other::type_id_to_u64(type, id));
  38. if(card_ptr)
  39. {
  40. log_info("处理呼救,card_id=%d, type=%d, ev_id=%d", id, type, ev_id);
  41. auto ev_ptr = event_list::instance()->get_event_card(id, type, ET_CARD_HELP,DT_COMMON);
  42. if(ev_ptr && ES_DEAL_HELP != ev_ptr->m_status)
  43. {
  44. ev_ptr->m_status = ES_DEAL_HELP;
  45. }
  46. }
  47. else
  48. {
  49. log_error("处理呼救,在全局列表中找不到卡,卡id=%d,卡type=%d", id, type);
  50. }
  51. }
  52. std::string module_call_help::get_json_help()
  53. {
  54. rapidjson::Document doc(rapidjson::kObjectType);
  55. rapidjson::Value data(rapidjson::kArrayType);
  56. rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
  57. auto tmp = event_list::instance()->m_map;
  58. auto it_ev=tmp.begin();
  59. for(;it_ev!=tmp.end();++it_ev)
  60. {
  61. //if(!it_ev->second->m_is_display || ET_CARD_HELP != it_ev->second->m_ev_type)
  62. if(ET_CARD_HELP != it_ev->second->m_ev_type)
  63. {
  64. continue;
  65. }
  66. std::string obj_id = it_ev->second->m_obj_id;
  67. int type= tool_other::id64_to_type(obj_id);
  68. rapidjson::Value alarm(rapidjson::kObjectType);
  69. tool_json::add_member(alarm, JSON_KEY_CARD_ID,it_ev->second->m_obj_id, allocator);
  70. alarm.AddMember(JSON_KEY_CARD_TYPE_ID,type, allocator);
  71. std::string time = tool_time::to_str(it_ev->second->m_cur_time);
  72. tool_json::add_member(alarm, "start_time",time, allocator);
  73. data.PushBack(alarm, allocator);
  74. }
  75. if(!data.Size())
  76. {
  77. return "";
  78. }
  79. doc.AddMember(JSON_ROOT_KEY_CMD,"helpme_req", allocator);
  80. doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
  81. doc.AddMember(JSON_ROOT_KEY_DATA,data, allocator);
  82. rapidjson::StringBuffer sb;
  83. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  84. doc.Accept(writer);
  85. return sb.GetString();
  86. }