1
0

module_device_net.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "module_device_net.h"
  2. #include <algorithm>
  3. #include "log.h"
  4. #include "db/db_tool.h"
  5. #include "tool_byte.h"
  6. #include "tool_time.h"
  7. void module_device_net::do_business(const std::string& ip, const int& id, const int& type)
  8. {
  9. // 如果IP设备不存在,则创建设备列表
  10. if(!exist(ip)){
  11. std::shared_ptr<net_device_info> ptr_data = std::make_shared<net_device_info>(ip);
  12. m_socket_device_list.insert(std::make_pair(ip, ptr_data));
  13. }
  14. auto it = m_socket_device_list.find(ip);
  15. if(it == m_socket_device_list.end()){
  16. return;
  17. }
  18. std::string did = tool_byte::to_cardid(type, id);
  19. if(it->second->exist(did)){
  20. return;
  21. }
  22. // 不存在设备,则创建设备网络连接信息
  23. device_info data(did, id, type, time(0));
  24. it->second->m_device_list.insert(std::make_pair(did, data));
  25. char sql[500] = {0};
  26. snprintf(sql, 500, "INSERT INTO his_device_net_log(device_id, device_type, connect_time) VALUES(%d, %d,'%s');", id, type, tool_time::to_str(data.m_time).c_str());
  27. //logn_info(2, "[device-net] %s", sql);
  28. db_tool::PushAsync(sql);
  29. }
  30. bool module_device_net::exist(const std::string& ip)
  31. {
  32. std::lock_guard<std::mutex> lg(m_mtx);
  33. auto it = m_socket_device_list.find(ip);
  34. if(it != m_socket_device_list.end()){
  35. return true;
  36. }
  37. return false;
  38. }
  39. void module_device_net::clear()
  40. {
  41. std::lock_guard<std::mutex> lg(m_mtx);
  42. for(auto it : m_socket_device_list)
  43. {
  44. clear(it.first);
  45. }
  46. }
  47. void module_device_net::load_db()
  48. {
  49. }
  50. void module_device_net::clear(const std::string& ip)
  51. {
  52. std::lock_guard<std::mutex> lg(m_mtx);
  53. log_info("[device-net] clear ip=%s", ip.c_str());
  54. auto it = std::find_if(m_socket_device_list.begin(), m_socket_device_list.end(), [&](std::pair<std::string, std::shared_ptr<net_device_info>> item){
  55. log_info("[device-net] exist ip=%s", item.second->m_ip.c_str());
  56. if(item.second->m_ip == ip){
  57. return true;
  58. }else{
  59. return false;
  60. }
  61. });
  62. if(it == m_socket_device_list.end()){
  63. return;
  64. }
  65. if(it->second->m_device_list.size() <= 0){
  66. log_info("[device-net] there is no device which the ip is %s", ip.c_str());
  67. return;
  68. }
  69. // 构造sql插入数据库
  70. for(auto itd : it->second->m_device_list){
  71. int dt = 0;
  72. dt = time(0) - itd.second.m_time;
  73. char sql[500] = {0};
  74. snprintf(sql, 500, "UPDATE his_device_net_log SET disconnect_time = '%s' , duration=%d WHERE device_id=%d AND device_type=%d AND connect_time='%s';", tool_time::to_str(time(0)).c_str(), dt , itd.second.m_id, itd.second.m_type, tool_time::to_str(itd.second.m_time).c_str());
  75. db_tool::PushAsync(sql);
  76. //logn_info(2, "[device-net] %s", sql);
  77. }
  78. m_socket_device_list.erase(it);
  79. }