add codes for update and upgrade.

This commit is contained in:
pandx 2024-10-31 18:50:23 +08:00
parent d1b714be78
commit 202da28673
2 changed files with 148 additions and 3 deletions

View File

@ -314,6 +314,64 @@ long SensorScheduler::CalcNextTimestamp(int id) {
return available_ts;
}
int SensorScheduler::UpdateSensorConfig(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 %d", short_addr);
return 1;
} else {
id = iter->second;
}
if (update_.count(id) > 0) {
return 0;
}
update_.insert(id);
UpdateConfigFile();
return 0;
}
int SensorScheduler::UpdateConfigResult(int short_addr, int result) {
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 %d", short_addr);
return 1;
} else {
id = iter->second;
}
if (result != 0) {
return 0;
}
update_.erase(id);
UpdateConfigFile();
return 0;
}
int SensorScheduler::UpgradeSensor(int short_addr, std::string sensor_type, int hw_version,
std::string current_sw_version, std::string upgrade_sw_version) {
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 %d", short_addr);
return 1;
} else {
id = iter->second;
}
UpgradeInfo info;
info.try_times = 0;
info.sensor_type = sensor_type;
info.hw_version = hw_version;
info.current_sw_version = current_sw_version;
info.upgrade_sw_version = upgrade_sw_version;
long ts = GetLocalTs();
info.submit_time = GetUTCTime(ts);
upgrade_[id] = info;
UpdateUpgradeFile();
return 0;
}
int SensorScheduler::GetAvailableId(int short_addr) {
int max_support_sensor[128] = {0};
for (auto it = short_addr_map_.begin(); it != short_addr_map_.end(); ++it) {
@ -463,9 +521,27 @@ int SensorScheduler::StartSchedule(int short_addr, int &next_duration) {
}
}
int SensorScheduler::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 SensorScheduler::UpgradeResult(int short_addr, int result) {
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 %d", short_addr);
return 1;
} else {
id = iter->second;
}
if (result == kUpgradeSuccess ||
result == kProductTypeMismatch ||
result == kZigbeeHWMismatch ||
result == kUpgradeDoneBefore) {
upgrade_.erase(id);
UpdateUpgradeFile();
}
return 0;
}
int SensorScheduler::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) {
if (!support_modification_) {
zlog_warn(zct, "not support modification");
return 1;
@ -580,3 +656,38 @@ std::string SensorScheduler::GetUTCTime(long ts) {
std::string world_time = str;
return world_time;
}
void SensorScheduler::UpdateConfigFile() {
Json::Value root;
for (auto item : update_) {
root.append(item);
}
Json::StyledStreamWriter streamWriter;
std::ofstream out_file(CONFIG_UPDATE);
streamWriter.write(out_file, root);
out_file.close();
}
void SensorScheduler::UpdateUpgradeFile() {
Json::Value root;
for (auto item : upgrade_) {
Json::Value upgrade_item;
upgrade_item["id"] = item.first;
upgrade_item["try_times"] = item.second.try_times;
upgrade_item["type"] = item.second.sensor_type;
upgrade_item["hw_version"] = item.second.hw_version;
upgrade_item["current_sw_version"] = item.second.current_sw_version;
upgrade_item["upgrade_sw_version"] = item.second.upgrade_sw_version;
upgrade_item["submit_time"] = item.second.submit_time;
Json::Value try_world_time;
for (auto entry : item.second.try_world_time1) {
try_world_time.append(entry);
}
upgrade_item["try_world_time"] = try_world_time;
root.append(upgrade_item);
}
Json::StyledStreamWriter streamWriter;
std::ofstream out_file(UPGRADE_CONFIG);
streamWriter.write(out_file, root);
out_file.close();
}

View File

@ -24,6 +24,15 @@ typedef enum {
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;
@ -49,6 +58,29 @@ public:
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);
@ -76,6 +108,8 @@ public:
int GetAvailableId(int short_addr);
private:
void UpdateUpgradeFile();
void UpdateConfigFile();
// user config
int eigen_value_send_interval_;