添加文件
This commit is contained in:
parent
e21df2cd85
commit
0932084ed4
118
scheduler/status_mgr.cpp
Normal file
118
scheduler/status_mgr.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
#include "status_mgr.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <json/json.h>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <zlog.h>
|
||||
|
||||
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;
|
||||
}
|
||||
23
scheduler/status_mgr.hpp
Normal file
23
scheduler/status_mgr.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef STATUS_MGR_HPP_
|
||||
#define STATUS_MGR_HPP_
|
||||
#include <string>
|
||||
/**
|
||||
* @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
|
||||
Loading…
x
Reference in New Issue
Block a user