Device.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. // 写入传感器的状态信息
  15. bool CDevice::InsertStatus(const SENSOR_STATUS st[9][2], const int dt[9][2])
  16. {
  17. CString sql;
  18. sql.Format("UPDATE RM_DEVICEINFO "
  19. "set st0 = %d,"
  20. "st1 = %d,"
  21. "st2 = %d,"
  22. "st3 = %d,"
  23. "st4 = %d,"
  24. "st5 = %d,"
  25. "st6 = %d,"
  26. "st7 = %d,"
  27. "st8 = %d,"
  28. "dt0 = %d,"
  29. "dt1 = %d,"
  30. "dt2 = %d,"
  31. "dt3 = %d,"
  32. "dt4 = %d,"
  33. "dt5 = %d,"
  34. "dt6 = %d,"
  35. "dt7 = %d,"
  36. "dt8 = %d"
  37. " where imei = '%s';",
  38. st[0][0], st[1][0], st[2][0], st[3][0],
  39. st[4][0], st[5][0], st[6][0], st[7][0],
  40. st[8][0],
  41. dt[0][0], dt[1][0], dt[2][0], dt[3][0],
  42. dt[4][0], dt[5][0], dt[6][0], dt[7][0],
  43. dt[8][0],
  44. this->imei.c_str());
  45. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  46. {
  47. CSimpleLog::Error("执行语句失败" + sql);
  48. return false;
  49. }
  50. return true;
  51. }
  52. void CDevice::Insert(int index, time_t time, int data0, int data1, int data2)
  53. {
  54. //if (m_tmMoveDetectTime == 0) m_tmMoveDetectTime = time / 10000 * 10;
  55. lock_guard<mutex> lock(m_mtx);
  56. this->imei;
  57. if (index == 0)
  58. {
  59. InsertData(map_resist_idx00, time, data0);
  60. InsertData(m_mapSecondStatInfo00, time, data0);
  61. InsertData(map_resist_idx01, time, data1);
  62. InsertData(m_mapSecondStatInfo01, time, data1);
  63. InsertData(map_resist_idx02, time, data2);
  64. InsertData(m_mapSecondStatInfo02, time, data2);
  65. }
  66. else if (index == 1)
  67. {
  68. InsertData(map_resist_idx10, time, data0);
  69. InsertData(m_mapSecondStatInfo10, time, data0);
  70. InsertData(map_resist_idx11, time, data1);
  71. InsertData(m_mapSecondStatInfo11, time, data1);
  72. InsertData(map_resist_idx12, time, data2);
  73. InsertData(m_mapSecondStatInfo12, time, data2);
  74. }
  75. else if (index == 2)
  76. {
  77. InsertData(map_resist_idx20, time, data0);
  78. InsertData(m_mapSecondStatInfo20, time, data0);
  79. InsertData(map_resist_idx21, time, data1);
  80. InsertData(m_mapSecondStatInfo21, time, data1);
  81. InsertData(map_resist_idx22, time, data2);
  82. InsertData(m_mapSecondStatInfo22, time, data2);
  83. }
  84. else
  85. {
  86. assert(0);
  87. }
  88. }
  89. void CDevice::LoadHist()
  90. {
  91. if (imei.length() <= 5) return;
  92. CString sql;
  93. /*
  94. return; // 暂时不加载历史数据
  95. auto start = chrono::steady_clock::now();
  96. SYSTEMTIME stNow;
  97. GetLocalTime(&stNow);
  98. char tablename[300] = { 0 };
  99. sprintf_s(tablename, 50, "rm_resistance_%04d%02d%02d", stNow.wYear, stNow.wMonth, stNow.wDay);
  100. #ifdef _DEBUG
  101. sql.Format("SELECT TOP 1000 [acquisitiontime],[idx],[data0],[data1],[data2] FROM %s WHERE IMEI = '%s' order by acquisitiontime, idx", tablename, imei.c_str());
  102. #else
  103. 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());
  104. #endif // _DEBUG
  105. COdbcStatement stmt;
  106. #ifndef _DEBUG
  107. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  108. {
  109. CSimpleLog::Error("sql语句执行失败:" + sql);
  110. return;
  111. }
  112. TIMESTAMP_STRUCT ts;
  113. uint8_t idx;
  114. int data0, data1, data2;
  115. int nCol = 1;
  116. stmt.BindTimeStampCol(nCol++, &ts);
  117. stmt.BindTinyIntCol(nCol++, &idx);
  118. stmt.BindIntCol(nCol++, &data0);
  119. stmt.BindIntCol(nCol++, &data1);
  120. stmt.BindIntCol(nCol++, &data2);
  121. do
  122. {
  123. if (stmt.FetchNext() != 0)
  124. break;
  125. CTime ctTime;
  126. try
  127. {
  128. ctTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  129. }
  130. catch (...)
  131. {
  132. continue;
  133. }
  134. time_t tt = ctTime.GetTime() * 1000 + ts.fraction / 1000000;
  135. Insert(idx, tt, data0, data1, data2);
  136. } while (true);
  137. this->map_resist_idx00, this->map_resist_idx01, this->map_resist_idx02;
  138. auto dif = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start).count();
  139. if (dif > 100)
  140. {
  141. sprintf_s(tablename, 300, "执行语句耗时:(%I64d)ms.(%s)", dif, (LPCSTR)sql);
  142. CSimpleLog::Info(tablename);
  143. }
  144. #endif // _DEBUG
  145. */
  146. //加载设备心跳时间
  147. {
  148. TIMESTAMP_STRUCT ts;
  149. COdbcStatement stmt;
  150. sql.Format("SELECT updatetime FROM rm_deviceinfo WHERE IMEI = '%s';", imei.c_str());
  151. if (CDBConnectPool::Instance()->DBQuery(stmt, sql))
  152. {
  153. stmt.BindTimeStampCol(1, &ts);
  154. if (stmt.FetchNext() == 0)
  155. {
  156. try {
  157. m_ctUpdateTime = CTime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
  158. }
  159. catch (...) {};
  160. }
  161. }
  162. else
  163. {
  164. CSimpleLog::Error("sql语句执行失败:" + sql);
  165. }
  166. }
  167. }
  168. std::map<time_t, int>* CDevice::GetMapData(uint8_t idx, uint8_t no)
  169. {
  170. if (idx > 2 || no > 2)
  171. return nullptr;
  172. else if (idx == 0 && no == 0)
  173. return &map_resist_idx00;
  174. else if (idx == 0 && no == 1)
  175. return &map_resist_idx01;
  176. else if (idx == 0 && no == 2)
  177. return &map_resist_idx02;
  178. else if (idx == 1 && no == 0)
  179. return &map_resist_idx10;
  180. else if (idx == 1 && no == 1)
  181. return &map_resist_idx11;
  182. else if (idx == 1 && no == 2)
  183. return &map_resist_idx12;
  184. else if (idx == 2 && no == 0)
  185. return &map_resist_idx20;
  186. else if (idx == 2 && no == 1)
  187. return &map_resist_idx21;
  188. else if (idx == 2 && no == 2)
  189. return &map_resist_idx22;
  190. return nullptr;
  191. }
  192. std::map<time_t, tagSecondStatInfo>* CDevice::GetStatInfo(uint8_t idx, uint8_t no)
  193. {
  194. if (idx > 2 || no > 2)
  195. return nullptr;
  196. else if (idx == 0 && no == 0)
  197. return &m_mapSecondStatInfo00;
  198. else if (idx == 0 && no == 1)
  199. return &m_mapSecondStatInfo01;
  200. else if (idx == 0 && no == 2)
  201. return &m_mapSecondStatInfo02;
  202. else if (idx == 1 && no == 0)
  203. return &m_mapSecondStatInfo10;
  204. else if (idx == 1 && no == 1)
  205. return &m_mapSecondStatInfo11;
  206. else if (idx == 1 && no == 2)
  207. return &m_mapSecondStatInfo12;
  208. else if (idx == 2 && no == 0)
  209. return &m_mapSecondStatInfo20;
  210. else if (idx == 2 && no == 1)
  211. return &m_mapSecondStatInfo21;
  212. else if (idx == 2 && no == 2)
  213. return &m_mapSecondStatInfo22;
  214. return nullptr;
  215. }
  216. BOOL CDevice::IsDeviceOnline(uint8_t idx, time_t tmNow, int inteval)
  217. {
  218. if (idx == 0)
  219. {
  220. if (tmNow - m_tmUpdateTime0 < inteval)
  221. return TRUE;
  222. }
  223. else if (idx == 1)
  224. {
  225. if (tmNow - m_tmUpdateTime0 < inteval)
  226. return TRUE;
  227. }
  228. else if (idx == 2)
  229. {
  230. if (tmNow - m_tmUpdateTime0 < inteval)
  231. return TRUE;
  232. }
  233. return FALSE;
  234. }
  235. CDeviceMng::CDeviceMng()
  236. {
  237. }
  238. CDeviceMng::~CDeviceMng()
  239. {
  240. lock_guard<mutex> lock(m_mtx);
  241. for (auto& it : m_map_devices)
  242. {
  243. delete it.second;
  244. it.second = nullptr;
  245. }
  246. m_map_devices.clear();
  247. }
  248. CDevice* CDeviceMng::Insert(std::string imei)
  249. {
  250. lock_guard<mutex> lock(m_mtx);
  251. auto it = m_map_devices.find(imei);
  252. if (it == m_map_devices.end())
  253. {
  254. auto ret = m_map_devices.insert(make_pair(imei, new CDevice));
  255. assert(ret.second == true);
  256. if (ret.second)
  257. {
  258. ret.first->second->imei = imei;
  259. ret.first->second->LoadHist();
  260. return ret.first->second;
  261. }
  262. else delete ret.first->second;
  263. return nullptr;
  264. }
  265. else
  266. {
  267. return it->second;
  268. }
  269. }
  270. CDevice* CDeviceMng::Find(std::string imei)
  271. {
  272. lock_guard<mutex> lock(m_mtx);
  273. auto it = m_map_devices.find(imei);
  274. if (it == m_map_devices.end()) return nullptr;
  275. else return it->second;
  276. }
  277. BOOL CDeviceMng::IsDeviceOnline(std::string imei, int8_t idx, int interval /*= 180000*/)
  278. {
  279. time_t tmNow;
  280. time(&tmNow);
  281. lock_guard<mutex> lock(m_mtx);
  282. auto it = m_map_devices.find(imei);
  283. if (it == m_map_devices.end()) return FALSE;
  284. return it->second->IsDeviceOnline(idx, tmNow, interval);
  285. }
  286. CDeviceMng CDeviceMng::obj;