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.

908 lines
36 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "disinfection_ctl_service.hpp"
  2. #include <cstring>
  3. #include <iostream>
  4. #include "configs/gconfig.hpp"
  5. #include "configs/project_setting.hpp"
  6. #include "iflytop/components/uart_printer/uart_printer.hpp"
  7. #include "iflytoplinuxsdk/src/iflytop/components/ziconv.hpp"
  8. #include "service/device_state_service.hpp"
  9. // #define PROJECT_TYPE_LARGE_SPACE_DISINFECTION 1 // 大空间
  10. // #define PROJECT_TYPE_SMALL_SPACE_DISINFECTION 1 // 小空间
  11. using namespace iflytop;
  12. using namespace std;
  13. #define DVALUE_COMPUTEPERIOD_TIME_S (10.0)
  14. #define DUMP_LOG_PERIOD (5 * 60.0)
  15. #define MAX_VOLUME (5000)
  16. namespace iflytop {
  17. extern bool g_in_test;
  18. }
  19. static string formattimeS(int sec) {
  20. if (sec >= 0) {
  21. return fmt::format("{:0>2}:{:0>2}:{:0>2}", sec / 3600, sec % 3600 / 60, sec % 60);
  22. } else {
  23. return fmt::format("--:--:--");
  24. }
  25. }
  26. static string getTime() {
  27. struct tm tm = {0};
  28. time_t t = time(nullptr);
  29. localtime_r(&t, &tm);
  30. return fmt::format("{:0>4}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}", tm.tm_year + 1900, //
  31. tm.tm_mon + 1, //
  32. tm.tm_mday, //
  33. tm.tm_hour, //
  34. tm.tm_min, tm.tm_sec);
  35. }
  36. static bool zfeq(float a, float b, float eps = 0.01) {
  37. if (fabs(a - b) < eps) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. DisinfectionCtrlService::DisinfectionCtrlService() {}
  43. void DisinfectionCtrlService::initialize() {
  44. GET_TO_SERVICE(m_deviceIoControlService);
  45. GET_TO_SERVICE(m_dbService);
  46. GET_TO_SERVICE(m_disinfectionLogsManager);
  47. GET_TO_SERVICE(m_disinfectionPrinterService);
  48. m_deviceIoControlService->drainingPump_close();
  49. m_deviceIoControlService->replenishingFluidsPump_close();
  50. m_deviceIoControlService->sprayLiquidPump_close();
  51. m_deviceIoControlService->heartingPlate_setPower(false);
  52. m_deviceIoControlService->airBlower_setState(false);
  53. m_deviceIoControlService->airCompressor_setState(false);
  54. m_dvalueComputer.initialize();
  55. }
  56. string DisinfectionCtrlService::createDisinfectionID() {
  57. struct tm tm = {0};
  58. time_t t = time(nullptr);
  59. if (t == -1) {
  60. logger->error("time(nullptr) failed");
  61. exit(-1);
  62. }
  63. struct tm* tmp = localtime_r(&t, &tm);
  64. if (!tmp) {
  65. logger->error("localtime_r failed");
  66. exit(-1);
  67. }
  68. // tm = *utctime::tm_increment_hour(&tm, 8);
  69. // logger->info("trace sendmsg_startCapture {}:{}", __FILE__, __LINE__);
  70. return fmt::format("{:0>4}-{:0>2}{:0>2}-{:0>2}{:0>2}{:0>2}", tm.tm_year + 1900, //
  71. tm.tm_mon + 1, //
  72. tm.tm_mday, //
  73. tm.tm_hour, //
  74. tm.tm_min, tm.tm_sec);
  75. }
  76. float DisinfectionCtrlService::getDisinfectionDValue(float ppm) { //
  77. return m_dvalueComputer.computeDValue(ppm);
  78. }
  79. float DisinfectionCtrlService::computeNowLogLevel(DisinfectionContext& context) {
  80. float dvalue = context.state_dvalue;
  81. if (dvalue > 0) {
  82. /**
  83. * @brief state_now_loglevel
  84. */
  85. return context.state_now_loglevel + DVALUE_COMPUTEPERIOD_TIME_S / (dvalue * 60);
  86. }
  87. return context.state_now_loglevel;
  88. }
  89. void DisinfectionCtrlService::computeRemainTime(DisinfectionContext& context) {
  90. /**
  91. * @brief Dvalue
  92. */
  93. float dvalue = context.state_dvalue;
  94. if (dvalue > 0) {
  95. /**
  96. * @brief state_now_loglevel
  97. */
  98. if (context.cfg_targetLoglevel >= context.state_now_loglevel) {
  99. context.state_remaintime = (context.cfg_targetLoglevel - context.state_now_loglevel) * (dvalue * 60);
  100. } else {
  101. context.state_remaintime = 0;
  102. }
  103. } else {
  104. //
  105. }
  106. logger->info("computeRemainTime minh2o2 {} dvalue {}", context.min_h2o2, dvalue);
  107. }
  108. shared_ptr<DisinfectionLogger> DisinfectionCtrlService::createCSVLogger(string log_file_name) {
  109. shared_ptr<DisinfectionLogger> _logger = m_disinfectionLogsManager->createNewLogger(log_file_name);
  110. #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION)
  111. _logger->write(
  112. fmt::format("{},"
  113. " {} , {} , {} , {} ,"
  114. " {} , {} , {} , {} ,"
  115. " {} , {} , {} , {} ,"
  116. " {} , {} , {} ,"
  117. " {} , {} , {} , {} ,"
  118. " {} , {}"
  119. "\n",
  120. ZIconv::noChange("时间"), //
  121. ZIconv::noChange("仓内-气化过氧化氢浓度"), ZIconv::noChange("仓内-温度"), ZIconv::noChange("仓内-相对湿度"), ZIconv::noChange("仓内-相对饱和度"), //
  122. ZIconv::noChange("环境1-气化过氧化氢浓度"), ZIconv::noChange("环境1-温度"), ZIconv::noChange("环境1-相对湿度"), ZIconv::noChange("环境1-相对饱和度"), //
  123. ZIconv::noChange("环境2-气化过氧化氢浓度"), ZIconv::noChange("环境2-温度"), ZIconv::noChange("环境2-相对湿度"), ZIconv::noChange("环境2-相对饱和度"), //
  124. ZIconv::noChange("D值"), ZIconv::noChange("当前LOG"), ZIconv::noChange("目标LOG"), //
  125. ZIconv::noChange("加热器电源"), ZIconv::noChange("风机电源"), ZIconv::noChange("空压机电源"), ZIconv::noChange("喷液泵(g/min)"), //
  126. ZIconv::noChange("消毒剩余剂量(g)"), ZIconv::noChange("剩余时间(s)")));
  127. #endif
  128. #ifdef PROJECT_TYPE_PIPE_DISINFECTION
  129. _logger->write(
  130. fmt::format(" {} ,"
  131. " {} , {} , {} , {} ,"
  132. " {} , {} , {} ,"
  133. " {} , {} , {} , {} ,"
  134. " {} , {} "
  135. "\n",
  136. ZIconv::noChange("时间"), //
  137. ZIconv::noChange("仓内-气化过氧化氢浓度"), ZIconv::noChange("仓内-温度"), ZIconv::noChange("仓内-相对湿度"), ZIconv::noChange("仓内-相对饱和度"), //
  138. ZIconv::noChange("D值"), ZIconv::noChange("当前LOG"), ZIconv::noChange("目标LOG"), //
  139. ZIconv::noChange("加热器电源"), ZIconv::noChange("风机电源"), ZIconv::noChange("空压机电源"), ZIconv::noChange("喷液泵(g/min)"), //
  140. ZIconv::noChange("消毒剩余剂量(g)"), ZIconv::noChange("剩余时间(s)")));
  141. #endif
  142. return _logger;
  143. }
  144. void DisinfectionCtrlService::dumpDisinfectionLogsToCSV(DisinfectionContext& context) {
  145. #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION)
  146. static_assert(MAX_H2O2_SENSOR_NUM == 3, "MAX_H2O2_SENSOR_NUM must be 3");
  147. auto& cx = context;
  148. string h2o2str[MAX_H2O2_SENSOR_NUM];
  149. string tempstr[MAX_H2O2_SENSOR_NUM];
  150. string humidstr[MAX_H2O2_SENSOR_NUM];
  151. string satstr[MAX_H2O2_SENSOR_NUM];
  152. for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  153. h2o2str[i] = fmt::format("{}", cx.h2o2[i]);
  154. tempstr[i] = fmt::format("{}", cx.temp[i]);
  155. humidstr[i] = fmt::format("{}", cx.humid[i]);
  156. satstr[i] = fmt::format("{}", cx.saturation[i]);
  157. }
  158. for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  159. if (cx.h2o2[i] < 0) h2o2str[i] = "N/A";
  160. if (cx.temp[i] < 0) tempstr[i] = "N/A";
  161. if (cx.humid[i] < 0) humidstr[i] = "N/A";
  162. if (cx.saturation[i] < 0) satstr[i] = "N/A";
  163. }
  164. auto ds = m_deviceIoControlService;
  165. float dvalue = 0;
  166. if (m_context.state_dvalue <= 0) {
  167. dvalue = 0;
  168. } else {
  169. dvalue = m_context.state_dvalue;
  170. }
  171. int remaintime = getEstimatedRemainingTimeS();
  172. context.csvlogger->write(
  173. fmt::format(" {} ,"
  174. " {} , {} , {} , {} ,"
  175. " {} , {} , {} , {} ,"
  176. " {} , {} , {} , {} ,"
  177. " {} , {} , {} ,"
  178. " {} , {} , {} , {} ,"
  179. " {} , {} "
  180. "\n",
  181. getTime(), //
  182. h2o2str[0], tempstr[0], humidstr[0], satstr[0], //
  183. h2o2str[1], tempstr[1], humidstr[1], satstr[1], //
  184. h2o2str[2], tempstr[2], humidstr[2], satstr[2], //
  185. (int32_t)dvalue, (int32_t)m_context.state_now_loglevel, (int32_t)m_context.cfg_targetLoglevel, //
  186. ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), //
  187. m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime)));
  188. #endif
  189. #ifdef PROJECT_TYPE_PIPE_DISINFECTION
  190. static_assert(MAX_H2O2_SENSOR_NUM == 1, "MAX_H2O2_SENSOR_NUM must be 1");
  191. auto& cx = context;
  192. string h2o2str[MAX_H2O2_SENSOR_NUM];
  193. string tempstr[MAX_H2O2_SENSOR_NUM];
  194. string humidstr[MAX_H2O2_SENSOR_NUM];
  195. string satstr[MAX_H2O2_SENSOR_NUM];
  196. for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  197. h2o2str[i] = fmt::format("{}", cx.h2o2[i]);
  198. tempstr[i] = fmt::format("{}", cx.temp[i]);
  199. humidstr[i] = fmt::format("{}", cx.humid[i]);
  200. satstr[i] = fmt::format("{}", cx.saturation[i]);
  201. }
  202. for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  203. if (cx.h2o2[i] < 0) h2o2str[i] = "N/A";
  204. if (cx.temp[i] < 0) tempstr[i] = "N/A";
  205. if (cx.humid[i] < 0) humidstr[i] = "N/A";
  206. if (cx.saturation[i] < 0) satstr[i] = "N/A";
  207. }
  208. auto ds = m_deviceIoControlService;
  209. float dvalue = 0;
  210. if (m_context.state_dvalue <= 0) {
  211. dvalue = 0;
  212. } else {
  213. dvalue = m_context.state_dvalue;
  214. }
  215. int remaintime = getEstimatedRemainingTimeS();
  216. context.csvlogger->write(
  217. fmt::format(" {} ,"
  218. " {} , {} , {} , {} ,"
  219. " {} , {} , {} ,"
  220. " {} , {} , {} , {} ,"
  221. " {} , {} "
  222. "\n",
  223. getTime(), //
  224. h2o2str[0], tempstr[0], humidstr[0], satstr[0], //
  225. (int32_t)dvalue, (int32_t)m_context.state_now_loglevel, (int32_t)m_context.cfg_targetLoglevel, //
  226. ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), //
  227. m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime)));
  228. #endif
  229. }
  230. void DisinfectionCtrlService::pushDisinfectionPrinterTask(DisinfectionContext& context) { //
  231. shared_ptr<DisinfectionPrinterTask> task = make_shared<DisinfectionPrinterTask>();
  232. auto ds = GET_SERVICE(DeviceStateService);
  233. auto dio = m_deviceIoControlService;
  234. task->start_tp = context.start_tp;
  235. task->complete_tp = context.complete_tp;
  236. task->stateSnapshotList = context.stateSnapshotList;
  237. task->disinfectantUsage = context.beforeDisinfectantVolume_g - context.afterDisinfectantVolume_g;
  238. task->disinfection_id = context.m_disinfectionID;
  239. task->usr = ds->getLoginUid();
  240. task->targetLog = context.cfg_targetLoglevel;
  241. task->actualLog = context.state_now_loglevel;
  242. task->disinfectantVolume = dio->getDisinfectantVolume_g();
  243. m_disinfectionPrinterService->pushPrintTask(task);
  244. }
  245. void DisinfectionCtrlService::log(DisinfectionContext& context) {
  246. #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION)
  247. auto& cx = context;
  248. auto ds = m_deviceIoControlService;
  249. float dvalue = 0;
  250. if (m_context.state_dvalue <= 0) {
  251. dvalue = 0;
  252. } else {
  253. dvalue = m_context.state_dvalue;
  254. }
  255. int remaintime = getEstimatedRemainingTimeS();
  256. logger->info(
  257. fmt::format("{},"
  258. "s0({},{},{},{}),"
  259. "s1({},{},{},{}),"
  260. "s2({},{},{},{}),"
  261. "min_h2o2:{},max_h2o2:{},max_hum:{},max_sa:{},"
  262. "dv:{},log:{},tlog:{},"
  263. "h:{},airB:{},airC:{},pump:{},"
  264. "g:{},remainS:{}"
  265. "\n",
  266. getTime(), //
  267. cx.h2o2[0], cx.temp[0], cx.humid[0], cx.saturation[0], //
  268. cx.h2o2[1], cx.temp[1], cx.humid[1], cx.saturation[1], //
  269. cx.h2o2[2], cx.temp[2], cx.humid[2], cx.saturation[2], //
  270. m_context.min_h2o2, m_context.max_h2o2, m_context.max_humid, m_context.max_saturation, //
  271. (int32_t)dvalue, m_context.state_now_loglevel, (int32_t)m_context.cfg_targetLoglevel, //
  272. ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), //
  273. m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime)));
  274. #endif
  275. #ifdef PROJECT_TYPE_PIPE_DISINFECTION
  276. auto& cx = context;
  277. auto ds = m_deviceIoControlService;
  278. float dvalue = 0;
  279. if (m_context.state_dvalue <= 0) {
  280. dvalue = 0;
  281. } else {
  282. dvalue = m_context.state_dvalue;
  283. }
  284. int remaintime = getEstimatedRemainingTimeS();
  285. logger->info(
  286. fmt::format("{},"
  287. "s0({},{},{},{}),"
  288. "min_h2o2:{},max_h2o2:{},max_hum:{},max_sa:{},"
  289. "dv:{},log:{},tlog:{},"
  290. "h:{},airB:{},airC:{},pump:{},"
  291. "g:{},remainS:{}"
  292. "\n",
  293. getTime(), //
  294. cx.h2o2[0], cx.temp[0], cx.humid[0], cx.saturation[0], //
  295. m_context.min_h2o2, m_context.max_h2o2, m_context.max_humid, m_context.max_saturation, //
  296. (int32_t)dvalue, m_context.state_now_loglevel, (int32_t)m_context.cfg_targetLoglevel, //
  297. ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), //
  298. m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime)));
  299. #endif
  300. }
  301. /*******************************************************************************
  302. * WORK *
  303. *******************************************************************************/
  304. void DisinfectionCtrlService::initContext(DisinfectionContext& context, //
  305. int loglevel, //
  306. float injection_pump_speed, //
  307. float stoped_gs, //
  308. float continued_gs, //
  309. float stoped_satur, //
  310. float continued_satur, //
  311. float stoped_humi, //
  312. float continued_humi //
  313. ) {
  314. context.m_disinfectionID = createDisinfectionID();
  315. context.pre_heat_time_s = m_dbService->getSettingVal("pre_heat_time_s");
  316. context.stoped_gs = stoped_gs;
  317. context.continued_gs = continued_gs;
  318. context.stoped_satur = stoped_satur;
  319. context.continued_satur = continued_satur;
  320. context.stoped_humi = stoped_humi;
  321. context.continued_humi = continued_humi;
  322. context.injection_pump_speed = injection_pump_speed;
  323. context.injection_pump_speed_changed = true;
  324. if (g_in_test) {
  325. logger->warn("in test mode, pre_heat_time_s = 5");
  326. context.pre_heat_time_s = 5;
  327. }
  328. logger->info("startDisinfection {} {}", m_context.cfg_targetLoglevel, m_context.m_disinfectionID);
  329. logger->info(" stoped_gs {}", context.stoped_gs);
  330. logger->info(" continued_gs {}", context.continued_gs);
  331. logger->info(" stoped_satur {}", context.stoped_satur);
  332. logger->info(" continued_satur {}", context.continued_satur);
  333. logger->info(" stoped_humi {}", context.stoped_humi);
  334. logger->info(" continued_humi {}", context.continued_humi);
  335. logger->info(" pre_heat_time_s {}", context.pre_heat_time_s);
  336. logger->info("");
  337. context.state_remaintime = context.pre_heat_time_s + loglevel * 90 * 60; // 计算总的加热时间
  338. m_disinfectionWorkState = 1;
  339. context.cfg_targetLoglevel = loglevel;
  340. context.state_now_loglevel = 0;
  341. m_context.state_dvalue = 0;
  342. m_context.m_state = kstate_preheat;
  343. m_context.start_tp = zsystem_clock().now();
  344. m_context.start_steady_tp = zsteady_clock().now();
  345. for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  346. m_context.h2o2[i] = 0;
  347. m_context.humid[i] = 0;
  348. m_context.temp[i] = 0;
  349. m_context.saturation[i] = 0;
  350. }
  351. m_context.min_h2o2 = 0;
  352. m_context.max_h2o2 = 0;
  353. m_context.max_humid = 0;
  354. m_context.max_saturation = 0;
  355. m_context.stateSnapshotList.clear();
  356. m_context.afterDisinfectantVolume_g = 0;
  357. m_context.beforeDisinfectantVolume_g = m_deviceIoControlService->getDisinfectantVolume_g();
  358. m_deviceIoControlService->warningLightCtrl(0, 0, 1, 0);
  359. m_deviceIoControlService->heartingPlate_setPower(true);
  360. m_deviceIoControlService->airBlower_setState(true);
  361. context.csvlogger = createCSVLogger(context.m_disinfectionID);
  362. m_context.firstLog = true;
  363. }
  364. void DisinfectionCtrlService::finishDisinfection(DisinfectionContext& context) {
  365. context.state_remaintime = 0;
  366. logger->info("stop disinfection {}", context.m_disinfectionID);
  367. // sprayLiquidPump_close();
  368. m_deviceIoControlService->sprayLiquidPump_close();
  369. usleep(1000 * 1000);
  370. // airCompressor(false);
  371. m_deviceIoControlService->airCompressor_setState(false);
  372. usleep(1000 * 1000);
  373. // blower_setPower(false);
  374. m_deviceIoControlService->airBlower_setState(false);
  375. usleep(1000 * 1000);
  376. // heartingPlate_setPower(false);
  377. m_deviceIoControlService->heartingPlate_setPower(false);
  378. m_disinfectionWorkState = 3;
  379. m_deviceIoControlService->warningLightCtrl(0, 1, 0, 0);
  380. }
  381. void DisinfectionCtrlService::processPreheatState(DisinfectionContext& context) {
  382. int hasstarttime = zsteady_clock().elapsedTimeS(context.start_steady_tp);
  383. // logger->info("preheat {}", context.m_disinfectionID);
  384. if ((context.m_state == kstate_preheat && hasstarttime > m_context.pre_heat_time_s)) {
  385. logger->info("preheat finished {}", context.m_disinfectionID);
  386. m_deviceIoControlService->airBlower_setState(true);
  387. #ifdef PROJECT_TYPE_PIPE_DISINFECTION
  388. m_deviceIoControlService->airCompressor_channelSelect(1);
  389. m_deviceIoControlService->airCompressor_setValve1(1);
  390. m_deviceIoControlService->airCompressor_setValve2(1);
  391. #endif
  392. usleep(1000 * 1000);
  393. m_deviceIoControlService->airCompressor_setState(true);
  394. usleep(1000 * 1000);
  395. m_deviceIoControlService->sprayLiquidPump_open(context.injection_pump_speed);
  396. context.m_state = kstate_disinfection;
  397. } else {
  398. logger->info("{}: preheat {}", context.m_disinfectionID, m_context.pre_heat_time_s - hasstarttime);
  399. }
  400. }
  401. /**
  402. * @brief
  403. *
  404. */
  405. void DisinfectionCtrlService::processDisinfectionState(DisinfectionContext& context) {
  406. /**
  407. * @brief
  408. */
  409. /**
  410. * @brief 湿
  411. */
  412. if (!m_context.state_is_disinfection_take_break) {
  413. /**
  414. * @brief
  415. */
  416. float nowSatur = m_context.max_saturation;
  417. float nowh2o2 = m_context.max_h2o2;
  418. float humid = m_context.max_humid;
  419. if (m_context.injection_pump_speed_changed) {
  420. m_deviceIoControlService->sprayLiquidPump_open(context.injection_pump_speed);
  421. m_context.injection_pump_speed_changed = false;
  422. }
  423. // humid > m_context.stoped_satur
  424. if (nowSatur > m_context.stoped_satur || nowh2o2 > m_context.stoped_gs || humid > m_context.stoped_humi) {
  425. logger->info("stop sprayLiquid");
  426. m_deviceIoControlService->sprayLiquidPump_close();
  427. usleep(1000 * 1000);
  428. m_deviceIoControlService->airCompressor_setState(false);
  429. // m_context.sprayLiquidFlag = false;
  430. m_context.state_is_disinfection_take_break = true;
  431. }
  432. } else {
  433. float nowSatur = m_context.max_saturation;
  434. float nowh2o2 = m_context.max_h2o2;
  435. float humid = m_context.max_humid;
  436. // && humid < m_context.continued_satur
  437. if (nowSatur < m_context.continued_satur && nowh2o2 < m_context.continued_gs && humid < context.continued_humi) {
  438. logger->info("start sprayLiquid");
  439. m_deviceIoControlService->sprayLiquidPump_open(context.injection_pump_speed);
  440. usleep(1000 * 1000);
  441. m_deviceIoControlService->airCompressor_setState(true);
  442. m_context.state_is_disinfection_take_break = false;
  443. }
  444. }
  445. }
  446. void DisinfectionCtrlService::processState_Preheat(DisinfectionContext& context) {
  447. /**
  448. * @brief
  449. */
  450. m_context.state_dvalue = 0;
  451. processPreheatState(m_context);
  452. }
  453. void DisinfectionCtrlService::processState_Disinfection(DisinfectionContext& context, bool& updatedval) {
  454. m_context.state_dvalue = getDisinfectionDValue(m_context.min_h2o2);
  455. if (zsteady_clock().elapsedTimeS(m_context.state_last_compute_dvalue_tp) > DVALUE_COMPUTEPERIOD_TIME_S) {
  456. m_context.state_last_compute_dvalue_tp = zsteady_clock().now();
  457. m_context.state_now_loglevel = computeNowLogLevel(m_context);
  458. computeRemainTime(m_context);
  459. updatedval = true;
  460. }
  461. /**
  462. * @brief
  463. */
  464. processDisinfectionState(m_context);
  465. //
  466. if (m_context.state_remaintime <= 0 && m_context.state_now_loglevel > (m_context.cfg_targetLoglevel + 0.01)) {
  467. m_context.state_remaintime = 0;
  468. m_context.state_now_loglevel = m_context.cfg_targetLoglevel + 0.01;
  469. logger->info("disinfection finished {},but waitting for h2o2 to safe", m_context.m_disinfectionID);
  470. m_deviceIoControlService->sprayLiquidPump_close();
  471. usleep(1000 * 1000);
  472. m_deviceIoControlService->airCompressor_setState(false);
  473. usleep(1000 * 1000);
  474. m_deviceIoControlService->heartingPlate_setPower(false);
  475. m_context.m_state = kstate_degradation;
  476. }
  477. }
  478. void DisinfectionCtrlService::processState_Degradation(DisinfectionContext& context) {
  479. // 降解
  480. m_context.state_dvalue = 0;
  481. logger->info("waitting for h2o2 concentration to safe value {}=>{}", m_context.min_h2o2, 1);
  482. if (m_context.min_h2o2 < 1) {
  483. logger->info("h2o2 concentration to safe value");
  484. m_context.m_state = kstate_finished;
  485. }
  486. }
  487. void DisinfectionCtrlService::disinfectionLoop(bool& breakflag) {
  488. // logger->info("disinfection running {} {}s preheatFlag:{}", m_context.m_disinfectionID, m_context.state_remaintime, m_context.m_preheatFlag);
  489. ThisThread thisThread;
  490. disinfection_state_t enterstate;
  491. disinfection_state_t exitstate;
  492. bool statechanged = false;
  493. bool updatedval = false;
  494. enterstate = m_context.m_state;
  495. m_context.state_remaintime--;
  496. if (m_context.state_remaintime < 0) m_context.state_remaintime = 0;
  497. updateH2O2SensorData(m_context);
  498. /*******************************************************************************
  499. * *
  500. *******************************************************************************/
  501. if (thisThread.getExitFlag()) {
  502. m_context.m_state = kstate_finished;
  503. } else if (m_context.m_state == kstate_preheat) {
  504. processState_Preheat(m_context);
  505. } else if (m_context.m_state == kstate_disinfection) {
  506. processState_Disinfection(m_context, updatedval);
  507. } else if (m_context.m_state == kstate_degradation) {
  508. processState_Degradation(m_context);
  509. }
  510. exitstate = m_context.m_state;
  511. if (exitstate != enterstate) {
  512. statechanged = true;
  513. }
  514. /*******************************************************************************
  515. * *
  516. *******************************************************************************/
  517. if (exitstate == kstate_finished) {
  518. /**
  519. * @brief
  520. */
  521. breakflag = true;
  522. m_context.complete_tp = zsystem_clock().now();
  523. m_context.afterDisinfectantVolume_g = m_deviceIoControlService->getDisinfectantVolume_g();
  524. if (m_context.m_state != kstate_finished) {
  525. m_context.m_state = kstate_finished;
  526. }
  527. m_disinfectionWorkState = 0;
  528. takeStateSnapshot(m_context);
  529. dumpDisinfectionLogsToCSV(m_context);
  530. log(m_context);
  531. pushDisinfectionPrinterTask(m_context);
  532. finishDisinfection(m_context);
  533. m_context.csvlogger = nullptr;
  534. } else {
  535. if (updatedval) {
  536. log(m_context);
  537. }
  538. if (m_context.firstLog || statechanged || zsteady_clock().elapsedTimeS(m_context.state_lastlog_tp) > DUMP_LOG_PERIOD) {
  539. m_context.state_lastlog_tp = zsteady_clock().now();
  540. takeStateSnapshot(m_context);
  541. dumpDisinfectionLogsToCSV(m_context);
  542. m_context.firstLog = false;
  543. }
  544. }
  545. }
  546. void DisinfectionCtrlService::changeDisinfectionParameter(int injection_pump_speed, //
  547. int stoped_gs, //
  548. int continued_gs, //
  549. int stoped_satur, //
  550. int continued_satur, //
  551. int stoped_humi, //
  552. int continued_humi) {
  553. lock_guard<recursive_mutex> lock(lock_);
  554. m_context.injection_pump_speed = injection_pump_speed;
  555. m_context.stoped_gs = stoped_gs;
  556. m_context.continued_gs = continued_gs;
  557. m_context.stoped_satur = stoped_satur;
  558. m_context.continued_satur = continued_satur;
  559. m_context.stoped_humi = stoped_humi;
  560. m_context.continued_humi = continued_humi;
  561. m_context.injection_pump_speed_changed = true;
  562. logger->info("changeDisinfectionParameter {} {} {} {} {} {} {}", //
  563. injection_pump_speed, stoped_gs, continued_gs, stoped_satur, continued_satur, stoped_humi, continued_humi);
  564. }
  565. void DisinfectionCtrlService::startDisinfection(int loglevel, //
  566. int injection_pump_speed, //
  567. int stoped_gs, //
  568. int continued_gs, //
  569. int stoped_satur, //
  570. int continued_satur, //
  571. int stoped_humi, //
  572. int continued_humi //
  573. ) {
  574. lock_guard<recursive_mutex> lock(lock_);
  575. if (m_disinfectionThread) stopDisinfection();
  576. initContext(m_context, loglevel, injection_pump_speed, stoped_gs, continued_gs, stoped_satur, continued_satur, stoped_humi, continued_humi);
  577. m_disinfectionThread.reset(new Thread("m_disinfectionThread", [this]() {
  578. ThisThread thisThread;
  579. while (true) {
  580. thisThread.sleepForMs(1000);
  581. bool breakflag = false;
  582. disinfectionLoop(breakflag);
  583. if (breakflag) {
  584. break;
  585. }
  586. }
  587. }));
  588. //
  589. }
  590. void DisinfectionCtrlService::stopDisinfection() {
  591. lock_guard<recursive_mutex> lock(lock_);
  592. if (m_disinfectionThread) {
  593. m_disinfectionThread->join();
  594. m_disinfectionThread = nullptr;
  595. }
  596. }
  597. int DisinfectionCtrlService::getDisinfectionWorkState() { return m_context.m_state; }
  598. int32_t DisinfectionCtrlService::getEstimatedRemainingTimeS() {
  599. if (m_context.m_state == kstate_preheat) {
  600. return getPreHeatRaminTimeS();
  601. } else if (m_context.m_state == kstate_disinfection) {
  602. if (m_context.state_dvalue > 0) {
  603. return m_context.state_remaintime;
  604. } else {
  605. return -1;
  606. }
  607. } else {
  608. return 0;
  609. }
  610. }
  611. int32_t DisinfectionCtrlService::getPreHeatRaminTimeS() {
  612. int32_t remaintime = 0;
  613. if (m_context.m_state == kstate_preheat) {
  614. remaintime = m_context.pre_heat_time_s - zsteady_clock().elapsedTimeS(m_context.start_steady_tp);
  615. if (remaintime < 0) {
  616. remaintime = 0;
  617. }
  618. return remaintime;
  619. } else {
  620. return 0;
  621. }
  622. }
  623. string DisinfectionCtrlService::getDisinfectionID() { return m_context.m_disinfectionID; }
  624. bool DisinfectionCtrlService::isPreheatState() { return m_context.m_state == kstate_preheat; }
  625. /*******************************************************************************
  626. * // 加液 *
  627. *******************************************************************************/
  628. int DisinfectionCtrlService::getReplenishingFluidsWorkState() { return m_replenishingFluidsWorkState; }
  629. void DisinfectionCtrlService::startReplenishingFluids(int stopatg) {
  630. lock_guard<recursive_mutex> lock(lock_);
  631. if (m_disinfectionThread) {
  632. m_disinfectionThread->join();
  633. m_disinfectionThread = nullptr;
  634. }
  635. int32_t nowvolume = m_deviceIoControlService->getDisinfectantVolume_g();
  636. int maxg = DISINFECTANT_BUCKET_CAPACITY;
  637. if (stopatg > maxg) {
  638. logger->warn("start Replenishing fail, stopatg {} > maxg {}", stopatg, maxg);
  639. stopatg = maxg;
  640. }
  641. if (nowvolume > stopatg) {
  642. logger->warn("start Replenishing fail, nowvolume {} > stopatg {}", nowvolume, stopatg);
  643. return;
  644. }
  645. m_disinfectionThread.reset(new Thread("disinfectionThread", [this, stopatg]() {
  646. ThisThread thisThread;
  647. m_deviceIoControlService->replenishingFluidsPump_open();
  648. logger->info("startReplenishingFluids {}g", stopatg);
  649. while (!thisThread.getExitFlag()) {
  650. int32_t nowvolume = m_deviceIoControlService->getDisinfectantVolume_g();
  651. logger->info("replenishingFluids {}g", nowvolume);
  652. if (nowvolume > stopatg) {
  653. break;
  654. }
  655. if (nowvolume > MAX_VOLUME) {
  656. logger->warn("replenishingFluids reach full level {}g", nowvolume);
  657. break;
  658. }
  659. thisThread.sleepForMs(1000);
  660. }
  661. logger->info("stopReplenishingFluids");
  662. // replenishingFluidsPump_close();
  663. m_deviceIoControlService->replenishingFluidsPump_close();
  664. m_replenishingFluidsWorkState = 0;
  665. }));
  666. //
  667. m_replenishingFluidsWorkState = 1;
  668. logger->info("startReplenishingFluids ");
  669. }
  670. void DisinfectionCtrlService::stopReplenishingFluids() {
  671. lock_guard<recursive_mutex> lock(lock_);
  672. if (m_disinfectionThread) {
  673. m_disinfectionThread->join();
  674. m_disinfectionThread = nullptr;
  675. }
  676. logger->info("stopReplenishingFluids");
  677. // replenishingFluidsPump_close();
  678. m_deviceIoControlService->replenishingFluidsPump_close();
  679. m_replenishingFluidsWorkState = 0;
  680. }
  681. /*******************************************************************************
  682. * *
  683. *******************************************************************************/
  684. void DisinfectionCtrlService::startDraining() {
  685. lock_guard<recursive_mutex> lock(lock_);
  686. if (m_disinfectionThread) {
  687. m_disinfectionThread->join();
  688. m_disinfectionThread = nullptr;
  689. }
  690. m_disinfectionThread.reset(new Thread("disinfectionThread", [this]() {
  691. ThisThread thisThread;
  692. m_deviceIoControlService->drainingPump_open();
  693. logger->info("startDraining ");
  694. auto startdrainingtime = zsteady_clock().now();
  695. zsteady_tp volumeReachZeroTime;
  696. bool volumeReachZeroFlag = false;
  697. while (!thisThread.getExitFlag()) {
  698. int32_t nowvolume = m_deviceIoControlService->getDisinfectantVolume_g();
  699. logger->info("draining remain {} g", nowvolume);
  700. if (!volumeReachZeroFlag && nowvolume == 0) {
  701. volumeReachZeroTime = zsteady_clock().now();
  702. volumeReachZeroFlag = true;
  703. }
  704. if (volumeReachZeroFlag) {
  705. logger->info("stopDraining after {} s", 30 - zsteady_clock().elapsedTimeS(volumeReachZeroTime));
  706. if (zsteady_clock().elapsedTimeS(volumeReachZeroTime) > 30) {
  707. break;
  708. }
  709. }
  710. thisThread.sleepForMs(1000);
  711. }
  712. logger->info("stopDraining");
  713. // replenishingFluidsPump_close();
  714. m_deviceIoControlService->drainingPump_close();
  715. m_drainingWorkState = 0;
  716. }));
  717. logger->info("startDraining");
  718. // drainingPump_open();
  719. m_drainingWorkState = 1;
  720. }
  721. void DisinfectionCtrlService::stopDraining() {
  722. lock_guard<recursive_mutex> lock(lock_);
  723. if (m_disinfectionThread) {
  724. m_disinfectionThread->join();
  725. m_disinfectionThread = nullptr;
  726. }
  727. logger->info("stopDraining");
  728. m_drainingWorkState = 0;
  729. // drainingPump_close();
  730. m_deviceIoControlService->drainingPump_close();
  731. }
  732. int DisinfectionCtrlService::getDrainingWorkState() { return m_drainingWorkState; }
  733. void DisinfectionCtrlService::updateH2O2SensorData(DisinfectionContext& context) {
  734. auto& cx = context;
  735. for (size_t i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  736. cx.h2o2[i] = m_deviceIoControlService->H2O2Sensor_readH2O2PPM(i);
  737. }
  738. for (size_t i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  739. cx.humid[i] = m_deviceIoControlService->H2O2Sensor_readHumid(i);
  740. }
  741. for (size_t i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  742. cx.temp[i] = m_deviceIoControlService->H2O2Sensor_readTemperature(i);
  743. }
  744. for (size_t i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  745. cx.saturation[i] = m_deviceIoControlService->H2O2Sensor_readSaturation(i);
  746. }
  747. cx.min_h2o2 = cx.h2o2[0];
  748. cx.max_h2o2 = cx.h2o2[0];
  749. cx.max_humid = cx.humid[0];
  750. cx.max_saturation = cx.saturation[0];
  751. for (size_t i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  752. if (cx.h2o2[i] < cx.min_h2o2) {
  753. cx.min_h2o2 = cx.h2o2[i];
  754. }
  755. if (cx.h2o2[i] > cx.max_h2o2) {
  756. cx.max_h2o2 = cx.h2o2[i];
  757. }
  758. if (cx.humid[i] > cx.max_humid) {
  759. cx.max_humid = cx.humid[i];
  760. }
  761. if (cx.saturation[i] > cx.max_saturation) {
  762. cx.max_saturation = cx.saturation[i];
  763. }
  764. }
  765. if (cx.max_h2o2 < 0) cx.max_h2o2 = 0;
  766. if (cx.min_h2o2 < 0) cx.min_h2o2 = 0;
  767. if (cx.max_humid < 0) cx.max_humid = 0;
  768. if (cx.max_saturation < 0) cx.max_saturation = 0;
  769. }
  770. void DisinfectionCtrlService::takeStateSnapshot(DisinfectionContext& context) {
  771. auto& cx = context;
  772. shared_ptr<StateSnapshot> snapshot = make_shared<StateSnapshot>();
  773. snapshot->time = zsystem_clock().now();
  774. for (size_t i = 0; i < MAX_H2O2_SENSOR_NUM; i++) {
  775. snapshot->h2o2[i] = cx.h2o2[i];
  776. snapshot->humid[i] = cx.humid[i];
  777. snapshot->temp[i] = cx.temp[i];
  778. snapshot->saturation[i] = cx.saturation[i];
  779. }
  780. snapshot->min_h2o2 = cx.min_h2o2;
  781. snapshot->max_h2o2 = cx.max_h2o2;
  782. snapshot->max_humid = cx.max_humid;
  783. snapshot->max_saturation = cx.max_saturation;
  784. snapshot->state = cx.m_state;
  785. snapshot->dloglevel = cx.state_now_loglevel;
  786. cx.stateSnapshotList.push_back(snapshot);
  787. }