From e1a0783aa77acdab741078e0387f08287ad98c0c Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sat, 21 Sep 2024 12:55:53 +0800 Subject: [PATCH] add storage in realtime --- .vscode/settings.json | 3 +- .../core/components/zcsv/zstream_csv_writer.cpp | 20 +++++++++++++ .../core/components/zcsv/zstream_csv_writer.hpp | 34 +++++++++++++++++++++ appsrc/service/disinfection_logs_service.cpp | 35 ++++++++++++++-------- appsrc/service/disinfection_logs_service.hpp | 14 +++++---- 5 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 appdep/iflytop/core/components/zcsv/zstream_csv_writer.cpp create mode 100644 appdep/iflytop/core/components/zcsv/zstream_csv_writer.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 4e00480..5451663 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -70,6 +70,7 @@ "any": "cpp", "unordered_set": "cpp", "csignal": "cpp", - "zh2o2_computer.h": "c" + "zh2o2_computer.h": "c", + "zstream_csvhcpp": "cpp" } } \ No newline at end of file diff --git a/appdep/iflytop/core/components/zcsv/zstream_csv_writer.cpp b/appdep/iflytop/core/components/zcsv/zstream_csv_writer.cpp new file mode 100644 index 0000000..6411e87 --- /dev/null +++ b/appdep/iflytop/core/components/zcsv/zstream_csv_writer.cpp @@ -0,0 +1,20 @@ +#include "zstream_csv_writer.hpp" + +using namespace std; +using namespace iflytop; + +void ZStreamCSVWriter::addColumn(string key) { columns.push_back(key); } +void ZStreamCSVWriter::setValue(string key, string value) { // + onelineDataCache[key] = value; +} +void ZStreamCSVWriter::addNewLine() { + outfile << endl; + for (auto& col : columns) { + if (onelineDataCache.find(col) == onelineDataCache.end()) { + outfile << ","; + } else { + outfile << onelineDataCache[col] << ","; + } + } + onelineDataCache.clear(); +} \ No newline at end of file diff --git a/appdep/iflytop/core/components/zcsv/zstream_csv_writer.hpp b/appdep/iflytop/core/components/zcsv/zstream_csv_writer.hpp new file mode 100644 index 0000000..2f5fcd6 --- /dev/null +++ b/appdep/iflytop/core/components/zcsv/zstream_csv_writer.hpp @@ -0,0 +1,34 @@ +#pragma once +#include +#include // 引入文件流库 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace iflytop { +using namespace std; + +class ZStreamCSVWriter { + private: + list columns; + map onelineDataCache; + + ofstream outfile; + + public: + ZStreamCSVWriter(string filename) : outfile(filename) {}; + + void addColumn(string key); + void storageColumns(); + + void setValue(string key, string value); + void addNewLine(); +}; + +} // namespace iflytop \ No newline at end of file diff --git a/appsrc/service/disinfection_logs_service.cpp b/appsrc/service/disinfection_logs_service.cpp index af3c178..0f91730 100644 --- a/appsrc/service/disinfection_logs_service.cpp +++ b/appsrc/service/disinfection_logs_service.cpp @@ -207,6 +207,9 @@ void DisinfectionLogsService::initialize() { // TODO: 检查指令iconv是否可用 + /** + * @brief 监听事件 + */ AppEventBus::ins()->onEvent.connect([this](shared_ptr event) { if (auto e = dynamic_pointer_cast(event); e) { // logger->info("DisinfectionLogsService: start new disinfection session"); @@ -227,8 +230,6 @@ void DisinfectionLogsService::initialize() { } }); - // m_recordFiles - /** * @brief 读取文件夹下所有文件,并按照时间排序 */ @@ -252,22 +253,23 @@ 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); - m_recordFiles.remove_if([this](const string& s) { return s == m_sessionId; }); // 删除已经存在的记录 - m_recordFiles.push_back(m_sessionId); + // clearRecordFiles(); + + // dumpDisinfectionRecord(m_sessionId, m_snapshots); + // dumpDisinfectionToPrinterLog(m_sessionId, m_snapshots); + + // m_recordFiles.remove_if([this](const string& s) { return s == m_sessionId; }); // 删除已经存在的记录 + // m_recordFiles.push_back(m_sessionId); /** * @brief * 1.创建打印文件缓存 * 2.创建PDF文件缓存 */ - if (printfLogWhenDisinfectionFinished) printRecordFile(m_sessionId); + // if (printfLogWhenDisinfectionFinished) printRecordFile(m_sessionId); } - +#if 0 /** * @brief 保存消毒记录到csv文件 * @@ -470,6 +472,9 @@ void DisinfectionLogsService::dumpDisinfectionToPrinterLog(string sessionId, lis pdfwriter.dump(); } void DisinfectionLogsService::dumpDisinfectionToPrinterPdf(string sessionId, list> snapshots) {} +#endif + +#if 0 /** * @brief 删除记录,多余记录文件 */ @@ -505,6 +510,7 @@ void DisinfectionLogsService::clearRecordFiles() { m_recordFiles.clear(); } } +#endif void DisinfectionLogsService::deleteRecordFile(string file) { string filepath = fmt::format("{}{}.*", LOG_STORGE_PATH, file); dosystem(fmt::format("rm -rf {}", filepath)); @@ -564,7 +570,8 @@ int32_t DisinfectionLogsService::exportDisinfectionData(vector files) { /**********************************拷贝PDF**********************************/ // 拷贝pdf文件 - dosystem(fmt::format("cp -rf /app/disinfection_logs/{}.pdf {}", file, EXPORT_PATH)); + // TODO: 生成pdf,并导出pdf + // dosystem(fmt::format("cp -rf /app/disinfection_logs/{}.pdf {}", file, EXPORT_PATH)); } // 卸载目录 @@ -639,7 +646,11 @@ void DisinfectionLogsService::printRecord(shared_ptr cxt, str printRecordFile(logName); } -chrono::system_clock::time_point time_point_increment(chrono::system_clock::time_point& tp, int seconds) { +/******************************************************************************* + * 测试使用 * + *******************************************************************************/ + +static chrono::system_clock::time_point time_point_increment(chrono::system_clock::time_point& tp, int seconds) { tp += chrono::seconds(seconds); return tp; } diff --git a/appsrc/service/disinfection_logs_service.hpp b/appsrc/service/disinfection_logs_service.hpp index 61ea6b0..91439d4 100644 --- a/appsrc/service/disinfection_logs_service.hpp +++ b/appsrc/service/disinfection_logs_service.hpp @@ -35,6 +35,7 @@ class DisinfectionLogsService : public enable_shared_from_this statistics); private: - void dumpDisinfectionRecord(string sessionId, list> snapshots); - void dumpDisinfectionToPrinterLog(string sessionId, list> snapshots); - void dumpDisinfectionToPrinterPdf(string sessionId, list> snapshots); + + // void dumpDisinfectionRecord(string sessionId, list> snapshots); + // void dumpDisinfectionToPrinterLog(string sessionId, list> snapshots); + // void dumpDisinfectionToPrinterPdf(string sessionId, list> snapshots); private: /******************************************************************************* * 消毒日志相关接口 * @@ -66,9 +68,9 @@ class DisinfectionLogsService : public enable_shared_from_this files); - void clearRecordFiles(); - void deleteRecordFile(string file); - void printRecordFile(string file); + // void clearRecordFiles(); + void deleteRecordFile(string file); + void printRecordFile(string file); bool dosystem(string cmd);