MQTT通讯功能联调

This commit is contained in:
2023-08-07 20:07:58 +08:00
parent 2d55d0460a
commit e35b975609
15 changed files with 229 additions and 122 deletions

View File

@@ -21,4 +21,12 @@ public interface ICsTopicService extends IService<CsTopic> {
* @param list 主题集合
*/
void addTopic(String nDid,List<CsTopic> list);
/**
* 获取设备的协议版本
* @param nDid 网络设备码
*/
String getVersion(String nDid);
}

View File

@@ -765,6 +765,7 @@ public class CsDevModelServiceImpl implements ICsDevModelService {
if (Objects.isNull(eleEpdPqd)){
throw new BusinessException(AccessResponseEnum.DICT_MISSING);
}
// M 代表没有数据,因为influxDB要录入数据此字段是主键给个默认值
if (!Objects.isNull(eleEpdPqd.getHarmStart()) && !Objects.isNull(eleEpdPqd.getHarmEnd())){
if (Objects.equals(eleEpdPqd.getHarmStart(),1)){
for (int i = eleEpdPqd.getHarmStart(); i <= eleEpdPqd.getHarmEnd(); i++) {

View File

@@ -1,17 +1,16 @@
package com.njcn.access.service.impl;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
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.dto.AccessDto;
import com.njcn.access.pojo.dto.CsModelDto;
import com.njcn.access.pojo.dto.ReqAndResDto;
import com.njcn.access.service.ICsDeviceService;
import com.njcn.access.service.ICsTopicService;
import com.njcn.access.utils.MqttUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
@@ -34,6 +33,7 @@ import org.slf4j.LoggerFactory;
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;
@@ -72,6 +72,8 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
private final MqttUtil mqttUtil;
private final ICsTopicService csTopicService;
@Override
@Transactional(rollbackFor = {Exception.class})
public void devRegister(String nDid) {
@@ -90,12 +92,15 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
throw new BusinessException(AccessResponseEnum.DEV_IS_NOT_ZL);
}
//3.判断客户端是否在线
//mqttUtil.judgeClientOnline(nDid);
boolean mqttClient = mqttUtil.judgeClientOnline("access-boot1234567");
String clientName = "NJCN-" + nDid.substring(nDid.length() - 6);
boolean mqttClient = mqttUtil.judgeClientOnline(clientName);
if (!mqttClient){
throw new BusinessException(AccessResponseEnum.MISSING_CLIENT);
}
//3.MQTT询问装置用的模板并判断库中是否存在模板
//4.询问设备支持的主题信息
//将支持的主题入库
askTopic(nDid);
//5.MQTT询问装置用的模板并判断库中是否存在模板
//存在则建立关系;不存在则告警出来
SysDicTreePO dictData = dictTreeFeignClient.queryById(csEquipmentDeliveryVO.getDevModel()).getData();
if (Objects.isNull(dictData)){
@@ -137,14 +142,15 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
csLedgerParam.setSort(0);
csLedgerFeignClient.add(csLedgerParam);
//2.监测点表录入关系
//todo 获取逻辑设备信息更新监测点pt、ct相关信息 LdevInfo
devAccessParam.getList().forEach(item->{
String id = IdUtil.fastSimpleUUID();
CsLinePO po = new CsLinePO();
po.setLineId(id);
po.setName(item.getName());
po.setPosition(item.getPosition());
po.setVolGrade(item.getVolGrade());
//todo 目前ct、pt数据不确定 后期补
//todo 目前电压等级、ct、pt数据不确定 后期补
// po.setVolGrade(item.getVolGrade());
// po.setPtRatio(item.getPtRatio());
// po.setCtRatio(item.getCtRatio());
po.setStatus(1);
@@ -168,39 +174,75 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
//3.监测点拓扑图表录入关系
csLineTopologyFeignClient.addList(appLineTopologyDiagramPoList);
//4.新增装置-模板关系
List<String> modelId = objectToList(redisUtil.getObjectByKey("MODEL" + devAccessParam.getNDid()));
List<CsModelDto> modelId = objectToList(redisUtil.getObjectByKey("MODEL" + devAccessParam.getNDid()));
modelId.forEach(item->{
CsDevModelRelationAddParm csDevModelRelationAddParm = new CsDevModelRelationAddParm();
csDevModelRelationAddParm.setDevId(vo.getId());
csDevModelRelationAddParm.setModelId(item);
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.装置接入之后再设置心跳时间,超时改为掉线
//redisUtil.saveByKeyWithExpire("MQTT:" + devAccessParam.getNDid(), Instant.now().toEpochMilli(),180L);
//7.绑定装置和人的关系
//6.绑定装置和人的关系
CsDeviceUserPO po = new CsDeviceUserPO();
po.setPrimaryUserId(RequestUtil.getUserIndex());
po.setStatus("1");
po.setSubUserId(RequestUtil.getUserIndex());
po.setDeviceId(vo.getId());
csDeviceUserFeignClient.add(Collections.singletonList(po));
//8.删除redis监测点模板信息
//7.删除redis监测点模板信息
redisUtil.delete("LINE" + devAccessParam.getNDid());
//todo 9.记录操作日志
//todo 录入软件信息 SoftInfo
//todo 9.记录注册日志
//发起自动接入请求
devAccess(devAccessParam.getNDid());
//todo 10.记录接入日志
} catch (Exception e) {
throw new BusinessException(CommonResponseEnum.FAIL);
}
}
public void devAccess(String nDid) {
String version = csTopicService.getVersion(nDid);
ReqAndResDto.Req reqAndResParam = new ReqAndResDto.Req();
reqAndResParam.setMid(1);
reqAndResParam.setDid(0);
reqAndResParam.setPri(AccessEnum.FIRST_CHANNEL.getCode());
reqAndResParam.setType(Integer.parseInt(TypeEnum.TYPE_5.getCode()));
reqAndResParam.setExpire(-1);
publisher.send("/Pfm/DevCmd/"+version+"/"+nDid, PubUtils.obj2json(reqAndResParam),1,false);
}
/**
* 平台对设备发起主题询问命令
*/
public void askTopic(String nDid) {
ReqAndResDto.Req reqAndResParam = new ReqAndResDto.Req();
reqAndResParam.setMid(1);
reqAndResParam.setDid(0);
reqAndResParam.setPri(AccessEnum.FIRST_CHANNEL.getCode());
reqAndResParam.setType(Integer.parseInt(TypeEnum.TYPE_1.getCode()));
reqAndResParam.setExpire(-1);
publisher.send("/Pfm/DevTopic/"+nDid, PubUtils.obj2json(reqAndResParam),1,false);
}
/**
* 平台对设备发起注册命令
* @param nDid
* @param devType
*/
public void zhiLianRegister(String nDid,String devType) {
ReqAndResDto.Req reqAndResParam = new ReqAndResDto.Req();
reqAndResParam.setMid(1);
reqAndResParam.setDid("0");
reqAndResParam.setDid(0);
reqAndResParam.setPri(AccessEnum.FIRST_CHANNEL.getCode());
reqAndResParam.setType(TypeEnum.TYPE_2.getCode());
reqAndResParam.setType(Integer.parseInt(TypeEnum.TYPE_2.getCode()));
reqAndResParam.setExpire(-1);
AccessDto accessDto = new AccessDto();
accessDto.setNDid(nDid);
@@ -209,12 +251,12 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
publisher.send("/Pfm/DevReg/"+nDid, PubUtils.obj2json(reqAndResParam),1,false);
}
public List<String> objectToList(Object object) {
List<String> urlList = new ArrayList<>();
public List<CsModelDto> objectToList(Object object) {
List<CsModelDto> urlList = new ArrayList<>();
if (object != null) {
if (object instanceof ArrayList<?>) {
for (Object o : (List<?>) object) {
urlList.add((String) o);
urlList.add((CsModelDto) o);
}
}
}

View File

@@ -24,11 +24,16 @@ public class CsTopicServiceImpl extends ServiceImpl<CsTopicMapper, CsTopic> impl
@Override
public void addTopic(String nDid, List<CsTopic> list) {
LambdaQueryWrapper<CsTopic> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(CsTopic::getNdid,nDid);
lambdaQueryWrapper.eq(CsTopic::getNDid,nDid);
List<CsTopic> topics = this.baseMapper.selectList(lambdaQueryWrapper);
if (CollectionUtil.isNotEmpty(topics)){
this.remove(lambdaQueryWrapper);
}
this.saveBatch(list,1000);
this.saveBatch(list);
}
@Override
public String getVersion(String nDid) {
return this.lambdaQuery().eq(CsTopic::getNDid,nDid).isNotNull(CsTopic::getVersion).list().get(0).getVersion();
}
}