From 960cdeac3d65c5cb922f270868a732fa23941e88 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 19 Aug 2024 16:14:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B6=88=E6=AF=92=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=89=93=E5=8D=B0=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appdep/iflytop/core/components/zcsv/zcsv.cpp | 4 +- appsrc/service/disinfection_logs_service.cpp | 176 ++++++++++++++++----------- appsrc/service/disinfection_logs_service.hpp | 2 + 3 files changed, 109 insertions(+), 73 deletions(-) diff --git a/appdep/iflytop/core/components/zcsv/zcsv.cpp b/appdep/iflytop/core/components/zcsv/zcsv.cpp index 306d261..6311a1c 100644 --- a/appdep/iflytop/core/components/zcsv/zcsv.cpp +++ b/appdep/iflytop/core/components/zcsv/zcsv.cpp @@ -139,7 +139,7 @@ void ZCSV::dumpCSV(string filename) { } void ZCSV::addValue(string key, string value) { - printf("addValue key=%s value=%s\n", key.c_str(), value.c_str()); + // printf("addValue key=%s value=%s\n", key.c_str(), value.c_str()); // 查找0行的key是否存在 int keyColNum = -1; for (int i = 0; i <= m_maxColNum; i++) { @@ -171,7 +171,7 @@ void ZCSV::addValue(string key, string value) { lastRowNum = m_maxRowNum; } - printf("lastRowNum=%d m_maxRowNum:%d keyColNum:%d\n", lastRowNum, m_maxRowNum, keyColNum); + // printf("lastRowNum=%d m_maxRowNum:%d keyColNum:%d\n", lastRowNum, m_maxRowNum, keyColNum); setdata(keyColNum, lastRowNum + 1, value); } diff --git a/appsrc/service/disinfection_logs_service.cpp b/appsrc/service/disinfection_logs_service.cpp index f23b689..7492829 100644 --- a/appsrc/service/disinfection_logs_service.cpp +++ b/appsrc/service/disinfection_logs_service.cpp @@ -53,13 +53,7 @@ static string formattimeS(int sec) { return fmt::format("N/A"); } } -static string formatSensorVal(int val) { - if (val < 0) { - return fmt::format("N/A"); - } else { - return fmt::format("{}", val); - } -} +static string formatSensorVal(int val) { return fmt::format("{}", val); } static string format_zsystem_tp(zsystem_tp tp) { time_t time = system_clock().to_time_t(tp); struct tm tm = {0}; @@ -246,6 +240,8 @@ void DisinfectionLogsService::onAppDisinfectionStartEvent(string sessionId) { / } void DisinfectionLogsService::onAppDisinfectionSnapshotEvent(shared_ptr snapshot) { m_snapshots.push_back(snapshot); } void DisinfectionLogsService::onAppDisinfectionFinishedEvent(shared_ptr statistics) { + clearRecordFiles(); + m_statistics = statistics; dumpDisinfectionRecord(m_sessionId, m_snapshots); dumpDisinfectionToPrinterLog(m_sessionId, m_snapshots); @@ -255,7 +251,6 @@ void DisinfectionLogsService::onAppDisinfectionFinishedEvent(shared_ptrtime; csv.addValue(m_csvHeaderDict.getChName(CSVHeader::time), /******************/ tu_sys::fmt(s->time, "%Y-%m-%d %H:%M:%S")); - csv.addValue(m_csvHeaderDict.getChName(CSVHeader::state), /*****************/ m_dmStateDict.getChName(s->state)); + if (s->state == DisinfectionState::error) { + csv.addValue(m_csvHeaderDict.getChName(CSVHeader::state), /*****************/ m_dmStateDict.getChName(s->state) + ":" + ecode2str(s->ecode)); + } else { + csv.addValue(m_csvHeaderDict.getChName(CSVHeader::state), /*****************/ m_dmStateDict.getChName(s->state)); + } auto h2o2data = s->h2o2Snapshot; for (int i = 0; i < h2o2data->getSensorDataNum(); i++) { @@ -346,14 +345,14 @@ void DisinfectionLogsService::dumpDisinfectionToPrinterLog(string sessionId, lis content += fmt::format("= = = = = = = = = = = = = = = \n"); content += fmt::format(" 全思美特 \n"); - content += fmt::format("操作人 {}\n", GET_SERVICE(DeviceStateService)->getLoginUid()); - content += fmt::format("开始时间 {}\n", format_zsystem_tp(m_statistics->start_tp)); - content += fmt::format("结束时间 {}\n", format_zsystem_tp(m_statistics->complete_tp)); - content += fmt::format("总耗时(hh:mm::ss) {}:{}:{}\n", total_hours, total_mins, total_secs); - content += fmt::format("消毒液使用 {}g\n", m_statistics->disinfectantVolume_g); - content += fmt::format("目标LOG {}\n", m_statistics->tLog); - content += fmt::format("实际LOG {}\n", m_statistics->finalLog); - content += fmt::format("日志打印间隔: {}min\n", logdtime / 60); + content += fmt::format("操作人员: {}\n", GET_SERVICE(DeviceStateService)->getLoginUid()); + content += fmt::format("开始时间: {}\n", format_zsystem_tp(m_statistics->start_tp)); + content += fmt::format("结束时间: {}\n", format_zsystem_tp(m_statistics->complete_tp)); + content += fmt::format("总耗时: {}:{}:{}\n", total_hours, total_mins, total_secs); + content += fmt::format("目标LOG: {}\n", m_statistics->tLog); + content += fmt::format("实际LOG: {}\n", m_statistics->finalLog); + content += fmt::format("消毒液使用: {}g\n", m_statistics->disinfectantVolume_g); + content += fmt::format("日志打印间隔: {}min\n", logdtime / 60); content += fmt::format("= = = = = = = = = = = = = = = \n"); DisinfectionState state = DisinfectionState::idle; @@ -373,36 +372,44 @@ void DisinfectionLogsService::dumpDisinfectionToPrinterLog(string sessionId, lis if (tu_sys().dToS(snapshot->time - lastlogtime) > logdtime) logthis = true; if (!logthis) continue; - state = snapshot->state; - content += fmt::format("{}\n", format_zsystem_tp(snapshot->time)); - if (stateChanged) { - content += fmt::format("{}\n", m_dmStateDict.getChName(snapshot->state)); - } + state = snapshot->state; + first = false; + lastlogtime = snapshot->time; if (state == DisinfectionState::disinfection) { + content += fmt::format("{}\n", format_zsystem_tp(snapshot->time)); + if (stateChanged) content += fmt::format("{}\n", m_dmStateDict.getChName(snapshot->state)); content += fmt::format("LOG: {}\n", snapshot->nlog); - } - if (state == DisinfectionState::disinfection) { auto h2o2data = snapshot->h2o2Snapshot; for (int i = 0; i < h2o2data->getSensorDataNum(); i++) { if (i == 0) { - content += fmt::format(" [S0] {}ppm {}%RS {}%RH \n", h2o2data->isExpired[0] ? "N/A" : formatSensorVal(h2o2data->h2o2[0]), // - h2o2data->isExpired[0] ? "N/A" : formatSensorVal(h2o2data->saturation[0]), // + content += fmt::format(" [S0] {}(ppm) {}(%RS) {}(%RH) \n", h2o2data->isExpired[0] ? "N/A" : formatSensorVal(h2o2data->h2o2[0]), // + h2o2data->isExpired[0] ? "N/A" : formatSensorVal(h2o2data->saturation[0]), // h2o2data->isExpired[0] ? "N/A" : formatSensorVal(h2o2data->humid[0])); } if (i == 1 && !h2o2data->isExpired[1]) { - content += fmt::format(" [S1] {}ppm {}%RS {}%RH \n", formatSensorVal(h2o2data->h2o2[1]), // - formatSensorVal(h2o2data->saturation[1]), // + content += fmt::format(" [S1] {}(ppm) {}(%RS) {}(%RH) \n", formatSensorVal(h2o2data->h2o2[1]), // + formatSensorVal(h2o2data->saturation[1]), // formatSensorVal(h2o2data->humid[1])); } if (i == 2 && !h2o2data->isExpired[2]) { - content += fmt::format(" [S2] {}ppm {}%RS {}%RH \n", formatSensorVal(h2o2data->h2o2[2]), // - formatSensorVal(h2o2data->saturation[2]), // + content += fmt::format(" [S2] {}(ppm) {}(%RS) {}(%RH) \n", formatSensorVal(h2o2data->h2o2[2]), // + formatSensorVal(h2o2data->saturation[2]), // formatSensorVal(h2o2data->humid[2])); } } + } else if (state == DisinfectionState::error) { + content += fmt::format("{}\n", format_zsystem_tp(snapshot->time)); + content += fmt::format("{}\n", m_dmStateDict.getChName(snapshot->state)); + content += fmt::format(" 错误:{}\n", ecode2str(snapshot->ecode)); + + } else { + if (stateChanged) { + content += fmt::format("{}\n", format_zsystem_tp(snapshot->time)); + content += fmt::format("{}\n", m_dmStateDict.getChName(snapshot->state)); + } } } content += ("\n"); @@ -562,54 +569,81 @@ void DisinfectionLogsService::fn_printRecord(shared_ptr cxt) // } +chrono::system_clock::time_point time_point_increment(chrono::system_clock::time_point& tp, int seconds) { + tp += chrono::seconds(seconds); + return tp; +} + void DisinfectionLogsService::fn_selftest(shared_ptr cxt) { string ssid = createDisinfectionID(); onAppDisinfectionStartEvent(ssid); + auto now = zsystem_clock().now(); + auto start_tp = now; + int sensorNum = 3; + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(now, DisinfectionState::idle, sensorNum, true)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::idle, sensorNum, false)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::init, sensorNum, false)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::preheat, sensorNum, false)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::disinfection, sensorNum, false)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::disinfection, sensorNum, true)); + for (int i = 0; i < 600; i++) { + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::disinfection, sensorNum, false)); + } + for (int i = 0; i < 600; i++) { + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::degradation, sensorNum, false)); + } + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::dehumidificationBeforeDisinfection, sensorNum, false)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::dehumidificationAfterDisinfection, sensorNum, false)); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::emptyLiquidFromTheLine, sensorNum, false)); + auto esnap = selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::error, sensorNum, false); + esnap->ecode = err::kappe_the_bottom_of_the_device_has_water; + onAppDisinfectionSnapshotEvent(esnap); + onAppDisinfectionSnapshotEvent(selftest_createFakeSnapshot(time_point_increment(now, 10), DisinfectionState::finished, sensorNum, false)); + + shared_ptr statistics = make_shared(); + statistics->start_tp = start_tp; + statistics->complete_tp = now; + statistics->disinfectantVolume_g = 100; + statistics->tLog = 10; + statistics->finalLog = 9; + + onAppDisinfectionFinishedEvent(statistics); +} + +shared_ptr DisinfectionLogsService::selftest_createFakeSnapshot(zsystem_tp time, DisinfectionState state, int sensorNum, bool forceLog) { shared_ptr h2o2Snapshot = make_shared(); - h2o2Snapshot->h2o2.push_back(0.1); - h2o2Snapshot->h2o2.push_back(0.2); - h2o2Snapshot->h2o2.push_back(0.3); - h2o2Snapshot->humid.push_back(0.4); - h2o2Snapshot->humid.push_back(0.5); - h2o2Snapshot->humid.push_back(0.6); - h2o2Snapshot->temp.push_back(0.7); - h2o2Snapshot->temp.push_back(0.8); - h2o2Snapshot->temp.push_back(0.9); - h2o2Snapshot->saturation.push_back(1.0); - h2o2Snapshot->saturation.push_back(1.1); - h2o2Snapshot->saturation.push_back(1.2); - h2o2Snapshot->isExpired.push_back(false); - h2o2Snapshot->isExpired.push_back(false); - h2o2Snapshot->isExpired.push_back(false); - - h2o2Snapshot->minH2O2 = 10; - h2o2Snapshot->maxH2O2 = 20; - h2o2Snapshot->maxHumid = 30; - h2o2Snapshot->maxSaturation = 40; + h2o2Snapshot->h2o2.push_back(rand() % 100 / 2.0); + h2o2Snapshot->h2o2.push_back(rand() % 100 / 2.0); + h2o2Snapshot->h2o2.push_back(rand() % 100 / 2.0); + h2o2Snapshot->humid.push_back(rand() % 100 / 2.0); + h2o2Snapshot->humid.push_back(rand() % 100 / 2.0); + h2o2Snapshot->humid.push_back(rand() % 100 / 2.0); + h2o2Snapshot->temp.push_back(rand() % 100 / 2.0); + h2o2Snapshot->temp.push_back(rand() % 100 / 2.0); + h2o2Snapshot->temp.push_back(rand() % 100 / 2.0); + h2o2Snapshot->saturation.push_back(rand() % 100 / 2.0); + h2o2Snapshot->saturation.push_back(rand() % 100 / 2.0); + h2o2Snapshot->saturation.push_back(rand() % 100 / 2.0); + h2o2Snapshot->isExpired.push_back(!(sensorNum >= 1)); + h2o2Snapshot->isExpired.push_back(!(sensorNum >= 2)); + h2o2Snapshot->isExpired.push_back(!(sensorNum >= 3)); + + h2o2Snapshot->minH2O2 = rand() % 100 / 2.0; + h2o2Snapshot->maxH2O2 = rand() % 100 / 2.0; + h2o2Snapshot->maxHumid = rand() % 100 / 2.0; + h2o2Snapshot->maxSaturation = rand() % 100 / 2.0; shared_ptr snapshot = make_shared(); - snapshot->state = DisinfectionState::disinfection; - snapshot->time = zsystem_clock().now(); + snapshot->state = state; + snapshot->time = time; snapshot->h2o2Snapshot = h2o2Snapshot; - snapshot->forcelog = false; - snapshot->dval = 0; - snapshot->nlog = 0; - snapshot->tlog = 0; - snapshot->remainDisinfectant = 0; - snapshot->remainTime = 0; + snapshot->forcelog = forceLog; + snapshot->dval = rand() % 100 / 2.0; + snapshot->nlog = rand() % 100 / 2.0; + snapshot->tlog = rand() % 100 / 2.0; + snapshot->remainDisinfectant = rand() % 100 / 2.0; + snapshot->remainTime = rand() % 100 / 2.0; snapshot->ecode = 0; - - onAppDisinfectionSnapshotEvent(snapshot); - onAppDisinfectionSnapshotEvent(snapshot); - onAppDisinfectionSnapshotEvent(snapshot); - - shared_ptr statistics = make_shared(); - statistics->start_tp = zsystem_clock().now(); - statistics->complete_tp = zsystem_clock().now(); - statistics->disinfectantVolume_g = 0; - statistics->tLog = 0; - statistics->finalLog = 0; - - onAppDisinfectionFinishedEvent(statistics); + return snapshot; } diff --git a/appsrc/service/disinfection_logs_service.hpp b/appsrc/service/disinfection_logs_service.hpp index 93da37c..8553abc 100644 --- a/appsrc/service/disinfection_logs_service.hpp +++ b/appsrc/service/disinfection_logs_service.hpp @@ -64,5 +64,7 @@ class DisinfectionLogsService : public enable_shared_from_this selftest_createFakeSnapshot(zsystem_tp time, DisinfectionState state, int sensorNum, bool forceLog); }; } // namespace iflytop