refine codes.

This commit is contained in:
pandx 2024-11-05 14:44:08 +08:00
parent cec944a05a
commit 3227dab0bf
8 changed files with 252 additions and 179 deletions

View File

@ -8,6 +8,8 @@
#include <iomanip>
#include <json/json.h>
#include <zlog.h>
#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; // 有时间片的索引
@ -96,7 +91,6 @@ SensorScheduler::SensorScheduler() {
free_slice_ = root["free_slice"].asInt();
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;
}
@ -563,7 +454,7 @@ int SensorScheduler::UpdateSensorConfig(int short_addr) {
return 0;
}
update_.insert(id);
UpdateConfigFile();
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_);
}

View File

@ -7,12 +7,9 @@
#include <vector>
#include <stdint.h>
#include <boost/container/detail/singleton.hpp>
#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<std::string> try_world_time1;
} UpgradeInfo;
class SensorScheduler {
public:
SensorScheduler();
int Init();
// 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_;

View File

@ -0,0 +1,55 @@
#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;
}

View File

@ -0,0 +1,14 @@
#ifndef SHORT_ADDR_CFG_HPP_
#define SHORT_ADDR_CFG_HPP_
#include <stdint.h>
#include <map>
#define BASE_RELATION "/opt/configenv/base_relation.json"
class ShortAddrCfg {
public:
static int ReadCfg(std::map<uint16_t, int> &short_addr_map);
static int WriteCfg(std::map<uint16_t, int> &short_addr_map);
};
#endif // SHORT_ADDR_CFG_HPP_

47
scheduler/update_cfg.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "update_cfg.hpp"
#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;
}

14
scheduler/update_cfg.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef UPDATE_CFG_HPP_
#define UPDATE_CFG_HPP_
#include <unordered_set>
#define CONFIG_UPDATE "/opt/configenv/config_update.json"
class UpdateCfg {
public:
static int ReadCfg(std::unordered_set<int> &update);
static int WriteCfg(std::unordered_set<int> &update);
};
#endif // UPDATE_CFG_HPP_

75
scheduler/upgrade_cfg.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "upgrade_cfg.hpp"
#include <json/json.h>
#include <zlog.h>
extern zlog_category_t *zct;
extern zlog_category_t *zbt;
int UpgradeCfg::ReadCfg(std::map<int, UpgradeInfo> &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<int, UpgradeInfo> &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;
}

25
scheduler/upgrade_cfg.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef UPGRADE_CFG_HPP_
#define UPGRADE_CFG_HPP_
#include <string>
#include <vector>
#include <map>
#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<std::string> try_world_time1;
} UpgradeInfo;
class UpgradeCfg {
public:
static int ReadCfg(std::map<int, UpgradeInfo> &upgrade);
static int WriteCfg(std::map<int, UpgradeInfo> &upgrade);
};
#endif // UPGRADE_CFG_HPP_