fix divide 0 bugs

This commit is contained in:
zhangsheng 2024-11-18 09:49:49 +08:00
commit b0de051861
9 changed files with 40 additions and 162 deletions

View File

@ -101,7 +101,7 @@
</extensions> </extensions>
</storageModule> </storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0"> <storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" errorParsers="org.eclipse.cdt.core.GASErrorParser;org.eclipse.cdt.core.GmakeErrorParser;org.eclipse.cdt.core.GLDErrorParser;org.eclipse.cdt.core.CWDLocator;org.eclipse.cdt.core.GCCErrorParser" id="cdt.managedbuild.config.gnu.cross.exe.release.770635657" name="Release" optionalBuildProperties="org.eclipse.cdt.docker.launcher.containerbuild.property.selectedvolumes=,org.eclipse.cdt.docker.launcher.containerbuild.property.volumes=" parent="cdt.managedbuild.config.gnu.cross.exe.release" postannouncebuildStep="" postbuildStep="cp WirelessGateway Cidn-SH;arm-linux-gnueabihf-strip Cidn-SH" preannouncebuildStep="" prebuildStep=""> <configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" errorParsers="org.eclipse.cdt.core.GASErrorParser;org.eclipse.cdt.core.GmakeErrorParser;org.eclipse.cdt.core.GLDErrorParser;org.eclipse.cdt.core.CWDLocator;org.eclipse.cdt.core.GCCErrorParser" id="cdt.managedbuild.config.gnu.cross.exe.release.770635657" name="Release" optionalBuildProperties="org.eclipse.cdt.docker.launcher.containerbuild.property.selectedvolumes=,org.eclipse.cdt.docker.launcher.containerbuild.property.volumes=" parent="cdt.managedbuild.config.gnu.cross.exe.release" postannouncebuildStep="" postbuildStep="cp WLG Cidn-SH;arm-linux-gnueabihf-strip Cidn-SH" preannouncebuildStep="" prebuildStep="">
<folderInfo id="cdt.managedbuild.config.gnu.cross.exe.release.770635657." name="/" resourcePath=""> <folderInfo id="cdt.managedbuild.config.gnu.cross.exe.release.770635657." name="/" resourcePath="">
<toolChain errorParsers="" id="cdt.managedbuild.toolchain.gnu.cross.exe.release.1737804687" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.release"> <toolChain errorParsers="" id="cdt.managedbuild.toolchain.gnu.cross.exe.release.1737804687" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.release">
<option id="cdt.managedbuild.option.gnu.cross.prefix.1625525504" name="Prefix" superClass="cdt.managedbuild.option.gnu.cross.prefix" useByScannerDiscovery="false" value="arm-linux-gnueabihf-" valueType="string"/> <option id="cdt.managedbuild.option.gnu.cross.prefix.1625525504" name="Prefix" superClass="cdt.managedbuild.option.gnu.cross.prefix" useByScannerDiscovery="false" value="arm-linux-gnueabihf-" valueType="string"/>

View File

@ -1345,142 +1345,7 @@ int SqliteDB::QueryofflineData() {
return res; return res;
} }
int SqliteDB::CalculateData() {
int res = 0;
int featureInterVal = 0;
int featureInterTime = 0;
int waveInterVal = 0;
int waveInterTime = 0;
int maxSensorNum = 0;
scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum);
array_t vecRet = GetDataMultiLine(T_SENSOR_INFO(TNAME), " dataNodeNo,MeasurementID,NodeWaveSend,featureInterVal,waveInterVal,ZigbeePower ", NULL);
if (vecRet.size() > 0) {
int planCount = 0, planCountStatistic = 0, StatisticCountTotal = 0,CountWaveXTotal = 0;
int CountWaveYTotal = 0, CountWaveZTotal = 0;
for (size_t i = 0; i < vecRet.size(); i++) {
planCount = 1440 / waveInterVal;
planCountStatistic = 1440 / featureInterVal;
int rate = readIntValue("config", "waveRate", (char *)GlobalConfig::Config_G.c_str());
char tableName[50] = {0x00};
char whereCon[128] = {0x00};
sprintf(tableName, "t_dataStatic_%s", vecRet[i][0].c_str());
const char *sql = " timestamp >= strftime('%s', 'now', '-1 day', 'start of day','utc') "
"AND timestamp < strftime('%s', 'now', '-1 day','start of day','utc','+24 hours') ";
int StatisticCount = GetTableRows(tableName, sql);
zlog_info(zct, "nodeWaveSend = %s,waveInterVal = %d", vecRet[i][2].c_str(), waveInterVal);
if (vecRet[i][2] == "") continue;
std::vector<std::string> nodeWaveSend;
boost::split(nodeWaveSend, vecRet[i][2], boost::is_any_of(","), boost::token_compress_on);
int CountWaveX = 0, CountWaveY = 0, CountWaveZ = 0;
int CountWaveX2 = 0, CountWaveY2 = 0, CountWaveZ2 = 0;
int CountWaveX3 = 0, CountWaveY3 = 0, CountWaveZ3 = 0;
int rateX = 0, rateY = 0, rateZ = 0;
if (nodeWaveSend.size() < 3) {
continue;
}
sprintf(tableName, "t_data_waveSend");
const char *sql1 = "timeStamp >= strftime('%s', 'now', '-1 day', 'start of day','utc')"
" AND timeStamp < strftime('%s', 'now','-1 day', 'start of day','utc','+24 hours')";
const char *sql2 = "timeStamp >= strftime('%s', 'now', '-2 day', 'start of day','utc')"
" AND timeStamp < strftime('%s', 'now','-2 day', 'start of day','utc','+24 hours')";
const char *sql3 = "timeStamp >= strftime('%s', 'now', '-3 day', 'start of day','utc')"
" AND timeStamp < strftime('%s', 'now','-3 day', 'start of day','utc','+24 hours')";
if (nodeWaveSend[0] == "0") {
sprintf(whereCon, " and channelID = '%s-X'", vecRet[i][1].c_str());
std::string strsql = std::string(sql1) + std::string(whereCon);
zlog_info(zct, "sql = %s", strsql.c_str());
CountWaveX = GetTableRows(tableName, strsql.c_str());
sprintf(whereCon, " and channelID = '%s-X'", vecRet[i][1].c_str());
strsql = std::string(sql2) + std::string(whereCon);
CountWaveX2 = GetTableRows(tableName, strsql.c_str());
sprintf(whereCon, " and channelID = '%s-X'", vecRet[i][1].c_str());
strsql = std::string(sql3) + std::string(whereCon);
CountWaveX3 = GetTableRows(tableName, strsql.c_str());
if ((CountWaveX > 0 && (CountWaveX / planCount > (rate / 100))) && (CountWaveX2 > 0 && (CountWaveX2 / planCount > (rate / 100))) && (CountWaveX3 > 0 && (CountWaveX3 / planCount > (rate / 100)))) {
rateX = 1;
} else {
rateX = -1;
}
}
if (nodeWaveSend[1] == "0") {
sprintf(whereCon, " and channelID = '%s-Y'", vecRet[i][1].c_str());
std::string strsql = std::string(sql1) + std::string(whereCon);
CountWaveY = GetTableRows(tableName, strsql.c_str());
sprintf(whereCon, " and channelID = '%s-Y'", vecRet[i][1].c_str());
strsql = std::string(sql2) + std::string(whereCon);
CountWaveY2 = GetTableRows(tableName, strsql.c_str());
sprintf(whereCon, " and channelID = '%s-Y'", vecRet[i][1].c_str());
strsql = std::string(sql3) + std::string(whereCon);
CountWaveY3 = GetTableRows(tableName, strsql.c_str());
if ((CountWaveY > 0 && (CountWaveY / planCount > (rate / 100))) && (CountWaveY2 > 0 && (CountWaveY2 / planCount > (rate / 100))) && (CountWaveY3 > 0 && (CountWaveY3 / planCount > (rate / 100)))) {
rateY = 1;
} else {
rateY = -1;
}
}
if (nodeWaveSend[2] == "0") {
sprintf(whereCon, " and channelID = '%s-Z'", vecRet[i][1].c_str());
std::string strsql = std::string(sql1) + std::string(whereCon);
CountWaveZ = GetTableRows(tableName, strsql.c_str());
sprintf(whereCon, " and channelID = '%s-Z'", vecRet[i][1].c_str());
strsql = std::string(sql2) + std::string(whereCon);
CountWaveZ2 = GetTableRows(tableName, strsql.c_str());
sprintf(whereCon, " and channelID = '%s-Z'", vecRet[i][1].c_str());
strsql = std::string(sql3) + std::string(whereCon);
CountWaveZ3 = GetTableRows(tableName, strsql.c_str());
if ((CountWaveZ > 0 && (CountWaveZ / planCount > (rate / 100))) && (CountWaveZ2 > 0 && (CountWaveZ2 / planCount > (rate / 100))) && (CountWaveZ3 > 0 && (CountWaveZ3 / planCount > (rate / 100)))) {
rateZ = 1;
} else {
rateZ = -1;
}
}
StatisticCountTotal += StatisticCount;
CountWaveXTotal += CountWaveX;
CountWaveYTotal += CountWaveY;
CountWaveZTotal += CountWaveZ;
zlog_info(zct, "dataNodeNo = %s , Statistic Count = %d , CountWaveX = %d , CountWaveY = %d , CountWaveZ = %d , featureInterVal = %d , waveInterVal = %d , NodeWaveSend = %s", vecRet[i][0].c_str(), StatisticCount
, CountWaveX, CountWaveY, CountWaveZ, featureInterVal, waveInterVal, vecRet[i][2].c_str());
int zigbeepowerEnable = readIntValue("config", "zigbeepowerEnable", (char *)GlobalConfig::Config_G.c_str());
if (zigbeepowerEnable) {
zlog_info(zct, "rateX = %d,rateY = %d,rateZ = %d,zigbeepower = %s", rateX, rateY, rateZ, vecRet[i][5].c_str());
if (rateX != -1 && rateY != -1 && rateZ != -1 && vecRet[i][5] == "3") {
char updateSql[50] = {0x00};
sprintf(updateSql, "ZigbeePower = '2',UpdateFlag = 0 ");
memset(whereCon, 0x00, sizeof(whereCon));
sprintf(whereCon, " dataNodeNo = '%s'", vecRet[i][0].c_str());
res = UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
if(res !=0 ){
zlog_error(zct, "res = %d", res);
}
zlog_info(zct, "2 update ZigbeePower ");
}
if ((rateX == -1 || rateY == -1 || rateZ == -1) && vecRet[i][5] == "2") {
char updateSql[50] = {0x00};
sprintf(updateSql, "ZigbeePower = '3',UpdateFlag = 0 ");
memset(whereCon, 0x00, sizeof(whereCon));
sprintf(whereCon, " dataNodeNo = '%s'", vecRet[i][0].c_str());
res = UpdateTableData(T_SENSOR_INFO(TNAME), updateSql, whereCon);
if(res !=0 ){
zlog_error(zct, "res = %d", res);
}
zlog_info(zct, "3 update ZigbeePower ");
}
}
}
zlog_info(zct, "Node Count = %d , featureInterVal = %d , waveInterVal = %d", vecRet.size(), featureInterVal, waveInterVal);
zlog_info(zct, "plan Statistic Count = %d , CountWaveX = %d ", planCountStatistic * vecRet.size(), planCount * vecRet.size());
zlog_info(zct, "reality Statistic Count = %d , CountWaveX = %d ,CountWaveY = %d ,CountWaveZ = %d ", StatisticCountTotal, CountWaveXTotal, CountWaveYTotal, CountWaveZTotal);
}
return res;
}
int SqliteDB::TransBegin() { return sqlite3_exec(mDBAcess, "begin;", 0, 0, 0); } int SqliteDB::TransBegin() { return sqlite3_exec(mDBAcess, "begin;", 0, 0, 0); }

View File

@ -67,7 +67,6 @@ public:
int CalculateDip(); int CalculateDip();
int InintGateway(); int InintGateway();
int QueryofflineData(); int QueryofflineData();
int CalculateData();
int CalculateWaveRate(); int CalculateWaveRate();
std::string GetNodeConfigureInfor(const char *whereCon); std::string GetNodeConfigureInfor(const char *whereCon);
int CloseDB(); int CloseDB();

View File

@ -24,7 +24,7 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
} }
current_ts_ = GetLocalTs(); current_ts_ = GetLocalTs();
CleanIdleOccupiedSet(current_ts_); CleanIdleOccupiedSet();
nth_wave_start_slice_ = (current_ts_ - start_timestamp_) / wave_form_send_interval_; 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_; 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) { 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_; 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; 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()); 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; break;
} }
@ -641,12 +641,18 @@ void SensorScheduler::ClearScheduleCfg(int short_addr) {
} }
void SensorScheduler::CleanIdleOccupiedSet(long ts) { void SensorScheduler::CleanIdleOccupiedSet() {
if (free_slice_ocuppied_.size() > 5) { int min_num = 0;
for (auto it = free_slice_ocuppied_.begin(); it != free_slice_ocuppied_.end();) { while (free_slice_ocuppied_.size() > free_slice_ && free_slice_ > min_num) {
if ((*it) < ts) { free_slice_ocuppied_.pop_front();
it = free_slice_ocuppied_.erase(it);
} else ++it;
}
} }
} }
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 <iostream>
#include <map> #include <map>
#include <unordered_set> #include <unordered_set>
#include <list>
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>
#include <boost/container/detail/singleton.hpp> #include <boost/container/detail/singleton.hpp>
@ -101,7 +102,8 @@ public:
private: private:
void UpdateUpgradeInfo(int id); void UpdateUpgradeInfo(int id);
void CleanIdleOccupiedSet(long ts); void CleanIdleOccupiedSet();
bool IdleSliceOccupied(int slice_id); // 索引号
// user config // user config
int eigen_value_send_interval_; int eigen_value_send_interval_;
@ -125,7 +127,7 @@ private:
std::map<uint16_t, int> short_addr_map_; // base_relation.json 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 // sensor config update
std::unordered_set<int> update_; std::unordered_set<int> update_;

View File

@ -36,6 +36,10 @@ int UpdateCfg::ReadCfg(std::unordered_set<int>& update) {
} }
int UpdateCfg::WriteCfg(std::unordered_set<int> &update) { int UpdateCfg::WriteCfg(std::unordered_set<int> &update) {
if (update.size() == 0) {
ClearCfg();
return 0;
}
Json::Value root; Json::Value root;
for (auto item : update) { for (auto item : update) {
root.append(item); root.append(item);

View File

@ -13,6 +13,14 @@ int UpgradeCfg::ReadCfg(std::map<int, UpgradeInfo> &upgrade) {
return 0; 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::Reader reader;
Json::Value root; Json::Value root;
if (!reader.parse(upgrade_file, root, false)) { if (!reader.parse(upgrade_file, root, false)) {
@ -51,6 +59,10 @@ int UpgradeCfg::ReadCfg(std::map<int, UpgradeInfo> &upgrade) {
} }
int UpgradeCfg::WriteCfg(std::map<int, UpgradeInfo> &upgrade) { int UpgradeCfg::WriteCfg(std::map<int, UpgradeInfo> &upgrade) {
if (upgrade.size() == 0) {
ClearCfg();
return 0;
}
Json::Value root; Json::Value root;
for (auto item : upgrade) { for (auto item : upgrade) {
Json::Value upgrade_item; Json::Value upgrade_item;

View File

@ -100,16 +100,6 @@ void CheckThread() {
std::string data = jd.JsonCmd_07(); std::string data = jd.JsonCmd_07();
data_publish(data.c_str(), GlobalConfig::Topic_G.mPubStatus.c_str()); data_publish(data.c_str(), GlobalConfig::Topic_G.mPubStatus.c_str());
HardStatus = 0; HardStatus = 0;
int hour = 0;
struct tm *tm_info = get_current_date();
hour = tm_info->tm_hour;
int statistics = readIntValue("config", "statistics", (char *)GlobalConfig::Config_G.c_str());
if (statistics == 0 && hour > 13) {
writeIntValue("config", "statistics", 1, (char *)GlobalConfig::Config_G.c_str());
sqlite_db_ctrl::instance().CalculateData();
} else if (statistics == 1 && hour < 13) {
writeIntValue("config", "statistics", 0, (char *)GlobalConfig::Config_G.c_str());
}
} }
if (mqttresend == 7200) { if (mqttresend == 7200) {
mqttresend = 0; mqttresend = 0;

View File

@ -226,7 +226,7 @@ int Uart::ZigbeeTest() {
} }
void Uart::WriteToUart(const char *strSend, int pLen) { void Uart::WriteToUart(const char *strSend, int pLen) {
if (!bUpdate) if (bUpdate)
{ {
printf( "Write To Uart Start:\n"); printf( "Write To Uart Start:\n");
for (int i = 0; i < pLen; i++) { for (int i = 0; i < pLen; i++) {