Device.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #pragma once
  2. #include <string>
  3. #include <queue>
  4. #include <map>
  5. #include <mutex>
  6. #include "AlarmDefine.h"
  7. using namespace std;
  8. //缓冲数据 1小时 毫秒
  9. #define MAX_SAVE_TIME_MILLI 300000 //十分钟
  10. #define MAX_SAVE_TIME_SECOND 300 //五分钟
  11. enum class SENSOR_STATUS : int8_t
  12. {
  13. UNKONW = -1,
  14. ABNORMAL = 0,
  15. NORMAL = 1
  16. };
  17. class CDevice
  18. {
  19. public:
  20. CDevice();
  21. virtual ~CDevice();
  22. void Insert(int index, time_t time, int data0, int data1, int data2);
  23. //调用Insert数据插入完 来判断是否需要插入数据 返回false 不需要 返回true 需要
  24. inline bool CalcBInsertData(int index, time_t* time, int* data0, int* data1, int* data2)
  25. {
  26. lock_guard<mutex> lock(m_mtx);
  27. if (index == 0)
  28. return CalcBInsertData(m_mapSecondStatInfo00, m_mapSecondStatInfo01, m_mapSecondStatInfo02, time, data0, data1, data2);
  29. else if (index == 1)
  30. return CalcBInsertData(m_mapSecondStatInfo10, m_mapSecondStatInfo11, m_mapSecondStatInfo12, time, data0, data1, data2);
  31. else
  32. return CalcBInsertData(m_mapSecondStatInfo20, m_mapSecondStatInfo21, m_mapSecondStatInfo22, time, data0, data1, data2);
  33. }
  34. void LoadHist();
  35. std::map<time_t, int>* GetMapData(uint8_t idx, uint8_t no);
  36. std::map<time_t, tagSecondStatInfo>* GetStatInfo(uint8_t idx, uint8_t no);
  37. BOOL IsDeviceOnline(uint8_t idx, time_t tmNow, int inteval);
  38. inline BOOL GetSendStatInfo(time_t tmStart, time_t tmEnd, uint8_t idx,
  39. std::map<time_t, tagSecondStatInfo>& map0, std::map<time_t, tagSecondStatInfo>& map1, std::map<time_t, tagSecondStatInfo>& map2)
  40. {
  41. if (idx == 0)
  42. {
  43. if (m_mapSecondStatInfo00.size() == 0) return FALSE;
  44. for (auto i = tmStart; i < tmEnd; i++)
  45. {
  46. auto it0 = m_mapSecondStatInfo00.find(i);
  47. if (it0 == m_mapSecondStatInfo00.end()) continue;
  48. auto it1 = m_mapSecondStatInfo01.find(i);
  49. auto it2 = m_mapSecondStatInfo02.find(i);
  50. if (it1 == m_mapSecondStatInfo01.end() || it2 == m_mapSecondStatInfo02.end())
  51. {
  52. ASSERT(FALSE);
  53. }
  54. auto it00 = it0, it01 = it1, it02 = it2;
  55. for (; it00 != m_mapSecondStatInfo00.end();)
  56. {
  57. if (it00->first <= tmEnd)
  58. {
  59. it00++;
  60. it01++;
  61. it02++;
  62. }
  63. else
  64. {
  65. map0.insert(it0, it00);
  66. map1.insert(it1, it01);
  67. map2.insert(it2, it02);
  68. return TRUE;
  69. }
  70. }
  71. if (it0 != it00)
  72. {
  73. map0.insert(it0, it00);
  74. map1.insert(it1, it01);
  75. map2.insert(it2, it02);
  76. return TRUE;
  77. }
  78. }
  79. }
  80. else if (idx == 1)
  81. {
  82. if (m_mapSecondStatInfo10.size() == 0) return FALSE;
  83. for (auto i = tmStart; i < tmEnd; i++)
  84. {
  85. auto it0 = m_mapSecondStatInfo10.find(i);
  86. if (it0 == m_mapSecondStatInfo10.end()) continue;
  87. auto it1 = m_mapSecondStatInfo11.find(i);
  88. auto it2 = m_mapSecondStatInfo12.find(i);
  89. if (it1 == m_mapSecondStatInfo11.end() || it2 == m_mapSecondStatInfo12.end())
  90. {
  91. ASSERT(FALSE);
  92. }
  93. auto it00 = it0, it01 = it1, it02 = it2;
  94. for (; it00 != m_mapSecondStatInfo10.end();)
  95. {
  96. if (it00->first <= tmEnd)
  97. {
  98. it00++;
  99. it01++;
  100. it02++;
  101. }
  102. else
  103. {
  104. map0.insert(it0, it00);
  105. map1.insert(it1, it01);
  106. map2.insert(it2, it02);
  107. return TRUE;
  108. }
  109. }
  110. if (it0 != it00)
  111. {
  112. map0.insert(it0, it00);
  113. map1.insert(it1, it01);
  114. map2.insert(it2, it02);
  115. return TRUE;
  116. }
  117. }
  118. }
  119. else if (idx == 2)
  120. {
  121. if (m_mapSecondStatInfo20.size() == 0) return FALSE;
  122. for (auto i = tmStart; i < tmEnd; i++)
  123. {
  124. auto it0 = m_mapSecondStatInfo20.find(i);
  125. if (it0 == m_mapSecondStatInfo20.end()) continue;
  126. auto it1 = m_mapSecondStatInfo21.find(i);
  127. auto it2 = m_mapSecondStatInfo22.find(i);
  128. if (it1 == m_mapSecondStatInfo21.end() || it2 == m_mapSecondStatInfo22.end())
  129. {
  130. ASSERT(FALSE);
  131. }
  132. auto it00 = it0, it01 = it1, it02 = it2;
  133. for (; it00 != m_mapSecondStatInfo20.end();)
  134. {
  135. if (it00->first <= tmEnd)
  136. {
  137. it00++;
  138. it01++;
  139. it02++;
  140. }
  141. else
  142. {
  143. map0.insert(it0, it00);
  144. map1.insert(it1, it01);
  145. map2.insert(it2, it02);
  146. return TRUE;
  147. }
  148. }
  149. if (it0 != it00)
  150. {
  151. map0.insert(it0, it00);
  152. map1.insert(it1, it01);
  153. map2.insert(it2, it02);
  154. return TRUE;
  155. }
  156. }
  157. }
  158. return FALSE;
  159. }
  160. //新的接口
  161. inline void Insert(const uint8_t idx, const __time64_t tmStartTime, const int step, const vector<int>& data0, const vector<int>& data1, const vector<int>& data2,
  162. const vector<bool> result, const size_t len, const SECOND_STAT_INFO& stSS1, const SECOND_STAT_INFO& stSS2, const SECOND_STAT_INFO& stSS3)
  163. {
  164. //TRACE("%s:%d %s\r\n", __FUNCTION__, __LINE__, CTime(tmStartTime / 1000).Format("%Y-%m-%d %H:%M:%S"));
  165. lock_guard<mutex> lock(m_mtx);
  166. switch (idx)
  167. {
  168. case 0:
  169. {
  170. for (auto i = 0; i < len; i++)
  171. {
  172. if (result[i] == false) continue;
  173. const auto tt = tmStartTime + i * step;
  174. map_resist_idx00[tt] = data0[i];
  175. map_resist_idx01[tt] = data1[i];
  176. map_resist_idx02[tt] = data2[i];
  177. }
  178. auto tmStartTimeSecond = tmStartTime / 1000;
  179. m_mapSecondStatInfo00[tmStartTimeSecond] = stSS1;
  180. m_mapSecondStatInfo01[tmStartTimeSecond] = stSS2;
  181. m_mapSecondStatInfo02[tmStartTimeSecond] = stSS3;
  182. //time(&m_tmMoveDetectTime0);//暂时不知道为啥赋值,先注释 2024年5月12日
  183. {
  184. auto it = map_resist_idx00.begin();
  185. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  186. it = map_resist_idx00.erase(it);
  187. it = map_resist_idx01.begin();
  188. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  189. it = map_resist_idx01.erase(it);
  190. it = map_resist_idx02.begin();
  191. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  192. it = map_resist_idx02.erase(it);
  193. }
  194. {
  195. auto it = m_mapSecondStatInfo00.begin();
  196. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  197. it = m_mapSecondStatInfo00.erase(it);
  198. it = m_mapSecondStatInfo01.begin();
  199. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  200. it = m_mapSecondStatInfo01.erase(it);
  201. it = m_mapSecondStatInfo02.begin();
  202. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  203. it = m_mapSecondStatInfo02.erase(it);
  204. }
  205. }
  206. break;
  207. case 1:
  208. {
  209. for (auto i = 0; i < len; i++)
  210. {
  211. if (result[i] == false) continue;
  212. const auto tt = tmStartTime + i * step;
  213. map_resist_idx10[tt] = data0[i];
  214. map_resist_idx11[tt] = data1[i];
  215. map_resist_idx12[tt] = data2[i];
  216. }
  217. auto tmStartTimeSecond = tmStartTime / 1000;
  218. m_mapSecondStatInfo10[tmStartTimeSecond] = stSS1;
  219. m_mapSecondStatInfo11[tmStartTimeSecond] = stSS2;
  220. m_mapSecondStatInfo12[tmStartTimeSecond] = stSS3;
  221. //time(&m_tmMoveDetectTime1);//暂时不知道为啥赋值,先注释 2024年5月12日
  222. {
  223. auto it = map_resist_idx10.begin();
  224. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  225. it = map_resist_idx10.erase(it);
  226. it = map_resist_idx11.begin();
  227. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  228. it = map_resist_idx11.erase(it);
  229. it = map_resist_idx12.begin();
  230. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  231. it = map_resist_idx12.erase(it);
  232. }
  233. {
  234. auto it = m_mapSecondStatInfo10.begin();
  235. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  236. it = m_mapSecondStatInfo10.erase(it);
  237. it = m_mapSecondStatInfo11.begin();
  238. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  239. it = m_mapSecondStatInfo11.erase(it);
  240. it = m_mapSecondStatInfo12.begin();
  241. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  242. it = m_mapSecondStatInfo12.erase(it);
  243. }
  244. }
  245. break;
  246. case 2:
  247. {
  248. for (auto i = 0; i < len; i++)
  249. {
  250. if (result[i] == false) continue;
  251. const auto tt = tmStartTime + i * step;
  252. map_resist_idx20[tt] = data0[i];
  253. map_resist_idx21[tt] = data1[i];
  254. map_resist_idx22[tt] = data2[i];
  255. }
  256. auto tmStartTimeSecond = tmStartTime / 1000;
  257. m_mapSecondStatInfo20[tmStartTimeSecond] = stSS1;
  258. m_mapSecondStatInfo21[tmStartTimeSecond] = stSS2;
  259. m_mapSecondStatInfo22[tmStartTimeSecond] = stSS3;
  260. //time(&m_tmMoveDetectTime2);//暂时不知道为啥赋值,先注释 2024年5月12日
  261. {
  262. auto it = map_resist_idx20.begin();
  263. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  264. it = map_resist_idx20.erase(it);
  265. it = map_resist_idx21.begin();
  266. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  267. it = map_resist_idx21.erase(it);
  268. it = map_resist_idx22.begin();
  269. while (tmStartTime - it->first > MAX_SAVE_TIME_MILLI)
  270. it = map_resist_idx22.erase(it);
  271. }
  272. {
  273. auto it = m_mapSecondStatInfo20.begin();
  274. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  275. it = m_mapSecondStatInfo20.erase(it);
  276. it = m_mapSecondStatInfo21.begin();
  277. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  278. it = m_mapSecondStatInfo21.erase(it);
  279. it = m_mapSecondStatInfo22.begin();
  280. while (tmStartTimeSecond - it->first > MAX_SAVE_TIME_SECOND)
  281. it = m_mapSecondStatInfo22.erase(it);
  282. }
  283. }
  284. break;
  285. default:
  286. ASSERT(0);
  287. break;
  288. }
  289. //TRACE("%s:%d %s\r\n", __FUNCTION__, __LINE__, fmt::format("size 1:{} 2:{} 3:{}", m_mapSecondStatInfo00.size(), m_mapSecondStatInfo10.size(), m_mapSecondStatInfo20.size()).c_str());
  290. }
  291. bool IsHaveTemp()const { return m_nTemperature != INT_MIN; }
  292. bool IsHaveHumi()const { return m_nHumidity != INT_MIN; }
  293. public:
  294. //设备唯一号
  295. std::string imei;
  296. //0通道数据
  297. std::map<time_t, int> map_resist_idx00; //毫秒数, 阻力值
  298. std::map<time_t, int> map_resist_idx01;
  299. std::map<time_t, int> map_resist_idx02;
  300. //秒数, 统计信息
  301. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo00;
  302. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo01;
  303. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo02;
  304. //1通道数据
  305. std::map<time_t, int> map_resist_idx10;
  306. std::map<time_t, int> map_resist_idx11;
  307. std::map<time_t, int> map_resist_idx12;
  308. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo10;
  309. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo11;
  310. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo12;
  311. //2通道数据
  312. std::map<time_t, int> map_resist_idx20;
  313. std::map<time_t, int> map_resist_idx21;
  314. std::map<time_t, int> map_resist_idx22;
  315. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo20;
  316. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo21;
  317. std::map<time_t, tagSecondStatInfo> m_mapSecondStatInfo22;
  318. std::mutex m_mtx;
  319. COleDateTime m_odt_data0;
  320. COleDateTime m_odt_data1;
  321. COleDateTime m_odt_data2;
  322. //心跳时间
  323. CTime m_ctUpdateTime = 0;
  324. SENSOR_STATUS m_sensor_status[9][2];
  325. //最后扳动的检测时间
  326. time_t m_tmMoveDetectTime0 = 0;
  327. time_t m_tmMoveDetectTime1 = 0;
  328. time_t m_tmMoveDetectTime2 = 0;
  329. int m_nTemperature = INT_MIN;
  330. int m_nHumidity = INT_MIN;
  331. private:
  332. inline static void InsertData(std::map<time_t, int>& map, time_t time, int val)
  333. {
  334. if (map.size() >= 2)
  335. {
  336. auto it = map.end();
  337. auto last = --it;
  338. auto second_last = --it;
  339. if (time / 1000 == last->first / 1000 && second_last->first / 1000 == time / 1000)
  340. {
  341. if (abs(last->second - val) <= 30 && abs(second_last->second - val) <= 30 && abs(last->second - second_last->second) <= 30)
  342. {
  343. #ifdef _DEBUG
  344. TRACE("%d:%d.%d\r\n", time % 100000, last->first % 100000, (time - last->first) % 1000);
  345. #endif // _DEBUG
  346. //都相等 先删除
  347. map.erase(last);
  348. }
  349. }
  350. else if (last->first == second_last->first + 980 && abs(last->second - second_last->second) <= 30)
  351. {
  352. map.erase(last);
  353. }
  354. map[time] = val;
  355. }
  356. else
  357. {
  358. map[time] = val;
  359. }
  360. auto it = map.begin();
  361. if ((time - it->first) > MAX_SAVE_TIME_MILLI) map.erase(it);
  362. }
  363. inline static void InsertData(std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo, time_t tTime, int val)
  364. {
  365. time_t tt = tTime / 1000; //换算成秒数
  366. if (mapSecondStatInfo[tt].first_val == INT_MIN)
  367. {
  368. mapSecondStatInfo[tt].first_val = val; //存储第一个值
  369. }
  370. auto& it = mapSecondStatInfo[tt];
  371. if (val > it.max_val)
  372. {
  373. it.max_val = val;
  374. it.max_time = tTime;
  375. }
  376. if (val < it.min_val)
  377. {
  378. it.min_val = val;
  379. it.min_time = tTime;
  380. }
  381. it.dif_val = it.max_val - it.min_val;
  382. it.sum_val += val;
  383. it.cout++;
  384. it.end_val = val;
  385. }
  386. inline static bool CalcBInsertData(std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo0,
  387. std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo1, std::map<time_t, tagSecondStatInfo>& mapSecondStatInfo2,
  388. time_t* time, int* data0, int* data1, int* data2)
  389. {
  390. if (mapSecondStatInfo0.size() < 2) return false;
  391. //for (auto it = mapSecondStatInfo0.crbegin(); it != mapSecondStatInfo0.crend(); ++it) {
  392. // TRACE(fmt::format("{}:{}", it->first, it->second.dif_val).c_str());
  393. //}
  394. auto it0 = mapSecondStatInfo0.crbegin();
  395. auto it1 = mapSecondStatInfo1.crbegin();
  396. auto it2 = mapSecondStatInfo2.crbegin();
  397. if (it0->second.dif_val < 100 && it1->second.dif_val < 100 && it2->second.dif_val < 100)
  398. return false; //无波动
  399. if (it0->second.dif_val > 100)
  400. {
  401. auto it = it0;
  402. it++;
  403. if (it->second.dif_val < 100 && it0->first - it->first > 2 && it0->first - it->first < 60) //前面无波动 相隔时差 大于2秒 小于 60秒
  404. {
  405. *time = it0->first * 1000 - 20;
  406. *data0 = (++it0)->second.end_val;
  407. *data1 = (++it1)->second.end_val;
  408. *data2 = (++it2)->second.end_val;
  409. return true;
  410. }
  411. }
  412. else if (it1->second.dif_val > 100)
  413. {
  414. auto it = it1;
  415. it++;
  416. if (it->second.dif_val < 100 && it1->first - it->first > 2 && it1->first - it->first < 60) //前面无波动 相隔时差 大于2秒 小于 60秒
  417. {
  418. *time = it1->first * 1000 - 20;
  419. *data0 = (++it0)->second.end_val;
  420. *data1 = (++it1)->second.end_val;
  421. *data2 = (++it2)->second.end_val;
  422. return true;
  423. }
  424. }
  425. else if (it2->second.dif_val > 100)
  426. {
  427. auto it = it2;
  428. it++;
  429. if (it->second.dif_val < 100 && it2->first - it->first > 2 && it2->first - it->first < 60) //前面无波动 相隔时差 大于2秒 小于 60秒
  430. {
  431. *time = it2->first * 1000 - 20;
  432. *data0 = (++it0)->second.end_val;
  433. *data1 = (++it1)->second.end_val;
  434. *data2 = (++it2)->second.end_val;
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. };
  441. class CDeviceMng
  442. {
  443. private:
  444. CDeviceMng();
  445. ~CDeviceMng();
  446. public:
  447. static inline CDeviceMng* Instance() { return &obj; };
  448. CDevice* Insert(std::string imei);
  449. CDevice* Find(std::string imei);
  450. BOOL IsDeviceOnline(std::string imei, int8_t idx, int interval = 180000); //默认3分钟判断是否在线
  451. private:
  452. static CDeviceMng obj;
  453. public:
  454. std::mutex m_mtx;
  455. //imei
  456. std::map<std::string, CDevice*> m_map_devices;
  457. };