#include "short_addr_cfg.hpp" #include #include #include extern zlog_category_t *zct; extern zlog_category_t *zbt; int ShortAddrCfg::ReadCfg(std::map& 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 1; } base_relation_file.close(); if (!root.isArray()) { zlog_error(zbt, "[ShortAddrCfg] invalid format, not an array: %s", BASE_RELATION); return 2; } if (root.size() == 0) { zlog_info(zbt, "[ShortAddrCfg] no element in %s", BASE_RELATION); return 0; } 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 &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; }