设备自动连接逻辑优化
This commit is contained in:
@@ -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,32 @@ 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 = "runStatus", 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);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@PostMapping("/onlineRegister")
|
||||
@ApiOperation("监测设备接入")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "projectId", value = "项目id", required = true),
|
||||
@ApiImplicitParam(name = "nDid", value = "设备识别码", required = true)
|
||||
})
|
||||
@ReturnMsg
|
||||
public HttpResult<String> onlineRegister(@RequestParam("projectId") String projectId,@RequestParam("nDid") String nDid){
|
||||
String methodDescribe = getMethodDescribe("onlineRegister");
|
||||
String result = csDeviceService.onlineRegister(projectId,nDid);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import java.util.List;
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/csLineLatestData")
|
||||
@Api(tags = "暂降事件")
|
||||
@Api(tags = "治理设备模块运行状态记录")
|
||||
@AllArgsConstructor
|
||||
public class CsLineLatestDataController extends BaseController {
|
||||
|
||||
|
||||
@@ -56,4 +56,6 @@ public interface ICsDeviceService {
|
||||
void wlAccess(String nDid);
|
||||
|
||||
String autoPortableLedger();
|
||||
|
||||
String onlineRegister(String projectId,String nDid);
|
||||
}
|
||||
|
||||
@@ -531,6 +531,113 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
|
||||
return csProjectPO.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public String onlineRegister(String projectId,String nDid) {
|
||||
String result = "fail";
|
||||
// 根据模板接入设备
|
||||
DeviceLogDTO logDto = new DeviceLogDTO();
|
||||
logDto.setUserName(RequestUtil.getUserNickname());
|
||||
logDto.setLoginName(RequestUtil.getUsername());
|
||||
logDto.setOperate("监测设备"+nDid+"注册、接入");
|
||||
logDto.setResult(1);
|
||||
try {
|
||||
// 设备状态判断
|
||||
checkDeviceStatus(nDid);
|
||||
// 询问设备支持的主题信息,并将支持的主题入库
|
||||
askAndStoreTopics(nDid);
|
||||
Thread.sleep(2000);
|
||||
// MQTT询问装置用的模板,并判断库中是否存在模板
|
||||
checkDeviceModel(nDid);
|
||||
Thread.sleep(2000);
|
||||
CsEquipmentDeliveryVO vo = equipmentFeignClient.queryEquipmentByndid(nDid).getData();
|
||||
List<CsLinePO> csLinePoList = new ArrayList<>();
|
||||
//1.录入装置台账信息
|
||||
//新增便携式设备
|
||||
CsLedgerParam csLedgerParam = new CsLedgerParam();
|
||||
csLedgerParam.setId(vo.getId());
|
||||
csLedgerParam.setPid(projectId);
|
||||
csLedgerParam.setName(vo.getName());
|
||||
csLedgerParam.setLevel(2);
|
||||
csLedgerParam.setSort(0);
|
||||
csLedgerService.addLedgerTree(csLedgerParam);
|
||||
//2.根据模板获取监测点个数,插入监测点表
|
||||
Thread.sleep(2000);
|
||||
List<CsModelDto> modelList = channelObjectUtil.objectToList(redisUtil.getObjectByKey(AppRedisKey.MODEL + nDid),CsModelDto.class);
|
||||
if (CollUtil.isEmpty(modelList)) {
|
||||
throwExceptionAndLog(nDid,AccessResponseEnum.MODEL_ERROR, logDto);
|
||||
}
|
||||
List<CsDataSet> list = csDataSetService.getDataSetData(modelList.get(0).getModelId());
|
||||
list.forEach(item->{
|
||||
CsLinePO po = new CsLinePO();
|
||||
po.setLineId(nDid + item.getClDev().toString());
|
||||
po.setName(item.getClDev().toString() + "#监测点");
|
||||
po.setStatus(1);
|
||||
po.setClDid(item.getClDev());
|
||||
po.setLineNo(item.getClDev());
|
||||
po.setRunStatus(0);
|
||||
po.setDeviceId(vo.getId());
|
||||
po.setDataSetId(item.getId());
|
||||
po.setDataModelId(item.getPid());
|
||||
//防止主键重复
|
||||
QueryWrapper<CsLinePO> qw = new QueryWrapper<>();
|
||||
qw.eq("line_id",po.getLineId());
|
||||
if(csLineService.getBaseMapper().selectList(qw).isEmpty()){
|
||||
csLinePoList.add(po);
|
||||
}
|
||||
//3.生成台账树监测点数据
|
||||
CsLedgerParam param = new CsLedgerParam();
|
||||
param.setId(nDid + item.getClDev().toString());
|
||||
param.setPid(vo.getId());
|
||||
param.setName(item.getClDev().toString() + "#监测点");
|
||||
param.setLevel(3);
|
||||
param.setSort(0);
|
||||
csLedgerService.addLedgerTree(param);
|
||||
});
|
||||
csLineService.saveBatch(csLinePoList);
|
||||
redisUtil.saveByKeyWithExpire("accessLineInfo:" + nDid,csLinePoList,30L);
|
||||
//4.生成装置和模板的关系表
|
||||
CsDevModelRelationAddParm csDevModelRelationAddParm = new CsDevModelRelationAddParm();
|
||||
csDevModelRelationAddParm.setDevId(vo.getId());
|
||||
csDevModelRelationAddParm.setModelId(modelList.get(0).getModelId());
|
||||
csDevModelRelationAddParm.setDid(modelList.get(0).getDid());
|
||||
csDevModelRelationService.addDevModelRelation(csDevModelRelationAddParm);
|
||||
//5.绑定装置和人的关系
|
||||
CsDeviceUserPO po = new CsDeviceUserPO();
|
||||
po.setPrimaryUserId(RequestUtil.getUserIndex());
|
||||
po.setStatus("1");
|
||||
po.setSubUserId(RequestUtil.getUserIndex());
|
||||
po.setDeviceId(vo.getId());
|
||||
csDeviceUserService.saveBatch(Collections.singletonList(po));
|
||||
|
||||
//发起自动接入请求
|
||||
Thread.sleep(2000);
|
||||
//先获取版本
|
||||
String version = "V1";
|
||||
devAccessAskTemplate(nDid,version,1);
|
||||
//6.修改流程,接入成功即为实际环境
|
||||
csEquipmentDeliveryService.updateProcessBynDid(nDid,4);
|
||||
//7.存储日志
|
||||
csLogsFeignClient.addUserLog(logDto);
|
||||
//9.删除redis监测点模板信息
|
||||
redisUtil.delete(AppRedisKey.MODEL + nDid);
|
||||
redisUtil.delete(AppRedisKey.LINE + nDid);
|
||||
//判断接入状态
|
||||
Thread.sleep(5000);
|
||||
Object object = redisUtil.getObjectByKey("online" + nDid);
|
||||
if (Objects.nonNull(object)) {
|
||||
result = "success";
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logDto.setResult(0);
|
||||
logDto.setFailReason(e.getMessage());
|
||||
csLogsFeignClient.addUserLog(logDto);
|
||||
resetFactory(nDid);
|
||||
throw new BusinessException(e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void checkDeviceStatus(String nDid) {
|
||||
DeviceLogDTO logDto = createLogDto("当前设备"+nDid+"状态判断");
|
||||
CsEquipmentDeliveryVO csEquipmentDeliveryVO = csEquipmentDeliveryService.queryEquipmentBynDid(nDid);
|
||||
@@ -542,7 +649,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
|
||||
throwExceptionAndLog(nDid,AccessResponseEnum.DEV_NOT_FIND, logDto);
|
||||
}
|
||||
String code = sysDicTreePo.getCode();
|
||||
if (!Objects.equals(code, DicDataEnum.PORTABLE.getCode())) {
|
||||
if (!Objects.equals(code, DicDataEnum.PORTABLE.getCode()) && !Objects.equals(code, DicDataEnum.DEV_CLD.getCode())) {
|
||||
throwExceptionAndLog(nDid,AccessResponseEnum.DEV_IS_NOT_PORTABLE, logDto);
|
||||
}
|
||||
if (!mqttUtil.judgeClientOnline("NJCN-" + nDid.substring(nDid.length() - 6))) {
|
||||
|
||||
Reference in New Issue
Block a user