125 lines
3.7 KiB
C++
125 lines
3.7 KiB
C++
#include "status_mgr.hpp"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <json/json.h>
|
|
#include <json/reader.h>
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <zlog.h>
|
|
|
|
extern zlog_category_t *zct;
|
|
|
|
ScheduleStatus get_schedule_status() {
|
|
// std::ifstream status_file("/opt/configenv/status.json");
|
|
// Json::Value json_data;
|
|
// Json::CharReaderBuilder reader;
|
|
// std::string errs;
|
|
|
|
// if (status_file.is_open()) {
|
|
// if (Json::parseFromStream(reader, status_file, &json_data, &errs)) {
|
|
// 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;
|
|
// }
|
|
// } else {
|
|
// std::cerr << "Failed to parse JSON: " << errs << std::endl;
|
|
// }
|
|
// status_file.close();
|
|
// } else {
|
|
// std::cerr << "Unable to open status.json" << std::endl;
|
|
// }
|
|
|
|
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.is_open()) {
|
|
// if (Json::parseFromStream(Json::CharReaderBuilder(), history_file, &history_data, nullptr)) {
|
|
// // successfully read existing history
|
|
// }
|
|
// 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(zct, "fail to get status desc:%d", status);
|
|
break;
|
|
}
|
|
return status_str;
|
|
}
|