@@ -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 ) ;
}
}
}