2026-01-27 19:35:24 +08:00
|
|
|
#include "status_mgr.hpp"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <json/json.h>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <zlog.h>
|
|
|
|
|
|
2026-01-28 19:26:45 +08:00
|
|
|
extern zlog_category_t *zbt;
|
2026-01-27 19:35:24 +08:00
|
|
|
|
|
|
|
|
ScheduleStatus get_schedule_status() {
|
2026-01-28 19:26:45 +08:00
|
|
|
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;
|
2026-01-27 19:35:24 +08:00
|
|
|
|
2026-01-28 19:26:45 +08:00
|
|
|
if (reader.parse(status_file, json_data, false)) {
|
|
|
|
|
std::string status = json_data["status"].asString();
|
2026-01-27 19:35:24 +08:00
|
|
|
|
2026-01-28 19:26:45 +08:00
|
|
|
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();
|
2026-01-27 19:35:24 +08:00
|
|
|
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;
|
2026-01-28 19:26:45 +08:00
|
|
|
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();
|
|
|
|
|
}
|
2026-01-27 19:35:24 +08:00
|
|
|
|
|
|
|
|
// 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";
|
2026-01-28 19:26:45 +08:00
|
|
|
zlog_error(zbt, "fail to get status desc:%d", status);
|
2026-01-27 19:35:24 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return status_str;
|
|
|
|
|
}
|