From 7190da4c9baa80a09f0fd013f3e8a08d97f50c1b Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 12 Oct 2023 19:07:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main_control_service.cpp | 10 +++++ src/service/disinfection_logs_manager.cpp | 62 +++++++++++++++++++++++++++++++ src/service/disinfection_logs_manager.hpp | 7 ++++ 3 files changed, 79 insertions(+) diff --git a/src/main_control_service.cpp b/src/main_control_service.cpp index a23bf30..829e5a2 100644 --- a/src/main_control_service.cpp +++ b/src/main_control_service.cpp @@ -496,6 +496,16 @@ void MainControlService::processFrontEndMessage_test(weak_ptr webSock } return; } + + if (cmdstr == "disinfectionLogsGetList") { + receipt["disinfectionLogList"] = m_disinfectionLogsManager->getLoggerList(); + return; + } + if (cmdstr == "disinfectionLogsGetRecord") { + string loggerName = cmd["disinfectionLogName"]; + receipt["record"] = m_disinfectionLogsManager->getlogger(loggerName); + return; + } } void MainControlService::processFrontEndMessage_exportData(weak_ptr webSocket, json& cmd, json& receipt) { diff --git a/src/service/disinfection_logs_manager.cpp b/src/service/disinfection_logs_manager.cpp index 2b85ea4..8738d4a 100644 --- a/src/service/disinfection_logs_manager.cpp +++ b/src/service/disinfection_logs_manager.cpp @@ -1,5 +1,11 @@ #include "disinfection_logs_manager.hpp" + +#include +#include + +#include "iflytop/core/components/fileutils.hpp" using namespace iflytop; +using namespace core; using namespace std; #define LOG_STORGE_PATH "./disinfection_logs/" @@ -35,3 +41,59 @@ shared_ptr DisinfectionLogsManager::createNewLogger(string l logger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name)); return logger; } + +static void split(const string& s, vector& sv, const char delim = ' ') { + sv.clear(); + istringstream iss(s); + string temp; + + while (getline(iss, temp, delim)) { + sv.push_back(temp); + } + return; +} +void DisinfectionLogsManager::list_dir_csvfile(string path, vector& sv) { + sv.clear(); + DIR* dir; + struct dirent* ptr; + if ((dir = opendir(path.c_str())) == NULL) { + logger->error("Open dir {} error...", path); + return; + } + while ((ptr = readdir(dir)) != NULL) { + if (ptr->d_name[0] == '.') continue; + if (ptr->d_type == 8) { + string filename = ptr->d_name; + if (filename.find(".csv") != string::npos) { + sv.push_back(filename); + } + } + } + closedir(dir); + return; +} + +nlohmann::json DisinfectionLogsManager::getlogger(string log_file_name) { + string content = FileUtils().readFileAsString(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name)); + nlohmann::json csvcontent; + vector lines; + split(content, lines, '\n'); + + for (auto& line : lines) { + csvcontent["content"].push_back(line); + } + return csvcontent; +} +nlohmann::json DisinfectionLogsManager::getLoggerList() { + // + // 1. get all files in LOG_STORGE_PATH + vector files; + list_dir_csvfile(LOG_STORGE_PATH, files); + nlohmann::json loggerlist; + for (auto& file : files) { + // 获取文件名去掉.csv + file = file.substr(0, file.find(".csv")); + loggerlist.push_back(file); + } + return loggerlist; +} \ No newline at end of file diff --git a/src/service/disinfection_logs_manager.hpp b/src/service/disinfection_logs_manager.hpp index 84b267d..fa694a3 100644 --- a/src/service/disinfection_logs_manager.hpp +++ b/src/service/disinfection_logs_manager.hpp @@ -10,6 +10,7 @@ #include #include +#include "iflytop/core/basic/nlohmann/json.hpp" #include "iflytop/core/core.hpp" namespace iflytop { using namespace std; @@ -36,6 +37,12 @@ class DisinfectionLogsManager { void initialize(){}; shared_ptr createNewLogger(string log_file_name); + + nlohmann::json getlogger(string log_file_name); + nlohmann::json getLoggerList(); + + private: + void list_dir_csvfile(string path, vector& sv); }; } // namespace iflytop \ No newline at end of file