add protection for upgrade file.

This commit is contained in:
pandx 2024-11-17 16:32:53 +08:00
parent b406513804
commit 95e00f919b
4 changed files with 36 additions and 16 deletions

View File

@ -1351,6 +1351,12 @@ int SqliteDB::CalculateData() {
int planCount = 0, planCountStatistic = 0, StatisticCountTotal = 0, SixCountTotal = 0, TimingCountTotal = 0, CountWaveXTotal = 0, waveInterVal = 0, featureInterVal = 0;
int CountWaveYTotal = 0, CountWaveZTotal = 0;
for (size_t i = 0; i < vecRet.size(); i++) {
featureInterVal = atoi(vecRet[i][3].c_str());
waveInterVal = atoi(vecRet[i][4].c_str());
if (featureInterVal == 0 || waveInterVal == 0) {
zlog_error(zct, "featureInterVal:%d or waveInterVal:%d is 0", featureInterVal, waveInterVal);
continue;
}
planCount = 1440 / atoi(vecRet[i][4].c_str());
planCountStatistic = 1440 / atoi(vecRet[i][3].c_str());
int rate = readIntValue("config", "waveRate", (char *)GlobalConfig::Config_G.c_str());
@ -1472,9 +1478,7 @@ int SqliteDB::CalculateData() {
}
zlog_info(zct, "3 update ZigbeePower ");
}
}
featureInterVal = atoi(vecRet[i][3].c_str());
waveInterVal = atoi(vecRet[i][4].c_str());
}
}
zlog_info(zct, "Node Count = %d , featureInterVal = %d , waveInterVal = %d", vecRet.size(), featureInterVal, waveInterVal);
zlog_info(zct, "plan Statistic Count = %d , 06 Count = %d , Timing Count = %d , CountWaveX = %d ", planCountStatistic * vecRet.size(), planCount * vecRet.size(), planCount * vecRet.size(), planCount * vecRet.size());

View File

@ -24,7 +24,7 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
}
current_ts_ = GetLocalTs();
CleanIdleOccupiedSet(current_ts_);
CleanIdleOccupiedSet();
nth_wave_start_slice_ = (current_ts_ - start_timestamp_) / wave_form_send_interval_;
current_wave_start_ts_ = nth_wave_start_slice_ * wave_form_send_interval_ + start_timestamp_;
@ -167,9 +167,9 @@ long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) {
if (slice_sensor_id_[i+forward_wave_slice_num] == 0) {
// 判断此空闲位置是否被占用
long current_wave_slice_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + i * seconds_per_wave_slice_;
if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) {
if (!IdleSliceOccupied(i+forward_wave_slice_num)) {
available_ts = current_wave_slice_ts;
free_slice_ocuppied_.insert(available_ts);
free_slice_ocuppied_.push_back(i+forward_wave_slice_num);
zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to upgrade, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str());
break;
}
@ -641,12 +641,18 @@ void SensorScheduler::ClearScheduleCfg(int short_addr) {
}
void SensorScheduler::CleanIdleOccupiedSet(long ts) {
if (free_slice_ocuppied_.size() > 5) {
for (auto it = free_slice_ocuppied_.begin(); it != free_slice_ocuppied_.end();) {
if ((*it) < ts) {
it = free_slice_ocuppied_.erase(it);
} else ++it;
void SensorScheduler::CleanIdleOccupiedSet() {
int min_num = 0;
while (free_slice_ocuppied_.size() > free_slice_ && free_slice_ > min_num) {
free_slice_ocuppied_.pop_front();
}
}
bool SensorScheduler::IdleSliceOccupied(int slice_id) {
for (auto item : free_slice_ocuppied_) {
if (slice_id == item) {
return true;
}
}
}
return false;
}

View File

@ -4,6 +4,7 @@
#include <iostream>
#include <map>
#include <unordered_set>
#include <list>
#include <vector>
#include <stdint.h>
#include <boost/container/detail/singleton.hpp>
@ -101,7 +102,8 @@ public:
private:
void UpdateUpgradeInfo(int id);
void CleanIdleOccupiedSet(long ts);
void CleanIdleOccupiedSet();
bool IdleSliceOccupied(int slice_id); // 索引号
// user config
int eigen_value_send_interval_;
@ -125,7 +127,7 @@ private:
std::map<uint16_t, int> short_addr_map_; // base_relation.json
// 空闲时间戳被占用
std::unordered_set<long> free_slice_ocuppied_;
std::list<int> free_slice_ocuppied_;
// sensor config update
std::unordered_set<int> update_;

View File

@ -7,12 +7,20 @@ extern zlog_category_t *zct;
extern zlog_category_t *zbt;
int UpgradeCfg::ReadCfg(std::map<int, UpgradeInfo> &upgrade) {
std::ifstream upgrade_file(UPGRADE_CONFIG);
std::ifstream upgrade_file(UPGRADE_CONFIG);
if (!upgrade_file.good()) {
zlog_info(zbt, "[UpgradeCfg] no such file");
return 0;
}
upgrade_file.seekg(0, std::ios::end);
std::streampos file_size = upgrade_file.tellg();
if (file_size > 256000) {
zlog_error(zct, "upgrade file exception, will remove it");
ClearCfg();
return 1;
}
upgrade_file.seekg(0, std::ios::beg);
Json::Reader reader;
Json::Value root;
if (!reader.parse(upgrade_file, root, false)) {