From 3227dab0bfb460600936610f2ae64f525aed0923 Mon Sep 17 00:00:00 2001 From: pandx Date: Tue, 5 Nov 2024 14:44:08 +0800 Subject: [PATCH] refine codes. --- scheduler/schedule.cpp | 180 ++++------------------------------- scheduler/schedule.hpp | 21 +--- scheduler/short_addr_cfg.cpp | 55 +++++++++++ scheduler/short_addr_cfg.hpp | 14 +++ scheduler/update_cfg.cpp | 47 +++++++++ scheduler/update_cfg.hpp | 14 +++ scheduler/upgrade_cfg.cpp | 75 +++++++++++++++ scheduler/upgrade_cfg.hpp | 25 +++++ 8 files changed, 252 insertions(+), 179 deletions(-) create mode 100644 scheduler/short_addr_cfg.cpp create mode 100644 scheduler/short_addr_cfg.hpp create mode 100644 scheduler/update_cfg.cpp create mode 100644 scheduler/update_cfg.hpp create mode 100644 scheduler/upgrade_cfg.cpp create mode 100644 scheduler/upgrade_cfg.hpp diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index e7fa0df..fe7c113 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -8,6 +8,8 @@ #include #include #include +#include "short_addr_cfg.hpp" +#include "update_cfg.hpp" extern zlog_category_t *zct; extern zlog_category_t *zbt; @@ -27,13 +29,6 @@ int SensorScheduler::WriteScheduleCfg(long &ts, std::string &world_time) { root["available_slice"] = available_slice_; root["free_slice"] = free_slice_; root["support_modification"] = true; -// int total_eigen_value_send_duration = eigen_value_send_duration_ * max_sensor_num_; -// int rest_duration = eigen_value_send_interval_ - total_eigen_value_send_duration; -// int slice_per_eigen_value_interval = rest_duration / wave_form_send_duration_; -// int seconds_per_slice = rest_duration / slice_per_eigen_value_interval; - -// int num = wave_form_send_interval_ / eigen_value_send_interval_; -// long current_period_start = ts; int *slice_allocation = new int[available_slice_]; int slice_index = 0; // 有时间片的索引 @@ -94,9 +89,8 @@ SensorScheduler::SensorScheduler() { max_sensor_num_ = root["max_sensor_num"].asInt(); available_slice_ = root["available_slice"].asInt(); free_slice_ = root["free_slice"].asInt(); - support_modification_ = root["support_modification"].asBool(); + support_modification_ = root["support_modification"].asBool(); -// size_t id_num = root["id"].size(); const Json::Value ids = root["id"]; int sensor_id = 0; int i = 0; @@ -128,101 +122,13 @@ SensorScheduler::SensorScheduler() { } short_addr_map_.clear(); - std::ifstream base_relation_file(BASE_RELATION); - if (base_relation_file.good()) { - Json::Reader reader; - Json::Value root; - if (!reader.parse(base_relation_file, root, false)) { - zlog_error(zbt, "invalid format, fail to parse %s", BASE_RELATION); - base_relation_file.close(); - return; - } - base_relation_file.close(); - if (!root.isArray()) { - zlog_error(zbt, "invalid format, not an array: %s", BASE_RELATION); - return; - } - if (root.size() == 0) { - zlog_info(zbt, "no element in %s", BASE_RELATION); - return; - } - -// size_t sensor_num = root.size(); - 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, "index:%d, short addr:%d", index, short_addr); - short_addr_map_[short_addr] = index; - } - } + ShortAddrCfg::ReadCfg(short_addr_map_); // read upgrade config file: UPGRADE_CONFIG - std::ifstream upgrade_file(UPGRADE_CONFIG); - if (upgrade_file.good()) { - Json::Reader reader; - Json::Value root; - if (!reader.parse(upgrade_file, root, false)) { - zlog_error(zbt, "invalid format, fail to parse %s", UPGRADE_CONFIG); - upgrade_file.close(); - return; - } - upgrade_file.close(); - if (!root.isArray()) { - zlog_error(zbt, "invalid format, not an array: %s", UPGRADE_CONFIG); - return; - } - if (root.size() > 0) { - zlog_info(zbt, "element in %s", UPGRADE_CONFIG); - UpgradeInfo info; - for (const auto &item : root) { - info.try_times = item["try_times"].asInt(); - info.sensor_type = item["type"].asString(); - info.hw_version = item["hw_version"].asInt(); - info.current_sw_version = item["current_sw_version"].asString(); - info.upgrade_sw_version = item["upgrade_sw_version"].asString(); - info.submit_time = item["submit_time"].asString(); - for (const auto &time_item : item["try_world_time"]) { - info.try_world_time1.push_back(time_item.asString()); - } - upgrade_[item["id"].asInt()] = info; - zlog_info(zbt, "id:%d need to upgrade from:%s to %s", item["id"].asInt(), - info.current_sw_version.c_str(), info.upgrade_sw_version.c_str()); - } - } - } + UpgradeCfg::ReadCfg(upgrade_); // read config update file: CONFIG_UPDATE - std::ifstream config_update_file(CONFIG_UPDATE); - if (config_update_file.good()) { - Json::Reader reader; - Json::Value root; - if (!reader.parse(config_update_file, root, false)) { - zlog_error(zbt, "invalid format, fail to parse %s", CONFIG_UPDATE); - config_update_file.close(); - return; - } - config_update_file.close(); - if (!root.isArray()) { - zlog_error(zbt, "invalid format, not an array: %s", CONFIG_UPDATE); - return; - } - if (root.size() > 0) { - zlog_info(zbt, "element in %s", CONFIG_UPDATE); - for (const auto &item : root) { - update_.insert(item.asInt()); - zlog_info(zbt, "sensor id:%d need to update", item.asInt()); - } - } - } -} - -int SensorScheduler::Init() -{ - // 读入schedule.json文件 - return 0; + UpdateCfg::ReadCfg(update_); } int SensorScheduler::GetNextDuration(int short_addr) { @@ -326,24 +232,9 @@ int SensorScheduler::GetAvailableId(int short_addr) { break; } } - zlog_warn(zct, "[GetAvailableId][%d] pan id : %d", available_id, short_addr); - Json::Value root; - Json::Value item; - item["id"] = available_id; - item["short_addr"] = short_addr; - root.append(item); - - 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); - out_file.close(); + zlog_warn(zct, "[GetAvailableId][%d] short addr : %d", available_id, short_addr); short_addr_map_[short_addr] = available_id; + ShortAddrCfg::WriteCfg(short_addr_map_); return available_id; } @@ -562,8 +453,8 @@ int SensorScheduler::UpdateSensorConfig(int short_addr) { if (update_.count(id) > 0) { return 0; } - update_.insert(id); - UpdateConfigFile(); + update_.insert(id); + UpdateCfg::WriteCfg(update_); return 0; } @@ -579,9 +470,9 @@ int SensorScheduler::UpdateConfigResult(int short_addr, int result) { if (result != 0) { return 0; } - + zlog_info(zbt, "[%d] short addr:%d update successfully", id, short_addr); update_.erase(id); - UpdateConfigFile(); + UpdateCfg::WriteCfg(update_); return 0; } @@ -604,7 +495,8 @@ int SensorScheduler::UpgradeSensor(int short_addr, std::string sensor_type, int long ts = GetLocalTs(); info.submit_time = GetUTCTime(ts); upgrade_[id] = info; - UpdateUpgradeFile(); + zlog_info(zbt, "[%d] short addr:%d add upgrade info", id, short_addr); + UpgradeCfg::WriteCfg(upgrade_); return 0; } @@ -623,13 +515,14 @@ int SensorScheduler::UpgradeResult(int short_addr, int result) { result == kZigbeeHWMismatch || result == kUpgradeDoneBefore) { upgrade_.erase(id); - UpdateUpgradeFile(); + zlog_info(zbt, "[%d] short addr:%d upgrade successfully", id, short_addr); + UpgradeCfg::WriteCfg(upgrade_); } else { auto upgrade_iter = upgrade_.find(id); if (upgrade_iter->second.try_times >= 10) { zlog_error(zct, "[%d] short addr:%d upgrade 10 time failure", id, short_addr); upgrade_.erase(id); - UpdateUpgradeFile(); + UpgradeCfg::WriteCfg(upgrade_); // TODO: call interface to write into database } } @@ -667,45 +560,10 @@ std::string SensorScheduler::GetUTCTime(long ts) { return world_time; } -void SensorScheduler::UpdateConfigFile() { - 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(); -} - void SensorScheduler::UpdateUpgradeInfo(int id) { auto upgrade_iter = upgrade_.find(id); upgrade_iter->second.try_times++; long ts = GetLocalTs(); upgrade_iter->second.try_world_time1.push_back(GetUTCTime(ts)); - UpdateUpgradeFile(); -} - -void SensorScheduler::UpdateUpgradeFile() { - Json::Value root; - for (auto item : upgrade_) { - Json::Value upgrade_item; - upgrade_item["id"] = item.first; - upgrade_item["try_times"] = item.second.try_times; - upgrade_item["type"] = item.second.sensor_type; - upgrade_item["hw_version"] = item.second.hw_version; - upgrade_item["current_sw_version"] = item.second.current_sw_version; - upgrade_item["upgrade_sw_version"] = item.second.upgrade_sw_version; - upgrade_item["submit_time"] = item.second.submit_time; - Json::Value try_world_time; - for (auto entry : item.second.try_world_time1) { - try_world_time.append(entry); - } - upgrade_item["try_world_time"] = try_world_time; - root.append(upgrade_item); - } - Json::StyledStreamWriter streamWriter; - std::ofstream out_file(UPGRADE_CONFIG); - streamWriter.write(out_file, root); - out_file.close(); -} + UpgradeCfg::WriteCfg(upgrade_); +} \ No newline at end of file diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index cab4f2b..e4a1db9 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -7,12 +7,9 @@ #include #include #include +#include "upgrade_cfg.hpp" -#define SCHEDULE_CONFIG "./schedule.json" -#define BASE_RELATION "./base_relation.json" -#define CONFIG_UPDATE "./config_update.json" -#define UPGRADE_CONFIG "./upgrade.json" - +#define SCHEDULE_CONFIG "/opt/configenv/schedule.json" typedef enum { kScheduleResultNone = 0, @@ -33,20 +30,9 @@ typedef enum { kUpgradeDoneBefore = 5 // 当前就是这个版本,不需要升级了 } FirmFileCheckResult; -typedef struct { - int try_times; - std::string sensor_type; - int hw_version; - std::string current_sw_version; - std::string upgrade_sw_version; - std::string submit_time; - std::vector try_world_time1; -} UpgradeInfo; - class SensorScheduler { public: - SensorScheduler(); - int Init(); + SensorScheduler(); // kScheduleConfigSensor kScheduleUpgrade 等有结果,调我接口通知结果 // kScheduleEigenValue kScheduleWaveForm @@ -110,7 +96,6 @@ public: private: void UpdateUpgradeInfo(int id); void UpdateUpgradeFile(); - void UpdateConfigFile(); // user config int eigen_value_send_interval_; diff --git a/scheduler/short_addr_cfg.cpp b/scheduler/short_addr_cfg.cpp new file mode 100644 index 0000000..2c05713 --- /dev/null +++ b/scheduler/short_addr_cfg.cpp @@ -0,0 +1,55 @@ +#include "short_addr_cfg.hpp" +#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; + } + 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 &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; +} diff --git a/scheduler/short_addr_cfg.hpp b/scheduler/short_addr_cfg.hpp new file mode 100644 index 0000000..4c18603 --- /dev/null +++ b/scheduler/short_addr_cfg.hpp @@ -0,0 +1,14 @@ +#ifndef SHORT_ADDR_CFG_HPP_ +#define SHORT_ADDR_CFG_HPP_ +#include +#include + +#define BASE_RELATION "/opt/configenv/base_relation.json" + +class ShortAddrCfg { +public: + static int ReadCfg(std::map &short_addr_map); + static int WriteCfg(std::map &short_addr_map); +}; + +#endif // SHORT_ADDR_CFG_HPP_ \ No newline at end of file diff --git a/scheduler/update_cfg.cpp b/scheduler/update_cfg.cpp new file mode 100644 index 0000000..c6a0481 --- /dev/null +++ b/scheduler/update_cfg.cpp @@ -0,0 +1,47 @@ +#include "update_cfg.hpp" +#include +#include + +extern zlog_category_t *zct; +extern zlog_category_t *zbt; + +int UpdateCfg::ReadCfg(std::unordered_set& 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 &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; +} diff --git a/scheduler/update_cfg.hpp b/scheduler/update_cfg.hpp new file mode 100644 index 0000000..8b1ef53 --- /dev/null +++ b/scheduler/update_cfg.hpp @@ -0,0 +1,14 @@ +#ifndef UPDATE_CFG_HPP_ +#define UPDATE_CFG_HPP_ +#include + +#define CONFIG_UPDATE "/opt/configenv/config_update.json" + +class UpdateCfg { +public: + static int ReadCfg(std::unordered_set &update); + static int WriteCfg(std::unordered_set &update); +}; + + +#endif // UPDATE_CFG_HPP_ \ No newline at end of file diff --git a/scheduler/upgrade_cfg.cpp b/scheduler/upgrade_cfg.cpp new file mode 100644 index 0000000..c5d913c --- /dev/null +++ b/scheduler/upgrade_cfg.cpp @@ -0,0 +1,75 @@ +#include "upgrade_cfg.hpp" +#include +#include + +extern zlog_category_t *zct; +extern zlog_category_t *zbt; + +int UpgradeCfg::ReadCfg(std::map &upgrade) { + std::ifstream upgrade_file(UPGRADE_CONFIG); + if (!upgrade_file.good()) { + zlog_info(zbt, "[UpgradeCfg] no such file"); + return 0; + } + + Json::Reader reader; + Json::Value root; + if (!reader.parse(upgrade_file, root, false)) { + zlog_error(zbt, "[UpgradeCfg] invalid format, fail to parse %s", UPGRADE_CONFIG); + upgrade_file.close(); + return 1; + } + upgrade_file.close(); + if (!root.isArray()) { + zlog_error(zbt, "[UpgradeCfg] invalid format, not an array: %s", UPGRADE_CONFIG); + return 2; + } + + if (root.size() == 0) { + zlog_info(zbt, "[UpgradeCfg] no element"); + return 0; + } + + UpgradeInfo info; + for (const auto &item : root) { + info.try_times = item["try_times"].asInt(); + info.sensor_type = item["type"].asString(); + info.hw_version = item["hw_version"].asInt(); + info.current_sw_version = item["current_sw_version"].asString(); + info.upgrade_sw_version = item["upgrade_sw_version"].asString(); + info.submit_time = item["submit_time"].asString(); + for (const auto &time_item : item["try_world_time"]) { + info.try_world_time1.push_back(time_item.asString()); + } + upgrade[item["id"].asInt()] = info; + zlog_info(zbt, "[UpgradeCfg] id:%d need to upgrade from:%s to %s", item["id"].asInt(), + info.current_sw_version.c_str(), info.upgrade_sw_version.c_str()); + } + + return 0; +} + +int UpgradeCfg::WriteCfg(std::map &upgrade) { + Json::Value root; + for (auto item : upgrade) { + Json::Value upgrade_item; + upgrade_item["id"] = item.first; + upgrade_item["try_times"] = item.second.try_times; + upgrade_item["type"] = item.second.sensor_type; + upgrade_item["hw_version"] = item.second.hw_version; + upgrade_item["current_sw_version"] = item.second.current_sw_version; + upgrade_item["upgrade_sw_version"] = item.second.upgrade_sw_version; + upgrade_item["submit_time"] = item.second.submit_time; + Json::Value try_world_time; + for (auto entry : item.second.try_world_time1) { + try_world_time.append(entry); + } + upgrade_item["try_world_time"] = try_world_time; + root.append(upgrade_item); + } + Json::StyledStreamWriter streamWriter; + std::ofstream out_file(UPGRADE_CONFIG); + streamWriter.write(out_file, root); + out_file.close(); + return 0; +} diff --git a/scheduler/upgrade_cfg.hpp b/scheduler/upgrade_cfg.hpp new file mode 100644 index 0000000..d69c285 --- /dev/null +++ b/scheduler/upgrade_cfg.hpp @@ -0,0 +1,25 @@ +#ifndef UPGRADE_CFG_HPP_ +#define UPGRADE_CFG_HPP_ +#include +#include +#include + +#define UPGRADE_CONFIG "/opt/configenv/upgrade.json" + +typedef struct { + int try_times; + std::string sensor_type; + int hw_version; + std::string current_sw_version; + std::string upgrade_sw_version; + std::string submit_time; + std::vector try_world_time1; +} UpgradeInfo; + +class UpgradeCfg { +public: + static int ReadCfg(std::map &upgrade); + static int WriteCfg(std::map &upgrade); +}; + +#endif // UPGRADE_CFG_HPP_ \ No newline at end of file