Device.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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. CSimpleLog::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. std::map<time_t, tagSecondStatInfo>* CDevice::GetStatInfo(uint8_t idx, uint8_t no)
  153. {
  154. if (idx > 2 || no > 2)
  155. return nullptr;
  156. else if (idx == 0 && no == 0)
  157. return &m_mapSecondStatInfo00;
  158. else if (idx == 0 && no == 1)
  159. return &m_mapSecondStatInfo01;
  160. else if (idx == 0 && no == 2)
  161. return &m_mapSecondStatInfo02;
  162. else if (idx == 1 && no == 0)
  163. return &m_mapSecondStatInfo10;
  164. else if (idx == 1 && no == 1)
  165. return &m_mapSecondStatInfo11;
  166. else if (idx == 1 && no == 2)
  167. return &m_mapSecondStatInfo12;
  168. else if (idx == 2 && no == 0)
  169. return &m_mapSecondStatInfo20;
  170. else if (idx == 2 && no == 1)
  171. return &m_mapSecondStatInfo21;
  172. else if (idx == 2 && no == 2)
  173. return &m_mapSecondStatInfo22;
  174. return nullptr;
  175. }
  176. BOOL CDevice::IsDeviceOnline(uint8_t idx, time_t tmNow, int inteval)
  177. {
  178. if (idx == 0)
  179. {
  180. if (tmNow - m_tmUpdateTime0 < inteval)
  181. return TRUE;
  182. }
  183. else if (idx == 1)
  184. {
  185. if (tmNow - m_tmUpdateTime0 < inteval)
  186. return TRUE;
  187. }
  188. else if (idx == 2)
  189. {
  190. if (tmNow - m_tmUpdateTime0 < inteval)
  191. return TRUE;
  192. }
  193. return FALSE;
  194. }
  195. CDeviceMng::CDeviceMng()
  196. {
  197. }
  198. CDeviceMng::~CDeviceMng()
  199. {
  200. lock_guard<mutex> lock(m_mtx);
  201. for (auto& it : m_map_devices)
  202. {
  203. delete it.second;
  204. it.second = nullptr;
  205. }
  206. m_map_devices.clear();
  207. }
  208. CDevice* CDeviceMng::Insert(std::string imei)
  209. {
  210. lock_guard<mutex> lock(m_mtx);
  211. auto it = m_map_devices.find(imei);
  212. if (it == m_map_devices.end())
  213. {
  214. auto ret = m_map_devices.insert(make_pair(imei, new CDevice));
  215. assert(ret.second == true);
  216. if (ret.second)
  217. {
  218. ret.first->second->imei = imei;
  219. ret.first->second->LoadHist();
  220. return ret.first->second;
  221. }
  222. else delete ret.first->second;
  223. return nullptr;
  224. }
  225. else
  226. {
  227. return it->second;
  228. }
  229. }
  230. CDevice* CDeviceMng::Find(std::string imei)
  231. {
  232. lock_guard<mutex> lock(m_mtx);
  233. auto it = m_map_devices.find(imei);
  234. if (it == m_map_devices.end()) return nullptr;
  235. else return it->second;
  236. }
  237. BOOL CDeviceMng::IsDeviceOnline(std::string imei, int8_t idx, int interval /*= 180000*/)
  238. {
  239. time_t tmNow;
  240. time(&tmNow);
  241. lock_guard<mutex> lock(m_mtx);
  242. auto it = m_map_devices.find(imei);
  243. if (it == m_map_devices.end()) return FALSE;
  244. return it->second->IsDeviceOnline(idx, tmNow, interval);
  245. }
  246. CDeviceMng CDeviceMng::obj;