add log and adjust logic.

This commit is contained in:
pandx 2024-11-12 14:18:32 +08:00
parent 4ef26e7865
commit 5e654d4f50
2 changed files with 73 additions and 60 deletions

View File

@ -14,6 +14,16 @@
extern zlog_category_t *zct;
extern zlog_category_t *zbt;
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;
}
}
}
int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
int id = 0;
auto iter = short_addr_map_.find(short_addr);
@ -24,15 +34,16 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
}
current_ts_ = GetLocalTs();
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_;
seconds_in_current_wave_slice_ = current_ts_ - current_wave_start_ts_;
nth_eigen_value_slice_ = seconds_in_current_wave_slice_ / eigen_value_send_interval_;
seconds_in_current_eigen_slice_ = seconds_in_current_wave_slice_ % eigen_value_send_interval_;
ts_in_eigen_slice_ = true;
if (seconds_in_current_eigen_slice_ > eigen_value_slice_total_seconds_ - 2) {
ts_in_eigen_slice_ = false;
if (seconds_in_current_eigen_slice_ < eigen_value_slice_total_seconds_ - 3) {
ts_in_eigen_slice_ = true;
}
if (ts_in_eigen_slice_) {
@ -40,8 +51,8 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
} else {
nth_wave_slice_ = (seconds_in_current_eigen_slice_ - eigen_value_slice_total_seconds_ + 3) / seconds_per_wave_slice_;
}
zlog_warn(zct, "[%d] current utc:%s, nth eigen_value slice:%d, seconds in eigen slice:%d, eigen slice:%d",
id, GetUTCTime(current_ts_).c_str(), nth_eigen_value_slice_+1, seconds_in_current_eigen_slice_, ts_in_eigen_slice_);
zlog_warn(zct, "[%d:%x] ts:%ld, current utc:%s, nth eigen_value slice:%d, seconds in eigen slice:%d, eigen slice:%d",
id, short_addr, current_ts_, GetUTCTime(current_ts_).c_str(), nth_eigen_value_slice_+1, seconds_in_current_eigen_slice_, ts_in_eigen_slice_);
if (ts_in_eigen_slice_) {
if (id == nth_eigen_slice_ + 1) {
// 传感器需要执行上送特征值任务, 如果有配置需要下发的话,下发配置
@ -57,16 +68,16 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
return kScheduleEigenValue;
}
} else {
zlog_warn(zct, "[%d] Invalid request, revive in %d eigen slice", id, nth_eigen_slice_ + 1);
zlog_warn(zct, "[%d:%x] Invalid request, revive in %d eigen slice", id, short_addr, nth_eigen_slice_ + 1);
if (id < nth_eigen_slice_ + 1) {
// 不正确的请求
long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1) * eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_;
next_duration = available_ts - current_ts_;
zlog_warn(zct, "[%d] wrong time in eigen slice, next feature in next interval send utc time:[%s], duration:%d", id, GetUTCTime(available_ts).c_str(), next_duration);
zlog_warn(zct, "[%d:%x] wrong time in eigen slice, next feature in next interval send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration);
} else {
long available_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_;
next_duration = available_ts - current_ts_;
zlog_warn(zct, "[%d] wrong time in eigen slice, next feature in current interval send utc time:[%s], duration:%d", id, GetUTCTime(available_ts).c_str(), next_duration);
zlog_warn(zct, "[%d:%x] wrong time in eigen slice, next feature in current interval send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration);
}
return kScheduleWrongTime;
}
@ -74,9 +85,10 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_ + 1;
auto wave_slice_iter = sensor_id_nth_slice_.find(id);
if (wave_slice_iter == sensor_id_nth_slice_.end()) {
zlog_error(zct, "[%d]invaild id, not find wave slice id, need to check further", id);
zlog_error(zct, "[%d:%x]invaild id, not find wave slice id, need to check further", id, short_addr);
return kScheduleUnknownSensor;
} else {
}
if (nth_wave_slice == wave_slice_iter->second) {
// 需要发送波形, 需要判断是否有配置,升级需求
auto upgrade_iter = upgrade_.find(id);
@ -84,8 +96,8 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
if (upgrade_iter->second.try_times < 10) {
current_request_ = kScheduleUpgrade;
UpdateUpgradeInfo(id);
zlog_warn(zct, "[%d] in wave slice to upgrade now from version:%s to %s, try time:%d",
id, upgrade_iter->second.current_sw_version.c_str(),
zlog_warn(zct, "[%d:%x] in wave slice to upgrade now from version:%s to %s, try time:%d",
id, short_addr, upgrade_iter->second.current_sw_version.c_str(),
upgrade_iter->second.upgrade_sw_version.c_str(), upgrade_iter->second.try_times);
return kScheduleUpgrade;
}
@ -93,23 +105,24 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
if (update_.count(id)) {
// execute config
zlog_debug(zct, "[%d] in wave slice to update config", id);
zlog_warn(zct, "[%d:%x] in wave slice to update config", id, short_addr);
current_request_ = kScheduleConfigSensor;
return kScheduleConfigSensor;
}
zlog_warn(zct, "[%d] it is wave time", id);
zlog_warn(zct, "[%d:%x] it is wave time", id, short_addr);
current_request_ = kScheduleWaveForm;
return kScheduleWaveForm;
} else {
if (slice_sensor_id_[nth_wave_slice] == 0) {
if (slice_sensor_id_[nth_wave_slice-1] == 0) {
zlog_warn(zct, "[%d:%x] in idle time", id, short_addr);
// idle time
auto upgrade_iter = upgrade_.find(id);
if (upgrade_iter != upgrade_.end()) {
if (upgrade_iter->second.try_times < 10) {
current_request_ = kScheduleUpgrade;
UpdateUpgradeInfo(id);
zlog_debug(zct, "[%d] in idle to upgrade now from version:%s to %s, try time:%d",
id, upgrade_iter->second.current_sw_version.c_str(),
zlog_warn(zct, "[%d:%x] in idle to upgrade now from version:%s to %s, try time:%d",
id, short_addr, upgrade_iter->second.current_sw_version.c_str(),
upgrade_iter->second.upgrade_sw_version.c_str(), upgrade_iter->second.try_times);
return kScheduleUpgrade;
}
@ -117,7 +130,7 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
if (update_.count(id)) {
// execute config
zlog_debug(zct, "[%d] in idle time to update config", id);
zlog_warn(zct, "[%d:%x] in idle time to update config", id, short_addr);
current_request_ = kScheduleConfigSensor;
return kScheduleConfigSensor;
}
@ -125,22 +138,21 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
// wrong time to come
long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1) * eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_;
next_duration = available_ts - current_ts_;
zlog_warn(zct, "[%d] wrong time in wave slice, next feature send utc time:[%s], duration:%d", id, GetUTCTime(available_ts).c_str(), next_duration);
zlog_warn(zct, "[%d:%x] wrong time in wave slice, next feature send utc time:[%s], duration:%d", id, short_addr, GetUTCTime(available_ts).c_str(), next_duration);
return kScheduleWrongTime;
}
}
}
}
long SensorScheduler::CalcNextTimestamp(int id) {
long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) {
// current_ts_ = GetLocalTs();
if (ts_in_eigen_slice_) {
int forward_wave_slice_num = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_;
auto wave_slice_iter = sensor_id_nth_slice_.find(id);
if (wave_slice_iter == sensor_id_nth_slice_.end()) {
zlog_error(zct, "invaild id:%d, not find wave slice id", id);
zlog_error(zct, "[Nxt] invaild id:%d, not find wave slice id", id);
long available_ts = current_wave_start_ts_ + eigen_value_send_interval_ + nth_eigen_slice_ * eigen_value_send_duration_;
zlog_error(zct, "[%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str());
zlog_error(zct, "[Nxt] [%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str());
return available_ts;
}
int wave_slice = wave_slice_iter->second; // 从1开始
@ -151,7 +163,7 @@ long SensorScheduler::CalcNextTimestamp(int id) {
for (int i = forward_wave_slice_num; i <= forward_wave_slice_num + wave_slice_num_per_eigen_interval_; ++i) {
if (wave_slice - 1 == i) {
send_wave_ts = current_wave_start_ts_ + nth_eigen_value_slice_ * eigen_value_send_interval_ + eigen_value_slice_total_seconds_ + (i - forward_wave_slice_num) * seconds_per_wave_slice_;
zlog_debug(zct, "[%d] send wave time:[%s]\n", id, GetUTCTime(send_wave_ts).c_str());
zlog_warn(zct, "[Nxt] [%d:%x] send wave time:[%s]", id, short_addr, GetUTCTime(send_wave_ts).c_str());
break;
}
}
@ -168,7 +180,7 @@ long SensorScheduler::CalcNextTimestamp(int id) {
if (free_slice_ocuppied_.count(current_wave_slice_ts) == 0) {
available_ts = current_wave_slice_ts;
free_slice_ocuppied_.insert(available_ts);
zlog_debug(zct, "[%d] %d nth free wave slice will be used to upgrade, utc time:[%s]", id, 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;
}
}
@ -177,19 +189,19 @@ long SensorScheduler::CalcNextTimestamp(int id) {
}
if (send_wave_ts > 0 && available_ts > 0) {
long min_ts = std::min(send_wave_ts, available_ts);
zlog_warn(zct, "[%d] next feature send utc time1:%s", id, GetUTCTime(min_ts).c_str());
zlog_warn(zct, "[Nxt] [%d:%x] next feature send utc time1:%s", id, short_addr, GetUTCTime(min_ts).c_str());
return min_ts;
}
if (send_wave_ts + available_ts > 0) {
long max_ts = std::max(send_wave_ts, available_ts);
zlog_warn(zct, "[%d] next feature send utc time2:%s", id, GetUTCTime(max_ts).c_str());
zlog_warn(zct, "[Nxt] [%d:%x] next feature send utc time2:%s", id, short_addr, GetUTCTime(max_ts).c_str());
return max_ts;
}
}
// 如果是在当前波形时间窗中,不管是空闲时间窗,还是发送波形的时间窗,下一个时间窗是特征值
long available_ts = current_wave_start_ts_ + (nth_eigen_value_slice_ + 1)* eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_;
zlog_warn(zct, "[%d] next feature send utc time3:[%s]", id, GetUTCTime(available_ts).c_str());
zlog_warn(zct, "[Nxt] [%d:%x] next feature send utc time3:[%s]", id, short_addr, GetUTCTime(available_ts).c_str());
return available_ts;
}
@ -203,9 +215,9 @@ int SensorScheduler::GetNextDuration(int short_addr) {
id = iter->second;
}
long current_ts = GetLocalTs();
long next_ts = CalcNextTimestamp(id);
long next_ts = CalcNextTimestamp(id, short_addr);
int duration = next_ts - current_ts;
zlog_warn(zct, "[%d] next duration is %d", id, duration);
zlog_warn(zct, "[Nxt] [%d:%x] next duration is %d", id, short_addr, duration);
return duration;
}

View File

@ -42,7 +42,7 @@ public:
int GetNextDuration(int short_addr);
long GetBaseTimestamp(int id);
long CalcNextTimestamp(int id);
long CalcNextTimestamp(int id, uint16_t short_addr);
// 当有传感器需要更新配置时调用
int UpdateSensorConfig(int short_addr);
@ -101,6 +101,7 @@ public:
private:
void UpdateUpgradeInfo(int id);
void CleanIdleOccupiedSet(long ts);
// user config
int eigen_value_send_interval_;