#include "stdafx.h" #include "Device.h" #include #include #include using namespace std; CDevice::CDevice(std::string a, std::string b, std::string c, std::string d) : taskid(a), m_station(b), m_mo(c), m_mp(d) { memset(&m_sensor_status, (int)SENSOR_STATUS::UNKONW, sizeof(m_sensor_status)); time(&m_tmLastActive); } CDevice::~CDevice() { } void CDevice::Insert(int index, time_t time, int data0, int data1, int data2) { //if (m_tmMoveDetectTime == 0) m_tmMoveDetectTime = time / 10000 * 10; if (index == 0) { InsertData(map_resist_idx00, time, data0); InsertData(m_mapSecondStatInfo00, time, data0); InsertData(map_resist_idx01, time, data1); InsertData(m_mapSecondStatInfo01, time, data1); InsertData(map_resist_idx02, time, data2); InsertData(m_mapSecondStatInfo02, time, data2); } else if (index == 1) { InsertData(map_resist_idx10, time, data0); InsertData(m_mapSecondStatInfo10, time, data0); InsertData(map_resist_idx11, time, data1); InsertData(m_mapSecondStatInfo11, time, data1); InsertData(map_resist_idx12, time, data2); InsertData(m_mapSecondStatInfo12, time, data2); } else if (index == 2) { InsertData(map_resist_idx20, time, data0); InsertData(m_mapSecondStatInfo20, time, data0); InsertData(map_resist_idx21, time, data1); InsertData(m_mapSecondStatInfo21, time, data1); InsertData(map_resist_idx22, time, data2); InsertData(m_mapSecondStatInfo22, time, data2); } else { assert(0); } } void CDevice::LoadHist() { string imei; if (imei.length() <= 5) return; auto start = chrono::steady_clock::now(); SYSTEMTIME stNow; GetLocalTime(&stNow); char tablename[300] = { 0 }; sprintf_s(tablename, 50, "rm_resistance_%04d%02d%02d", stNow.wYear, stNow.wMonth, stNow.wDay); CString sql; #ifdef _DEBUG sql.Format("SELECT TOP 1000 [acquisitiontime],[idx],[data0],[data1],[data2] FROM %s WHERE IMEI = '%s' order by acquisitiontime, idx", tablename, imei.c_str()); #else sql.Format("SELECT [acquisitiontime],[idx],[data0],[data1],[data2] FROM %s WHERE IMEI = '%s' AND acquisitiontime > dateadd(hour,-1,GETDATE()) order by acquisitiontime, idx", tablename, imei.c_str()); #endif // _DEBUG COdbcStatement stmt; #ifndef _DEBUG if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) { CSimpleLog::Error("sql语句执行失败:" + sql); return; } TIMESTAMP_STRUCT ts; uint8_t idx; int data0, data1, data2; int nCol = 1; stmt.BindTimeStampCol(nCol++, &ts); stmt.BindTinyIntCol(nCol++, &idx); stmt.BindIntCol(nCol++, &data0); stmt.BindIntCol(nCol++, &data1); stmt.BindIntCol(nCol++, &data2); do { if (stmt.FetchNext() != 0) break; CTime ctTime; try { ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); } catch (...) { continue; } time_t tt = ctTime.GetTime() * 1000 + ts.fraction / 1000000; Insert(idx, tt, data0, data1, data2); } while (true); this->map_resist_idx00, this->map_resist_idx01, this->map_resist_idx02; auto dif = chrono::duration_cast(chrono::steady_clock::now() - start).count(); if (dif > 100) { sprintf_s(tablename, 300, "执行语句耗时:(%I64d)ms.(%s)", dif, (LPCSTR)sql); SPDLOG_INFO(tablename); } #endif // _DEBUG //加载设备心跳时间 { TIMESTAMP_STRUCT ts; COdbcStatement stmt; sql.Format("SELECT updatetime FROM rm_deviceinfo WHERE IMEI = '%s';", imei.c_str()); if (CDBConnectPool::Instance()->DBQuery(stmt, sql)) { stmt.BindTimeStampCol(1, &ts); if (stmt.FetchNext() == 0) { try { m_ctUpdateTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); } catch (...) {}; } } else { CSimpleLog::Error("sql语句执行失败:" + sql); } } } std::map* CDevice::GetMapData(uint8_t idx, uint8_t no) { if (idx > 2 || no > 2) return nullptr; else if (idx == 0 && no == 0) return &map_resist_idx00; else if (idx == 0 && no == 1) return &map_resist_idx01; else if (idx == 0 && no == 2) return &map_resist_idx02; else if (idx == 1 && no == 0) return &map_resist_idx10; else if (idx == 1 && no == 1) return &map_resist_idx11; else if (idx == 1 && no == 2) return &map_resist_idx12; else if (idx == 2 && no == 0) return &map_resist_idx20; else if (idx == 2 && no == 1) return &map_resist_idx21; else if (idx == 2 && no == 2) return &map_resist_idx22; return nullptr; } void CDevice::InsertData(std::map& map, time_t time, int value) { if (map.size() >= 2) { auto it = map.end(); auto last = --it; auto second_last = --it; if (time / 1000 == last->first / 1000 && second_last->first / 1000 == time / 1000) { if (abs(last->second - value) <= 30 && abs(second_last->second - value) <= 30 && abs(last->second - second_last->second) <= 30) { #ifdef _DEBUG TRACE("%d:%d.%d\r\n", time % 100000, last->first % 100000, (time - last->first) % 1000); #endif // _DEBUG //都相等 先删除 map.erase(last); } } else if (last->first == second_last->first + 980 && abs(last->second - second_last->second) <= 30) { map.erase(last); } map[time] = value; } else { map[time] = value; } auto it = map.begin(); if ((time - it->first) > MAX_SAVE_TIME_MILLI) map.erase(it); } CDeviceMng::CDeviceMng() { } CDeviceMng::~CDeviceMng() { lock_guard lock(m_mtx); for (auto& it : m_map_devices) { delete it.second; it.second = nullptr; } m_map_devices.clear(); } void CDeviceMng::LoadDevice() { for (auto& it : m_map_devices) delete it.second; m_map_devices.clear(); char mo[50]; char mp[50]; char name1[20]; char name2[20]; char name3[20]; char direct1[20];//伸出 char direct2[20];//缩进 char station[50]; char mo_name[20]; CString sql = "SELECT mo, mp, [name1], [name2], [name3], [direct1], [direct2],ISNULL([up],''),ISNULL([name],'') FROM rm_map AS a LEFT JOIN rm_mo as b ON a.mo = b.id"; COdbcStatement stmt; if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) { SPDLOG_ERROR("语句执行错误:{}", sql); return; } int nCol = 1; stmt.BindCharCol(nCol++, mo, sizeof(mo)); stmt.BindCharCol(nCol++, mp, sizeof(mp)); stmt.BindCharCol(nCol++, name1, sizeof(name1)); stmt.BindCharCol(nCol++, name2, sizeof(name2)); stmt.BindCharCol(nCol++, name3, sizeof(name3)); stmt.BindCharCol(nCol++, direct1, sizeof(direct1)); stmt.BindCharCol(nCol++, direct2, sizeof(direct2)); stmt.BindCharCol(nCol++, station, sizeof(station)); stmt.BindCharCol(nCol++, mo_name, sizeof(mo_name)); lock_guard lock(m_mtx); while (true) { memset(mo, 0, sizeof(mo)); memset(mp, 0, sizeof(mp)); memset(name1, 0, sizeof(name1)); memset(name2, 0, sizeof(name2)); memset(name3, 0, sizeof(name3)); memset(direct1, 0, sizeof(direct1)); memset(direct2, 0, sizeof(direct2)); memset(station, 0, sizeof(station)); memset(mo_name, 0, sizeof(mo_name)); if (stmt.FetchNext() != 0) { break; } auto taskid = fmt::format("{}.{}", mo, mp); auto pDevice = new CDevice(taskid, station, mo, mp); pDevice->name1 = name1; pDevice->name2 = name2; pDevice->name3 = name3; pDevice->direct1 = direct1; pDevice->direct2 = direct2; pDevice->mo_name = mo_name; pDevice->mp_name = mp; m_map_devices[taskid] = pDevice; } } bool CDeviceMng::Insert(CDevice* p) { lock_guard lock(m_mtx); m_map_devices[p->taskid] = p; return true; } CDevice* CDeviceMng::Find(std::string taskid) { lock_guard lock(m_mtx); auto it = m_map_devices.find(taskid); if (it == m_map_devices.end()) return nullptr; else return it->second; } BOOL CDeviceMng::IsDeviceOnline(std::string taskid, int interval /*= 180000*/) { time_t tmNow; time(&tmNow); lock_guard lock(m_mtx); auto it = m_map_devices.find(taskid); if (it == m_map_devices.end()) return FALSE; return it->second->IsDeviceOnline(tmNow * 1000, interval); } CDeviceMng CDeviceMng::obj;