diff --git a/dbaccess/sql_db.cpp b/dbaccess/sql_db.cpp index 3672c62..54368da 100644 --- a/dbaccess/sql_db.cpp +++ b/dbaccess/sql_db.cpp @@ -846,7 +846,7 @@ int SqliteDB::CalculateBattery() { memset(whereCon, 0x00, sizeof(whereCon)); sprintf(whereCon, " dataNodeNo = '%s' order by timeStamp asc limit 0,1 ", vecRes[i][7].c_str()); vecResSig = sqlite_db_ctrl::instance().GetDataSingleLine(T_BATTERY_INFO(TNAME), " * ", whereCon); - if (vecResSig.size() <= 0) { //一条数据都没有 + if (vecResSig.size() <= 0) { // 一条数据都没有 continue; } } else { @@ -877,15 +877,15 @@ int SqliteDB::CalculateBattery() { for (size_t j = 0; j < vecResbattery.size(); j++) { float b = 2 - 25 * k; - float dpm = k * atoi(vecResbattery[j][2].c_str()) + b; //温度 - float cost_e = dpm * atoi(vecRes[i][3].c_str()); //特征值时间间隔 + float dpm = k * atoi(vecResbattery[j][2].c_str()) + b; // 温度 + float cost_e = dpm * atoi(vecRes[i][3].c_str()); // 特征值时间间隔 float cost_h = cost_e / 60; vecworkTime.push_back(atol(vecResbattery[j][3].c_str())); vecsendTime.push_back(atol(vecResbattery[j][4].c_str())); to_math += cost_h; } - zlog_info(zct, "自放电 = %f", to_math); + zlog_info(zct, "self discharge = %f", to_math); long sumworkTime = 0.0, sumsendTime = 0.0; for (size_t j = 0; j < vecworkTime.size(); j++) { sumworkTime += vecworkTime[j]; @@ -901,7 +901,7 @@ int SqliteDB::CalculateBattery() { if (to_math < 0) to_math = 0; float usageBattery = usageworkTime + usagesendTime + to_math; - zlog_info(zct, "已经使用 = %f", atof(vecResSig[6].c_str())); + zlog_info(zct, "have used = %f", atof(vecResSig[6].c_str())); float remainBattery = capacity - usageBattery * 0.2; if (remainBattery < 10) { @@ -990,9 +990,9 @@ int SqliteDB::CalculateDip() { memset(whereCon, 0x00, sizeof(whereCon)); if (vParam[1] == "2") { //人工干预 sprintf(whereCon, " timeStamp > '%ld' ", atol(vParam[2].c_str())); - } else if (vParam[1] == "0") { //正常状态 - sprintf(whereCon, " timeStamp > '%ld' ", atol(localtimestamp) - 86400); //一天数据 - } else if (vParam[1] == "1") { //松动状态 + } else if (vParam[1] == "0") { // 正常状态 + sprintf(whereCon, " timeStamp > '%ld' ", atol(localtimestamp) - 86400); // 一天数据 + } else if (vParam[1] == "1") { // 松动状态 continue; } zlog_info(zct, "vParam[0]=%s,vParam[1]=%s", vParam[0].c_str(), vParam[1].c_str()); @@ -1038,7 +1038,7 @@ int SqliteDB::CalculateDip() { } int SqliteDB::InintGateway() { - //更新网关配置表 + // 更新网关配置表 std::string strIP = GetGwIp_("eth0"); std::string strServerIP = ReadStrByOpt(SERVERCONFIG, "Server", "localServerIpAddress"); std::string strServerPort = ReadStrByOpt(SERVERCONFIG, "Server", "localServerPort"); @@ -1074,7 +1074,7 @@ int SqliteDB::InintGateway() { } } - //上传网关配置到MQTT + // 上传网关配置到MQTT Json::Value jsSystemSetting; Json::Value jsBody; Json::FastWriter showValue; @@ -1180,7 +1180,7 @@ std::string SqliteDB::GetNodeConfigureInfor(const char *whereCon) { } else { jsArray.resize(0); jsonVal["success"] = false; - jsonVal["message"] = "查询失败"; + jsonVal["message"] = "fail to query"; } Json::Value jsBody; @@ -1231,7 +1231,7 @@ int SqliteDB::QueryofflineData() { valNodeData.append(valNodeFeature); } } - //无线传感器信息 + // 无线传感器信息 Json::Value root; Json::Value valdatastatic; Json::Value valNodeData; @@ -1347,8 +1347,6 @@ int SqliteDB::QueryofflineData() { return res; } - - int SqliteDB::TransBegin() { return sqlite3_exec(mDBAcess, "begin;", 0, 0, 0); } int SqliteDB::TransRollback() { return sqlite3_exec(mDBAcess, "rollback;", 0, 0, 0); } diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index fc86fb2..88f37a7 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -24,7 +24,7 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) { } current_ts_ = GetLocalTs(); - CleanIdleOccupiedSet(); + CleanIdleOccupiedSet(current_ts_); 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 (!IdleSliceOccupied(i+forward_wave_slice_num)) { + if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) { available_ts = current_wave_slice_ts; - free_slice_ocuppied_.push_back(i+forward_wave_slice_num); + free_slice_ocuppied_.insert(available_ts); 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; } @@ -228,8 +228,22 @@ SensorScheduler::SensorScheduler() { zlog_error(zbt, "invalid format, not an object: %s", SCHEDULE_CONFIG); return; } - start_timestamp_ = std::stol(root["schedule_start_timestamp"].asString()); + start_timestamp_ = std::stol(root["schedule_start_timestamp"].asString()); start_ts_str_ = root["schedule_start_time"].asString(); + long current_ts = GetLocalTs(); + if (current_ts < start_timestamp_) { + zlog_warn(zbt, "current ts: %ld less than start ts: %ld, go to adjust it", current_ts, start_timestamp_); + start_timestamp_ = current_ts; + start_ts_str_ = GetUTCTime(current_ts); + root["schedule_start_timestamp"] = std::to_string(start_timestamp_); + root["schedule_start_time"] = start_ts_str_; + + Json::StyledStreamWriter streamWriter; + std::ofstream out_file(SCHEDULE_CONFIG); + streamWriter.write(out_file, root); + out_file.close(); + } + eigen_value_send_interval_ = root["eigen_value_send_interval"].asInt(); eigen_value_send_duration_ = root["eigen_value_send_duration"].asInt(); wave_form_send_interval_ = root["wave_form_send_interval"].asInt(); @@ -641,19 +655,12 @@ void SensorScheduler::ClearScheduleCfg(int short_addr) { } -void SensorScheduler::CleanIdleOccupiedSet() { - int min_num = 0; - size_t free_slice = (size_t)free_slice_; - 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; +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; } } - return false; } diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index e9939ad..f3b72ad 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -102,8 +101,7 @@ public: private: void UpdateUpgradeInfo(int id); - void CleanIdleOccupiedSet(); - bool IdleSliceOccupied(int slice_id); // 索引号 + void CleanIdleOccupiedSet(long ts); // user config int eigen_value_send_interval_; @@ -127,7 +125,7 @@ private: std::map short_addr_map_; // base_relation.json // 空闲时间戳被占用 - std::list free_slice_ocuppied_; + std::unordered_set free_slice_ocuppied_; // sensor config update std::unordered_set update_; diff --git a/scheduler/update_cfg.cpp b/scheduler/update_cfg.cpp index 66e79ae..341d1d9 100644 --- a/scheduler/update_cfg.cpp +++ b/scheduler/update_cfg.cpp @@ -35,11 +35,7 @@ int UpdateCfg::ReadCfg(std::unordered_set& update) { return 0; } -int UpdateCfg::WriteCfg(std::unordered_set &update) { - if (update.size() == 0) { - ClearCfg(); - return 0; - } +int UpdateCfg::WriteCfg(std::unordered_set &update) { Json::Value root; for (auto item : update) { root.append(item);