3 Commits

Author SHA1 Message Date
xy
6b24e49651 设备自动连接逻辑优化 2026-04-08 10:18:07 +08:00
xy
b3d2727a64 设备接入优化 2026-04-08 09:26:52 +08:00
xy
441b5d04fe 微调 2026-04-07 14:22:13 +08:00
10 changed files with 90 additions and 13 deletions

View File

@@ -0,0 +1,20 @@
package com.njcn.access.api;
import com.njcn.access.api.fallback.CsDeviceClientFallbackFactory;
import com.njcn.common.pojo.constant.ServerInfo;
import com.njcn.common.pojo.response.HttpResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author xy
*/
@FeignClient(value = ServerInfo.ACCESS_BOOT, path = "/device", fallbackFactory = CsDeviceClientFallbackFactory.class,contextId = "device")
public interface CsDeviceFeignClient {
@PostMapping("/updateRunStatus")
HttpResult<String> updateRunStatus(@RequestParam("nDid") String nDid, @RequestParam("runStatus") Integer runStatus);
}

View File

@@ -0,0 +1,34 @@
package com.njcn.access.api.fallback;
import com.njcn.access.api.CsDeviceFeignClient;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author xy
*/
@Slf4j
@Component
public class CsDeviceClientFallbackFactory implements FallbackFactory<CsDeviceFeignClient> {
@Override
public CsDeviceFeignClient create(Throwable cause) {
//判断抛出异常是否为解码器抛出的业务异常
Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
if (cause.getCause() instanceof BusinessException) {
BusinessException businessException = (BusinessException) cause.getCause();
}
Enum<?> finalExceptionEnum = exceptionEnum;
return new CsDeviceFeignClient() {
@Override
public HttpResult<String> updateRunStatus(String nDid, Integer runStatus) {
log.error("{}异常,降级处理,异常为:{}","云设备状态调整异常",cause.toString());
throw new BusinessException(finalExceptionEnum);
}
};
}
}

View File

@@ -2,6 +2,7 @@ package com.njcn.access.controller;
import com.njcn.access.param.DevAccessParam;
import com.njcn.access.service.ICsDeviceService;
import com.njcn.access.service.ICsEquipmentDeliveryService;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
@@ -34,6 +35,7 @@ import org.springframework.web.bind.annotation.*;
public class CsDeviceController extends BaseController {
private final ICsDeviceService csDeviceService;
private final ICsEquipmentDeliveryService csEquipmentDeliveryService;
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@PostMapping("/register")
@@ -130,4 +132,17 @@ public class CsDeviceController extends BaseController {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
}
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@PostMapping("/updateRunStatus")
@ApiOperation("设备状态调整")
@ApiImplicitParams({
@ApiImplicitParam(name = "nDid", value = "设备识别码", required = true),
@ApiImplicitParam(name = "status", value = "状态", required = true)
})
public HttpResult<String> updateRunStatus(@RequestParam String nDid, @RequestParam Integer runStatus){
String methodDescribe = getMethodDescribe("updateRunStatus");
csEquipmentDeliveryService.updateRunStatusBynDid(nDid,runStatus);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
}
}

View File

@@ -32,7 +32,7 @@ import java.util.List;
@Slf4j
@RestController
@RequestMapping("/csLineLatestData")
@Api(tags = "暂降事件")
@Api(tags = "治理设备模块运行状态记录")
@AllArgsConstructor
public class CsLineLatestDataController extends BaseController {

View File

@@ -312,7 +312,7 @@ public class MqttMessageHandler {
if (Objects.equals(res.getCode(),AccessEnum.SUCCESS.getCode())){
int mid = 1;
//修改装置状态
csEquipmentDeliveryService.updateStatusBynDid(nDid,AccessEnum.ACCESS.getCode());
csEquipmentDeliveryService.updateStatusBynDid(nDid,AccessEnum.ACCESS.getCode(),null,null);
csEquipmentDeliveryService.updateRunStatusBynDid(nDid,AccessEnum.ONLINE.getCode());
//记录设备上线
PqsCommunicateDto dto = new PqsCommunicateDto();

View File

@@ -140,7 +140,7 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
//装置下线
csEquipmentDeliveryService.updateRunStatusBynDid(nDid, AccessEnum.OFFLINE.getCode());
//装置调整为注册状态
csEquipmentDeliveryService.updateStatusBynDid(nDid,AccessEnum.REGISTERED.getCode());
csEquipmentDeliveryService.updateStatusBynDid(nDid,AccessEnum.REGISTERED.getCode(),null,null);
logDto.setOperate(nDid +"装置离线");
sendMessage(nDid);
//记录装置掉线时间

View File

@@ -22,7 +22,7 @@ public interface ICsEquipmentDeliveryService extends IService<CsEquipmentDeliver
* 根据网关id修改装置的状态
* @param nDid 网关id
*/
void updateStatusBynDid(String nDid,Integer status);
void updateStatusBynDid(String nDid,Integer status,String engineeringId, String projectId);
/**
* 根据网关id修改软件信息

View File

@@ -295,8 +295,8 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
po.setSubUserId(RequestUtil.getUserIndex());
po.setDeviceId(vo.getId());
csDeviceUserService.saveBatch(Collections.singletonList(po));
//6.修改装置状态
csEquipmentDeliveryService.updateStatusBynDid(devAccessParam.getNDid(), AccessEnum.REGISTERED.getCode());
//6.修改装置状态;修改设备接入的工程、项目
csEquipmentDeliveryService.updateStatusBynDid(devAccessParam.getNDid(), AccessEnum.REGISTERED.getCode(),devAccessParam.getEngineeringId(), devAccessParam.getProjectId());
//7.发起自动接入请求
devAccessAskTemplate(devAccessParam.getNDid(),version,1);
//8.删除redis监测点模板信息
@@ -323,10 +323,6 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
csMarketDataFeignClient.insertData(userVO.getId(), devAccessParam.getEngineeringId());
}
}
//12.如果设备接入,发现接入的工程项目和之前预设的工程项目不一致,则需要更新原来的预设,使用接入的工程项目
if (!Objects.equals(vo.getAssociatedEngineering(),devAccessParam.getEngineeringId()) || !Objects.equals(vo.getAssociatedProject(),devAccessParam.getProjectId())) {
equipmentFeignClient.updateLedger(devAccessParam.getNDid(), devAccessParam.getEngineeringId(), devAccessParam.getProjectId());
}
} catch (Exception e) {
logDto.setResult(0);
logDto.setFailReason(e.getMessage());
@@ -528,7 +524,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
param.setDescription("便携式项目");
param.setTopoIds(Collections.singletonList("99ed9b9c8cf9007cc4d2ac4c7127b7e4"));
param.setSort(Integer.MAX_VALUE);
csProjectPO = appProjectFeignClient.addAppProject(param).getData();
csProjectPO = appProjectFeignClient.addPortableProject(param).getData();
}
//修改已存在的便携式设备
csLedgerService.updatePortableLedger(csEngineeringPO.getId(),csProjectPO.getId());

View File

@@ -41,9 +41,15 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
private final CsLogsFeignClient csLogsFeignClient;
@Override
public void updateStatusBynDid(String nDid,Integer status) {
public void updateStatusBynDid(String nDid,Integer status,String engineeringId, String projectId) {
LambdaUpdateWrapper<CsEquipmentDeliveryPO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.set(CsEquipmentDeliveryPO::getStatus,status).eq(CsEquipmentDeliveryPO::getNdid,nDid);
if (engineeringId != null && !engineeringId.isEmpty()) {
lambdaUpdateWrapper.set(CsEquipmentDeliveryPO::getAssociatedEngineering,engineeringId);
}
if (projectId != null && !projectId.isEmpty()) {
lambdaUpdateWrapper.set(CsEquipmentDeliveryPO::getAssociatedProject,projectId);
}
this.update(lambdaUpdateWrapper);
}

View File

@@ -2,7 +2,9 @@ package com.njcn.stat.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.njcn.access.api.CsDeviceFeignClient;
import com.njcn.access.api.CsLineLatestDataFeignClient;
import com.njcn.access.enums.AccessEnum;
import com.njcn.access.pojo.po.CsLineLatestData;
import com.njcn.access.utils.ChannelObjectUtil;
import com.njcn.common.pojo.exception.BusinessException;
@@ -57,6 +59,7 @@ public class StatServiceImpl implements IStatService {
private final RedisUtil redisUtil;
private final ChannelObjectUtil channelObjectUtil;
private final CsLineLatestDataFeignClient csLineLatestDataFeignClient;
private final CsDeviceFeignClient csDeviceFeignClient;
private static final Map<String, String> PHASE_MAPPING = new HashMap<String, String>() {{
put("AB", "A");
put("BC", "B");
@@ -106,7 +109,6 @@ public class StatServiceImpl implements IStatService {
//云前置设备
else if (Objects.equals(DicDataEnum.DEV_CLD.getCode(),code)) {
lineId = appAutoDataMessage.getId() + appAutoDataMessage.getMsg().getClDid();
}
//获取当前设备信息
@@ -159,6 +161,10 @@ public class StatServiceImpl implements IStatService {
csLineLatestData.setTimeId(Objects.isNull(time) ? LocalDateTime.now() : time);
csLineLatestDataFeignClient.addData(csLineLatestData);
}
//判断设备运行状态
if (!Objects.isNull(po.getRunStatus()) && po.getRunStatus() == 1) {
csDeviceFeignClient.updateRunStatus(appAutoDataMessage.getId(), AccessEnum.ONLINE.getCode());
}
}
System.gc();
}