module_screen.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <string.h>
  2. #include "db/db_tool.h"
  3. #include "module_screen.h"
  4. #include "log.h"
  5. #include "common.h"
  6. #include <chrono>
  7. /*
  8. * @brief 更新大屏内显示所需人员id,区域id,部门id,电池电量状态等数据
  9. * @param 卡数据
  10. * @return 无
  11. * @note
  12. * @warning
  13. * @bug
  14. *
  15. * */
  16. void module_screen::do_write(const std::map<uint64_t, sys::_CARD_POS_> card_pos_list)
  17. {
  18. std::string p_stat = "";
  19. char item[200] = { 0 };
  20. for(auto it : card_pos_list)
  21. {
  22. memset(item, 0, 200*sizeof(char));
  23. // 电量状态0表示正常,1表示电量不足,
  24. snprintf(item, 200, "%d,%d,%d,%d,%d,%d;",it.second.ID, it.second.area_id, it.second.dept_id, (it.second.battery_stat), it.second.biz_stat, it.second.level_id);
  25. p_stat += std::string(item);
  26. }
  27. if(p_stat == ""){
  28. return;
  29. }
  30. log_info("[screen] stat info=%s", p_stat.c_str());
  31. char sql[4096] = {0};
  32. snprintf(sql, 4096, "update rt_screen_msg set person_stat='%s' where id=1;", p_stat.c_str());
  33. log_info("[screen] sql=%s", sql);
  34. db_tool::PushAsync(sql);
  35. }
  36. void module_screen::start()
  37. {
  38. m_exited = false;
  39. m_thread.reset(new std::thread(std::bind(&module_screen::run, this)));
  40. m_thread->detach();
  41. }
  42. void module_screen::run()
  43. {
  44. while(!m_exited){
  45. do_wellhead();
  46. std::this_thread::sleep_for(std::chrono::seconds(1));
  47. }
  48. }
  49. /*
  50. * @brief 更新井口人员数据,并更新电量低人员:小于等于30%
  51. * @param 无
  52. * @return 无
  53. * @note
  54. * @warning
  55. * @bug
  56. *
  57. * */
  58. void module_screen::do_wellhead()
  59. {
  60. std::lock_guard<std::mutex> lg(m_mtx);
  61. if(m_card_list.size() <= 0){
  62. return;
  63. }
  64. std::string sid = "";
  65. char buf[12] = {0};
  66. for(auto it = m_card_list.begin(); it != m_card_list.end();){
  67. if(time(NULL) - it->second.m_recv_time < 120){
  68. snprintf(buf, 12, "%ld,%d;", it->first, it->second.m_battery);
  69. sid += std::string(buf);
  70. it++;
  71. }else{
  72. m_card_list.erase(it++);
  73. }
  74. }
  75. char sql[4096] = {0};
  76. snprintf(sql, 4096, "update rt_screen_msg set wellhead_stat='%s' where id=1;", sid.c_str());
  77. log_info("[screen] sql=%s", sql);
  78. db_tool::PushAsync(sql);
  79. }