diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index e010011..eefa84d 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -7,6 +7,10 @@ #include #include #include +#include + +extern zlog_category_t *zct; +extern zlog_category_t *zbt; // 当一个传感器来进行通信时 @@ -68,17 +72,17 @@ SensorScheduler::SensorScheduler() { support_modification_ = true; std::ifstream schedule_file(SCHEDULE_CONFIG); if (schedule_file.good()) { - printf("exist configuration file\n"); + zlog_info(zbt, "exist configuration file"); Json::Reader reader; Json::Value root; if (!reader.parse(schedule_file, root, false)) { - printf("invalid format, fail to parse %s\n", SCHEDULE_CONFIG); + zlog_error(zbt, "invalid format, fail to parse %s", SCHEDULE_CONFIG); schedule_file.close(); return; } schedule_file.close(); if (!root.isObject()) { - printf("invalid format, not an object: %s\n", SCHEDULE_CONFIG); + zlog_error(zbt, "invalid format, not an object: %s", SCHEDULE_CONFIG); return; } start_timestamp_ = std::stol(root["schedule_start_timestamp"].asString()); @@ -110,7 +114,7 @@ SensorScheduler::SensorScheduler() { wave_slice_num_per_eigen_interval_ = rest_duration / wave_form_send_duration_; seconds_per_wave_slice_ = rest_duration / wave_slice_num_per_eigen_interval_; } else { - printf("use default configuration\n"); + zlog_info(zbt, "use default configuration"); int eigen_value_send_interval = 300; int wave_form_send_interval = 7200; int eigen_value_send_duration = 6; @@ -129,17 +133,17 @@ SensorScheduler::SensorScheduler() { Json::Reader reader; Json::Value root; if (!reader.parse(base_relation_file, root, false)) { - printf("invalid format, fail to parse %s\n", BASE_RELATION); + zlog_error(zbt, "invalid format, fail to parse %s", BASE_RELATION); base_relation_file.close(); return; } base_relation_file.close(); if (!root.isArray()) { - printf("invalid format, not an array: %s\n", BASE_RELATION); + zlog_error(zbt, "invalid format, not an array: %s", BASE_RELATION); return; } if (root.size() == 0) { - printf("no element in %s\n", BASE_RELATION); + zlog_info(zbt, "no element in %s", BASE_RELATION); return; } @@ -150,7 +154,7 @@ SensorScheduler::SensorScheduler() { for (const auto &item : root) { short_addr = item["pan_id"].asInt(); index = item["id"].asInt(); - printf("index:%d, short addr:%d\n", index, short_addr); + zlog_info(zbt, "index:%d, short addr:%d", index, short_addr); short_addr_map_[short_addr] = index; } } @@ -161,17 +165,17 @@ SensorScheduler::SensorScheduler() { Json::Reader reader; Json::Value root; if (!reader.parse(upgrade_file, root, false)) { - printf("invalid format, fail to parse %s\n", UPGRADE_CONFIG); + zlog_error(zbt, "invalid format, fail to parse %s", UPGRADE_CONFIG); upgrade_file.close(); return; } upgrade_file.close(); if (!root.isArray()) { - printf("invalid format, not an array: %s\n", UPGRADE_CONFIG); + zlog_error(zbt, "invalid format, not an array: %s", UPGRADE_CONFIG); return; } if (root.size() > 0) { - printf("element in %s\n", UPGRADE_CONFIG); + zlog_info(zbt, "element in %s", UPGRADE_CONFIG); UpgradeInfo info; for (const auto &item : root) { info.try_times = item["try_times"].asInt(); @@ -184,7 +188,7 @@ SensorScheduler::SensorScheduler() { info.try_world_time1.push_back(time_item.asString()); } upgrade_[item["id"].asInt()] = info; - printf("id:%d need to upgrade from:%s to %s\n", item["id"].asInt(), + zlog_info(zbt, "id:%d need to upgrade from:%s to %s", item["id"].asInt(), info.current_sw_version.c_str(), info.upgrade_sw_version.c_str()); } } @@ -196,20 +200,20 @@ SensorScheduler::SensorScheduler() { Json::Reader reader; Json::Value root; if (!reader.parse(config_update_file, root, false)) { - printf("invalid format, fail to parse %s\n", CONFIG_UPDATE); + zlog_error(zbt, "invalid format, fail to parse %s", CONFIG_UPDATE); config_update_file.close(); return; } config_update_file.close(); if (!root.isArray()) { - printf("invalid format, not an array: %s\n", CONFIG_UPDATE); + zlog_error(zbt, "invalid format, not an array: %s", CONFIG_UPDATE); return; } if (root.size() > 0) { - printf("element in %s\n", CONFIG_UPDATE); + zlog_info(zbt, "element in %s", CONFIG_UPDATE); for (const auto &item : root) { update_.insert(item.asInt()); - printf("sensor id:%d need to update\n", item.asInt()); + zlog_info(zbt, "sensor id:%d need to update", item.asInt()); } } } @@ -221,15 +225,43 @@ int SensorScheduler::Init() return 0; } +int SensorScheduler::GetNextDuration(int pan_id) { + int id = 0; + auto iter = short_addr_map_.find(pan_id); + if (iter == short_addr_map_.end()) { + zlog_error(zct, "cannot find id for pan_id %d", pan_id); + return 0; + } else { + id = iter->second; + } + long current_ts = GetLocalTs(); + long next_ts = CalcNextTimestamp(id); + int duration = next_ts - current_ts; + zlog_debug(zct, "[%d] next duration is %d", id, duration); + return 0; +} + +long SensorScheduler::GetBaseTimestamp(int pan_id) { + int id = 0; + auto iter = short_addr_map_.find(pan_id); + if (iter == short_addr_map_.end()) { + zlog_error(zct, "cannot find id for pan_id %d", pan_id); + return 0; + } else { + id = iter->second; + } + return start_timestamp_ + (id - 1) * eigen_value_send_duration_; +} + long SensorScheduler::CalcNextTimestamp(int id) { // 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()) { - printf("[Error] invaild id:%d, not find wave slice id\n", id); + zlog_error(zct, "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_; - printf("[Error] [%d] next feature send utc time:[%s]\n", id, GetUTCTime(available_ts).c_str()); + zlog_error(zct, "[%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); return available_ts; } int wave_slice = wave_slice_iter->second; // 从1开始 @@ -240,7 +272,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_ + eigen_value_slice_total_seconds_ + (i - forward_wave_slice_num) * seconds_per_wave_slice_; - printf("[%d] send wave time:[%s]\n", id, GetUTCTime(send_wave_ts).c_str()); + zlog_debug(zct, "[%d] send wave time:[%s]\n", id, GetUTCTime(send_wave_ts).c_str()); break; } } @@ -257,7 +289,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); - printf("[%d] %d nth free wave slice will be used to upgrade, utc time:[%s]\n", id, i+forward_wave_slice_num, GetUTCTime(available_ts).c_str()); + 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()); break; } } @@ -266,19 +298,19 @@ long SensorScheduler::CalcNextTimestamp(int id) { } if (send_wave_ts > 0 && available_ts > 0) { long min_ts = std::min(send_wave_ts, available_ts); - printf("[%d] will use nearest time:%s\n", id, GetUTCTime(min_ts).c_str()); + zlog_debug(zct, "[%d] will use nearest time:%s", id, 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); - printf("[%d] will use vaild time:%s\n", id, GetUTCTime(max_ts).c_str()); + zlog_debug(zct, "[%d] will use vaild time:%s", id, GetUTCTime(max_ts).c_str()); return max_ts; } } // 如果是在当前波形时间窗中,不管是空闲时间窗,还是发送波形的时间窗,下一个时间窗是特征值 long available_ts = current_wave_start_ts_ + eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - printf("[%d] next feature send utc time:[%s]\n", id, GetUTCTime(available_ts).c_str()); + zlog_debug(zct, "[%d] next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); return available_ts; } @@ -294,7 +326,7 @@ int SensorScheduler::GetAvailableId(int pan_id) { break; } } - printf("[GetAvailableId][%d] pan id : %d\n", available_id, pan_id); + zlog_warn(zct, "[GetAvailableId][%d] pan id : %d", available_id, pan_id); Json::Value root; Json::Value item; item["id"] = available_id; @@ -323,12 +355,8 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { } else { id = iter->second; } - // 通过pan_id找到id - // current_ts_ = 1730170142; // 当前时间 - // current_ts_ = 1730170148; // 第2个特征值时间片 - // current_ts_ = 1730170154; // 第3个特征值时间片 - current_ts_ = 1730177342; // 2小时后的第一个时间片 - // current_ts_ = GetLocalTs(); + + 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_; @@ -347,31 +375,31 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { } - printf("[%d] current utc:%s\n", id, GetUTCTime(current_ts_).c_str()); + zlog_debug(zct, "[%d] current utc:%s", id, GetUTCTime(current_ts_).c_str()); if (ts_in_eigen_slice_) { if (id == nth_eigen_slice_ + 1) { // 传感器需要执行上送特征值任务, 如果有配置需要下发的话,下发配置 if (update_.count(id)) { // execute config - printf("[%d] update config in eigen slice\n", id); + zlog_debug(zct, "[%d] update config in eigen slice", id); current_request_ = kScheduleConfigSensor; return kScheduleConfigSensor; } else { // 执行上送特征值任务 - printf("[%d] send eigen value in eigen slice\n", id); + zlog_debug(zct, "[%d] send eigen value in eigen slice", id); current_request_ = kScheduleEigenValue; return kScheduleEigenValue; } } else { - printf("[%d] Invalid request, revive in %d eigen slice\n", id, nth_eigen_slice_ + 1); + zlog_debug(zct, "[%d] Invalid request, revive in %d eigen slice", id, nth_eigen_slice_ + 1); if (id < nth_eigen_slice_ + 1) { // 不正确的请求 long available_ts = current_wave_start_ts_ + eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - printf("[%d] wrong time in eigen slice, next feature in next interval send utc time:[%s]\n", id, GetUTCTime(available_ts).c_str()); + zlog_debug(zct, "[%d] wrong time in eigen slice, next feature in next interval send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); next_duration = available_ts - current_ts_; } else { long available_ts = current_wave_start_ts_ + (id - 1) * eigen_value_send_duration_; - printf("[%d] wrong time in eigen slice, next feature in current interval send utc time:[%s]\n", id, GetUTCTime(available_ts).c_str()); + zlog_debug(zct, "[%d] wrong time in eigen slice, next feature in current interval send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); next_duration = available_ts - current_ts_; } return kScheduleWrongTime; @@ -380,7 +408,7 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { int nth_wave_slice = nth_eigen_value_slice_ * wave_slice_num_per_eigen_interval_ + nth_wave_slice_; auto wave_slice_iter = sensor_id_nth_slice_.find(id); if (wave_slice_iter == sensor_id_nth_slice_.end()) { - printf("[%d]invaild id, not find wave slice id, need to check further\n", id); + zlog_error(zct, "[%d]invaild id, not find wave slice id, need to check further", id); return kScheduleUnknownSensor; } else { if (nth_wave_slice == wave_slice_iter->second) { @@ -389,7 +417,7 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { if (upgrade_iter != upgrade_.end()) { if (upgrade_iter->second.try_times < 10) { current_request_ = kScheduleUpgrade; - printf("[%d] in wave slice to upgrade now from version:%s to %s, try time:%d\n", + 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(), upgrade_iter->second.upgrade_sw_version.c_str(), upgrade_iter->second.try_times); return kScheduleUpgrade; @@ -398,7 +426,7 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { if (update_.count(id)) { // execute config - printf("[%d] in wave slice to update config\n", id); + zlog_debug(zct, "[%d] in wave slice to update config", id); current_request_ = kScheduleConfigSensor; return kScheduleConfigSensor; } @@ -411,7 +439,7 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { if (upgrade_iter != upgrade_.end()) { if (upgrade_iter->second.try_times < 10) { current_request_ = kScheduleUpgrade; - printf("[%d] in idle to upgrade now from version:%s to %s, try time:%d\n", + 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(), upgrade_iter->second.upgrade_sw_version.c_str(), upgrade_iter->second.try_times); return kScheduleUpgrade; @@ -420,14 +448,14 @@ int SensorScheduler::StartSchedule(int pan_id, int &next_duration) { if (update_.count(id)) { // execute config - printf("[%d] in idle time to update config\n", id); + zlog_debug(zct, "[%d] in idle time to update config", id); current_request_ = kScheduleConfigSensor; return kScheduleConfigSensor; } } // wrong time to come long available_ts = current_wave_start_ts_ + eigen_value_send_interval_ + (id - 1) * eigen_value_send_duration_; - printf("[%d] wrong time in wave slice, next feature send utc time:[%s]\n", id, GetUTCTime(available_ts).c_str()); + zlog_warn(zct, "[%d] wrong time in wave slice, next feature send utc time:[%s]", id, GetUTCTime(available_ts).c_str()); next_duration = available_ts - current_ts_; return kScheduleWrongTime; } @@ -439,7 +467,7 @@ int SensorScheduler::Config(int eigen_value_send_interval, int wave_form_send_in int eigen_value_send_duration, int wave_form_send_duration, int max_sensor_num) { if (!support_modification_) { - printf("not support modification"); + zlog_warn(zct, "not support modification"); return 1; } @@ -456,7 +484,6 @@ int SensorScheduler::Config(int eigen_value_send_interval, int wave_form_send_in return ret; } - eigen_value_send_interval_ = eigen_value_send_interval; eigen_value_send_duration_ = eigen_value_send_duration; wave_form_send_interval_ = wave_form_send_interval; @@ -489,24 +516,24 @@ int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_ int eigen_value_send_duration, int wave_form_send_duration, int max_sensor_num, int &available_slice, int &free_slice) { if (max_sensor_num <= 0) { - printf("invalid max_sensor_num:%d\n", max_sensor_num); + zlog_error(zbt, "invalid max_sensor_num:%d", max_sensor_num); return 1; } if (max_sensor_num * eigen_value_send_duration > eigen_value_send_interval) { - printf("invalid eigen_value_send_interval:%d and eigen_value_send_duration:%d, max_sensor_num:%d\n", + zlog_error(zbt, "invalid eigen_value_send_interval:%d and eigen_value_send_duration:%d, max_sensor_num:%d", eigen_value_send_interval, eigen_value_send_duration, max_sensor_num); return 2; } if (max_sensor_num * wave_form_send_duration > wave_form_send_interval) { - printf("invalid wave_form_send_interval:%d and wave_form_send_duration:%d, max_sensor_num:%d\n", + zlog_error(zbt, "invalid wave_form_send_interval:%d and wave_form_send_duration:%d, max_sensor_num:%d", wave_form_send_interval, wave_form_send_duration, max_sensor_num); return 3; } if (wave_form_send_interval % eigen_value_send_interval != 0) { - printf("invalid eigen_value_send_interval:%d and wave_form_send_interval:%d\n", + zlog_error(zbt, "invalid eigen_value_send_interval:%d and wave_form_send_interval:%d", eigen_value_send_interval, wave_form_send_interval); return 4; } @@ -517,7 +544,7 @@ int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_ available_slice = wave_form_send_interval / eigen_value_send_interval * slice_per_eigen_value_interval; free_slice = available_slice - max_sensor_num; if (free_slice < 0) { - printf("invalid config, available slice:%d, required slice:%d\n", available_slice, max_sensor_num); + zlog_error(zbt, "invalid config, available slice:%d, required slice:%d", available_slice, max_sensor_num); return 5; } return 0; @@ -526,7 +553,7 @@ int SensorScheduler::CalcAvailableSlice(int eigen_value_send_interval, int wave_ long SensorScheduler::GetLocalTs() { auto now = std::chrono::system_clock::now(); auto timestamp = std::chrono::duration_cast(now.time_since_epoch()).count(); - printf("current timestamp:%lld\n", timestamp); + // zlog_debug(zct, "current timestamp:%lld", timestamp); return timestamp; } @@ -539,7 +566,7 @@ long SensorScheduler::GetLocalWorldTime(std::string &world_time) { local_time->tm_year + 1900, local_time->tm_mon+1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec); world_time = str; auto timestamp = std::chrono::duration_cast(now.time_since_epoch()).count(); - printf("world time:%s, timestamp:%lld\n", world_time.c_str(), timestamp); + // zlog_debug(zct, "world time:%s, timestamp:%lld", world_time.c_str(), timestamp); return timestamp; } diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index f4c1117..97af321 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -8,7 +8,7 @@ #include #include -#define SCHEDULE_CONFIG "./schedule_test.json" +#define SCHEDULE_CONFIG "./schedule.json" #define BASE_RELATION "./base_relation.json" #define CONFIG_UPDATE "./config_update.json" #define UPGRADE_CONFIG "./upgrade.json" @@ -40,6 +40,8 @@ public: int Init(); int StartSchedule(int pan_id, int &next_duration); + int GetNextDuration(int pan_id); + long GetBaseTimestamp(int id); long CalcNextTimestamp(int id); int Config(int eigen_value_send_interval, int wave_form_send_interval,