package com.dy.rtuMw.server.upgrade;
|
|
import com.dy.common.util.Callback;
|
|
import java.util.List;
|
|
/**
|
* @Author: liurunyu
|
* @Date: 2024/11/4 16:03
|
* @Description
|
*/
|
public class UpgradeManager {
|
|
private static final UpgradeManager INSTANCE = new UpgradeManager();
|
|
private Integer failTryTimes ;//升级失败后,重新偿试升级次数,0表示不重新偿试升级
|
private Integer ugMaxRtuSameTime ;//同时升级RTU最大个数
|
|
private UpgradeTask task ;//升级任务
|
|
private boolean taskIsOver = false ;//升级任务是否结束
|
private int triggerTimes = 0 ;//连接触发次数
|
|
private UpgradeManager(){}
|
|
public static UpgradeManager getInstance() {
|
return UpgradeManager.INSTANCE;
|
}
|
|
/**
|
* 初始化配置信息
|
*/
|
public void initOption(UpgradeUnitConfigVo configVo) {
|
this.failTryTimes = configVo.failTryTimes;
|
this.ugMaxRtuSameTime = configVo.ugMaxRtuSameTime;
|
}
|
|
/**
|
* 设置升级任务
|
* @param softFileName 升级程序文件名
|
* @param softStoreAddr 升级程序存放地址
|
* @param softStartAddr 程序覆盖起始地址
|
* @param softFileData 升级程序字节数组
|
* @param softBytesCalculate 升级程序字节数(按公式计算)
|
* @param rtuAddrList 升级RTU
|
* @throws Exception
|
*/
|
public void setUpgradeTask(String softFileName,
|
String softStoreAddr,
|
String softStartAddr,
|
byte[] softFileData,
|
Integer softBytesCalculate,
|
List<String> rtuAddrList) throws Exception {
|
if(this.task != null && !this.task.isOver()){
|
throw new Exception("当前存在升级任务,请等待当前任务执行完或强制停止当前任务");
|
}else{
|
this.task.forceOver();
|
this.task = new UpgradeTask();
|
this.task.initOption(this.failTryTimes, this.ugMaxRtuSameTime);
|
this.task.setTask(softFileName,
|
softStoreAddr,
|
softStartAddr,
|
softFileData,
|
softBytesCalculate,
|
rtuAddrList);
|
}
|
}
|
|
/**
|
* 停止当前升级任务
|
* @throws Exception
|
*/
|
public void stopUpgradeTask() throws Exception {
|
if(this.task != null){
|
this.task.forceOver();
|
}
|
this.task = null ;
|
}
|
|
/**
|
* RTU有上行数据了,触发下发升级数据
|
* @param rtuAddr
|
* @param code
|
* @param protocolName
|
* @param protocolVersion
|
* @param callback
|
*/
|
public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callback){
|
if(task != null && !taskIsOver){
|
triggerTimes ++ ;
|
if(triggerTimes == 100){
|
triggerTimes = 0 ;
|
if(this.task.isOver()){
|
taskIsOver = true ;
|
}
|
}
|
if(!taskIsOver){
|
this.task.trigger(rtuAddr, code, protocolName, protocolVersion, callback);
|
}
|
}
|
}
|
|
////////////////////////////////////////////////////
|
//
|
// 查询升级状态信息
|
//
|
////////////////////////////////////////////////////
|
/**
|
* 当前升级状态
|
* @return
|
*/
|
public UpgradeState currentUpgradeState() {
|
if(task != null){
|
return task.currentUpgradeState() ;
|
}else{
|
return null ;
|
}
|
}
|
|
/**
|
* Rtu升级信息
|
* @param rtuAddr
|
* @return
|
*/
|
public UpgradeRtu upgradeInfos(String rtuAddr){
|
if(task != null){
|
return task.upgradeInfos(rtuAddr) ;
|
}else{
|
return null ;
|
}
|
}
|
|
/**
|
* Rtu升级信息
|
* @param rtuAddrList
|
* @return
|
*/
|
public List<UpgradeRtu> upgradeInfos(List<String> rtuAddrList){
|
if(task != null){
|
return task.upgradeInfos(rtuAddrList) ;
|
}else{
|
return null ;
|
}
|
}
|
|
|
}
|