Device.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "stdafx.h"
  2. #include "Device.h"
  3. #include <ODBC/DBConnectPool.h>
  4. #include <Simplelog.h>
  5. #include <assert.h>
  6. using namespace std;
  7. CDevice::CDevice(std::string a, std::string b, std::string c, std::string d) :
  8. taskid(a), m_station(b), m_mo(c), m_mp(d)
  9. {
  10. memset(&m_sensor_status, (int)SENSOR_STATUS::UNKONW, sizeof(m_sensor_status));
  11. time(&m_tmLastActive);
  12. }
  13. CDevice::~CDevice()
  14. {
  15. }
  16. void CDevice::Insert(int index, time_t time, int data0, int data1, int data2)
  17. {
  18. //if (m_tmMoveDetectTime == 0) m_tmMoveDetectTime = time / 10000 * 10;
  19. if (index == 0)
  20. {
  21. InsertData(map_resist_idx00, time, data0);
  22. InsertData(m_mapSecondStatInfo00, time, data0);
  23. InsertData(map_resist_idx01, time, data1);
  24. InsertData(m_mapSecondStatInfo01, time, data1);
  25. InsertData(map_resist_idx02, time, data2);
  26. InsertData(m_mapSecondStatInfo02, time, data2);
  27. }
  28. else if (index == 1)
  29. {
  30. InsertData(map_resist_idx10, time, data0);
  31. InsertData(m_mapSecondStatInfo10, time, data0);
  32. InsertData(map_resist_idx11, time, data1);
  33. InsertData(m_mapSecondStatInfo11, time, data1);
  34. InsertData(map_resist_idx12, time, data2);
  35. InsertData(m_mapSecondStatInfo12, time, data2);
  36. }
  37. else if (index == 2)
  38. {
  39. InsertData(map_resist_idx20, time, data0);
  40. InsertData(m_mapSecondStatInfo20, time, data0);
  41. InsertData(map_resist_idx21, time, data1);
  42. InsertData(m_mapSecondStatInfo21, time, data1);
  43. InsertData(map_resist_idx22, time, data2);
  44. InsertData(m_mapSecondStatInfo22, time, data2);
  45. }
  46. else
  47. {
  48. assert(0);
  49. }
  50. }
  51. void CDevice::LoadHist()
  52. {
  53. string imei;
  54. if (imei.length() <= 5) return;
  55. auto start = chrono::steady_clock::now();
  56. SYSTEMTIME stNow;
  57. GetLocalTime(&stNow);
  58. char tablename[300] = { 0 };
  59. sprintf_s(tablename, 50, "rm_resistance_%04d%02d%02d", stNow.wYear, stNow.wMonth, stNow.wDay);
  60. CString sql;
  61. #ifdef _DEBUG
  62. sql.Format("SELECT TOP 1000 [acquisitiontime],[idx],[data0],[data1],[data2] FROM %s WHERE IMEI = '%s' order by acquisitiontime, idx", tablename, imei.c_str());
  63. #else
  64. 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());
  65. #endif // _DEBUG
  66. COdbcStatement stmt;
  67. #ifndef _DEBUG
  68. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  69. {
  70. CSimpleLog::Error("sql语句执行失败:" + sql);
  71. return;
  72. }
  73. TIMESTAMP_STRUCT ts;
  74. uint8_t idx;
  75. int data0, data1, data2;
  76. int nCol = 1;
  77. stmt.BindTimeStampCol(nCol++, &ts);
  78. stmt.BindTinyIntCol(nCol++, &idx);
  79. stmt.BindIntCol(nCol++, &data0);
  80. stmt.BindIntCol(nCol++, &data1);
  81. stmt.BindIntCol(nCol++, &data2);
  82. do
  83. {
  84. if (stmt.FetchNext() != 0)
  85. break;
  86. CTime ctTime;
  87. try
  88. {
  89. ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  90. }
  91. catch (...)
  92. {
  93. continue;
  94. }
  95. time_t tt = ctTime.GetTime() * 1000 + ts.fraction / 1000000;
  96. Insert(idx, tt, data0, data1, data2);
  97. } while (true);
  98. this->map_resist_idx00, this->map_resist_idx01, this->map_resist_idx02;
  99. auto dif = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count();
  100. if (dif > 100)
  101. {
  102. sprintf_s(tablename, 300, "执行语句耗时:(%I64d)ms.(%s)", dif, (LPCSTR)sql);
  103. SPDLOG_INFO(tablename);
  104. }
  105. #endif // _DEBUG
  106. //加载设备心跳时间
  107. {
  108. TIMESTAMP_STRUCT ts;
  109. COdbcStatement stmt;
  110. sql.Format("SELECT updatetime FROM rm_deviceinfo WHERE IMEI = '%s';", imei.c_str());
  111. if (CDBConnectPool::Instance()->DBQuery(stmt, sql))
  112. {
  113. stmt.BindTimeStampCol(1, &ts);
  114. if (stmt.FetchNext() == 0)
  115. {
  116. try {
  117. m_ctUpdateTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  118. }
  119. catch (...) {};
  120. }
  121. }
  122. else
  123. {
  124. CSimpleLog::Error("sql语句执行失败:" + sql);
  125. }
  126. }
  127. }
  128. std::map<time_t, int>* CDevice::GetMapData(uint8_t idx, uint8_t no)
  129. {
  130. if (idx > 2 || no > 2)
  131. return nullptr;
  132. else if (idx == 0 && no == 0)
  133. return &map_resist_idx00;
  134. else if (idx == 0 && no == 1)
  135. return &map_resist_idx01;
  136. else if (idx == 0 && no == 2)
  137. return &map_resist_idx02;
  138. else if (idx == 1 && no == 0)
  139. return &map_resist_idx10;
  140. else if (idx == 1 && no == 1)
  141. return &map_resist_idx11;
  142. else if (idx == 1 && no == 2)
  143. return &map_resist_idx12;
  144. else if (idx == 2 && no == 0)
  145. return &map_resist_idx20;
  146. else if (idx == 2 && no == 1)
  147. return &map_resist_idx21;
  148. else if (idx == 2 && no == 2)
  149. return &map_resist_idx22;
  150. return nullptr;
  151. }
  152. void CDevice::InsertData(std::map<time_t, int>& map, time_t time, int value)
  153. {
  154. if (map.size() >= 2)
  155. {
  156. auto it = map.end();
  157. auto last = --it;
  158. auto second_last = --it;
  159. if (time / 1000 == last->first / 1000 && second_last->first / 1000 == time / 1000)
  160. {
  161. if (abs(last->second - value) <= 30 && abs(second_last->second - value) <= 30 && abs(last->second - second_last->second) <= 30)
  162. {
  163. #ifdef _DEBUG
  164. TRACE("%d:%d.%d\r\n", time % 100000, last->first % 100000, (time - last->first) % 1000);
  165. #endif // _DEBUG
  166. //都相等 先删除
  167. map.erase(last);
  168. }
  169. }
  170. else if (last->first == second_last->first + 980 && abs(last->second - second_last->second) <= 30)
  171. {
  172. map.erase(last);
  173. }
  174. map[time] = value;
  175. }
  176. else
  177. {
  178. map[time] = value;
  179. }
  180. auto it = map.begin();
  181. if ((time - it->first) > MAX_SAVE_TIME_MILLI) map.erase(it);
  182. }
  183. CDeviceMng::CDeviceMng()
  184. {
  185. }
  186. CDeviceMng::~CDeviceMng()
  187. {
  188. lock_guard<mutex> lock(m_mtx);
  189. for (auto& it : m_map_devices)
  190. {
  191. delete it.second;
  192. it.second = nullptr;
  193. }
  194. m_map_devices.clear();
  195. }
  196. void CDeviceMng::LoadDevice()
  197. {
  198. for (auto& it : m_map_devices)
  199. delete it.second;
  200. m_map_devices.clear();
  201. char mo[50];
  202. char mp[50];
  203. char name1[20];
  204. char name2[20];
  205. char name3[20];
  206. char direct1[20];//伸出
  207. char direct2[20];//缩进
  208. char station[50];
  209. char mo_name[20];
  210. 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";
  211. COdbcStatement stmt;
  212. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  213. {
  214. SPDLOG_ERROR("语句执行错误:{}", sql);
  215. return;
  216. }
  217. int nCol = 1;
  218. stmt.BindCharCol(nCol++, mo, sizeof(mo));
  219. stmt.BindCharCol(nCol++, mp, sizeof(mp));
  220. stmt.BindCharCol(nCol++, name1, sizeof(name1));
  221. stmt.BindCharCol(nCol++, name2, sizeof(name2));
  222. stmt.BindCharCol(nCol++, name3, sizeof(name3));
  223. stmt.BindCharCol(nCol++, direct1, sizeof(direct1));
  224. stmt.BindCharCol(nCol++, direct2, sizeof(direct2));
  225. stmt.BindCharCol(nCol++, station, sizeof(station));
  226. stmt.BindCharCol(nCol++, mo_name, sizeof(mo_name));
  227. lock_guard<mutex> lock(m_mtx);
  228. while (true)
  229. {
  230. memset(mo, 0, sizeof(mo));
  231. memset(mp, 0, sizeof(mp));
  232. memset(name1, 0, sizeof(name1));
  233. memset(name2, 0, sizeof(name2));
  234. memset(name3, 0, sizeof(name3));
  235. memset(direct1, 0, sizeof(direct1));
  236. memset(direct2, 0, sizeof(direct2));
  237. memset(station, 0, sizeof(station));
  238. memset(mo_name, 0, sizeof(mo_name));
  239. if (stmt.FetchNext() != 0)
  240. {
  241. break;
  242. }
  243. auto taskid = fmt::format("{}.{}", mo, mp);
  244. auto pDevice = new CDevice(taskid, station, mo, mp);
  245. pDevice->name1 = name1;
  246. pDevice->name2 = name2;
  247. pDevice->name3 = name3;
  248. pDevice->direct1 = direct1;
  249. pDevice->direct2 = direct2;
  250. pDevice->mo_name = mo_name;
  251. pDevice->mp_name = mp;
  252. m_map_devices[taskid] = pDevice;
  253. }
  254. }
  255. bool CDeviceMng::Insert(CDevice* p)
  256. {
  257. lock_guard<mutex> lock(m_mtx);
  258. m_map_devices[p->taskid] = p;
  259. return true;
  260. }
  261. CDevice* CDeviceMng::Find(std::string taskid)
  262. {
  263. lock_guard<mutex> lock(m_mtx);
  264. auto it = m_map_devices.find(taskid);
  265. if (it == m_map_devices.end()) return nullptr;
  266. else return it->second;
  267. }
  268. BOOL CDeviceMng::IsDeviceOnline(std::string taskid, int interval /*= 180000*/)
  269. {
  270. time_t tmNow;
  271. time(&tmNow);
  272. lock_guard<mutex> lock(m_mtx);
  273. auto it = m_map_devices.find(taskid);
  274. if (it == m_map_devices.end()) return FALSE;
  275. return it->second->IsDeviceOnline(tmNow * 1000, interval);
  276. }
  277. CDeviceMng CDeviceMng::obj;