From 87a4e44660d3859b46c72538883aefccc9219a56 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sat, 18 May 2024 16:49:22 +0800 Subject: [PATCH] update --- src/api/cmds/setting_page_cmd_impl.cpp | 1 - src/configs/gconfig.hpp | 1 + src/configs/project_setting.hpp | 4 +- src/main.cpp | 21 +- src/main_control_service.cpp | 35 +- src/service/device_io_control_service_test.cpp | 38 ++- .../disfection_ctl/disinfection_service.cpp | 377 ++++++++++----------- src/service/disfection_ctl/disinfection_state.hpp | 21 ++ .../disfection_ctl/disinfection_state_machine.cpp | 28 +- .../disfection_ctl/disinfection_state_machine.hpp | 8 +- src/service/disinfection_ctl_service.cpp | 26 +- 11 files changed, 282 insertions(+), 278 deletions(-) diff --git a/src/api/cmds/setting_page_cmd_impl.cpp b/src/api/cmds/setting_page_cmd_impl.cpp index eba5b21..b6959e3 100644 --- a/src/api/cmds/setting_page_cmd_impl.cpp +++ b/src/api/cmds/setting_page_cmd_impl.cpp @@ -112,7 +112,6 @@ void SettingPageCmdImpl::initialize() { GET_TO_SERVICE(m_frontMsgProcesser); m_msgProcesser->registerMsgProcesser("getAllSetting", BIND(SettingPageCmdImpl::getAllSetting)); - m_msgProcesser->registerMsgProcesser("getAllSetting", BIND(SettingPageCmdImpl::getAllSetting)); m_msgProcesser->registerMsgProcesser("setSettingVal", BIND(SettingPageCmdImpl::setSettingVal)); m_msgProcesser->registerMsgProcesser("getAllFormula", BIND(SettingPageCmdImpl::getAllFormula)); m_msgProcesser->registerMsgProcesser("addFormula", BIND(SettingPageCmdImpl::addFormula)); diff --git a/src/configs/gconfig.hpp b/src/configs/gconfig.hpp index 417ecd9..d01ee4e 100644 --- a/src/configs/gconfig.hpp +++ b/src/configs/gconfig.hpp @@ -22,6 +22,7 @@ marco(int32_t /* */, pipettingRobotCanBitrate, 500000) /*移液臂Can设备波特率*/ \ marco(float /* */, dvalueCoefficient, 1) /*数值越小,相对消毒时间越长*/ \ marco(float /* */, disinfectantWeightCorrectionFactor, 0.9) /*数值越小,相对消毒时间越长*/ \ + marco(bool /* */, testMode, false) /*数值越小,相对消毒时间越长*/ configTemplateDEFILE_CONFIG_SERVICE2( // GConfig, // diff --git a/src/configs/project_setting.hpp b/src/configs/project_setting.hpp index 4b9b97a..f384043 100644 --- a/src/configs/project_setting.hpp +++ b/src/configs/project_setting.hpp @@ -9,10 +9,10 @@ #define REG_DEVICE_ID 0x0001 // 重启标识 #define REG_EXCEPTION_FLAG 0x0100 // 异常标志位 -// #define PROJECT_TYPE_LARGE_SPACE_DISINFECTION 1 // 大空间 +#define PROJECT_TYPE_LARGE_SPACE_DISINFECTION 1 // 大空间 // #define PROJECT_TYPE_SMALL_SPACE_DISINFECTION 1 // 小空间 // #define PROJECT_TYPE_PIPE_DISINFECTION 1 // 管道消毒机 -#define PROJECT_TYPE_DRAW_BAR_BOX 1 // 拉感箱 +// #define PROJECT_TYPE_DRAW_BAR_BOX 1 // 拉感箱 // #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) diff --git a/src/main.cpp b/src/main.cpp index 3527afb..3a29cc3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include + #include "iflytop/components/uart_printer/uart_printer.hpp" using namespace iflytop; @@ -29,24 +30,6 @@ void Main::run(int argc, char *argv[]) { } int Main::main(int argc, char *argv[]) { /** - * @brief 解析命令行参数 - */ - string test_mode = "false"; - auto cli = ((required("--test_mode") & value("test_mode", test_mode)).doc("test_mode")); - if (!parse(argc, argv, cli)) { - test_mode = "false"; - // cout << make_man_page(cli, argv[0]); - // exit(-1); - } - if (test_mode == "false") { - g_in_test = false; - } else { - g_in_test = true; - } - // g_in_test = false; - // logger->info("test_mode:{}:{}", test_mode, g_in_test); - - /** * @brief 系统初始化 */ logger->info("system setup start."); @@ -61,6 +44,8 @@ int Main::main(int argc, char *argv[]) { BUILD_AND_REG_SERRVICE(GConfig); GET_SERVICE(GConfig)->initialize(); + g_in_test = GET_SERVICE(GConfig)->get_testMode(); + BUILD_AND_REG_SERRVICE(UartPrinter); GET_SERVICE(UartPrinter)->initialize("/dev/ttyS5", "9600"); diff --git a/src/main_control_service.cpp b/src/main_control_service.cpp index 3baa91d..cbb9186 100644 --- a/src/main_control_service.cpp +++ b/src/main_control_service.cpp @@ -55,6 +55,12 @@ void MainControlService::initialize() { BUILD_AND_REG_SERRVICE(FrontMsgProcesser); GET_SERVICE(FrontMsgProcesser)->initialize(); + BUILD_AND_REG_SERRVICE(DisinfectionPrinterService); + GET_SERVICE(DisinfectionPrinterService)->initialize(); + + BUILD_AND_REG_SERRVICE(DisinfectionCtrlService); + GET_SERVICE(DisinfectionCtrlService)->initialize(); + BUILD_AND_REG_SERRVICE(IflytopFrontEndService); GET_SERVICE(IflytopFrontEndService)->initialize("0.0.0.0"); GET_SERVICE(IflytopFrontEndService)->onMessage.connect([this](weak_ptr webSocket, json& cmd, json& receipt) { @@ -62,23 +68,6 @@ void MainControlService::initialize() { if (cmdstr != "getState") logger->info("rx:{}", cmd.dump()); m_frontMsgProcesser->processMsg(cmd, receipt); }); - GET_SERVICE(IflytopFrontEndService)->startListen(); - - BUILD_AND_REG_SERRVICE(DisinfectionPrinterService); - GET_SERVICE(DisinfectionPrinterService)->initialize(); - - BUILD_AND_REG_SERRVICE(DisinfectionCtrlService); - GET_SERVICE(DisinfectionCtrlService)->initialize(); - - GET_TO_SERVICE(m_dbService); - GET_TO_SERVICE(m_deviceStateService); - GET_TO_SERVICE(m_zcanhost); - GET_TO_SERVICE(m_disinfectionLogsManager); - GET_TO_SERVICE(m_deviceIoControlService); - GET_TO_SERVICE(m_dataExportService); - GET_TO_SERVICE(m_frontMsgProcesser); - GET_TO_SERVICE(m_iflytopwsService); - GET_TO_SERVICE(m_disinfectionCtrlService); BUILD_AND_REG_SERRVICE(DeviceStateCmdImpl); BUILD_AND_REG_SERRVICE(DisinfectionCmdImpl); @@ -104,6 +93,16 @@ void MainControlService::initialize() { GET_SERVICE(PipelineDisinfectionCmdAppend)->initialize(); #endif + GET_TO_SERVICE(m_dbService); + GET_TO_SERVICE(m_deviceStateService); + GET_TO_SERVICE(m_zcanhost); + GET_TO_SERVICE(m_disinfectionLogsManager); + GET_TO_SERVICE(m_deviceIoControlService); + GET_TO_SERVICE(m_dataExportService); + GET_TO_SERVICE(m_frontMsgProcesser); + GET_TO_SERVICE(m_iflytopwsService); + GET_TO_SERVICE(m_disinfectionCtrlService); + #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) m_deviceIoControlService->warningLightCtrl(0, 1, 0, 0); // 绿色灯亮 m_deviceIoControlService->airCompressor_setState(0); @@ -116,4 +115,6 @@ void MainControlService::initialize() { m_deviceIoControlService->AirOutletProportionalValve_setState(0); #endif #endif + + GET_SERVICE(IflytopFrontEndService)->startListen(); }; diff --git a/src/service/device_io_control_service_test.cpp b/src/service/device_io_control_service_test.cpp index 0997b93..2a086c3 100644 --- a/src/service/device_io_control_service_test.cpp +++ b/src/service/device_io_control_service_test.cpp @@ -13,10 +13,10 @@ void DeviceIoControlServiceTest::printerPrintf(string str) { // GET_SERVICE(UartPrinter)->print(ZIconv::utf8_to_gb2312(str)); } -int32_t DeviceIoControlServiceTest::H2O2Sensor_readH2O2PPM(int32_t sensorid) { return 300 + rand() % 1000; } -int32_t DeviceIoControlServiceTest::H2O2Sensor_readHumid(int32_t sensorid) { return 20 + rand() % 30; } -int32_t DeviceIoControlServiceTest::H2O2Sensor_readTemperature(int32_t sensorid) { return rand() % 30; } -int32_t DeviceIoControlServiceTest::H2O2Sensor_readSaturation(int32_t sensorid) { return 50 + rand() % 30; } +int32_t DeviceIoControlServiceTest::H2O2Sensor_readH2O2PPM(int32_t sensorid) { return rand() % 1000; } +int32_t DeviceIoControlServiceTest::H2O2Sensor_readHumid(int32_t sensorid) { return rand() % 100; } +int32_t DeviceIoControlServiceTest::H2O2Sensor_readTemperature(int32_t sensorid) { return rand() % 40; } +int32_t DeviceIoControlServiceTest::H2O2Sensor_readSaturation(int32_t sensorid) { return rand() % 100; } // int32_t DeviceIoControlServiceTest::H2O2Sensor_readH2O2PPM(int32_t sensorid) { return 1; } // int32_t DeviceIoControlServiceTest::H2O2Sensor_readHumid(int32_t sensorid) { return 2; } @@ -38,6 +38,7 @@ int DeviceIoControlServiceTest::heatingStrip_getio2() { return heatingStrip_io2 int DeviceIoControlServiceTest::heatingStrip_getstate() { return heatingStrip_state; } int DeviceIoControlServiceTest::heatingStrip_getcurrentValue() { return heatingStrip_currentValue; } void DeviceIoControlServiceTest::heartingPlate_setPower(bool val) { + logger->info("heartingPlate_setPower:{}", val); heatingStrip_state = val; heatingStrip_currentValue = val ? 10 : 0; heatingStrip_io1 = 1; @@ -57,6 +58,7 @@ int DeviceIoControlServiceTest::airBlower_getio2() { return airBlower_io2; } int DeviceIoControlServiceTest::airBlower_getstate() { return airBlower_state; } int DeviceIoControlServiceTest::airBlower_getcurrentValue() { return airBlower_currentValue; } int DeviceIoControlServiceTest::airBlower_setState(bool state) { + logger->info("airBlower_setState:{}", state); airBlower_state = state; airBlower_currentValue = state ? 11 : 0; airBlower_io1 = 1; @@ -72,6 +74,7 @@ static int airCompressor_io2; static int airCompressor_state; static int airCompressor_currentValue; void DeviceIoControlServiceTest::airCompressor_setState(bool val) { + logger->info("airCompressor_setState:{}", val); airCompressor_state = val; airCompressor_currentValue = val ? 12 : 0; airCompressor_io1 = 1; @@ -98,13 +101,19 @@ void DeviceIoControlServiceTest::replenishingFluidsPump_open_for_test(int speed) void DeviceIoControlServiceTest::replenishingFluidsPump_close_for_test() { drainingPump_rpm = 0; } // 喷液泵 static int sprayLiquidPump_rpm; -void DeviceIoControlServiceTest::sprayLiquidPump_open(int gpm) { sprayLiquidPump_rpm = 123; } -void DeviceIoControlServiceTest::sprayLiquidPump_close() { sprayLiquidPump_rpm = 0; } -int DeviceIoControlServiceTest::sprayLiquidPump_getState() { return sprayLiquidPump_rpm; } -int DeviceIoControlServiceTest::sprayLiquidPump_getRPM() { return sprayLiquidPump_rpm; } -int DeviceIoControlServiceTest::sprayLiquidPump_getGPM() { return sprayLiquidPump_rpm * 2; } -void DeviceIoControlServiceTest::sprayLiquidPump_open_for_test(int gpm) { sprayLiquidPump_rpm = gpm; } -void DeviceIoControlServiceTest::sprayLiquidPump_close_for_test() { sprayLiquidPump_rpm = 0; } +void DeviceIoControlServiceTest::sprayLiquidPump_open(int gpm) { + logger->info("sprayLiquidPump_open:{}", gpm); + sprayLiquidPump_rpm = 123; +} +void DeviceIoControlServiceTest::sprayLiquidPump_close() { + logger->info("sprayLiquidPump_close"); + sprayLiquidPump_rpm = 0; +} +int DeviceIoControlServiceTest::sprayLiquidPump_getState() { return sprayLiquidPump_rpm; } +int DeviceIoControlServiceTest::sprayLiquidPump_getRPM() { return sprayLiquidPump_rpm; } +int DeviceIoControlServiceTest::sprayLiquidPump_getGPM() { return sprayLiquidPump_rpm * 2; } +void DeviceIoControlServiceTest::sprayLiquidPump_open_for_test(int gpm) { sprayLiquidPump_rpm = gpm; } +void DeviceIoControlServiceTest::sprayLiquidPump_close_for_test() { sprayLiquidPump_rpm = 0; } /****************************************************************************** * 水浸传感器 * @@ -181,11 +190,10 @@ int32_t DeviceIoControlServiceTest::AirProportionalValve_isBusy(int32_t* isbusy) int32_t DBDB__sprayLiquidPump_state; void DeviceIoControlServiceTest::DBDB__sprayLiquidPump_run(int gpm) { logger->info("DBDB__sprayLiquidPump_run gpm:{}", gpm); - DBDB__sprayLiquidPump_state = 1; } + DBDB__sprayLiquidPump_state = 1; +} -int32_t DeviceIoControlServiceTest::DBDB__readPressureSensor(int index) { - - return index; } +int32_t DeviceIoControlServiceTest::DBDB__readPressureSensor(int index) { return index; } int32_t DeviceIoControlServiceTest::DBDB__sprayAirCompressorPowerCtrl(int power) { logger->info("DBDB__sprayAirCompressorPowerCtrl power:{}", power); return 0; diff --git a/src/service/disfection_ctl/disinfection_service.cpp b/src/service/disfection_ctl/disinfection_service.cpp index 4235fc2..b1cd97a 100644 --- a/src/service/disfection_ctl/disinfection_service.cpp +++ b/src/service/disfection_ctl/disinfection_service.cpp @@ -13,6 +13,7 @@ using namespace std; #define DVALUE_COMPUTEPERIOD_TIME_S (10.0) #define DUMP_LOG_PERIOD (5 * 60.0) +#define DBDB_MINI_PWM_BLOWER_VAL 50 namespace iflytop { extern bool g_in_test; @@ -36,7 +37,6 @@ void DisinfectionService::initialize() { m_deviceIoControlService->airBlower_setState(false); m_deviceIoControlService->airCompressor_setState(false); #elif (defined PROJECT_TYPE_DRAW_BAR_BOX) -#else #endif sm.regStateProcesser(kstate_idle, bind(&DisinfectionService::processStateIdle, this, placeholders::_1)); @@ -46,6 +46,8 @@ void DisinfectionService::initialize() { sm.regStateProcesser(kstate_finished, bind(&DisinfectionService::processStateFinished, this, placeholders::_1)); sm.regStateProcesser(kstate_dehumidification_before_disinfection, bind(&DisinfectionService::processStateDehumidification_before_disinfection, this, placeholders::_1)); sm.regStateProcesser(kstate_dehumidification_after_disinfection, bind(&DisinfectionService::processStateDehumidification_after_disinfection, this, placeholders::_1)); + + sm.regBfStateProcess(bind(&DisinfectionService::beforeStateProcess, this)); sm.startProcess(); } @@ -100,47 +102,37 @@ static bool zfeq(float a, float b, float eps = 0.01) { shared_ptr DisinfectionService::createCSVLogger(string log_file_name) { shared_ptr _logger = m_disinfectionLogsManager->createNewLogger(log_file_name); -#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) _logger->write( fmt::format("{}," +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) + " {} , {} , {} , {} ," " {} , {} , {} , {} ," " {} , {} , {} , {} ," +#else + " {} , {} , {} , {} ," +#endif " {} , {} , {} ," " {} , {} , {} , {} ," " {} , {}" "\n", - ZIconv::noChange("时间"), // + ZIconv::noChange("时间"), +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) ZIconv::noChange("仓内-气化过氧化氢浓度"), ZIconv::noChange("仓内-温度"), ZIconv::noChange("仓内-相对湿度"), ZIconv::noChange("仓内-相对饱和度"), // ZIconv::noChange("环境1-气化过氧化氢浓度"), ZIconv::noChange("环境1-温度"), ZIconv::noChange("环境1-相对湿度"), ZIconv::noChange("环境1-相对饱和度"), // ZIconv::noChange("环境2-气化过氧化氢浓度"), ZIconv::noChange("环境2-温度"), ZIconv::noChange("环境2-相对湿度"), ZIconv::noChange("环境2-相对饱和度"), // - ZIconv::noChange("D值"), ZIconv::noChange("当前LOG"), ZIconv::noChange("目标LOG"), // - ZIconv::noChange("加热器电源"), ZIconv::noChange("风机电源"), ZIconv::noChange("空压机电源"), ZIconv::noChange("喷液泵(g/min)"), // - ZIconv::noChange("消毒剩余剂量(g)"), ZIconv::noChange("剩余时间(s)"))); -#endif - -#ifdef PROJECT_TYPE_PIPE_DISINFECTION - - _logger->write( - fmt::format(" {} ," - " {} , {} , {} , {} ," - " {} , {} , {} ," - " {} , {} , {} , {} ," - " {} , {} " - "\n", - ZIconv::noChange("时间"), // +#else ZIconv::noChange("仓内-气化过氧化氢浓度"), ZIconv::noChange("仓内-温度"), ZIconv::noChange("仓内-相对湿度"), ZIconv::noChange("仓内-相对饱和度"), // - ZIconv::noChange("D值"), ZIconv::noChange("当前LOG"), ZIconv::noChange("目标LOG"), // - ZIconv::noChange("加热器电源"), ZIconv::noChange("风机电源"), ZIconv::noChange("空压机电源"), ZIconv::noChange("喷液泵(g/min)"), // - ZIconv::noChange("消毒剩余剂量(g)"), ZIconv::noChange("剩余时间(s)"))); + #endif + ZIconv::noChange("D值"), ZIconv::noChange("当前LOG"), ZIconv::noChange("目标LOG"), // + ZIconv::noChange("加热器电源"), ZIconv::noChange("风机电源"), ZIconv::noChange("空压机电源"), ZIconv::noChange("喷液泵(g/min)"), // + ZIconv::noChange("消毒剩余剂量(g)"), ZIconv::noChange("剩余时间(s)"))); return _logger; } void DisinfectionService::logstate() { -#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) - auto& cx = context; auto ds = m_deviceIoControlService; float dvalue = 0; @@ -154,56 +146,37 @@ void DisinfectionService::logstate() { logger->info( fmt::format("{}," + +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) "s0({},{},{},{})," "s1({},{},{},{})," "s2({},{},{},{})," - "min_h2o2:{},max_h2o2:{},max_hum:{},max_sa:{}," - "dv:{},log:{},tlog:{}," - "h:{},airB:{},airC:{},pump:{}," - "g:{},remainS:{}" - "\n", - getTime(), // - cx.h2o2[0], cx.temp[0], cx.humid[0], cx.saturation[0], // - cx.h2o2[1], cx.temp[1], cx.humid[1], cx.saturation[1], // - cx.h2o2[2], cx.temp[2], cx.humid[2], cx.saturation[2], // - context.min_h2o2, context.max_h2o2, context.max_humid, context.max_saturation, // - (int32_t)dvalue, context.now_loglevel, (int32_t)cfg.targetLoglevel, // - ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // - m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime))); -#endif -#ifdef PROJECT_TYPE_PIPE_DISINFECTION - auto& cx = context; - auto ds = m_deviceIoControlService; - float dvalue = 0; - if (context.dvalue <= 0) { - dvalue = 0; - } else { - dvalue = context.dvalue; - } - - int remaintime = getEstimatedRemainingTimeS(); +#else + "s2({},{},{},{})," - logger->info( - fmt::format("{}," - "s0({},{},{},{})," +#endif "min_h2o2:{},max_h2o2:{},max_hum:{},max_sa:{}," "dv:{},log:{},tlog:{}," "h:{},airB:{},airC:{},pump:{}," - "g:{},remainS:{}" - "\n", - getTime(), // - cx.h2o2[0], cx.temp[0], cx.humid[0], cx.saturation[0], // + "g:{},remainS:{}", + getTime(), // +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) + cx.h2o2[0], cx.temp[0], cx.humid[0], cx.saturation[0], // + cx.h2o2[1], cx.temp[1], cx.humid[1], cx.saturation[1], // + cx.h2o2[2], cx.temp[2], cx.humid[2], cx.saturation[2], // +#else + cx.h2o2[0], cx.temp[0], cx.humid[0], cx.saturation[0], // +#endif context.min_h2o2, context.max_h2o2, context.max_humid, context.max_saturation, // (int32_t)dvalue, context.now_loglevel, (int32_t)cfg.targetLoglevel, // ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime))); -#endif } void DisinfectionService::dumpDisinfectionLogsToCSV() { #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) - static_assert(MAX_H2O2_SENSOR_NUM == 3, "MAX_H2O2_SENSOR_NUM must be 3"); +#endif auto& cx = context; string h2o2str[MAX_H2O2_SENSOR_NUM]; @@ -234,73 +207,51 @@ void DisinfectionService::dumpDisinfectionLogsToCSV() { } int remaintime = getEstimatedRemainingTimeS(); - - context.csvlogger->write( - fmt::format(" {} ," - " {} , {} , {} , {} ," - " {} , {} , {} , {} ," - " {} , {} , {} , {} ," - " {} , {} , {} ," - " {} , {} , {} , {} ," - " {} , {} " - "\n", - getTime(), // - h2o2str[0], tempstr[0], humidstr[0], satstr[0], // - h2o2str[1], tempstr[1], humidstr[1], satstr[1], // - h2o2str[2], tempstr[2], humidstr[2], satstr[2], // - (int32_t)dvalue, (int32_t)context.now_loglevel, (int32_t)cfg.targetLoglevel, // - ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // - m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime))); + if (context.csvlogger) + context.csvlogger->write( + fmt::format(" {} ," +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) + " {} , {} , {} , {} ," + " {} , {} , {} , {} ," + " {} , {} , {} , {} ," +#else + " {} , {} , {} , {} ," #endif + " {} , {} , {} ," + " {} , {} , {} , {} ," + " {} , {} " + "\n", + getTime(), // -#ifdef PROJECT_TYPE_PIPE_DISINFECTION - - static_assert(MAX_H2O2_SENSOR_NUM == 1, "MAX_H2O2_SENSOR_NUM must be 1"); - - auto& cx = context; - string h2o2str[MAX_H2O2_SENSOR_NUM]; - string tempstr[MAX_H2O2_SENSOR_NUM]; - string humidstr[MAX_H2O2_SENSOR_NUM]; - string satstr[MAX_H2O2_SENSOR_NUM]; - - for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) { - h2o2str[i] = fmt::format("{}", cx.h2o2[i]); - tempstr[i] = fmt::format("{}", cx.temp[i]); - humidstr[i] = fmt::format("{}", cx.humid[i]); - satstr[i] = fmt::format("{}", cx.saturation[i]); - } +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) + h2o2str[0], tempstr[0], humidstr[0], satstr[0], // + h2o2str[1], tempstr[1], humidstr[1], satstr[1], // + h2o2str[2], tempstr[2], humidstr[2], satstr[2], // +#else + h2o2str[0], tempstr[0], humidstr[0], satstr[0], // +#endif - for (int i = 0; i < MAX_H2O2_SENSOR_NUM; i++) { - if (cx.h2o2[i] < 0) h2o2str[i] = "N/A"; - if (cx.temp[i] < 0) tempstr[i] = "N/A"; - if (cx.humid[i] < 0) humidstr[i] = "N/A"; - if (cx.saturation[i] < 0) satstr[i] = "N/A"; - } + (int32_t)dvalue, (int32_t)context.now_loglevel, (int32_t)cfg.targetLoglevel, // + ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // + m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime))); +} - auto ds = m_deviceIoControlService; - float dvalue = 0; - if (context.dvalue <= 0) { - dvalue = 0; - } else { - dvalue = context.dvalue; - } +void DisinfectionService::pushDisinfectionPrinterTask() { // + shared_ptr task = make_shared(); + auto ds = GET_SERVICE(DeviceStateService); + auto dio = m_deviceIoControlService; - int remaintime = getEstimatedRemainingTimeS(); + task->start_tp = context.start_tp; + task->complete_tp = context.complete_tp; + task->stateSnapshotList = context.stateSnapshotList; + task->disinfectantUsage = context.beforeDisinfectantVolume_g - context.afterDisinfectantVolume_g; + task->disinfection_id = context.id; + task->usr = ds->getLoginUid(); - context.csvlogger->write( - fmt::format(" {} ," - " {} , {} , {} , {} ," - " {} , {} , {} ," - " {} , {} , {} , {} ," - " {} , {} " - "\n", - getTime(), // - h2o2str[0], tempstr[0], humidstr[0], satstr[0], // - (int32_t)dvalue, (int32_t)context.now_loglevel, (int32_t)cfg.targetLoglevel, // - ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // - m_deviceIoControlService->getDisinfectantVolume_g(), formattimeS(remaintime))); + task->targetLog = cfg.targetLoglevel; + task->actualLog = context.now_loglevel; -#endif + m_disinfectionPrinterService->pushPrintTask(task); } void DisinfectionService::takeStateSnapshot() { @@ -398,11 +349,22 @@ void DisinfectionService::startDisinfection() { disinfection_event_t event; event.event = kdisinfection_event_start; sm.pushEvent(event); + + while (sm.getState() == kstate_finished) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } } void DisinfectionService::stopDisinfection() { + if (sm.getState() == kstate_finished) return; + if (sm.getState() == kstate_idle) return; + disinfection_event_t event; event.event = kdisinfection_event_stop; sm.pushEvent(event); + + while (sm.getState() != kstate_finished) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } } void DisinfectionService::updateDisinfectionCfg() { disinfection_event_t event; @@ -522,58 +484,6 @@ float DisinfectionService::computeNowLogLevel() { return context.now_loglevel; } -void DisinfectionService::beforeStateProcess() { updateH2O2SensorData(); } - -void DisinfectionService::processStateIdle(disinfection_event_t* event) { sm.changeState(kstate_finished); } -void DisinfectionService::processStatePreheat(disinfection_event_t* event) { - /** - * @brief 状态逻辑 - * 1. 记录日志 - * 2. 检查是否预热完成 - */ - - /******************************************************************************* - * 进入状态 * - *******************************************************************************/ - if (event->event == kdisinfection_event_enter_state) { - logger->info("preheat state enter"); - tryLogSatate(true); - -#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) - m_deviceIoControlService->heartingPlate_setPower(true); - m_deviceIoControlService->airBlower_setState(true); -#elif (defined PROJECT_TYPE_DRAW_BAR_BOX) -#else -#endif - } - - /******************************************************************************* - * 离开状态 * - *******************************************************************************/ - else if (event->event == kdisinfection_event_exit_state) { -#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) - m_deviceIoControlService->heartingPlate_setPower(false); - m_deviceIoControlService->airBlower_setState(false); -#elif (defined PROJECT_TYPE_DRAW_BAR_BOX) -#else -#endif - } - - /******************************************************************************* - * 状态周期调度事件 * - *******************************************************************************/ - - else if (event->event == kdisinfection_event_tmr_1s) { - if (sm.getStateHasPassedTimeMs() > cfg.pre_heat_time_s * 1000) { - /** - * @brief 退出预热状态,进入消毒状态 - */ - sm.changeState(kstate_disinfection); - } - tryLogSatate(false); - } -} - void DisinfectionService::updateRemainTime() { /** * @brief 计算Dvalue @@ -593,7 +503,7 @@ void DisinfectionService::updateRemainTime() { // } - logger->info("computeRemainTime minh2o2 {} dvalue {}", context.min_h2o2, dvalue); + // logger->info("computeRemainTime minh2o2 {} dvalue {}", context.min_h2o2, dvalue); } bool DisinfectionService::isTimeToPauseDisinfection() { @@ -603,9 +513,25 @@ bool DisinfectionService::isTimeToPauseDisinfection() { float nowSatur = context.max_saturation; float nowh2o2 = context.max_h2o2; float humid = context.max_humid; - if (nowSatur > cfg.stoped_satur || nowh2o2 > cfg.stoped_gs || humid > cfg.stoped_humi) { + // if (nowSatur > cfg.stoped_satur || nowh2o2 > cfg.stoped_gs || humid > cfg.stoped_humi) { + // return true; + // } + + if (nowSatur > cfg.stoped_satur) { + logger->info("nowSatur {} > cfg.stoped_satur {}", nowSatur, cfg.stoped_satur); + return true; + } + + if (nowh2o2 > cfg.stoped_gs) { + logger->info("nowh2o2 {} > cfg.stoped_gs {}", nowh2o2, cfg.stoped_gs); + return true; + } + + if (humid > cfg.stoped_humi) { + logger->info("humid {} > cfg.stoped_humi {}", humid, cfg.stoped_humi); return true; } + return false; } bool DisinfectionService::isTimeToResumeDisinfection() { @@ -617,8 +543,12 @@ bool DisinfectionService::isTimeToResumeDisinfection() { float humid = context.max_humid; if (nowSatur < cfg.continued_satur && nowh2o2 < cfg.continued_gs && humid < cfg.continued_humi) { + logger->info("nowSatur {} < cfg.continued_satur {} && nowh2o2 {} < cfg.continued_gs {} && humid {} < cfg.continued_humi {}", // + nowSatur, cfg.continued_satur, nowh2o2, cfg.continued_gs, humid, cfg.continued_humi); + return true; } + return false; } @@ -652,6 +582,65 @@ void DisinfectionService::updateSprayLiquidPumpSpeed() { #endif } +void DisinfectionService::beforeStateProcess() { updateH2O2SensorData(); } + +void DisinfectionService::processStateIdle(disinfection_event_t* event) { sm.changeState(kstate_finished); } +void DisinfectionService::processStatePreheat(disinfection_event_t* event) { + /** + * @brief 状态逻辑 + * 1. 记录日志 + * 2. 检查是否预热完成 + */ + + /******************************************************************************* + * 进入状态 * + *******************************************************************************/ + if (event->event == kdisinfection_event_enter_state) { + logger->info("preheat state enter"); + tryLogSatate(true); + +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) + m_deviceIoControlService->heartingPlate_setPower(true); + m_deviceIoControlService->airBlower_setState(true); +#elif (defined PROJECT_TYPE_DRAW_BAR_BOX) +#else +#endif + } + + /******************************************************************************* + * 离开状态 * + *******************************************************************************/ + else if (event->event == kdisinfection_event_exit_state) { +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) + m_deviceIoControlService->heartingPlate_setPower(false); + m_deviceIoControlService->airBlower_setState(false); +#elif (defined PROJECT_TYPE_DRAW_BAR_BOX) +#else +#endif + } + + /******************************************************************************* + * 状态周期调度事件 * + *******************************************************************************/ + + else if (event->event == kdisinfection_event_tmr_1s) { + if (sm.getStateHasPassedTimeMs() > cfg.pre_heat_time_s * 1000) { + /** + * @brief 退出预热状态,进入消毒状态 + */ + sm.changeState(kstate_disinfection); + } + tryLogSatate(false); + } + + /******************************************************************************* + * 结束消毒 * + *******************************************************************************/ + else if (event->event == kdisinfection_event_stop) { + sm.changeState(kstate_finished); + } +} + void DisinfectionService::processStateDisinfection(disinfection_event_t* event) { /** * @brief 状态逻辑 @@ -664,36 +653,32 @@ void DisinfectionService::processStateDisinfection(disinfection_event_t* event) *******************************************************************************/ if (event->event == kdisinfection_event_enter_state) { tryLogSatate(true); + context.is_disinfection_take_break = false; -#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) - m_deviceIoControlService->airBlower_setState(true); - usleep(1000 * 1000); - m_deviceIoControlService->airCompressor_setState(true); - usleep(1000 * 1000); - m_deviceIoControlService->sprayLiquidPump_open(cfg.injection_pump_speed); -#elif (defined PROJECT_TYPE_PIPE_DISINFECTION) +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) +#if (defined PROJECT_TYPE_PIPE_DISINFECTION) m_deviceIoControlService->airCompressor_channelSelect(1); m_deviceIoControlService->airCompressor_setValve1(1); m_deviceIoControlService->airCompressor_setValve2(1); - +#endif m_deviceIoControlService->airBlower_setState(true); usleep(1000 * 1000); m_deviceIoControlService->airCompressor_setState(true); usleep(1000 * 1000); m_deviceIoControlService->sprayLiquidPump_open(cfg.injection_pump_speed); +#endif -#elif (defined PROJECT_TYPE_DRAW_BAR_BOX) +#if (defined PROJECT_TYPE_DRAW_BAR_BOX) m_deviceIoControlService->DBDB__sprayAirCompressorPowerCtrl(1); usleep(1000 * 1000); m_deviceIoControlService->DBDB__heaterCtrl(1); usleep(100 * 1000); m_deviceIoControlService->DBDB__heaterCtrlSafeValve(1); usleep(100 * 1000); - m_deviceIoControlService->DBDB__miniPwmBlowerCtrl(1); + m_deviceIoControlService->DBDB__miniPwmBlowerCtrl(DBDB_MINI_PWM_BLOWER_VAL); // 风量 usleep(100 * 1000); m_deviceIoControlService->DBDB__extValveCtrl(IF_DeviceIoContrlService::kExtValveChannel_disinfectionChannel); -#else #endif } @@ -760,10 +745,12 @@ void DisinfectionService::processStateDisinfection(disinfection_event_t* event) if (!context.is_disinfection_take_break) { if (isTimeToPauseDisinfection()) { + logstate(); pauseDisinfection(); } } else { if (isTimeToResumeDisinfection()) { + logstate(); resumeDisinfection(); } } @@ -782,11 +769,17 @@ void DisinfectionService::processStateDisinfection(disinfection_event_t* event) updateSprayLiquidPumpSpeed(); } } + /******************************************************************************* + * 结束消毒 * + *******************************************************************************/ + else if (event->event == kdisinfection_event_stop) { + sm.changeState(kstate_finished); + } } void DisinfectionService::processStateFinished(disinfection_event_t* event) { if (event->event == kdisinfection_event_enter_state) { - if (event->enterState.last != kstate_idle) { + if (event->enterState.last != kstate_idle && event->enterState.last != kstate_finished) { /** * @brief 消毒结束 */ @@ -860,21 +853,3 @@ void DisinfectionService::processStateDegradation(disinfection_event_t* event) { } void DisinfectionService::processStateDehumidification_before_disinfection(disinfection_event_t* event) {} void DisinfectionService::processStateDehumidification_after_disinfection(disinfection_event_t* event) {} - -void DisinfectionService::pushDisinfectionPrinterTask() { // - shared_ptr task = make_shared(); - auto ds = GET_SERVICE(DeviceStateService); - auto dio = m_deviceIoControlService; - - task->start_tp = context.start_tp; - task->complete_tp = context.complete_tp; - task->stateSnapshotList = context.stateSnapshotList; - task->disinfectantUsage = context.beforeDisinfectantVolume_g - context.afterDisinfectantVolume_g; - task->disinfection_id = context.id; - task->usr = ds->getLoginUid(); - - task->targetLog = cfg.targetLoglevel; - task->actualLog = context.now_loglevel; - - m_disinfectionPrinterService->pushPrintTask(task); -} diff --git a/src/service/disfection_ctl/disinfection_state.hpp b/src/service/disfection_ctl/disinfection_state.hpp index 0ef5411..64e9f13 100644 --- a/src/service/disfection_ctl/disinfection_state.hpp +++ b/src/service/disfection_ctl/disinfection_state.hpp @@ -58,6 +58,27 @@ typedef enum { } disinfection_event_type_t; +static inline const char* disinfection_event_to_string(disinfection_event_type_t event) { + switch (event) { + case kdisinfection_event_start: + return "start"; + case kdisinfection_event_update_cfg: + return "update_cfg"; + case kdisinfection_event_stop: + return "stop"; + case kdisinfection_event_enter_state: + return "enter_state"; + case kdisinfection_event_exit_state: + return "exit_state"; + case kdisinfection_event_tmr_1s: + return "tmr_1s"; + case kdisinfection_event_change_state: + return "change_state"; + default: + return "unknown"; + } +} + static inline const char* disinfection_state_to_string(disinfection_state_t state) { switch (state) { case kstate_idle: diff --git a/src/service/disfection_ctl/disinfection_state_machine.cpp b/src/service/disfection_ctl/disinfection_state_machine.cpp index 1a69021..9be5396 100644 --- a/src/service/disfection_ctl/disinfection_state_machine.cpp +++ b/src/service/disfection_ctl/disinfection_state_machine.cpp @@ -8,12 +8,13 @@ void DisinfectionStateMachine::regStateProcesser( // void DisinfectionStateMachine::regBfStateProcess(function processer) { m_bf_state_processer = processer; } void DisinfectionStateMachine::pushEvent(disinfection_event_t event) { - lock_guard lock(lock_); - m_event_list.push_back(event); + lock_guard lock(lock_); + shared_ptr event_ptr(new disinfection_event_t); + *event_ptr = event; + m_event_list.push_back(event_ptr); } void DisinfectionStateMachine::changeState(disinfection_state_t state) { lock_guard lock(lock_); - m_state = state; m_event_list.clear(); disinfection_event_t event; event.event = kdisinfection_event_change_state; @@ -33,41 +34,44 @@ void DisinfectionStateMachine::callStateProcesser(disinfection_state_t state, di void DisinfectionStateMachine::startProcess() { m_process_thread_.reset(new Thread("DisinfectionStateMachineThread", [this]() { while (true) { - this_thread::sleep_for(chrono::milliseconds(10)); + this_thread::sleep_for(chrono::milliseconds(2)); - disinfection_event_t event; - bool popevent = false; + shared_ptr event; + bool popevent = false; { lock_guard lock(lock_); if (!m_event_list.empty()) { + logger->info("event list size: {}", m_event_list.size()); event = m_event_list.front(); m_event_list.pop_front(); + logger->info("event list size: {}", m_event_list.size()); popevent = true; } } if (popevent) { - if (event.event == kdisinfection_event_change_state) { + if (event->event == kdisinfection_event_change_state) { disinfection_event_t exit_event; disinfection_event_t enter_event; exit_event.event = kdisinfection_event_exit_state; - exit_event.exitState.next = event.changeState.to; + exit_event.exitState.next = event->changeState.to; enter_event.event = kdisinfection_event_enter_state; - enter_event.enterState.last = event.changeState.from; + enter_event.enterState.last = event->changeState.from; callStateProcesser(m_state, &exit_event); - m_state = event.changeState.to; + m_state = event->changeState.to; m_enter_state_tp = zsystem_clock().now(); logger->info("change state from {} to {}", // - disinfection_state_to_string(event.changeState.from), disinfection_state_to_string(event.changeState.to)); + disinfection_state_to_string(event->changeState.from), disinfection_state_to_string(event->changeState.to)); callStateProcesser(m_state, &enter_event); } else { - callStateProcesser(m_state, &event); + logger->info("process event: {}", disinfection_event_to_string(event->event)); + callStateProcesser(m_state, event.get()); } } diff --git a/src/service/disfection_ctl/disinfection_state_machine.hpp b/src/service/disfection_ctl/disinfection_state_machine.hpp index 72ed260..91cbac8 100644 --- a/src/service/disfection_ctl/disinfection_state_machine.hpp +++ b/src/service/disfection_ctl/disinfection_state_machine.hpp @@ -20,10 +20,10 @@ class DisinfectionStateMachine { ENABLE_LOGGER(DisinfectionStateMachine); private: - disinfection_state_t m_state = kstate_idle; - list m_event_list; - std::recursive_mutex lock_; - unique_ptr m_process_thread_; + disinfection_state_t m_state = kstate_idle; + list> m_event_list; + std::recursive_mutex lock_; + unique_ptr m_process_thread_; map> m_state_processer_map; diff --git a/src/service/disinfection_ctl_service.cpp b/src/service/disinfection_ctl_service.cpp index 5a12f2d..4d71fb9 100644 --- a/src/service/disinfection_ctl_service.cpp +++ b/src/service/disinfection_ctl_service.cpp @@ -1,8 +1,10 @@ #include "disinfection_ctl_service.hpp" using namespace iflytop; +/******************************************************************************* + * 消毒 * + *******************************************************************************/ void DisinfectionCtrlService::startDisinfection() { - stopDisinfection(); #if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) stopDraining(); stopReplenishingFluids(); @@ -12,21 +14,29 @@ void DisinfectionCtrlService::startDisinfection() { } void DisinfectionCtrlService::changeDisinfectionParameter() { m_disinfectionService->updateDisinfectionCfg(); } void DisinfectionCtrlService::stopDisinfection() { m_disinfectionService->stopDisinfection(); } -#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) -void DisinfectionCtrlService::startReplenishingFluids(int stopatg) { m_addLiquidService->startReplenishingFluids(stopatg); } -void DisinfectionCtrlService::stopReplenishingFluids() { +#if (defined PROJECT_TYPE_LARGE_SPACE_DISINFECTION) || (defined PROJECT_TYPE_SMALL_SPACE_DISINFECTION) || (defined PROJECT_TYPE_PIPE_DISINFECTION) +/******************************************************************************* + * 加液 * + *******************************************************************************/ +void DisinfectionCtrlService::startReplenishingFluids(int stopatg) { stopDisinfection(); stopDraining(); stopReplenishingFluids(); - m_addLiquidService->stopReplenishingFluids(); + m_addLiquidService->startReplenishingFluids(stopatg); } +void DisinfectionCtrlService::stopReplenishingFluids() { m_addLiquidService->stopReplenishingFluids(); } -void DisinfectionCtrlService::startDraining() { m_drainLiquidService->startDraining(); } -void DisinfectionCtrlService::stopDraining() { +/******************************************************************************* + * 排液 * + *******************************************************************************/ +void DisinfectionCtrlService::startDraining() { stopDisinfection(); stopDraining(); stopReplenishingFluids(); - m_drainLiquidService->stopDraining(); + + m_drainLiquidService->startDraining(); } + +void DisinfectionCtrlService::stopDraining() { m_drainLiquidService->stopDraining(); } #endif