1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include "module_device_net.h"
- #include <algorithm>
- #include "log.h"
- #include "db/db_tool.h"
- #include "tool_byte.h"
- #include "tool_time.h"
- void module_device_net::do_business(const std::string& ip, const int& id, const int& type)
- {
- // 如果IP设备不存在,则创建设备列表
- if(!exist(ip)){
- std::shared_ptr<net_device_info> ptr_data = std::make_shared<net_device_info>(ip);
- m_socket_device_list.insert(std::make_pair(ip, ptr_data));
- }
- auto it = m_socket_device_list.find(ip);
- if(it == m_socket_device_list.end()){
- return;
- }
- std::string did = tool_byte::to_cardid(type, id);
- if(it->second->exist(did)){
- return;
- }
- // 不存在设备,则创建设备网络连接信息
- device_info data(did, id, type, time(0));
- it->second->m_device_list.insert(std::make_pair(did, data));
- char sql[500] = {0};
- 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());
- //logn_info(2, "[device-net] %s", sql);
- db_tool::PushAsync(sql);
- }
- bool module_device_net::exist(const std::string& ip)
- {
- std::lock_guard<std::mutex> lg(m_mtx);
- auto it = m_socket_device_list.find(ip);
- if(it != m_socket_device_list.end()){
- return true;
- }
- return false;
- }
- void module_device_net::clear()
- {
- std::lock_guard<std::mutex> lg(m_mtx);
- for(auto it : m_socket_device_list)
- {
- clear(it.first);
- }
- }
- void module_device_net::load_db()
- {
- }
- void module_device_net::clear(const std::string& ip)
- {
- std::lock_guard<std::mutex> lg(m_mtx);
- log_info("[device-net] clear ip=%s", ip.c_str());
- 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){
- log_info("[device-net] exist ip=%s", item.second->m_ip.c_str());
- if(item.second->m_ip == ip){
- return true;
- }else{
- return false;
- }
- });
- if(it == m_socket_device_list.end()){
- return;
- }
- if(it->second->m_device_list.size() <= 0){
- log_info("[device-net] there is no device which the ip is %s", ip.c_str());
- return;
- }
- // 构造sql插入数据库
- for(auto itd : it->second->m_device_list){
- int dt = 0;
- dt = time(0) - itd.second.m_time;
- char sql[500] = {0};
- 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());
- db_tool::PushAsync(sql);
- //logn_info(2, "[device-net] %s", sql);
- }
- m_socket_device_list.erase(it);
- }
|