From 0932084ed4dda683cfea9fd0c8d173a55b3c4f9a Mon Sep 17 00:00:00 2001 From: Zhang0626 Date: Mon, 2 Mar 2026 19:02:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scheduler/status_mgr.cpp | 118 +++++++++++++++++++++++++++++++++++++++ scheduler/status_mgr.hpp | 23 ++++++++ 2 files changed, 141 insertions(+) create mode 100644 scheduler/status_mgr.cpp create mode 100644 scheduler/status_mgr.hpp diff --git a/scheduler/status_mgr.cpp b/scheduler/status_mgr.cpp new file mode 100644 index 0000000..3d897be --- /dev/null +++ b/scheduler/status_mgr.cpp @@ -0,0 +1,118 @@ +#include "status_mgr.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +extern zlog_category_t *zbt; + +ScheduleStatus get_schedule_status() { + std::ifstream status_file("/opt/configenv/status.json"); + if (!status_file.good()) { + zlog_info(zbt, "[ShortAddrCfg] no file /opt/configenv/status.json"); + return kScheduleStatusNormal; + } + Json::Value json_data; + Json::Reader reader; + + if (reader.parse(status_file, json_data, false)) { + std::string status = json_data["status"].asString(); + + if (status == "debug") { + status_file.close(); + return kScheduleStatusDebug; + } else if (status == "normal") { + status_file.close(); + return kScheduleStatusNormal; + } else if (status == "upgrade") { + status_file.close(); + return kScheduleStatusUpgrade; + } + } + status_file.close(); + return kScheduleStatusNormal; +} + +void set_schedule_status(ScheduleStatus status) { + std::string status_str; + switch (status) { + case kScheduleStatusNormal: + status_str = "normal"; + break; + case kScheduleStatusDebug: + status_str = "debug"; + break; + case kScheduleStatusUpgrade: + status_str = "upgrade"; + break; + } + + // Write to status.json + Json::Value json_data; + json_data["status"] = status_str; + + std::ofstream status_file("/opt/configenv/status.json"); + if (status_file.is_open()) { + status_file << json_data; + status_file.close(); + } else { + std::cerr << "Unable to open status.json for writing" << std::endl; + } + + // Append to status_history.json + Json::Value history_data; + std::ifstream history_file("/opt/configenv/status_history.json"); + if (history_file.good()) { + Json::Reader reader; + reader.parse(history_file, history_data, false); + history_file.close(); + } + + // Get the current time + auto now = std::chrono::system_clock::now(); + auto now_c = std::chrono::system_clock::to_time_t(now); + std::tm* now_tm = std::localtime(&now_c); + + // Format time as "YYYY-MM-DD HH:MM:SS" + char time_buffer[20]; + std::strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", now_tm); + + // Add the new status and time to history + Json::Value new_entry; + new_entry["status"] = status_str; + new_entry["time"] = time_buffer; + history_data.append(new_entry); + + // Write back to status_history.json + std::ofstream history_file_out("/opt/configenv/status_history.json"); + if (history_file_out.is_open()) { + history_file_out << history_data; + history_file_out.close(); + } else { + std::cerr << "Unable to open status_history.json for writing" << std::endl; + } +} + +std::string get_status_desc(ScheduleStatus status) { + std::string status_str; + switch (status) { + case kScheduleStatusNormal: + status_str = "normal"; + break; + case kScheduleStatusDebug: + status_str = "debug"; + break; + case kScheduleStatusUpgrade: + status_str = "upgrade"; + break; + default: + status_str = "normal"; + zlog_error(zbt, "fail to get status desc:%d", status); + break; + } + return status_str; +} diff --git a/scheduler/status_mgr.hpp b/scheduler/status_mgr.hpp new file mode 100644 index 0000000..0fe99c3 --- /dev/null +++ b/scheduler/status_mgr.hpp @@ -0,0 +1,23 @@ +#ifndef STATUS_MGR_HPP_ +#define STATUS_MGR_HPP_ +#include +/** + * @brief 对调度状态切换进行管理,并对调度切换时历史进行记录 + * + * @version 0.1 + * @author pandx (dxpan2002@163.com) + * @date 2026-01-21 + * @copyright Copyright (c) 2026 + */ + +typedef enum { + kScheduleStatusNormal = 1, + kScheduleStatusDebug = 2, + kScheduleStatusUpgrade = 3 +} ScheduleStatus; + +ScheduleStatus get_schedule_status(); +void set_schedule_status(ScheduleStatus status); +std::string get_status_desc(ScheduleStatus status); + +#endif \ No newline at end of file