You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
7.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "disinfection_ctl_service.hpp"
  2. using namespace iflytop;
  3. using namespace std;
  4. /**
  5. * @brief
  6. *
  7. * :
  8. *
  9. *
  10. * 1s
  11. *
  12. * 1s
  13. *
  14. * 1s
  15. *
  16. *
  17. *
  18. * 1s
  19. *
  20. * 1s
  21. *
  22. *
  23. */
  24. #define PRE_HEAT_TIME (5 * 60)
  25. void DisinfectionCtrlService::drainingPump_open() { m_zcanHost->pumpctrl_c1004(1, 100, -300, 1, 20); }
  26. void DisinfectionCtrlService::drainingPump_close() { m_zcanHost->pumpctrl_c1004(1, 100, 0, 1, 20); }
  27. void DisinfectionCtrlService::replenishingFluidsPump_open() { m_zcanHost->pumpctrl_c1004(1, 100, 300, 1, 20); }
  28. void DisinfectionCtrlService::replenishingFluidsPump_close() { m_zcanHost->pumpctrl_c1004(1, 100, 0, 1, 20); }
  29. void DisinfectionCtrlService::sprayLiquidPump_open() { m_zcanHost->pumpctrl_c1004(2, 100, 200, 1, 15); }
  30. void DisinfectionCtrlService::sprayLiquidPump_close() { m_zcanHost->pumpctrl_c1004(2, 100, 0, 1, 15); }
  31. void DisinfectionCtrlService::airCompressor(bool val) {
  32. if (val) {
  33. m_zcanHost->writeio(0, 1);
  34. usleep(500 * 100);
  35. m_zcanHost->writeio(1, 1);
  36. } else {
  37. m_zcanHost->writeio(1, 0);
  38. usleep(500 * 100);
  39. m_zcanHost->writeio(0, 0);
  40. }
  41. }
  42. void DisinfectionCtrlService::blower_setPower(bool val) {
  43. if (val) {
  44. m_zcanHost->writeio(2, 1);
  45. usleep(500 * 100);
  46. m_zcanHost->writeio(3, 1);
  47. } else {
  48. m_zcanHost->writeio(3, 0);
  49. usleep(500 * 100);
  50. m_zcanHost->writeio(2, 0);
  51. }
  52. }
  53. void DisinfectionCtrlService::heartingPlate_setPower(bool val) {
  54. if (val) {
  55. m_zcanHost->writeio(4, 1);
  56. usleep(500 * 100);
  57. m_zcanHost->writeio(5, 1);
  58. } else {
  59. m_zcanHost->writeio(5, 0);
  60. usleep(500 * 100);
  61. m_zcanHost->writeio(4, 0);
  62. }
  63. }
  64. DisinfectionCtrlService::DisinfectionCtrlService() {}
  65. string DisinfectionCtrlService::createDisinfectionID() {
  66. struct tm tm = {0};
  67. time_t t = time(nullptr);
  68. if (t == -1) {
  69. logger->error("time(nullptr) failed");
  70. exit(-1);
  71. }
  72. struct tm* tmp = localtime_r(&t, &tm);
  73. if (!tmp) {
  74. logger->error("localtime_r failed");
  75. exit(-1);
  76. }
  77. // tm = *utctime::tm_increment_hour(&tm, 8);
  78. // logger->info("trace sendmsg_startCapture {}:{}", __FILE__, __LINE__);
  79. return fmt::format("{:0>4}-{:0>2}{:0>2}-{:0>2}{:0>2}{:0>2}", tm.tm_year + 1900, //
  80. tm.tm_mon + 1, //
  81. tm.tm_mday, //
  82. tm.tm_hour, //
  83. tm.tm_min, tm.tm_sec);
  84. }
  85. void DisinfectionCtrlService::initialize() {
  86. GET_TO_SERVICE(m_zcanHost);
  87. GET_TO_SERVICE(m_sensorDataScan);
  88. drainingPump_close();
  89. replenishingFluidsPump_close();
  90. sprayLiquidPump_close();
  91. heartingPlate_setPower(false);
  92. blower_setPower(false);
  93. airCompressor(false);
  94. }
  95. void DisinfectionCtrlService::startDisinfection(int loglevel, float roomVol) {
  96. lock_guard<recursive_mutex> lock(lock_);
  97. if (m_disinfectionThread) {
  98. stopDisinfection();
  99. }
  100. m_disinfectionID = createDisinfectionID();
  101. // m_remaintime = loglevel * 20 * 60; // 计算总的加热时间
  102. m_remaintime = PRE_HEAT_TIME + loglevel * 20 * 60 * (roomVol / 20.0); // 计算总的加热时间
  103. m_disinfectionWorkState = 1;
  104. m_disinfectionThread.reset(new Thread("m_disinfectionThread", [this, roomVol, loglevel]() {
  105. ThisThread thisThread;
  106. // bool preHeartFinishedFlag = false;
  107. logger->info("startDisinfection {} {} {}", loglevel, roomVol, m_disinfectionID);
  108. auto starttime = zsteady_clock().now();
  109. heartingPlate_setPower(true);
  110. m_preheatFlag = 1;
  111. while (!thisThread.getExitFlag()) {
  112. thisThread.sleepForMs(1000);
  113. if (m_disinfectionWorkState == 2) {
  114. logger->info("disinfection paused {} {}", m_disinfectionID, m_remaintime);
  115. continue;
  116. }
  117. m_remaintime--;
  118. if (m_remaintime == 0) {
  119. logger->info("disinfection finished {}", m_disinfectionID);
  120. break;
  121. }
  122. logger->info("disinfection running {} {}s preheatFlag:{}", m_disinfectionID, m_remaintime, m_preheatFlag);
  123. if (m_preheatFlag && zsteady_clock().elapsedTimeS(starttime) > PRE_HEAT_TIME) {
  124. logger->info("preheat finished {}", m_disinfectionID);
  125. blower_setPower(true);
  126. usleep(1000 * 1000);
  127. airCompressor(true);
  128. usleep(1000 * 1000);
  129. sprayLiquidPump_open();
  130. m_preheatFlag = false;
  131. }
  132. }
  133. m_remaintime = 0;
  134. logger->info("stop disinfection {}", m_disinfectionID);
  135. sprayLiquidPump_close();
  136. usleep(1000 * 1000);
  137. airCompressor(false);
  138. usleep(1000 * 1000);
  139. blower_setPower(false);
  140. usleep(1000 * 1000);
  141. heartingPlate_setPower(false);
  142. m_disinfectionWorkState = 3;
  143. }));
  144. }
  145. int DisinfectionCtrlService::getDisinfectionWorkState() { return m_disinfectionWorkState; }
  146. void DisinfectionCtrlService::pauseDisinfection() { m_disinfectionWorkState = 2; }
  147. void DisinfectionCtrlService::continueDisinfection() { m_disinfectionWorkState = 1; }
  148. void DisinfectionCtrlService::stopDisinfection() {
  149. lock_guard<recursive_mutex> lock(lock_);
  150. if (m_disinfectionThread) {
  151. m_disinfectionThread->join();
  152. m_disinfectionThread = nullptr;
  153. }
  154. m_disinfectionWorkState = 0;
  155. }
  156. int32_t DisinfectionCtrlService::getEstimatedRemainingTimeS() {
  157. lock_guard<recursive_mutex> lock(lock_);
  158. return m_remaintime;
  159. }
  160. string DisinfectionCtrlService::getDisinfectionID() {
  161. lock_guard<recursive_mutex> lock(lock_);
  162. return m_disinfectionID;
  163. }
  164. // 加液
  165. void DisinfectionCtrlService::startReplenishingFluids(int stopatg) {
  166. lock_guard<recursive_mutex> lock(lock_);
  167. if (m_disinfectionThread) {
  168. m_disinfectionThread->join();
  169. m_disinfectionThread = nullptr;
  170. }
  171. int32_t nowvolume = m_sensorDataScan->getDisinfectantVolume_g();
  172. if (nowvolume > stopatg) {
  173. logger->warn("start Replenishing fail, nowvolume {} > stopatg {}", nowvolume, stopatg);
  174. return;
  175. }
  176. m_disinfectionThread.reset(new Thread("disinfectionThread", [this, stopatg]() {
  177. ThisThread thisThread;
  178. replenishingFluidsPump_open();
  179. logger->info("startReplenishingFluids {}g", stopatg);
  180. while (!thisThread.getExitFlag()) {
  181. int32_t nowvolume = m_sensorDataScan->getDisinfectantVolume_g();
  182. logger->info("replenishingFluids {}g", nowvolume);
  183. if (nowvolume > stopatg) {
  184. break;
  185. }
  186. if (nowvolume > 4500) {
  187. logger->warn("replenishingFluids reach full level {}g", nowvolume);
  188. break;
  189. }
  190. thisThread.sleepForMs(1000);
  191. }
  192. logger->info("stopReplenishingFluids");
  193. replenishingFluidsPump_close();
  194. m_replenishingFluidsWorkState = 0;
  195. }));
  196. //
  197. m_replenishingFluidsWorkState = 1;
  198. logger->info("startReplenishingFluids ");
  199. }
  200. // 停止加液
  201. void DisinfectionCtrlService::stopReplenishingFluids() {
  202. lock_guard<recursive_mutex> lock(lock_);
  203. if (m_disinfectionThread) {
  204. m_disinfectionThread->join();
  205. m_disinfectionThread = nullptr;
  206. }
  207. logger->info("stopReplenishingFluids");
  208. replenishingFluidsPump_close();
  209. m_replenishingFluidsWorkState = 0;
  210. }
  211. // 开始排液
  212. void DisinfectionCtrlService::startDraining() {
  213. lock_guard<recursive_mutex> lock(lock_);
  214. if (m_disinfectionThread) {
  215. m_disinfectionThread->join();
  216. m_disinfectionThread = nullptr;
  217. }
  218. logger->info("startDraining");
  219. drainingPump_open();
  220. m_drainingWorkState = 1;
  221. }
  222. // 停止排液体
  223. void DisinfectionCtrlService::stopDraining() {
  224. lock_guard<recursive_mutex> lock(lock_);
  225. if (m_disinfectionThread) {
  226. m_disinfectionThread->join();
  227. m_disinfectionThread = nullptr;
  228. }
  229. logger->info("stopDraining");
  230. m_drainingWorkState = 0;
  231. drainingPump_close();
  232. }
  233. int DisinfectionCtrlService::getReplenishingFluidsWorkState() { return m_replenishingFluidsWorkState; }
  234. int DisinfectionCtrlService::getDrainingWorkState() { return m_drainingWorkState; }