WLG/scheduler/schedule.hpp

119 lines
4.0 KiB
C++
Raw Normal View History

2024-10-29 19:53:09 +08:00
#ifndef SCHEDULE_HPP_
#define SCHEDULE_HPP_
#include <iostream>
#include <map>
#include <unordered_set>
#include <vector>
#include <stdint.h>
#include <boost/container/detail/singleton.hpp>
#define SCHEDULE_CONFIG "./schedule_test.json"
#define BASE_RELATION "./base_relation.json"
#define CONFIG_UPDATE "./config_update.json"
#define UPGRADE_CONFIG "./upgrade.json"
typedef enum {
kScheduleResultNone = 0,
kScheduleUnknownSensor = 1,
kScheduleConfigSensor = 2,
kScheduleEigenValue = 3,
kScheduleWaveForm = 4,
kScheduleUpgrade = 5,
kScheduleWrongTime = 6
} ScheduleResult;
typedef struct {
int try_times;
std::string sensor_type;
int hw_version;
std::string current_sw_version;
std::string upgrade_sw_version;
std::string submit_time;
std::vector<std::string> try_world_time1;
} UpgradeInfo;
class SensorScheduler {
public:
SensorScheduler();
int Init();
int StartSchedule(int pan_id, int &next_duration);
long CalcNextTimestamp(int id);
int Config(int eigen_value_send_interval, int wave_form_send_interval,
int eigen_value_send_duration, int wave_form_send_duration,
int max_sensor_num);
int CalcAvailableSlice(int eigen_value_send_interval, int wave_form_send_interval,
int eigen_value_send_duration, int wave_form_send_duration,
int max_sensor_num, int &available_slice, int &free_slice);
void GetSensorTs(int pan_id, long &eigen_ts, long &wave_ts);
// ======schedule.json操作开始======
// 无线网关程序重启时
// void ReadScheduleCfg();
// 配置完成后
int WriteScheduleCfg(long &ts, std::string &world_time);
// 当系统进行校时时,修改时间戳信息, "slices"字段
void ModifyScheduleTs(long start_ts);
// 当接入传感器时,设置为不可修改; 当停用所有传感器后,修改为可修改
void AdjustSupportModification(bool support_modification);
// ======schedule.json操作结束======
long GetLocalTs();
long GetLocalWorldTime(std::string &world_time);
std::string GetUTCTime(long ts);
int GetAvailableId(int pan_id);
private:
// user config
int eigen_value_send_interval_;
int eigen_value_send_duration_;
int wave_form_send_interval_;
int wave_form_send_duration_;
int max_sensor_num_;
// calc result
long start_timestamp_;
std::string start_ts_str_;
int available_slice_;
int free_slice_;
int wave_slice_num_per_eigen_interval_; // 每个特征值窗口中有几个波形发送窗口
int seconds_per_wave_slice_; // 波形窗口时长
int eigen_value_slice_total_seconds_; // 特征值窗口总时长
std::vector<int> slice_sensor_id_; // 每个时间窗是哪个传感器使用的
bool support_modification_;
std::map<int, int> sensor_id_nth_slice_; // 传感器编号与第几个波形发送窗口对应关系
std::map<uint16_t, int> short_addr_map_; // base_relation.json
// 空闲时间戳被占用
std::unordered_set<long> free_slice_ocuppied_;
// sensor config update
std::unordered_set<int> update_;
// sensor upgrade
std::map<int, UpgradeInfo> upgrade_;
// temp variables
long current_ts_; // 当前时间戳
long current_wave_start_ts_; // 当前所在的波形发送间隔的开始时间戳
long nth_wave_start_slice_; // 第几个波形发送窗口
long seconds_in_current_wave_slice_; // 时间戳相对波形开始时间戳的秒数
int nth_eigen_value_slice_; // 第几个特征值发送窗口
int seconds_in_current_eigen_slice_; // 相对特征值发送间隔的秒数
bool ts_in_eigen_slice_; // 时间位于特征值发送窗口中
int nth_eigen_slice_; // 如果ts_in_eigen_slice_是真的话此值表明是第几个特征值窗口
bool nth_wave_slice_; // 如果ts_in_eigen_slice_是假的话此值表明是第几个波形窗口
int current_request_;
};
typedef boost::container::dtl::singleton_default<SensorScheduler> scheduler;
#endif // SCHEDULE_HPP_