56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
#include "short_addr_cfg.hpp"
|
|
#include <json/json.h>
|
|
#include <zlog.h>
|
|
|
|
extern zlog_category_t *zct;
|
|
extern zlog_category_t *zbt;
|
|
|
|
int ShortAddrCfg::ReadCfg(std::map<uint16_t, int>& short_addr_map) {
|
|
std::ifstream base_relation_file(BASE_RELATION);
|
|
if (!base_relation_file.good()) {
|
|
zlog_info(zbt, "[ShortAddrCfg] no file %s", BASE_RELATION);
|
|
return 0;
|
|
}
|
|
Json::Reader reader;
|
|
Json::Value root;
|
|
if (!reader.parse(base_relation_file, root, false)) {
|
|
zlog_error(zbt, "[ShortAddrCfg] invalid format, fail to parse %s", BASE_RELATION);
|
|
base_relation_file.close();
|
|
return;
|
|
}
|
|
base_relation_file.close();
|
|
if (!root.isArray()) {
|
|
zlog_error(zbt, "[ShortAddrCfg] invalid format, not an array: %s", BASE_RELATION);
|
|
return;
|
|
}
|
|
if (root.size() == 0) {
|
|
zlog_info(zbt, "[ShortAddrCfg] no element in %s", BASE_RELATION);
|
|
return;
|
|
}
|
|
|
|
uint16_t short_addr;
|
|
int index = 0;
|
|
|
|
for (const auto &item : root) {
|
|
short_addr = item["short_addr"].asInt();
|
|
index = item["id"].asInt();
|
|
zlog_info(zbt, "[ShortAddrCfg] index:%d, short addr:%d", index, short_addr);
|
|
short_addr_map[short_addr] = index;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int ShortAddrCfg::WriteCfg(std::map<uint16_t, int> &short_addr_map) {
|
|
Json::Value root;
|
|
for (auto it = short_addr_map.begin(); it != short_addr_map.end(); ++it) {
|
|
Json::Value item;
|
|
item["id"] = it->second;
|
|
item["short_addr"] = it->first;
|
|
root.append(item);
|
|
}
|
|
Json::StyledStreamWriter streamWriter;
|
|
std::ofstream out_file(BASE_RELATION);
|
|
streamWriter.write(out_file, root);
|
|
return 0;
|
|
}
|