MQTT通讯功能联调

This commit is contained in:
2023-08-09 20:30:54 +08:00
parent e35b975609
commit 3412b0f0af
16 changed files with 430 additions and 81 deletions

View File

@@ -0,0 +1,16 @@
package com.njcn.access.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.access.pojo.po.CsSoftInfoPO;
/**
* <p>
* 系统软件表 服务类
* </p>
*
* @author xuyang
* @since 2023-08-09
*/
public interface ICsSoftInfoService extends IService<CsSoftInfoPO> {
}

View File

@@ -870,7 +870,7 @@ public class CsDevModelServiceImpl implements ICsDevModelService {
CsLineModel csLineModel = new CsLineModel();
csLineModel.setLineId(IdUtil.fastSimpleUUID());
csLineModel.setPid(pId);
//fixme 默认第一个监测点是负载侧,第二个是电网测
//fixme 默认第一个监测点是负载侧,第二个是电网测,后期数据错误可以在移动端调整
if (Objects.equals(item.getClDid(),1)){
csLineModel.setName("负载侧监测点");
csLineModel.setPosition(dicDataFeignClient.getDicDataByCode(DicDataEnum.LOAD_SIDE.getCode()).getData().getId());

View File

@@ -1,15 +1,21 @@
package com.njcn.access.service.impl;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.njcn.access.enums.AccessEnum;
import com.njcn.access.enums.AccessResponseEnum;
import com.njcn.access.enums.TypeEnum;
import com.njcn.access.param.DevAccessParam;
import com.njcn.access.pojo.RspDataDto;
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.po.CsSoftInfoPO;
import com.njcn.access.service.ICsDeviceService;
import com.njcn.access.service.ICsSoftInfoService;
import com.njcn.access.service.ICsTopicService;
import com.njcn.access.utils.MqttUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
@@ -23,6 +29,7 @@ import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.po.CsLinePO;
import com.njcn.csdevice.pojo.vo.CsEquipmentDeliveryVO;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.SysDicTreePO;
@@ -30,10 +37,10 @@ import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -74,6 +81,10 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
private final ICsTopicService csTopicService;
private final DicDataFeignClient dicDataFeignClient;
private final ICsSoftInfoService csSoftInfoService;
@Override
@Transactional(rollbackFor = {Exception.class})
public void devRegister(String nDid) {
@@ -130,6 +141,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
@Transactional(rollbackFor = {Exception.class})
public void devAccess(DevAccessParam devAccessParam) {
try {
String version = csTopicService.getVersion(devAccessParam.getNDid());
CsEquipmentDeliveryVO vo = equipmentFeignClient.queryEquipmentByndid(devAccessParam.getNDid()).getData();
List<CsLinePO> csLinePoList = new ArrayList<>();
List<AppLineTopologyDiagramPO> appLineTopologyDiagramPoList = new ArrayList<>();
@@ -141,18 +153,55 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
csLedgerParam.setLevel(2);
csLedgerParam.setSort(0);
csLedgerFeignClient.add(csLedgerParam);
//2.监测点表录入关系
//todo 获取逻辑设备信息更新监测点pt、ct相关信息 LdevInfo
devAccessParam.getList().forEach(item->{
List<CsModelDto> modelId = objectToList(redisUtil.getObjectByKey("MODEL" + devAccessParam.getNDid()));
Integer clDid = null;
//2.新增装置-模板关系、获取电能质量的逻辑设备id
for (CsModelDto item : modelId) {
CsDevModelRelationAddParm csDevModelRelationAddParm = new CsDevModelRelationAddParm();
csDevModelRelationAddParm.setDevId(vo.getId());
csDevModelRelationAddParm.setModelId(item.getModelId());
csDevModelRelationAddParm.setDid(item.getDid());
devModelRelationFeignClient.addDevModelRelation(csDevModelRelationAddParm);
if (Objects.equals(item.getType(),1)){
clDid = item.getDid();
}
}
if (Objects.isNull(clDid)){
throw new BusinessException(AccessResponseEnum.CLDID_IS_NULL);
}
askDevData(devAccessParam.getNDid(),AccessEnum.L_DEV_INFO.getCode(),version,clDid);
List<RspDataDto.LdevInfo> list = new ArrayList<>();
try {
Thread.sleep(500);
String key = "LINEDATA" + devAccessParam.getNDid();
list = objectToList2(redisUtil.getObjectByKey(key));
if (CollectionUtils.isEmpty(list)){
throw new BusinessException(AccessResponseEnum.LDEVINFO_IS_NULL);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//3.监测点表录入关系
for (DevAccessParam.LineParam item : devAccessParam.getList()) {
String location = dicDataFeignClient.getDicDataById(item.getPosition()).getData().getCode();
String id = IdUtil.fastSimpleUUID();
CsLinePO po = new CsLinePO();
po.setLineId(id);
po.setName(item.getName());
po.setPosition(item.getPosition());
//todo 目前电压等级、ct、pt数据不确定 后期补
// po.setVolGrade(item.getVolGrade());
// po.setPtRatio(item.getPtRatio());
// po.setCtRatio(item.getCtRatio());
if (Objects.equals(DicDataEnum.LOAD_SIDE.getCode(),location)){
RspDataDto.LdevInfo po1 = list.stream().filter(s -> Objects.equals(s.getClDid(),1)).findFirst().orElse(null);
po.setVolGrade(po1.getVolGrade());
po.setPtRatio(po1.getPtRatio());
po.setCtRatio(po1.getCtRatio());
po.setConType(po1.getConType());
} else if (Objects.equals(DicDataEnum.GRID_SIDE.getCode(),location)){
RspDataDto.LdevInfo po1 = list.stream().filter(s -> Objects.equals(s.getClDid(),2)).findFirst().orElse(null);
po.setVolGrade(po1.getVolGrade());
po.setPtRatio(po1.getPtRatio());
po.setCtRatio(po1.getCtRatio());
po.setConType(po1.getConType());
}
po.setStatus(1);
csLinePoList.add(po);
CsLedgerParam param = new CsLedgerParam();
@@ -169,20 +218,10 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
appLineTopologyDiagramPo.setLng(item.getLng());
appLineTopologyDiagramPo.setStatus("1");
appLineTopologyDiagramPoList.add(appLineTopologyDiagramPo);
});
}
csLineFeignClient.addLineList(csLinePoList);
//3.监测点拓扑图表录入关系
//4.监测点拓扑图表录入关系
csLineTopologyFeignClient.addList(appLineTopologyDiagramPoList);
//4.新增装置-模板关系
List<CsModelDto> modelId = objectToList(redisUtil.getObjectByKey("MODEL" + devAccessParam.getNDid()));
modelId.forEach(item->{
CsDevModelRelationAddParm csDevModelRelationAddParm = new CsDevModelRelationAddParm();
csDevModelRelationAddParm.setDevId(vo.getId());
csDevModelRelationAddParm.setModelId(item.getModelId());
csDevModelRelationAddParm.setDid(item.getDid());
devModelRelationFeignClient.addDevModelRelation(csDevModelRelationAddParm);
});
redisUtil.delete("MODEL" + devAccessParam.getNDid());
//5.修改装置状态
equipmentFeignClient.updateStatusBynDid(devAccessParam.getNDid(), AccessEnum.REGISTERED.getCode());
//6.绑定装置和人的关系
@@ -192,14 +231,33 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
po.setSubUserId(RequestUtil.getUserIndex());
po.setDeviceId(vo.getId());
csDeviceUserFeignClient.add(Collections.singletonList(po));
//7.删除redis监测点模板信息
redisUtil.delete("LINE" + devAccessParam.getNDid());
//todo 录入软件信息 SoftInfo
askDevData(devAccessParam.getNDid(),AccessEnum.SOFT_INFO.getCode(),version,0);
try {
Thread.sleep(500);
String key = "SOFTINFO" + devAccessParam.getNDid();
RspDataDto.SoftInfo softInfo = JSON.parseObject(JSON.toJSONString(redisUtil.getObjectByKey(key)), RspDataDto.SoftInfo.class);
if (Objects.isNull(softInfo)){
throw new BusinessException(AccessResponseEnum.SOFTINFO_IS_NULL);
}
//记录设备软件信息
CsSoftInfoPO csSoftInfoPo = new CsSoftInfoPO();
BeanUtils.copyProperties(softInfo,csSoftInfoPo);
csSoftInfoService.save(csSoftInfoPo);
//更新设备表软件信息
equipmentFeignClient.updateSoftInfoBynDid(devAccessParam.getNDid(),csSoftInfoPo.getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
//todo 9.记录注册日志
//删除redis监测点模板信息
redisUtil.delete("MODEL" + devAccessParam.getNDid());
redisUtil.delete("LINE" + devAccessParam.getNDid());
redisUtil.delete("LINEDATA" + devAccessParam.getNDid());
redisUtil.delete("SOFTINFO" + devAccessParam.getNDid());
//发起自动接入请求
devAccess(devAccessParam.getNDid());
//devAccess(devAccessParam.getNDid(),version);
//todo 10.记录接入日志
} catch (Exception e) {
@@ -208,8 +266,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
}
public void devAccess(String nDid) {
String version = csTopicService.getVersion(nDid);
public void devAccess(String nDid,String version) {
ReqAndResDto.Req reqAndResParam = new ReqAndResDto.Req();
reqAndResParam.setMid(1);
reqAndResParam.setDid(0);
@@ -263,4 +320,42 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
return urlList;
}
public List<RspDataDto.LdevInfo> objectToList2(Object object) {
List<RspDataDto.LdevInfo> urlList = new ArrayList<>();
if (object != null) {
if (object instanceof ArrayList<?>) {
for (Object o : (List<?>) object) {
urlList.add((RspDataDto.LdevInfo) o);
}
}
}
return urlList;
}
/**
* 平台向设备发送数据命令
*/
public void askDevData(String nDid,Integer dataType,String version,Integer did){
ReqAndResDto.Req reqAndResParam = new ReqAndResDto.Req();
reqAndResParam.setMid(1);
reqAndResParam.setPri(AccessEnum.FIRST_CHANNEL.getCode());
reqAndResParam.setType(Integer.parseInt(TypeEnum.TYPE_6.getCode()));
reqAndResParam.setExpire(-1);
AskDataDto askDataDto = new AskDataDto();
askDataDto.setClDid(-1);
askDataDto.setDataAttr(0);
askDataDto.setOperate(1);
askDataDto.setStartTime(-1);
askDataDto.setEndTime(-1);
if (Objects.equals(dataType,AccessEnum.SOFT_INFO.getCode())){
reqAndResParam.setDid(did);
askDataDto.setDataType(1);
} else if (Objects.equals(dataType,AccessEnum.L_DEV_INFO.getCode())){
reqAndResParam.setDid(did);
askDataDto.setDataType(2);
}
reqAndResParam.setMsg(askDataDto);
publisher.send("/Pfm/DevCmd/"+version+"/"+nDid, PubUtils.obj2json(reqAndResParam),1,false);
}
}

View File

@@ -0,0 +1,20 @@
package com.njcn.access.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.access.mapper.CsSoftInfoMapper;
import com.njcn.access.pojo.po.CsSoftInfoPO;
import com.njcn.access.service.ICsSoftInfoService;
import org.springframework.stereotype.Service;
/**
* <p>
* 系统软件表 服务实现类
* </p>
*
* @author xuyang
* @since 2023-08-09
*/
@Service
public class CsSoftInfoServiceImpl extends ServiceImpl<CsSoftInfoMapper, CsSoftInfoPO> implements ICsSoftInfoService {
}