Device.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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()
  8. {
  9. memset(&m_sensor_status, (int)SENSOR_STATUS::UNKONW, sizeof(m_sensor_status));
  10. }
  11. CDevice::~CDevice()
  12. {
  13. }
  14. void CDevice::Insert(int index, time_t time, int data0, int data1, int data2)
  15. {
  16. //if (m_tmMoveDetectTime == 0) m_tmMoveDetectTime = time / 10000 * 10;
  17. lock_guard<mutex> lock(m_mtx);
  18. this->imei;
  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. if (imei.length() <= 5) return;
  54. auto start = chrono::steady_clock::now();
  55. SYSTEMTIME stNow;
  56. GetLocalTime(&stNow);
  57. char tablename[300] = { 0 };
  58. sprintf_s(tablename, 50, "rm_resistance_%04d%02d%02d", stNow.wYear, stNow.wMonth, stNow.wDay);
  59. CString sql;
  60. #ifdef _DEBUG
  61. sql.Format("SELECT TOP 1000 [acquisitiontime],[idx],[data0],[data1],[data2] FROM %s WHERE IMEI = '%s' order by acquisitiontime, idx", tablename, imei.c_str());
  62. #else
  63. 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());
  64. #endif // _DEBUG
  65. COdbcStatement stmt;
  66. #ifndef _DEBUG
  67. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  68. {
  69. CSimpleLog::Error("sql语句执行失败:" + sql);
  70. return;
  71. }
  72. TIMESTAMP_STRUCT ts;
  73. uint8_t idx;
  74. int data0, data1, data2;
  75. int nCol = 1;
  76. stmt.BindTimeStampCol(nCol++, &ts);
  77. stmt.BindTinyIntCol(nCol++, &idx);
  78. stmt.BindIntCol(nCol++, &data0);
  79. stmt.BindIntCol(nCol++, &data1);
  80. stmt.BindIntCol(nCol++, &data2);
  81. do
  82. {
  83. if (stmt.FetchNext() != 0)
  84. break;
  85. CTime ctTime;
  86. try
  87. {
  88. ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  89. }
  90. catch (...)
  91. {
  92. continue;
  93. }
  94. time_t tt = ctTime.GetTime() * 1000 + ts.fraction / 1000000;
  95. Insert(idx, tt, data0, data1, data2);
  96. } while (true);
  97. this->map_resist_idx00, this->map_resist_idx01, this->map_resist_idx02;
  98. auto dif = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count();
  99. if (dif > 100)
  100. {
  101. sprintf_s(tablename, 300, "执行语句耗时:(%I64d)ms.(%s)", dif, (LPCSTR)sql);
  102. CSimpleLog::Info(tablename);
  103. }
  104. #endif // _DEBUG
  105. //加载设备心跳时间
  106. {
  107. TIMESTAMP_STRUCT ts;
  108. COdbcStatement stmt;
  109. sql.Format("SELECT updatetime FROM rm_deviceinfo WHERE IMEI = '%s';", imei.c_str());
  110. if (CDBConnectPool::Instance()->DBQuery(stmt, sql))
  111. {
  112. stmt.BindTimeStampCol(1, &ts);
  113. if (stmt.FetchNext() == 0)
  114. {
  115. try {
  116. m_ctUpdateTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  117. }
  118. catch (...) {};
  119. }
  120. }
  121. else
  122. {
  123. CSimpleLog::Error("sql语句执行失败:" + sql);
  124. }
  125. }
  126. }
  127. std::map<time_t, int>* CDevice::GetMapData(uint8_t idx, uint8_t no)
  128. {
  129. if (idx > 2 || no > 2)
  130. return nullptr;
  131. else if (idx == 0 && no == 0)
  132. return &map_resist_idx00;
  133. else if (idx == 0 && no == 1)
  134. return &map_resist_idx01;
  135. else if (idx == 0 && no == 2)
  136. return &map_resist_idx02;
  137. else if (idx == 1 && no == 0)
  138. return &map_resist_idx10;
  139. else if (idx == 1 && no == 1)
  140. return &map_resist_idx11;
  141. else if (idx == 1 && no == 2)
  142. return &map_resist_idx12;
  143. else if (idx == 2 && no == 0)
  144. return &map_resist_idx20;
  145. else if (idx == 2 && no == 1)
  146. return &map_resist_idx21;
  147. else if (idx == 2 && no == 2)
  148. return &map_resist_idx22;
  149. return nullptr;
  150. }
  151. std::map<time_t, tagSecondStatInfo>* CDevice::GetStatInfo(uint8_t idx, uint8_t no)
  152. {
  153. if (idx > 2 || no > 2)
  154. return nullptr;
  155. else if (idx == 0 && no == 0)
  156. return &m_mapSecondStatInfo00;
  157. else if (idx == 0 && no == 1)
  158. return &m_mapSecondStatInfo01;
  159. else if (idx == 0 && no == 2)
  160. return &m_mapSecondStatInfo02;
  161. else if (idx == 1 && no == 0)
  162. return &m_mapSecondStatInfo10;
  163. else if (idx == 1 && no == 1)
  164. return &m_mapSecondStatInfo11;
  165. else if (idx == 1 && no == 2)
  166. return &m_mapSecondStatInfo12;
  167. else if (idx == 2 && no == 0)
  168. return &m_mapSecondStatInfo20;
  169. else if (idx == 2 && no == 1)
  170. return &m_mapSecondStatInfo21;
  171. else if (idx == 2 && no == 2)
  172. return &m_mapSecondStatInfo22;
  173. return nullptr;
  174. }
  175. BOOL CDevice::IsDeviceOnline(uint8_t idx, time_t tmNow, int inteval)
  176. {
  177. if (idx == 0)
  178. {
  179. if (map_resist_idx00.size() == 0) return FALSE;
  180. if (tmNow - map_resist_idx00.rbegin()->first < inteval)
  181. return TRUE;
  182. }
  183. else if (idx == 1)
  184. {
  185. if (map_resist_idx10.size() == 0) return FALSE;
  186. auto tt = map_resist_idx10.rbegin()->first;
  187. if (tmNow - tt < inteval)
  188. return TRUE;
  189. }
  190. else if (idx == 2)
  191. {
  192. if (map_resist_idx20.size() == 0) return FALSE;
  193. if (tmNow - map_resist_idx20.rbegin()->first < inteval)
  194. return TRUE;
  195. }
  196. return FALSE;
  197. }
  198. CDeviceMng::CDeviceMng()
  199. {
  200. }
  201. CDeviceMng::~CDeviceMng()
  202. {
  203. lock_guard<mutex> lock(m_mtx);
  204. for (auto& it : m_map_devices)
  205. {
  206. delete it.second;
  207. it.second = nullptr;
  208. }
  209. m_map_devices.clear();
  210. }
  211. CDevice* CDeviceMng::Insert(std::string imei)
  212. {
  213. lock_guard<mutex> lock(m_mtx);
  214. auto it = m_map_devices.find(imei);
  215. if (it == m_map_devices.end())
  216. {
  217. auto ret = m_map_devices.insert(make_pair(imei, new CDevice));
  218. assert(ret.second == true);
  219. if (ret.second)
  220. {
  221. ret.first->second->imei = imei;
  222. ret.first->second->LoadHist();
  223. return ret.first->second;
  224. }
  225. else delete ret.first->second;
  226. return nullptr;
  227. }
  228. else
  229. {
  230. return it->second;
  231. }
  232. }
  233. CDevice* CDeviceMng::Find(std::string imei)
  234. {
  235. lock_guard<mutex> lock(m_mtx);
  236. auto it = m_map_devices.find(imei);
  237. if (it == m_map_devices.end()) return nullptr;
  238. else return it->second;
  239. }
  240. BOOL CDeviceMng::IsDeviceOnline(std::string imei, int8_t idx, int interval /*= 180000*/)
  241. {
  242. time_t tmNow;
  243. time(&tmNow);
  244. lock_guard<mutex> lock(m_mtx);
  245. auto it = m_map_devices.find(imei);
  246. if (it == m_map_devices.end()) return FALSE;
  247. return it->second->IsDeviceOnline(idx, tmNow * 1000, interval);
  248. }
  249. CDeviceMng CDeviceMng::obj;