设备一键恢复出厂设置功能

This commit is contained in:
2023-10-31 15:22:08 +08:00
parent 4e21813edf
commit 18186ae390
14 changed files with 290 additions and 62 deletions

View File

@@ -0,0 +1,32 @@
package com.njcn.access.pojo.param;
import lombok.Data;
/**
* 类的介绍:
*
* @author xuyang
* @version 1.0.0
* @createTime 2023/10/31 15:00
*/
@Data
public class DeviceStatusParam {
private String nDid;
/**
* 设备接入状态(1:未注册 2:注册 3:接入)
*/
private Integer status;
/**
* 设备运行状态(0:删除 1:离线 2:在线)
*/
private Integer runStatus;
/**
* 设备当前流程1:设备登记2功能调试3出厂调试 4设备投运)
*/
private Integer process;
}

View File

@@ -68,4 +68,14 @@ public class CsDeviceController extends BaseController {
csDeviceService.devAccess(devAccessParam);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
}
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@PostMapping("/resetFactory")
@ApiOperation("设备恢复出厂设置")
@ApiImplicitParam(name = "nDid", value = "设备id", required = true)
public HttpResult<String> resetFactory(@RequestParam String nDid){
String methodDescribe = getMethodDescribe("resetFactory");
csDeviceService.resetFactory(nDid);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
}
}

View File

@@ -92,13 +92,13 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
csDeviceService.devAccess(nDid,version);
//接入再次失败,则定时发起接入请求
Thread.sleep(1000);
Integer status = csEquipmentDeliveryService.queryEquipmentByndid(nDid).getRunStatus();
Integer status = csEquipmentDeliveryService.queryEquipmentBynDid(nDid).getRunStatus();
if (!Objects.isNull(status) && Objects.equals(status,AccessEnum.OFFLINE.getCode())){
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
ScheduledFuture<?> runnableFuture = executor.scheduleAtFixedRate(() -> {
log.info("定时发送接入请求...");
csDeviceService.devAccess(nDid, version);
Integer status2 = csEquipmentDeliveryService.queryEquipmentByndid(nDid).getRunStatus();
Integer status2 = csEquipmentDeliveryService.queryEquipmentBynDid(nDid).getRunStatus();
if (Objects.equals(status2,AccessEnum.ONLINE.getCode())){
throw new BusinessException(CommonResponseEnum.SUCCESS);
}

View File

@@ -0,0 +1,15 @@
package com.njcn.access.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
/**
*
* Description:
* Date: 2023/6/27 9:40【需求编号】
*
* @author clam
* @version V1.0.0
*/
public interface CsDeviceUserPOMapper extends BaseMapper<CsDeviceUserPO> {
}

View File

@@ -0,0 +1,19 @@
package com.njcn.access.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.njcn.csdevice.pojo.po.CsLinePO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
*
* Description:
* Date: 2023/5/18 14:01【需求编号】
*
* @author clam
* @version V1.0.0
*/
public interface CsLinePOMapper extends BaseMapper<CsLinePO> {
}

View File

@@ -1,47 +1,47 @@
//package com.njcn.access.runner;
//
//import com.njcn.access.service.ICsEquipmentDeliveryService;
//import com.njcn.access.service.ICsTopicService;
//import com.njcn.access.service.impl.CsDeviceServiceImpl;
//import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.ApplicationArguments;
//import org.springframework.boot.ApplicationRunner;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Objects;
//
///**
// * 类的介绍:用来重新发起设备的接入,存在程序意外停止了,缓存失效导致无法更新装置的状态,所以需要在程序启动时发起设备的接入
// *
// * @author xuyang
// * @version 1.0.0
// * @createTime 2023/8/28 13:57
// */
//@Component
//@Slf4j
//public class AccessApplicationRunner implements ApplicationRunner {
//
// @Resource
// private CsDeviceServiceImpl csDeviceService;
//
// @Resource
// private ICsTopicService csTopicService;
//
// @Resource
// private ICsEquipmentDeliveryService csEquipmentDeliveryService;
//
// @Override
// public void run(ApplicationArguments args){
// List<CsEquipmentDeliveryPO> list = csEquipmentDeliveryService.getAll();
// list.forEach(item->{
// String version = csTopicService.getVersion(item.getNdid());
// if (!Objects.isNull(version)){
// csDeviceService.devAccess(item.getNdid(),version);
// }
// });
// }
//
//}
package com.njcn.access.runner;
import com.njcn.access.service.ICsEquipmentDeliveryService;
import com.njcn.access.service.ICsTopicService;
import com.njcn.access.service.impl.CsDeviceServiceImpl;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
/**
* 类的介绍:用来重新发起设备的接入,存在程序意外停止了,缓存失效导致无法更新装置的状态,所以需要在程序启动时发起设备的接入
*
* @author xuyang
* @version 1.0.0
* @createTime 2023/8/28 13:57
*/
@Component
@Slf4j
public class AccessApplicationRunner implements ApplicationRunner {
@Resource
private CsDeviceServiceImpl csDeviceService;
@Resource
private ICsTopicService csTopicService;
@Resource
private ICsEquipmentDeliveryService csEquipmentDeliveryService;
@Override
public void run(ApplicationArguments args){
List<CsEquipmentDeliveryPO> list = csEquipmentDeliveryService.getAll();
list.forEach(item->{
String version = csTopicService.getVersion(item.getNdid());
if (!Objects.isNull(version)){
csDeviceService.devAccess(item.getNdid(),version);
}
});
}
}

View File

@@ -0,0 +1,20 @@
package com.njcn.access.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.vo.DevCountVO;
import com.njcn.csdevice.pojo.vo.DevUserVO;
import java.util.List;
/**
*
* Description:
* Date: 2023/6/27 9:40【需求编号】
*
* @author clam
* @version V1.0.0
*/
public interface CsDeviceUserPOService extends IService<CsDeviceUserPO>{
}

View File

@@ -0,0 +1,19 @@
package com.njcn.access.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.csdevice.pojo.param.CsLineParam;
import com.njcn.csdevice.pojo.po.CsLinePO;
import java.util.List;
/**
*
* Description:
* Date: 2023/5/18 14:01【需求编号】
*
* @author clam
* @version V1.0.0
*/
public interface CsLinePOService extends IService<CsLinePO>{
}

View File

@@ -30,4 +30,10 @@ public interface ICsDeviceService {
* @param devAccessParam
*/
void devAccess(DevAccessParam devAccessParam);
/**
* 设备恢复成出厂设置状态
* @param nDid
*/
void resetFactory(String nDid);
}

View File

@@ -3,6 +3,7 @@ package com.njcn.access.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.access.pojo.param.DeviceStatusParam;
import com.njcn.csdevice.pojo.param.CsEquipmentDeliveryAddParm;
import com.njcn.csdevice.pojo.param.CsEquipmentDeliveryAuditParm;
import com.njcn.csdevice.pojo.param.CsEquipmentDeliveryQueryParm;
@@ -45,14 +46,19 @@ public interface ICsEquipmentDeliveryService extends IService<CsEquipmentDeliver
/**
* 根据ndid查询装置信息
* @param ndid
* @param nDid
* @return
*/
CsEquipmentDeliveryVO queryEquipmentByndid(String ndid);
CsEquipmentDeliveryVO queryEquipmentBynDid(String nDid);
/**
* 获取所有装置信息
* @return
*/
List<CsEquipmentDeliveryPO> getAll();
/**
* 恢复出厂设置
*/
void devResetFactory(DeviceStatusParam param);
}

View File

@@ -2,9 +2,10 @@ package com.njcn.access.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.njcn.access.enums.AccessEnum;
import com.njcn.access.enums.AccessResponseEnum;
@@ -15,6 +16,7 @@ import com.njcn.access.pojo.dto.AccessDto;
import com.njcn.access.pojo.dto.AskDataDto;
import com.njcn.access.pojo.dto.CsModelDto;
import com.njcn.access.pojo.dto.ReqAndResDto;
import com.njcn.access.pojo.param.DeviceStatusParam;
import com.njcn.access.pojo.po.CsSoftInfoPO;
import com.njcn.access.service.*;
import com.njcn.access.utils.MqttUtil;
@@ -26,10 +28,7 @@ import com.njcn.csdevice.api.EquipmentFeignClient;
import com.njcn.csdevice.api.ProcessFeignClient;
import com.njcn.csdevice.pojo.param.CsDevModelRelationAddParm;
import com.njcn.csdevice.pojo.param.CsLedgerParam;
import com.njcn.csdevice.pojo.po.AppLineTopologyDiagramPO;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.po.CsEquipmentProcessPO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.pojo.po.*;
import com.njcn.csdevice.pojo.vo.CsEquipmentDeliveryVO;
import com.njcn.redis.pojo.enums.AppRedisKey;
import com.njcn.redis.utils.RedisUtil;
@@ -43,6 +42,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
@@ -93,12 +93,14 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
private final DicDataFeignClient dicDataFeignClient;
private final ICsSoftInfoService csSoftInfoService;
private final CsLogsFeignClient csLogsFeignClient;
private final ProcessFeignClient processFeignClient;
private final CsLinePOService csLinePOService;
private final CsDeviceUserPOService csDeviceUserPOService;
@Override
@Transactional(rollbackFor = {Exception.class})
public void devRegister(String nDid,Integer type) {
@@ -109,7 +111,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
logDto.setOperate("当前设备"+nDid+"状态判断");
logDto.setResult(1);
//1.判断nDid是否存在
CsEquipmentDeliveryVO csEquipmentDeliveryVO = equipmentFeignClient.queryEquipmentByndid(nDid).getData();
CsEquipmentDeliveryVO csEquipmentDeliveryVO = csEquipmentDeliveryService.queryEquipmentBynDid(nDid);
if (Objects.isNull(csEquipmentDeliveryVO.getNdid())){
logDto.setResult(0);
logDto.setFailReason(AccessResponseEnum.NDID_NO_FIND.getMessage());
@@ -309,6 +311,51 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
}
}
@Override
@Transactional(rollbackFor = {Exception.class})
public void resetFactory(String nDid) {
CsEquipmentDeliveryVO csEquipmentDeliveryVO = csEquipmentDeliveryService.queryEquipmentBynDid(nDid);
String devId = csEquipmentDeliveryVO.getId();
//修改装置状态
DeviceStatusParam param = new DeviceStatusParam();
param.setNDid(nDid);
param.setStatus(1);
param.setRunStatus(1);
param.setProcess(2);
csEquipmentDeliveryService.devResetFactory(param);
//清除关系表
QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();
/**
* 删除设备
*/
csLedgerQueryWrapper.clear();
csLedgerQueryWrapper.eq("id",devId);
csLedgerService.remove(csLedgerQueryWrapper);
csLedgerQueryWrapper.clear();
csLedgerQueryWrapper.eq("pid",devId);
List<CsLedger> list = csLedgerService.list(csLedgerQueryWrapper);
List<String> collect = list.stream().map(CsLedger::getId).collect(Collectors.toList());
csLedgerService.remove(csLedgerQueryWrapper);
QueryWrapper<CsDevModelRelationPO> csDevModelRelationPOQueryWrapper = new QueryWrapper<>();
csDevModelRelationPOQueryWrapper.clear();
csDevModelRelationPOQueryWrapper.eq("dev_id",devId);
csDevModelRelationService.remove(csDevModelRelationPOQueryWrapper);
QueryWrapper<CsDeviceUserPO> csDeviceUserPOQueryWrapper = new QueryWrapper<>();
csDeviceUserPOQueryWrapper.clear();
csDeviceUserPOQueryWrapper.eq("device_id",devId);
csDeviceUserPOService.remove(csDeviceUserPOQueryWrapper);
if (!CollectionUtils.isEmpty(collect)) {
QueryWrapper<CsLinePO> csLinePOQueryWrapper = new QueryWrapper<>();
csLinePOQueryWrapper.clear();
csLinePOQueryWrapper.in("line_id",collect);
csLinePOService.remove(csLinePOQueryWrapper);
QueryWrapper<AppLineTopologyDiagramPO> appLineTopologyDiagramPOQueryWrapper = new QueryWrapper<>();
appLineTopologyDiagramPOQueryWrapper.clear();
appLineTopologyDiagramPOQueryWrapper.in("line_id",collect);
appLineTopologyDiagramService.remove(appLineTopologyDiagramPOQueryWrapper);
}
}
public void devAccess(String nDid,String version) {
ReqAndResDto.Req reqAndResParam = new ReqAndResDto.Req();
reqAndResParam.setMid(1);

View File

@@ -0,0 +1,22 @@
package com.njcn.access.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.access.mapper.CsDeviceUserPOMapper;
import com.njcn.access.service.CsDeviceUserPOService;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
*
* Description:
* Date: 2023/6/27 9:40【需求编号】
*
* @author clam
* @version V1.0.0
*/
@Service
@RequiredArgsConstructor
public class CsDeviceUserPOServiceImpl extends ServiceImpl<CsDeviceUserPOMapper, CsDeviceUserPO> implements CsDeviceUserPOService {
}

View File

@@ -1,9 +1,9 @@
package com.njcn.access.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.access.mapper.CsEquipmentDeliveryMapper;
import com.njcn.access.pojo.param.DeviceStatusParam;
import com.njcn.access.service.ICsEquipmentDeliveryService;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.csdevice.pojo.vo.CsEquipmentDeliveryVO;
@@ -49,9 +49,9 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
}
@Override
public CsEquipmentDeliveryVO queryEquipmentByndid(String ndid) {
public CsEquipmentDeliveryVO queryEquipmentBynDid(String nDid) {
CsEquipmentDeliveryVO result = new CsEquipmentDeliveryVO();
CsEquipmentDeliveryPO csEquipmentDeliveryPo = lambdaQuery().eq(CsEquipmentDeliveryPO::getNdid,ndid).ne(CsEquipmentDeliveryPO::getRunStatus,0).one();
CsEquipmentDeliveryPO csEquipmentDeliveryPo = lambdaQuery().eq(CsEquipmentDeliveryPO::getNdid,nDid).ne(CsEquipmentDeliveryPO::getRunStatus,0).one();
if(Objects.isNull (csEquipmentDeliveryPo)){
return result;
}
@@ -64,4 +64,14 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
return this.lambdaQuery().ne(CsEquipmentDeliveryPO::getRunStatus,0).list();
}
@Override
public void devResetFactory(DeviceStatusParam param) {
LambdaUpdateWrapper<CsEquipmentDeliveryPO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(CsEquipmentDeliveryPO::getNdid,param.getNDid())
.set(CsEquipmentDeliveryPO::getRunStatus,param.getRunStatus())
.set(CsEquipmentDeliveryPO::getStatus,param.getStatus())
.set(CsEquipmentDeliveryPO::getProcess,param.getProcess());
this.update(lambdaUpdateWrapper);
}
}

View File

@@ -0,0 +1,22 @@
package com.njcn.access.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.access.mapper.CsLinePOMapper;
import com.njcn.access.service.CsLinePOService;
import com.njcn.csdevice.pojo.po.CsLinePO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
*
* Description:
* Date: 2023/5/18 14:01【需求编号】
*
* @author clam
* @version V1.0.0
*/
@Service
@RequiredArgsConstructor
public class CsLinePOServiceImpl extends ServiceImpl<CsLinePOMapper, CsLinePO> implements CsLinePOService {
}