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>
|
2024-11-05 14:44:08 +08:00
|
|
|
|
#include "upgrade_cfg.hpp"
|
2024-10-29 19:53:09 +08:00
|
|
|
|
|
2024-11-05 14:44:08 +08:00
|
|
|
|
#define SCHEDULE_CONFIG "/opt/configenv/schedule.json"
|
2024-10-29 19:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
|
kScheduleResultNone = 0,
|
|
|
|
|
|
kScheduleUnknownSensor = 1,
|
|
|
|
|
|
kScheduleConfigSensor = 2,
|
|
|
|
|
|
kScheduleEigenValue = 3,
|
|
|
|
|
|
kScheduleWaveForm = 4,
|
|
|
|
|
|
kScheduleUpgrade = 5,
|
|
|
|
|
|
kScheduleWrongTime = 6
|
|
|
|
|
|
} ScheduleResult;
|
|
|
|
|
|
|
2024-10-31 18:50:23 +08:00
|
|
|
|
typedef enum {
|
|
|
|
|
|
kUpgradeSuccess = 0, // 成功
|
|
|
|
|
|
kProductTypeMismatch = 1, // 包有问题,不再重试
|
|
|
|
|
|
kZigbeeHWMismatch = 2, // 包有问题,不再重试
|
|
|
|
|
|
kTransmitFileCrcError = 3, // 此返回值时,要重试
|
|
|
|
|
|
kRecvDataLenError = 4, // 此返回值时,要重试
|
|
|
|
|
|
kUpgradeDoneBefore = 5 // 当前就是这个版本,不需要升级了
|
|
|
|
|
|
} FirmFileCheckResult;
|
|
|
|
|
|
|
2024-10-29 19:53:09 +08:00
|
|
|
|
class SensorScheduler {
|
|
|
|
|
|
public:
|
2024-11-05 14:44:08 +08:00
|
|
|
|
SensorScheduler();
|
2024-10-29 19:53:09 +08:00
|
|
|
|
|
2024-10-30 21:17:33 +08:00
|
|
|
|
// kScheduleConfigSensor kScheduleUpgrade 等有结果,调我接口通知结果
|
|
|
|
|
|
// kScheduleEigenValue kScheduleWaveForm
|
|
|
|
|
|
// 上面4个结束,调GetNextDuration()获取休眠时间
|
|
|
|
|
|
// 如果是kScheduleWrongTime, 此函数next_duration表明休眠时间
|
|
|
|
|
|
int StartSchedule(int short_addr, int &next_duration);
|
|
|
|
|
|
int GetNextDuration(int short_addr);
|
|
|
|
|
|
|
2024-10-30 18:10:50 +08:00
|
|
|
|
long GetBaseTimestamp(int id);
|
2024-10-29 19:53:09 +08:00
|
|
|
|
long CalcNextTimestamp(int id);
|
|
|
|
|
|
|
2024-10-31 18:50:23 +08:00
|
|
|
|
// 当有传感器需要更新配置时调用
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2024-10-29 19:53:09 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
2024-11-05 17:44:38 +08:00
|
|
|
|
|
|
|
|
|
|
int GetScheduleConfig(int &eigen_value_send_interval, int &wave_form_send_interval,
|
|
|
|
|
|
int &eigen_value_send_duration, int &wave_form_send_duration,
|
|
|
|
|
|
int &max_sensor_num);
|
2024-10-29 19:53:09 +08:00
|
|
|
|
// ======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);
|
2024-10-30 21:17:33 +08:00
|
|
|
|
int GetAvailableId(int short_addr);
|
2024-10-29 19:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
2024-10-31 19:01:42 +08:00
|
|
|
|
void UpdateUpgradeInfo(int id);
|
2024-10-29 19:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 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_
|