WLG/scheduler/update_cfg.cpp

55 lines
1.5 KiB
C++

#include "update_cfg.hpp"
#include <fstream>
#include <json/json.h>
#include <zlog.h>
extern zlog_category_t *zct;
extern zlog_category_t *zbt;
int UpdateCfg::ReadCfg(std::unordered_set<int>& update) {
std::ifstream config_update_file(CONFIG_UPDATE);
if (!config_update_file.good()) {
zlog_info(zbt, "[UpdateCfg] no file");
return 0;
}
Json::Reader reader;
Json::Value root;
if (!reader.parse(config_update_file, root, false)) {
zlog_error(zbt, "[UpdateCfg] invalid format, fail to parse %s", CONFIG_UPDATE);
config_update_file.close();
return 1;
}
config_update_file.close();
if (!root.isArray()) {
zlog_error(zbt, "[UpdateCfg] invalid format, not an array: %s", CONFIG_UPDATE);
return 2;
}
if (root.size() > 0) {
zlog_info(zbt, "[UpdateCfg] element in %s", CONFIG_UPDATE);
for (const auto &item : root) {
update.insert(item.asInt());
zlog_info(zbt, "[UpdateCfg] sensor id:%d need to update", item.asInt());
}
}
return 0;
}
int UpdateCfg::WriteCfg(std::unordered_set<int> &update) {
Json::Value root;
for (auto item : update) {
root.append(item);
}
Json::StyledStreamWriter streamWriter;
std::ofstream out_file(CONFIG_UPDATE);
streamWriter.write(out_file, root);
out_file.close();
return 0;
}
void UpdateCfg::ClearCfg() {
std::string clear_cmd = "rm -rf ";
clear_cmd.append(CONFIG_UPDATE);
system(clear_cmd.c_str());
}