From a67b1840aa9fcd010bebbfea822be73d5231b116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Thu, 9 Nov 2023 17:10:07 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0NB=E9=80=9A=E9=81=93?= =?UTF-8?q?=E6=94=AF=E6=8C=81=202.=20=E6=8E=A7=E5=88=B6=E5=99=A8=E9=80=9A?= =?UTF-8?q?=E4=BF=A1=E5=8D=8F=E8=AE=AE=E6=9B=B4=E6=96=B02.0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=203.=20=E5=A2=9E=E5=8A=A0=E7=9C=81ID=EF=BC=8C?= =?UTF-8?q?=E5=8C=97=E6=96=97=E9=80=9A=E6=92=ADID=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/application-user.properties | 3 + .../netty/decode/YuanRongProtocolDecode.java | 93 +++++++++++-------- .../netty/encode/YuanRongProtocolEncode.java | 52 +++++++++-- .../common/impl/AlarmControlTypeName.java | 8 +- .../common/impl/AlarmTypeName.java | 10 +- .../common/impl/SensorControlTunnelName.java | 13 +-- .../beidouservice/config/CommonConfigure.java | 22 +++++ .../beidouservice/controller/AuthUser.java | 2 +- .../beidouservice/mapper/DeviceMapper.java | 4 + .../pojo/dto/task/AlarmDeviceTaskDTO.java | 2 + .../pojo/vo/binary/SensorId.java | 11 +++ .../pojo/vo/binary/SensorTaskStatus.java | 1 - .../beidouservice/service/DeviceService.java | 4 + .../service/impl/DeviceServiceImpl.java | 50 ++++++++-- src/main/resources/mappers/DeviceMapper.xml | 6 ++ 15 files changed, 208 insertions(+), 73 deletions(-) create mode 100644 src/main/java/com/zjyr/beidouservice/config/CommonConfigure.java create mode 100644 src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorId.java diff --git a/config/application-user.properties b/config/application-user.properties index 52776ab..41c7411 100644 --- a/config/application-user.properties +++ b/config/application-user.properties @@ -2,3 +2,6 @@ socket.server-host=localhost socket.server-port=10001 socket.server-mode=tcp socket.heart-timeout=60 + +common.provice-code = 33 +common.beidou-id = 1234588 diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java index dc50de5..96b2453 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java @@ -10,14 +10,17 @@ import com.zjyr.beidouservice.misc.HelperUtils; import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus; import com.zjyr.beidouservice.pojo.vo.binary.MessageContent; +import com.zjyr.beidouservice.pojo.vo.binary.SensorId; import com.zjyr.beidouservice.pojo.vo.binary.SensorStatusAck; import com.zjyr.beidouservice.pojo.vo.binary.SensorStatusInfo; import com.zjyr.beidouservice.pojo.vo.binary.SensorTaskAck; import com.zjyr.beidouservice.pojo.vo.binary.SensorTaskStatus; +import com.zjyr.beidouservice.service.DeviceService; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import jakarta.annotation.Resource; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -29,30 +32,35 @@ import java.util.List; @RequiredArgsConstructor @Slf4j public class YuanRongProtocolDecode extends ByteToMessageDecoder { + @Resource + DeviceService deviceService; + @Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf buf, List list) { log.info("\n{}", ByteBufUtil.prettyHexDump(buf)); try { - short msgLength = buf.readShort(); - byte version = buf.readByte(); - byte cryptCytp = buf.readByte(); - int timeStamp = buf.readInt(); - int statusCode = buf.readInt(); - byte msgType = buf.readByte(); - short msgSize = buf.readShort(); + short msgLength = buf.readShort(); + byte version = buf.readByte(); + byte cryptCytp = buf.readByte(); + int timeStamp = buf.readInt(); + int statusCode = buf.readInt(); + byte msgType = buf.readByte(); + short msgSize = buf.readShort(); ControlCommandName cmd = CommonEnumHandler.codeOf(ControlCommandName.class, msgType); if (cmd != null) { switch (cmd) { case COMMAND_REPORT_HEART -> { - int heartSeq = buf.readInt(); - SendStatusName sendStatus = CommonEnumHandler.codeOf(SendStatusName.class, buf.readByte()); - ControlChannelStatus beidouStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte()); - byte[] beidouCommFreq = new byte[3]; - buf.readBytes(beidouCommFreq, 0, 3); + int heartSeq = buf.readInt(); + SendStatusName sendStatus = SendStatusName.SEND_SUCCESS;//CommonEnumHandler.codeOf(SendStatusName.class, buf + // .readByte()); + ControlChannelStatus beidouStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte()); + byte[] beidouCommFreq = {0x00, 0x00, 0x00}; + //buf.readBytes(beidouCommFreq, 0, 3); byte[] beidouSignal = new byte[10]; buf.readBytes(beidouSignal, 0, 10); - ControlChannelStatus wireStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte()); + ControlChannelStatus wireStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte()); + buf.readByte(); // 无线信号强度,暂不使用 ControlChannelStatus phoneStatus = CommonEnumHandler.codeOf(ControlChannelStatus.class, buf.readByte()); ControllerStatus status = ControllerStatus.builder() @@ -78,20 +86,31 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder { .build()); } case COMMAND_REPORT_SENSOR -> { - int taskId = buf.readInt(); - byte nItem = buf.readByte(); + int taskId = buf.readInt(); + byte nItem = buf.readByte(); SensorTaskAck taskAck = SensorTaskAck.builder().taskId(taskId).ackTimestamp(timeStamp).sensorStatus(new ArrayList<>( nItem)).build(); for (int i = 0; i < nItem; i++) { - SensorTaskStatus taskStatus = SensorTaskStatus.builder() - .sensorId(buf.readInt()) - .taskResult(CommonEnumHandler.codeOf(TaskResultName.class, - buf.readByte())) - .tunnelName(CommonEnumHandler.codeOf(SensorControlTunnelName.class, - buf.readByte())) - .build(); + SensorId sensorId = SensorId.builder() + .countyId(buf.readByte()) + .seqId(buf.readByte()) + .build(); + Integer beidouId = deviceService.getDeviceBeidouIdBySeqId(sensorId.getSeqId()); + + if (beidouId == -1) { + log.error("Can not find device by seqId: {}", sensorId.getSeqId()); + continue; + } + + SensorTaskStatus taskStatus = SensorTaskStatus.builder() + .sensorId(beidouId) + .taskResult(CommonEnumHandler.codeOf(TaskResultName.class, + buf.readByte())) + .tunnelName(CommonEnumHandler.codeOf(SensorControlTunnelName.class, + buf.readByte())) + .build(); taskAck.getSensorStatus().add(taskStatus); } @@ -111,7 +130,7 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder { .build()); } case COMMAND_REPORT_QUERY_SENSOR -> { - int nItems = buf.readByte(); + int nItems = buf.readByte(); SensorStatusAck sensorAck = SensorStatusAck.builder().sensorStatus(new ArrayList<>()).build(); MessageContent msgCtx = MessageContent.builder() @@ -121,20 +140,20 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder { .build(); for (int i = 0; i < nItems; i++) { - short tmInfo = buf.readShort(); - SensorControlTunnelName tn = CommonEnumHandler.codeOf(SensorControlTunnelName.class, buf.readByte()); - int cmdType = buf.readByte(); - short sa = buf.readShort(); - short sid = buf.readShort(); - int warrStatus = buf.readByte(); - int emg0 = buf.readByte(); - int emg1 = buf.readByte(); - int emg2 = buf.readByte(); - int spk0 = buf.readByte(); - int spk1 = buf.readByte(); - int spk2 = buf.readByte(); - int runStatus = buf.readByte(); - byte[] signale = new byte[5]; + short tmInfo = buf.readShort(); + SensorControlTunnelName tn = CommonEnumHandler.codeOf(SensorControlTunnelName.class, buf.readByte()); + int cmdType = buf.readByte(); + short sa = buf.readShort(); + short sid = buf.readShort(); + int warrStatus = buf.readByte(); + int emg0 = buf.readByte(); + int emg1 = buf.readByte(); + int emg2 = buf.readByte(); + int spk0 = buf.readByte(); + int spk1 = buf.readByte(); + int spk2 = buf.readByte(); + int runStatus = buf.readByte(); + byte[] signale = new byte[5]; buf.readBytes(signale, 0, 5); byte[] id = new byte[3]; buf.readBytes(id, 0, 3); diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/encode/YuanRongProtocolEncode.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/encode/YuanRongProtocolEncode.java index 71f574f..df5e2db 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/encode/YuanRongProtocolEncode.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/encode/YuanRongProtocolEncode.java @@ -1,15 +1,19 @@ package com.zjyr.beidouservice.adapter.impl.netty.encode; import com.zjyr.beidouservice.common.impl.ControlCommandName; +import com.zjyr.beidouservice.config.CommonConfigure; import com.zjyr.beidouservice.misc.HelperUtils; import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; import com.zjyr.beidouservice.pojo.vo.binary.QuerySensorProtocol; import com.zjyr.beidouservice.pojo.vo.binary.SensorControlProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.SensorId; +import com.zjyr.beidouservice.service.DeviceService; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import jakarta.annotation.Resource; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -20,13 +24,19 @@ import java.io.UnsupportedEncodingException; @RequiredArgsConstructor @Slf4j public class YuanRongProtocolEncode extends MessageToByteEncoder> { + @Resource + CommonConfigure commonConfigure; + + @Resource + DeviceService deviceService; + @Override protected void encode(ChannelHandlerContext channelHandlerContext, BaseBinaryProtocol baseBinaryProtocol, ByteBuf byteBuf) throws UnsupportedEncodingException { if (baseBinaryProtocol == null || baseBinaryProtocol.getMsgContent() == null || - baseBinaryProtocol.getMsgContent().getMsgBody() == null) { + baseBinaryProtocol.getMsgContent().getMsgBody() == null) { return; } @@ -42,29 +52,50 @@ public class YuanRongProtocolEncode extends MessageToByteEncoder 0) { byteBuf.writeBytes(ttsArray); } @@ -92,7 +123,14 @@ public class YuanRongProtocolEncode extends MessageToByteEncoder selectDeviceByDeviceIds(@Param("ids") List deviceIdList); void batchUpdateDeviceAlarmStatus(@Param("ids") Set alarmingDeviceIds, @Param("status") Integer code); + + List getDeviceBySeqId(@Param("seqId") Integer seqId); + + List getDeviceByBeidouId(@Param("beidouId") Integer beidouId); } diff --git a/src/main/java/com/zjyr/beidouservice/pojo/dto/task/AlarmDeviceTaskDTO.java b/src/main/java/com/zjyr/beidouservice/pojo/dto/task/AlarmDeviceTaskDTO.java index 01b5283..3fbce40 100644 --- a/src/main/java/com/zjyr/beidouservice/pojo/dto/task/AlarmDeviceTaskDTO.java +++ b/src/main/java/com/zjyr/beidouservice/pojo/dto/task/AlarmDeviceTaskDTO.java @@ -2,8 +2,10 @@ package com.zjyr.beidouservice.pojo.dto.task; import com.zjyr.beidouservice.pojo.dataobject.AlarmDeviceTaskDO; import lombok.Data; +import lombok.EqualsAndHashCode; +@EqualsAndHashCode(callSuper = true) @Data public class AlarmDeviceTaskDTO extends AlarmDeviceTaskDO { diff --git a/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorId.java b/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorId.java new file mode 100644 index 0000000..8f1b93d --- /dev/null +++ b/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorId.java @@ -0,0 +1,11 @@ +package com.zjyr.beidouservice.pojo.vo.binary; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +public class SensorId { + private int countyId; + private int seqId; +} diff --git a/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorTaskStatus.java b/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorTaskStatus.java index 02b7df2..64eebae 100644 --- a/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorTaskStatus.java +++ b/src/main/java/com/zjyr/beidouservice/pojo/vo/binary/SensorTaskStatus.java @@ -10,6 +10,5 @@ import lombok.Data; public class SensorTaskStatus { private int sensorId; private TaskResultName taskResult; - private SensorControlTunnelName tunnelName; } diff --git a/src/main/java/com/zjyr/beidouservice/service/DeviceService.java b/src/main/java/com/zjyr/beidouservice/service/DeviceService.java index d0095b4..2dd8e6f 100644 --- a/src/main/java/com/zjyr/beidouservice/service/DeviceService.java +++ b/src/main/java/com/zjyr/beidouservice/service/DeviceService.java @@ -6,6 +6,7 @@ import com.zjyr.beidouservice.pojo.dto.DeviceByIdRespDTO; import com.zjyr.beidouservice.pojo.dto.DeviceReqDTO; import com.zjyr.beidouservice.pojo.dto.DeviceRespDTO; import com.zjyr.beidouservice.pojo.dto.DeviceSearchDTO; +import com.zjyr.beidouservice.pojo.vo.binary.SensorId; import java.util.List; @@ -31,4 +32,7 @@ public interface DeviceService { */ void refreshDeviceAlarmStatus(List deviceTaskDOS); + Integer getDeviceBeidouIdBySeqId(Integer seqId); + + SensorId getSensorIdByBeidouId(Integer beidouId); } diff --git a/src/main/java/com/zjyr/beidouservice/service/impl/DeviceServiceImpl.java b/src/main/java/com/zjyr/beidouservice/service/impl/DeviceServiceImpl.java index 1af1918..ad20248 100644 --- a/src/main/java/com/zjyr/beidouservice/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/zjyr/beidouservice/service/impl/DeviceServiceImpl.java @@ -11,6 +11,7 @@ import com.zjyr.beidouservice.pojo.dto.DeviceByLocationDTO; import com.zjyr.beidouservice.pojo.dto.DeviceReqDTO; import com.zjyr.beidouservice.pojo.dto.DeviceRespDTO; import com.zjyr.beidouservice.pojo.dto.DeviceSearchDTO; +import com.zjyr.beidouservice.pojo.vo.binary.SensorId; import com.zjyr.beidouservice.service.DeviceService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; @@ -37,11 +38,11 @@ public class DeviceServiceImpl implements DeviceService { //根据省份、地市、区信息查询设备 DeviceByLocationDTO searchDTO = DeviceByLocationDTO.builder() - .provinceCode(deviceReqDTO.getProvinceCode()) - .cityCode(deviceReqDTO.getCityCode()) - .countyCode(deviceReqDTO.getCountyCode()) - .deviceName(deviceReqDTO.getDeviceName()) - .manufacturer(deviceReqDTO.getManufacturer()).build(); + .provinceCode(deviceReqDTO.getProvinceCode()) + .cityCode(deviceReqDTO.getCityCode()) + .countyCode(deviceReqDTO.getCountyCode()) + .deviceName(deviceReqDTO.getDeviceName()) + .manufacturer(deviceReqDTO.getManufacturer()).build(); log.info("device search info : [{}]", searchDTO); List deviceDOList = deviceMapper.selectDeviceByLoc(searchDTO); @@ -60,7 +61,8 @@ public class DeviceServiceImpl implements DeviceService { public DeviceByIdRespDTO getDeviceByDeviceId(DeviceSearchDTO deviceSearchDTO) { log.info("get device info by deviceId: [{}]", deviceSearchDTO.getDeviceId()); - List deviceDOList = deviceMapper.selectDeviceByDeviceIds(Collections.singletonList(deviceSearchDTO.getDeviceId())); + List deviceDOList = + deviceMapper.selectDeviceByDeviceIds(Collections.singletonList(deviceSearchDTO.getDeviceId())); if (deviceDOList.isEmpty()) { log.warn("根据deviceId未查找到设备信息"); return null; @@ -72,12 +74,13 @@ public class DeviceServiceImpl implements DeviceService { @Override public void refreshDeviceAlarmStatus(List deviceTaskDOS) { // 全部有告警任务的设备id - Set deviceIdSet = deviceTaskDOS.stream().map(AlarmDeviceTaskDO::getDeviceId).collect(Collectors.toSet()); + Set deviceIdSet = + deviceTaskDOS.stream().map(AlarmDeviceTaskDO::getDeviceId).collect(Collectors.toSet()); // 告警状态为执行中的设备id Set alarmingDeviceIds = deviceTaskDOS.stream() - .filter(s -> TaskStatusName.TASK_EXECUTING.getValue().equals(s.getStatus())) - .map(AlarmDeviceTaskDO::getDeviceId) - .collect(Collectors.toSet()); + .filter(s -> TaskStatusName.TASK_EXECUTING.getValue().equals(s.getStatus())) + .map(AlarmDeviceTaskDO::getDeviceId) + .collect(Collectors.toSet()); // 去掉执行中的,剩下的需要更新为终态 deviceIdSet.removeAll(alarmingDeviceIds); @@ -89,4 +92,31 @@ public class DeviceServiceImpl implements DeviceService { deviceMapper.batchUpdateDeviceAlarmStatus(deviceIdSet, DeviceAlarmStatusEnum.FINAL_STATE.getCode()); } } + + @Override + public Integer getDeviceBeidouIdBySeqId(Integer seqId) { + List devList = deviceMapper.getDeviceBySeqId(seqId); + + if (devList.isEmpty()) { + return -1; + } else { + return devList.get(0).getBeidouId(); + } + } + + @Override + public SensorId getSensorIdByBeidouId(Integer beidouId) { + List devList = deviceMapper.getDeviceByBeidouId(beidouId); + + if (devList.isEmpty()) { + return null; + } else { + DeviceDO device = devList.get(0); + + return SensorId.builder() + .countyId(device.getCountyCode()) + .seqId(device.getDeviceId()) + .build(); + } + } } diff --git a/src/main/resources/mappers/DeviceMapper.xml b/src/main/resources/mappers/DeviceMapper.xml index 994df2b..6887453 100644 --- a/src/main/resources/mappers/DeviceMapper.xml +++ b/src/main/resources/mappers/DeviceMapper.xml @@ -69,5 +69,11 @@ + + \ No newline at end of file