2024-11-05 14:44:08 +08:00
|
|
|
#include "update_cfg.hpp"
|
2024-11-05 14:53:47 +08:00
|
|
|
#include <fstream>
|
2024-11-05 14:44:08 +08:00
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 09:37:04 +08:00
|
|
|
int UpdateCfg::WriteCfg(std::unordered_set<int> &update) {
|
|
|
|
|
if (update.size() == 0) {
|
|
|
|
|
ClearCfg();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-11-05 14:44:08 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2024-11-08 14:23:09 +08:00
|
|
|
|
|
|
|
|
void UpdateCfg::ClearCfg() {
|
|
|
|
|
std::string clear_cmd = "rm -rf ";
|
|
|
|
|
clear_cmd.append(CONFIG_UPDATE);
|
|
|
|
|
system(clear_cmd.c_str());
|
|
|
|
|
}
|