diff --git a/src/db/db_service.cpp b/src/db/db_service.cpp index 161c940..e351970 100644 --- a/src/db/db_service.cpp +++ b/src/db/db_service.cpp @@ -155,6 +155,9 @@ id setting_name setting_name_ch val_upper_limit val_lower_limi if (storage.get_all(where(c(&Setting::id) == 7)).size() == 0) storage.insert(Setting{7, "injection_pump_speed", "喷射蠕动泵转速", 0, 350, 2, 300}); + + if (storage.get_all(where(c(&Setting::id) == 8)).size() == 0) // + storage.insert(Setting{8, "pre_heat_time_s", "预热时间", 0, 600, 1, 120}); suc = true; } catch (const std::exception& e) { diff --git a/src/main_control_service.cpp b/src/main_control_service.cpp index 0b34ebd..8fed7a4 100644 --- a/src/main_control_service.cpp +++ b/src/main_control_service.cpp @@ -44,6 +44,12 @@ void MainControlService::initialize() { GET_SERVICE(ZCanHost)->initialize(m_zconfig->get_iflytopSubDeviceCanIFName(), m_zconfig->get_iflytopSubDeviceCanBitrate(), false); GET_TO_SERVICE(m_zcanhost); + BUILD_AND_REG_SERRVICE(SensorDataScan); + GET_SERVICE(SensorDataScan)->initialize(); + GET_TO_SERVICE(m_sensorDataScan); + + m_sensorDataScan->startScan(); + /** * @brief Get the to service object */ @@ -99,46 +105,7 @@ void MainControlService::initialize() { m_dbService.reset(new DBService()); m_dbService->initialize(); - // shared_ptr zcanReceiverHost = make_shared(); - // zcanReceiverHost->initialize(m_zconfig->get_iflytopSubDeviceCanIFName(), m_zconfig->get_iflytopSubDeviceCanBitrate(), false); - // while (true) { - // shared_ptr cmd = make_shared(); - // cmd->packetindex = 1; - // cmd->cmdid = 1004; - // cmd->subcmdid = 0; - // cmd->data[0] = 0x02; - // cmd->data[1] = 0; - // *(int16_t*)(&cmd->data[2]) = 300; - // *(int16_t*)(&cmd->data[4]) = -1000; - // cmd->len = 6; - // logger->info("send cmd:{}", cmd->toString()); - // zcanReceiverHost->sendcmdblock(cmd, 100); - // sleep(3); - // } - m_reportThread.reset(new Thread("reportThread", [this]() { - // - // { - // "command": "RealtimeSensorDataReport", - // "sensor_data": { - // "heating_strip": 1, - // "air_compressor": 0, - // "sprinkler_pump": 1, - // "disinfectant_volume": 99, - // "h2o2_1": 10, - // "temp_1": 11, - // "humid_1": 12, - // "saturation_1": 13, - // "h2o2_2": 14, - // "temp_2": 15, - // "humid_2": 16, - // "saturation_2": 17, - // "h2o2_3": 18, - // "temp_3": 19, - // "humid_3": 20, - // "saturation_3": 21 - // } - // } ThisThread thisThread; while (!thisThread.getExitFlag()) { json report; @@ -152,31 +119,30 @@ void MainControlService::initialize() { json MainControlService::createSensorDataJson() { json report; + report["airCompressor"]["io1"] = m_sensorDataScan->getAirCompressor_io1(); + report["airCompressor"]["io2"] = m_sensorDataScan->getAirCompressor_io2(); + report["airCompressor"]["currentVal"] = m_sensorDataScan->getAirCompressor_currentValue(); - report["airCompressor"]["io1"] = 1; - report["airCompressor"]["io2"] = 0; - report["airCompressor"]["currentVal"] = 2.2; - - report["airBlower"]["io1"] = 1; - report["airBlower"]["io2"] = 0; - report["airBlower"]["currentVal"] = 2.2; + report["airBlower"]["io1"] = m_sensorDataScan->getAirBlower_io1(); + report["airBlower"]["io2"] = m_sensorDataScan->getAirBlower_io2(); + report["airBlower"]["currentVal"] = m_sensorDataScan->getAirBlower_currentValue(); - report["heatingStrip"]["io1"] = 1; - report["heatingStrip"]["io2"] = 0; - report["heatingStrip"]["currentVal"] = 2.2; + report["heatingStrip"]["io1"] = m_sensorDataScan->getHeatingStrip_io1(); + report["heatingStrip"]["io2"] = m_sensorDataScan->getHeatingStrip_io2(); + report["heatingStrip"]["currentVal"] = m_sensorDataScan->getHeatingStrip_currentValue(); - report["sprinklerPump"] = 1000; - report["chargingPump"] = 999; + report["sprinklerPump"] = m_sensorDataScan->getSprinkler_PumpRPM(); + report["chargingPump"] = m_sensorDataScan->getChargingPump_PumpRPM(); - report["waterImmersionSensor1"] = 1; - report["waterImmersionSensor2"] = 1; + report["waterImmersionSensor1"] = m_sensorDataScan->getWaterImmersionSensor1(); + report["waterImmersionSensor2"] = m_sensorDataScan->getWaterImmersionSensor2(); // Water immersion sensor - report["disinfectant_volume"] = 99; - report["h2o2_1"] = 10; - report["temp_1"] = 11; - report["humid_1"] = 12; - report["saturation_1"] = 13; + report["disinfectant_volume"] = m_sensorDataScan->getDisinfectantVolume_g(); + report["h2o2_1"] = m_sensorDataScan->getH2O2SenSorData1().h2o2; + report["temp_1"] = m_sensorDataScan->getH2O2SenSorData1().temp; + report["humid_1"] = m_sensorDataScan->getH2O2SenSorData1().humid; + report["saturation_1"] = m_sensorDataScan->getH2O2SenSorData1().saturation; report["h2o2_2"] = -1; report["temp_2"] = -1; report["humid_2"] = -1; @@ -241,7 +207,7 @@ void MainControlService::processFrontEndMessage(weak_ptr webSocket, j if (cmdstr == "shutdown") { int delayms = jsonGet(cmd["delayms"]); logger->info("shutdown {} ms", delayms); - m_autoshutdownThread.reset(new Thread("autoShutdown",[delayms]() { + m_autoshutdownThread.reset(new Thread("autoShutdown", [delayms]() { ThisThread thisThread; thisThread.sleepForMs(delayms); system("shutdown -h now"); @@ -317,7 +283,8 @@ void MainControlService::processFrontEndMessage(weak_ptr webSocket, j if (cmdstr == "startReplenishingFluids") { logger->info("startReplenishingFluids"); - m_disinfectionCtrlService->startReplenishingFluids(); + int16_t stopAt = jsonGet(cmd["stopAt"]); + m_disinfectionCtrlService->startReplenishingFluids(stopAt); return; } diff --git a/src/main_control_service.hpp b/src/main_control_service.hpp index f3849e9..012857c 100644 --- a/src/main_control_service.hpp +++ b/src/main_control_service.hpp @@ -30,6 +30,7 @@ // #include "db/db_service.hpp" #include "iflytop/components/zcanreceiver/zcanhost.hpp" +#include "service/sensor_data_scan.hpp" #include "src/service/iflytop_can_host_device.hpp" /** @@ -56,6 +57,7 @@ class MainControlService : public enable_shared_from_this { shared_ptr m_dbService; shared_ptr m_deviceStateService; shared_ptr m_disinfectionCtrlService; + shared_ptr m_sensorDataScan; shared_ptr m_zcanhost; unique_ptr m_reportThread; diff --git a/src/service/disinfection_ctl_service.cpp b/src/service/disinfection_ctl_service.cpp index 0f338ac..a2c15ad 100644 --- a/src/service/disinfection_ctl_service.cpp +++ b/src/service/disinfection_ctl_service.cpp @@ -1,6 +1,28 @@ #include "disinfection_ctl_service.hpp" using namespace iflytop; using namespace std; +/** + * @brief + * 流程 + * 预热: + * + * 风机开 + * 1s + * 空压机 + * 1s + * 蠕动泵 + * 1s + * + * + * 关蠕动泵 + * 1s + * 关空压机 + * 1s + * 关风机 + * + */ + +#define PRE_HEAT_TIME 60 void DisinfectionCtrlService::drainingPump_open() { m_zcanHost->pumpctrl_c1004(1, 300, -300, 1, 20); } void DisinfectionCtrlService::drainingPump_close() { m_zcanHost->pumpctrl_c1004(1, 300, 0, 1, 20); } @@ -8,7 +30,7 @@ void DisinfectionCtrlService::drainingPump_close() { m_zcanHost->pumpctrl_c1004( void DisinfectionCtrlService::replenishingFluidsPump_open() { m_zcanHost->pumpctrl_c1004(1, 300, 300, 1, 20); } void DisinfectionCtrlService::replenishingFluidsPump_close() { m_zcanHost->pumpctrl_c1004(1, 300, 0, 1, 20); } -void DisinfectionCtrlService::sprayLiquidPump_open() { m_zcanHost->pumpctrl_c1004(1, 300, 300, 1, 10); } +void DisinfectionCtrlService::sprayLiquidPump_open() { m_zcanHost->pumpctrl_c1004(1, 300, 200, 1, 10); } void DisinfectionCtrlService::sprayLiquidPump_close() { m_zcanHost->pumpctrl_c1004(1, 300, 0, 1, 10); } void DisinfectionCtrlService::airCompressor(bool val) { @@ -64,7 +86,10 @@ string DisinfectionCtrlService::createDisinfectionID() { tm.tm_min, tm.tm_sec); } -void DisinfectionCtrlService::initialize() { GET_TO_SERVICE(m_zcanHost); } +void DisinfectionCtrlService::initialize() { + GET_TO_SERVICE(m_zcanHost); + GET_TO_SERVICE(m_sensorDataScan); +} void DisinfectionCtrlService::startDisinfection(int loglevel, float roomVol) { lock_guard lock(lock_); @@ -73,27 +98,17 @@ void DisinfectionCtrlService::startDisinfection(int loglevel, float roomVol) { } m_disinfectionID = createDisinfectionID(); - m_remaintime = loglevel * 20 * 60; + m_remaintime = loglevel * 20 * 60; // 计算总的加热时间 m_disinfectionThread.reset(new Thread("m_disinfectionThread", [this, roomVol, loglevel]() { - // do something - // m_disinfectionThread->detach(); ThisThread thisThread; + bool preHeartFinishedFlag = false; - // 发送开始拍照消息 - logger->info("start disinfection {}", m_disinfectionID); - - m_isDisinfectionRunning = true; - m_disinfectionWorkState = 1; logger->info("startDisinfection {} {} {}", loglevel, roomVol, m_disinfectionID); - airCompressor(true); - usleep(100 * 1000); - blower_setPower(true); - usleep(100 * 1000); + auto starttime = zsteady_clock().now(); + m_disinfectionWorkState = 1; heartingPlate_setPower(true); - usleep(100 * 1000); - sprayLiquidPump_open(); while (!thisThread.getExitFlag()) { thisThread.sleepForMs(1000); @@ -101,25 +116,36 @@ void DisinfectionCtrlService::startDisinfection(int loglevel, float roomVol) { logger->info("disinfection paused {} {}", m_disinfectionID, m_remaintime); continue; } + m_remaintime--; if (m_remaintime == 0) { logger->info("disinfection finished {}", m_disinfectionID); break; } - logger->info("disinfection running {} {}s", m_disinfectionID, m_remaintime); + logger->info("disinfection running {} {}s preheatFlag:{}", m_disinfectionID, m_remaintime, preHeartFinishedFlag); + + if (!preHeartFinishedFlag && zsteady_clock().elapsedTimeS(starttime) > PRE_HEAT_TIME) { + logger->info("preheat finished {}", m_disinfectionID); + blower_setPower(true); + usleep(1000 * 1000); + airCompressor(true); + usleep(1000 * 1000); + sprayLiquidPump_open(); + preHeartFinishedFlag = true; + } } m_remaintime = 0; logger->info("stop disinfection {}", m_disinfectionID); + sprayLiquidPump_close(); + usleep(1000 * 1000); airCompressor(false); - usleep(10 * 1000); + usleep(1000 * 1000); blower_setPower(false); - usleep(10 * 1000); + usleep(1000 * 1000); heartingPlate_setPower(false); - usleep(10 * 1000); - sprayLiquidPump_close(); m_disinfectionWorkState = 3; })); } @@ -135,10 +161,6 @@ void DisinfectionCtrlService::stopDisinfection() { m_disinfectionThread = nullptr; } } -bool DisinfectionCtrlService::isDisinfectionRunning() { - lock_guard lock(lock_); - return m_isDisinfectionRunning; -} int32_t DisinfectionCtrlService::getEstimatedRemainingTimeS() { lock_guard lock(lock_); @@ -150,16 +172,37 @@ string DisinfectionCtrlService::getDisinfectionID() { } // 加液 -void DisinfectionCtrlService::startReplenishingFluids() { +void DisinfectionCtrlService::startReplenishingFluids(int stopatg) { lock_guard lock(lock_); if (m_disinfectionThread) { m_disinfectionThread->join(); m_disinfectionThread = nullptr; } + int32_t nowvolume = m_sensorDataScan->getDisinfectantVolume_g(); + if (nowvolume > stopatg) { + logger->warn("start Replenishing fail, nowvolume {} > stopatg {}", nowvolume, stopatg); + return; + } + + m_disinfectionThread.reset(new Thread("disinfectionThread", [this, stopatg]() { + ThisThread thisThread; + replenishingFluidsPump_open(); + m_replenishingFluidsWorkState = 1; + while (thisThread.getExitFlag()) { + int32_t nowvolume = m_sensorDataScan->getDisinfectantVolume_g(); + logger->info("replenishingFluids {}g", nowvolume); + if (nowvolume > stopatg) { + break; + } + } + logger->info("stopReplenishingFluids"); + replenishingFluidsPump_close(); + m_replenishingFluidsWorkState = 0; + })); + + // - logger->info("startReplenishingFluids"); - replenishingFluidsPump_open(); - m_replenishingFluidsWorkState = 1; + logger->info("startReplenishingFluids "); } // 停止加液 void DisinfectionCtrlService::stopReplenishingFluids() { diff --git a/src/service/disinfection_ctl_service.hpp b/src/service/disinfection_ctl_service.hpp index 469b3cc..8f1d12f 100644 --- a/src/service/disinfection_ctl_service.hpp +++ b/src/service/disinfection_ctl_service.hpp @@ -16,6 +16,7 @@ #include "iflytop/components/zcanreceiver/zcanhost.hpp" #include "iflytop/core/core.hpp" +#include "service/sensor_data_scan.hpp" #include "zservice_container/zservice_container.hpp" /** @@ -36,11 +37,11 @@ using namespace core; class DisinfectionCtrlService : public enable_shared_from_this { ENABLE_LOGGER(DisinfectionCtrlService); - unique_ptr m_disinfectionThread; - string m_disinfectionID; + unique_ptr m_disinfectionThread; + shared_ptr m_sensorDataScan; + string m_disinfectionID; int m_remaintime = 0; - bool m_isDisinfectionRunning = false; recursive_mutex lock_; int m_disinfectionWorkState = 0; @@ -75,7 +76,7 @@ class DisinfectionCtrlService : public enable_shared_from_thispumpctrl_c1004_get_speed_cache(2); } int SensorDataScan::getChargingPump_PumpRPM() { return m_zcanHost->pumpctrl_c1004_get_speed_cache(1); } -int SensorDataScan::getDisinfectantVolume() { +int SensorDataScan::getDisinfectantVolume_g() { // kpa; int pa = m_zcanHost->huacheng_pressure_sensor_get_pa(&m_pressure_sensor_data[0]); // m(kg)=2.11*P(kPa) @@ -82,7 +82,7 @@ int SensorDataScan::getDisinfectantVolume() { int SensorDataScan::getWaterImmersionSensor1() { return m_waterImmersionSensor1 ? 1 : 0; } int SensorDataScan::getWaterImmersionSensor2() { return m_waterImmersionSensor2 ? 1 : 0; } -SensorDataScan::h2o2sensor_data_t SensorDataScan::getH2O2SenSorData(int id) { +SensorDataScan::h2o2sensor_data_t SensorDataScan::getH2O2SenSorData1() { SensorDataScan::h2o2sensor_data_t data; // int16_t hydrogen_peroxide_volume; // ppm 0x0100 过氧化氢浓度 // int16_t h2o_h2o2_rs; // %RS * 100 0x0101 过氧化氢相对饱和度 diff --git a/src/service/sensor_data_scan.hpp b/src/service/sensor_data_scan.hpp index 31ff600..03081cb 100644 --- a/src/service/sensor_data_scan.hpp +++ b/src/service/sensor_data_scan.hpp @@ -44,6 +44,7 @@ class SensorDataScan : public enable_shared_from_this { ZCanHost::hpp272_data_t m_hpp272_data_1; ZCanHost::huacheng_pressure_sensor_read_c1005_t m_pressure_sensor_data[4]; + // bool m_waterImmersionSensor1 = false; // 漏液检测 bool m_waterImmersionSensor2 = false; // 仓内液体检测 @@ -76,7 +77,7 @@ class SensorDataScan : public enable_shared_from_this { int getSprinkler_PumpRPM(); int getChargingPump_PumpRPM(); - int getDisinfectantVolume(); // g + int getDisinfectantVolume_g(); // g int getWaterImmersionSensor1(); int getWaterImmersionSensor2(); @@ -88,6 +89,6 @@ class SensorDataScan : public enable_shared_from_this { int saturation; // 相对饱和度 %RS } h2o2sensor_data_t; - h2o2sensor_data_t getH2O2SenSorData(int id); + h2o2sensor_data_t getH2O2SenSorData1(); }; } // namespace iflytop \ No newline at end of file