WLG/scheduler/schedule.hpp

160 lines
5.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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.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 enum {
kUpgradeSuccess = 0, // 成功
kProductTypeMismatch = 1, // 包有问题,不再重试
kZigbeeHWMismatch = 2, // 包有问题,不再重试
kTransmitFileCrcError = 3, // 此返回值时,要重试
kRecvDataLenError = 4, // 此返回值时,要重试
kUpgradeDoneBefore = 5 // 当前就是这个版本,不需要升级了
} FirmFileCheckResult;
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();
// kScheduleConfigSensor kScheduleUpgrade 等有结果,调我接口通知结果
// kScheduleEigenValue kScheduleWaveForm
// 上面4个结束调GetNextDuration()获取休眠时间
// 如果是kScheduleWrongTime, 此函数next_duration表明休眠时间
int StartSchedule(int short_addr, int &next_duration);
int GetNextDuration(int short_addr);
long GetBaseTimestamp(int id);
long CalcNextTimestamp(int id);
// 当有传感器需要更新配置时调用
int UpdateSensorConfig(int short_addr);
// 当更新结束后调用此接口result为0是成功其它值为失败
int UpdateConfigResult(int short_addr, int result);
/**
* @brief 当有传感器需要升级时调用
* sensor_type: DN101, DN102
* hw_version: 3, 4
* current_sw_version: 1.1
* upgrade_sw_version: 1.2
*/
int UpgradeSensor(int short_addr, std::string sensor_type, int hw_version,
std::string current_sw_version, std::string upgrade_sw_version);
/**
* @brief 升级后,无线传感器发回来的返回值
*
* @param short_addr
* @param result 参考FirmFileCheckResult
* 返回值表明操作是否成功0成功其它失败
*/
int UpgradeResult(int short_addr, int result);
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 short_addr, 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 short_addr);
private:
void UpdateUpgradeFile();
void UpdateConfigFile();
// 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_