From be8ba798a67197d8c6e443dc31a8b8b54f933596 Mon Sep 17 00:00:00 2001 From: pandx Date: Tue, 29 Apr 2025 11:13:09 +0800 Subject: [PATCH 1/2] modify schedule model. --- scheduler/schedule.cpp | 137 +++++++++++++++++++++++------------------ scheduler/schedule.hpp | 7 ++- uart/uart.cpp | 3 +- 3 files changed, 84 insertions(+), 63 deletions(-) diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index a534cff..71e1903 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -17,7 +17,7 @@ extern zlog_category_t *zbt; uint8_t g_x, g_y, g_z; -int SensorScheduler::StartSchedule(int short_addr, int &next_duration) { +int SensorScheduler::StartSchedule(int short_addr, int &next_duration, int &next_task_id) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { @@ -59,17 +59,17 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) { current_request_ = kScheduleConfigSensor; return kScheduleConfigSensor; } else { - wave_feature_set_inst::instance().GetFeatureCfg(short_addr, g_x, g_y, g_z); - if (g_x || g_y || g_z) { - // 执行上送特征值任务 - zlog_warn(zct, "[%d:%x] send eigen value in eigen slice", id, short_addr); - current_request_ = kScheduleEigenValue; - return kScheduleEigenValue; - } else { - next_duration = GetNextDuration(short_addr); + // wave_feature_set_inst::instance().GetFeatureCfg(short_addr, g_x, g_y, g_z); + // if (g_x || g_y || g_z) { + // // 执行上送特征值任务 + // zlog_warn(zct, "[%d:%x] send eigen value in eigen slice", id, short_addr); + // current_request_ = kScheduleEigenValue; + // return kScheduleEigenValue; + // } else { + next_duration = GetNextDuration(short_addr, next_task_id); zlog_warn(zct, "[%d:%x] no need for eigen", id, short_addr); - return kScheduleWrongTime; - } + return kScheduleResultNone; + // } } // } else { // zlog_warn(zct, "[%d:%x] Invalid request, revive in %d eigen slice", id, short_addr, nth_eigen_slice_ + 1); @@ -107,22 +107,16 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) { } } - // if (update_.count(id)) { - // // execute config - // zlog_warn(zct, "[%d:%x] in wave slice to update config", id, short_addr); - // current_request_ = kScheduleConfigSensor; - // return kScheduleConfigSensor; - // } - wave_feature_set_inst::instance().GetWaveCfg(short_addr, g_x, g_y, g_z); - if (g_x || g_y || g_z) { - zlog_warn(zct, "[%d:%x] it is wave time", id, short_addr); - current_request_ = kScheduleWaveForm; - return kScheduleWaveForm; - } else { - next_duration = GetNextDuration(short_addr); + // wave_feature_set_inst::instance().GetWaveCfg(short_addr, g_x, g_y, g_z); + // if (g_x || g_y || g_z) { + // zlog_warn(zct, "[%d:%x] it is wave time", id, short_addr); + // current_request_ = kScheduleWaveForm; + // return kScheduleWaveForm; + // } else { + next_duration = GetNextDuration(short_addr, next_task_id); zlog_warn(zct, "[%d:%x] no need for wave", id, short_addr); - return kScheduleWrongTime; - } + return kScheduleResultNone; + // } } else { if (slice_sensor_id_[nth_wave_slice-1] == 0) { zlog_warn(zct, "[%d:%x] in idle time", id, short_addr); @@ -139,33 +133,28 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) { } } - if (RetransferWave(short_addr)) { - zlog_warn(zct, "[%d:%x] it is retransfer wave time", id, short_addr); - current_request_ = kScheduleWaveForm; - return kScheduleWaveForm; - } else if (MissedWave(short_addr)) { - zlog_warn(zct, "[%d:%x] it is patch wave time", id, short_addr); - current_request_ = kScheduleWaveForm; - patch_set_.erase(short_addr); - return kScheduleWaveForm; - } - // if (update_.count(id)) { - // // execute config - // zlog_warn(zct, "[%d:%x] in idle time to update config", id, short_addr); - // current_request_ = kScheduleConfigSensor; - // return kScheduleConfigSensor; - // } + // if (RetransferWave(short_addr)) { + // zlog_warn(zct, "[%d:%x] it is retransfer wave time", id, short_addr); + // current_request_ = kScheduleWaveForm; + // return kScheduleWaveForm; + // } else if (MissedWave(short_addr)) { + // zlog_warn(zct, "[%d:%x] it is patch wave time", id, short_addr); + // current_request_ = kScheduleWaveForm; + // patch_set_.erase(short_addr); + // return kScheduleWaveForm; + // } } // 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_; + next_task_id = kScheduleEigenValue; 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; + return kScheduleResultNone; } } } -long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { +long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr, int& next_task_id) { // current_ts_ = GetLocalTs(); // 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_; @@ -185,6 +174,7 @@ long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { 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, "[Nxt] [%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); + next_task_id = kScheduleEigenValue; return available_ts; } int wave_slice = wave_slice_iter->second; // 从1开始 @@ -230,7 +220,9 @@ long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { available_ts = current_wave_slice_ts; 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; + // break; + next_task_id = kScheduleUpgrade; + return available_ts; } } } @@ -246,7 +238,9 @@ long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { available_ts = current_wave_slice_ts; free_slice_ocuppied_.insert(available_ts); zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to retransfer wave, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); - break; + // break; + next_task_id = kScheduleWaveForm; + return available_ts; } } } @@ -259,32 +253,37 @@ long SensorScheduler::CalcNextTimestamp(int id, uint16_t short_addr) { available_ts = current_wave_slice_ts; free_slice_ocuppied_.insert(available_ts); zlog_warn(zct, "[Nxt][%d:%x] %d nth free wave slice will be used to patch wave, utc time:[%s]", id, short_addr, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); - break; + // break; + next_task_id = kScheduleWaveForm; + return available_ts; } } } } } } - if (send_wave_ts > 0 && available_ts > 0) { - long min_ts = std::min(send_wave_ts, available_ts); - 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 > 0) { + // long min_ts = std::min(send_wave_ts, available_ts); + zlog_warn(zct, "[Nxt] [%d:%x] next wave send utc time1:%s", id, short_addr, GetUTCTime(send_wave_ts).c_str()); + next_task_id = kScheduleWaveForm; + return send_wave_ts; + // return min_ts; } - if (send_wave_ts + available_ts > 0) { - long max_ts = std::max(send_wave_ts, available_ts); - zlog_warn(zct, "[Nxt] [%d:%x] next feature send utc time2:%s", id, short_addr, GetUTCTime(max_ts).c_str()); - return max_ts; - } + // if (send_wave_ts + available_ts > 0) { + // long max_ts = std::max(send_wave_ts, available_ts); + // 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, "[Nxt] [%d:%x] next feature send utc time3:[%s]", id, short_addr, GetUTCTime(available_ts).c_str()); + next_task_id = kScheduleEigenValue; return available_ts; } -int SensorScheduler::GetNextDuration(int short_addr,int next_taskID) { +int SensorScheduler::GetNextDuration(int short_addr, int &next_task_id) { int id = 0; auto iter = short_addr_map_.find(short_addr); if (iter == short_addr_map_.end()) { @@ -294,16 +293,36 @@ int SensorScheduler::GetNextDuration(int short_addr,int next_taskID) { id = iter->second; } long current_ts = GetLocalTs(); - long next_ts = CalcNextTimestamp(id, short_addr); + long next_ts = CalcNextTimestamp(id, short_addr, next_task_id); int duration = next_ts - current_ts; if (duration < 0 || duration > eigen_value_send_interval_) { zlog_warn(zct, "[Nxt] exception duration: %d", duration); duration = eigen_value_send_interval_; } - zlog_warn(zct, "[Nxt] [%d:%x] next duration is %d", id, short_addr, duration); + zlog_warn(zct, "[Nxt] [%d:%x] next duration: %d, taskid: %d", id, short_addr, duration, next_task_id); return duration; } +// int SensorScheduler::GetNextDuration(int short_addr) { +// int id = 0; +// auto iter = short_addr_map_.find(short_addr); +// if (iter == short_addr_map_.end()) { +// zlog_error(zct, "cannot find id for short_addr %x", short_addr); +// return 0; +// } else { +// id = iter->second; +// } +// long current_ts = GetLocalTs(); +// long next_ts = CalcNextTimestamp(id, short_addr); +// int duration = next_ts - current_ts; +// if (duration < 0 || duration > eigen_value_send_interval_) { +// zlog_warn(zct, "[Nxt] exception duration: %d", duration); +// duration = eigen_value_send_interval_; +// } +// zlog_warn(zct, "[Nxt] [%d:%x] next duration is %d", id, short_addr, duration); +// return duration; +// } + SensorScheduler::SensorScheduler() { support_modification_ = true; std::ifstream schedule_file(SCHEDULE_CONFIG); diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index d5289c7..87d6292 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -38,13 +38,14 @@ public: // kScheduleEigenValue kScheduleWaveForm // 上面4个结束,调GetNextDuration()获取休眠时间 // 如果是kScheduleWrongTime, 此函数next_duration表明休眠时间 - int StartSchedule(int short_addr, int &next_duration); - int GetNextDuration(int short_addr,int next_taskID = 3); + int StartSchedule(int short_addr, int &next_duration, int &next_task_id); + int GetNextDuration(int short_addr,int &next_task_id); + // int GetNextDuration(int short_addr); int WaveError(uint16_t short_addr); void WaveSuccess(uint16_t short_addr); long GetBaseTimestamp(int id); - long CalcNextTimestamp(int id, uint16_t short_addr); + long CalcNextTimestamp(int id, uint16_t short_addr, int& next_task_id); // 当有传感器需要更新配置时调用 int UpdateSensorConfig(int short_addr); diff --git a/uart/uart.cpp b/uart/uart.cpp index 6106855..b46b7d6 100644 --- a/uart/uart.cpp +++ b/uart/uart.cpp @@ -334,7 +334,8 @@ int Uart::DealAskTask(uint16_t ushortAdd){ } ModifyDistAddr(ushortAdd); int next_duration = 0; - int taskID = scheduler::instance().StartSchedule(ushortAdd,next_duration); + int taskID; + scheduler::instance().StartSchedule(ushortAdd,next_duration, taskID); ScheduleTask scheduleTask; zlog_info(zct, "taskID = %d ", taskID); if (taskID == kScheduleEigenValue) //1.特征值 From 99d95f89a2564f23595924145414086f3f8f4e1d Mon Sep 17 00:00:00 2001 From: pandx Date: Tue, 29 Apr 2025 11:14:37 +0800 Subject: [PATCH 2/2] add doc. --- doc/readme.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 doc/readme.txt diff --git a/doc/readme.txt b/doc/readme.txt new file mode 100644 index 0000000..07c7c2a --- /dev/null +++ b/doc/readme.txt @@ -0,0 +1,10 @@ +2025-4-29 +1 场景1: 传感器发送特征值过来时,表明这是特征值发送时间或随机启动的,调用StartSchedule, +(1) 如果在特征值时间窗里,如果有配置需要更新的话,返回值为kScheduleConfigSensor,表明要进行配置更新,更新完成后调用GetNextDuration来获取下次启动时间与任务。 +(2) 如果在特征值时间窗里,如果没有配置需要更新的话,函数的出参StartSchedule(int short_addr, int &next_duration, int &next_task_id)会说明下次启动时间与任务, + 如果是波形任务,自行查找需要哪些波形,此函数返回值为kScheduleResultNone。 + +2 场景2:传感器发送波形过来时,处理完波形,调用int GetNextDuration(int short_addr,int &next_task_id)来获取下次启动时间与任务 + +3 场景3:传感器认为此时是升级时间,发送请求,网关调用StartSchedule,如果确实有升级任务,返回kScheduleUpgrade,如果没有升级任务, + 返回值为kScheduleResultNone, 出参中带有下次启动与任务信息。