diff --git a/src/service/disinfection_ctl_service.cpp b/src/service/disinfection_ctl_service.cpp index 66f496e..a51bfa4 100644 --- a/src/service/disinfection_ctl_service.cpp +++ b/src/service/disinfection_ctl_service.cpp @@ -81,6 +81,12 @@ string DisinfectionCtrlService::createDisinfectionID() { tm.tm_hour, // tm.tm_min, tm.tm_sec); } +static bool zfeq(float a, float b, float eps = 0.01) { + if (fabs(a - b) < eps) { + return true; + } + return false; +} float DisinfectionCtrlService::getDisinfectionDValue(float ppm) { /** * @brief @@ -91,6 +97,10 @@ float DisinfectionCtrlService::getDisinfectionDValue(float ppm) { * 浓度大于150时,y=-0.1405X+40.369 */ + if (zfeq(ppm, 0)) { + return -1; + } + float dvalue = 0; if (ppm < 150) { @@ -115,15 +125,20 @@ void DisinfectionCtrlService::computeRemainTime(DisinfectionContext& context) { */ float dvalue = context.dvalue; - /** - * @brief 计算 m_nowLoglevel - */ - context.m_nowLoglevel += DVALUE_COMPUTEPERIOD_TIME_S / (dvalue * 60); - if (context.m_targetLoglevel >= context.m_nowLoglevel) { - context.m_remaintime = (context.m_targetLoglevel - context.m_nowLoglevel) * (dvalue * 60); + if (dvalue > 0) { + /** + * @brief 计算 m_nowLoglevel + */ + context.m_nowLoglevel += DVALUE_COMPUTEPERIOD_TIME_S / (dvalue * 60); + if (context.m_targetLoglevel >= context.m_nowLoglevel) { + context.m_remaintime = (context.m_targetLoglevel - context.m_nowLoglevel) * (dvalue * 60); + } else { + context.m_remaintime = 0; + } } else { - context.m_remaintime = 0; + // } + logger->info("computeRemainTime minh2o2 {} dvalue {}", context.h2o2data.min_h2o2, dvalue); } @@ -186,39 +201,39 @@ void DisinfectionCtrlService::initContext(DisinfectionContext& context, // m_context.m_remaintime context.csvlogger->write( - "date," // - "hydrogen_peroxide_volume[1],temperature[1],relative_humidity[1],h2o_h2o2_rs[1]," // + "date," // + "hydrogen_peroxide_volume(ppm),temperature(C),relative_humidity(%RH),h2o_h2o2_rs(%RS)," // // "h2o22,temp2,humi2,saturation2," // // "h2o23,temp3,humi3,saturation3," // - "dvalue,loglevel,targetloglevel," // - "heating,blower,compressor,pump," // - // "disinfectantVolume," // - "remaintime\n" // + "dvalue,loglevel,targetloglevel," // + "heating,blower,compressor,pump(g/min)," // + "disinfectantVolume(g)," // + "remaintime(s)\n" // ); } - -void DisinfectionCtrlService::dumpDisinfectionLogsToCSV(DisinfectionContext& context) { +static string formattimeS(int sec) { return fmt::format("{:0>2}:{:0>2}:{:0>2}", sec / 3600, sec % 3600 / 60, sec % 60); } +void DisinfectionCtrlService::dumpDisinfectionLogsToCSV(DisinfectionContext& context) { auto* sensors = &m_context.h2o2data.h2o2sensor_data[0]; auto ds = m_deviceIoControlService; context.csvlogger->write( fmt::format("{}," // - "{},{},{},{}," // - // "{},{},{},{}," // - // "{},{},{},{}," // - "{},{},{}," // - "{},{},{},{}," // - // "{},{},{},{}," // - // "{}," // - "{}\n" // - , - getTime(), // - sensors[0].h2o2, sensors[0].temp, sensors[0].humid, sensors[0].saturation, // - // sensors[1].h2o2, sensors[1].temp, sensors[1].humid, sensors[1].saturation, // - // sensors[2].h2o2, sensors[2].temp, sensors[2].humid, sensors[2].saturation, // - m_context.dvalue, m_context.m_nowLoglevel, (int)m_context.m_targetLoglevel, // - ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // - // m_deviceIoControlService->getDisinfectantVolume_g(), // - m_context.m_remaintime)); + "{},{},{},{}," // + // "{},{},{},{}," // + // "{},{},{},{}," // + "{:.2f},{:.2f},{:.2f}," // + "{},{},{},{}," // + // "{},{},{},{}," // + "{}," // + "{}\n" // + , + getTime(), // + sensors[0].h2o2, sensors[0].temp, sensors[0].humid, sensors[0].saturation, // + // sensors[1].h2o2, sensors[1].temp, sensors[1].humid, sensors[1].saturation, // + // sensors[2].h2o2, sensors[2].temp, sensors[2].humid, sensors[2].saturation, // + m_context.dvalue, m_context.m_nowLoglevel, m_context.m_targetLoglevel, // + ds->heatingStrip_getstate(), ds->airBlower_getstate(), ds->airCompressor_getstate(), ds->sprayLiquidPump_getGPM(), // + m_deviceIoControlService->getDisinfectantVolume_g(), // + formattimeS(m_context.m_remaintime))); } void DisinfectionCtrlService::finishDisinfection(DisinfectionContext& context) { @@ -299,10 +314,7 @@ void DisinfectionCtrlService::processDisinfectionState(DisinfectionContext& cont /** * @brief 周期性计算剩余时间 */ - if (zsteady_clock().elapsedTimeS(m_context.m_lastComputeDvalueTp) > DVALUE_COMPUTEPERIOD_TIME_S) { - m_context.m_lastComputeDvalueTp = zsteady_clock().now(); - computeRemainTime(m_context); - } + /** * @brief 根据湿度启停喷雾 */ @@ -364,6 +376,11 @@ void DisinfectionCtrlService::disinfectionLoop(bool& breakflag) { */ m_context.dvalue = getDisinfectionDValue(m_context.h2o2data.min_h2o2); + if (zsteady_clock().elapsedTimeS(m_context.m_lastComputeDvalueTp) > DVALUE_COMPUTEPERIOD_TIME_S) { + m_context.m_lastComputeDvalueTp = zsteady_clock().now(); + computeRemainTime(m_context); + } + if (m_context.m_state == kpreheat) { /** * @brief 预热中 @@ -376,8 +393,8 @@ void DisinfectionCtrlService::disinfectionLoop(bool& breakflag) { processDisinfectionState(m_context); // if (m_context.m_remaintime <= 0 && m_context.m_nowLoglevel > (m_context.m_targetLoglevel + 0.01)) { - m_context.m_remaintime = 0; - m_context.m_nowLoglevel = m_context.m_targetLoglevel + 0.01; + m_context.m_remaintime = 0; + m_context.m_nowLoglevel = m_context.m_targetLoglevel + 0.01; logger->info("disinfection finished {},but waitting for h2o2 to safe", m_context.m_disinfectionID); m_deviceIoControlService->sprayLiquidPump_close(); usleep(1000 * 1000);