Compare commits

...

2 Commits

Author SHA1 Message Date
黄昕 cf4eb120f7 1. 增加协议编码异常日志 2023-11-20 15:28:29 +08:00
黄昕 a6a55309e2 1. 修正多SensorId组合出错问题 2023-11-20 15:26:46 +08:00
3 changed files with 94 additions and 99 deletions

View File

@ -6,13 +6,10 @@ 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;
@ -23,9 +20,6 @@ import java.io.UnsupportedEncodingException;
@RequiredArgsConstructor
@Slf4j
public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryProtocol<T>> {
@Resource
DeviceService deviceService;
@Override
protected void encode(ChannelHandlerContext channelHandlerContext,
BaseBinaryProtocol<T> baseBinaryProtocol,
@ -36,101 +30,86 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
return;
}
T msgBody = baseBinaryProtocol.getMsgContent().getMsgBody();
try {
T msgBody = baseBinaryProtocol.getMsgContent().getMsgBody();
byteBuf.writeByte(BaseBinaryProtocol.VERSIN & 0xFF); // 协议版本号
byteBuf.writeByte(baseBinaryProtocol.getCryptoType()); // 协议加密选项
byteBuf.writeInt((int) (System.currentTimeMillis() / 1000)); // UTC时间戳单位秒协议发送时的UTC时间
byteBuf.writeInt(baseBinaryProtocol.getStatusCode()); // 消息状态码
byteBuf.writeByte(BaseBinaryProtocol.VERSIN & 0xFF); // 协议版本号
byteBuf.writeByte(baseBinaryProtocol.getCryptoType()); // 协议加密选项
byteBuf.writeInt((int) (System.currentTimeMillis() / 1000)); // UTC时间戳单位秒协议发送时的UTC时间
byteBuf.writeInt(baseBinaryProtocol.getStatusCode()); // 消息状态码
if (msgBody instanceof HeartProtocol heartProtocol) {
byteBuf.writeByte(ControlCommandName.COMMAND_HEART.getValue().byteValue()); // 消息类型
byteBuf.writeShort(4); // 消息内容长度
byteBuf.writeInt(heartProtocol.getSeqId());
} else if (msgBody instanceof SensorControlProtocol msg) {
int ttsSize;
byte[] ttsArray = new byte[0];
if (msgBody instanceof HeartProtocol heartProtocol) {
byteBuf.writeByte(ControlCommandName.COMMAND_HEART.getValue().byteValue()); // 消息类型
byteBuf.writeShort(4); // 消息内容长度
byteBuf.writeInt(heartProtocol.getSeqId());
} else if (msgBody instanceof SensorControlProtocol msg) {
int ttsSize;
byte[] ttsArray = new byte[0];
if (msg.getTtsContent() != null && !msg.getTtsContent().isEmpty()) {
//ttsArray = msg.getTtsContent().getBytes(StandardCharsets.UTF_16BE);
ttsArray = HelperUtils.convertStringToGB2312(msg.getTtsContent());
ttsSize = ttsArray.length;
} else {
ttsSize = 0;
}
if (msg.getTtsContent() != null && !msg.getTtsContent().isEmpty()) {
//ttsArray = msg.getTtsContent().getBytes(StandardCharsets.UTF_16BE);
ttsArray = HelperUtils.convertStringToGB2312(msg.getTtsContent());
ttsSize = ttsArray.length;
} else {
ttsSize = 0;
}
if (!msg.getSensorId().isEmpty()) {
// 删除无效的ID
for (var c : msg.getSensorId()) {
SensorId sensor = deviceService.getSensorIdByBeidouId(c);
if (sensor == null) {
msg.getSensorId().remove(c);
byteBuf.writeByte(ControlCommandName.COMMAND_SENSOR.getValue().byteValue()); // 消息类型
// 消息内容长度
byteBuf.writeShort(27 + ttsSize + msg.getCityCode().size() + msg.getDistrictsCode().size() + msg.getSensorId().size() * 2);
// 任务ID, 当前长度 4
byteBuf.writeInt(msg.getTaskId());
// 警报控制, 当前长度 5
byteBuf.writeByte(msg.getAlarmMode().getValue().byteValue());
// 警报模式, 当前长度 6
byteBuf.writeByte(msg.getAlarmControlType().getValue().byteValue());
// 警报类型, 当前长度 7
byteBuf.writeByte(msg.getAlarmType().getValue().byteValue());
// 警报通道, 当前长度 8
byteBuf.writeByte(msg.getControlTunnel().byteValue());
// 警报时间, 当前长度 12
byteBuf.writeInt(msg.getTimeStamp());
// 警报重复次数, 当前长度 13
byteBuf.writeByte(msg.getRepeatTimes().byteValue());
// 北斗ID, 当前长度 21
byteBuf.writeLong(msg.getBeidouCommunicationId());
// 省ID, 当前长度 22(不含可变内容长度)
byteBuf.writeByte(msg.getProviceCode().byteValue());
// TTS 内容长度, 当前长度 24
byteBuf.writeShort((short) ttsSize);
if (ttsSize > 0) {
byteBuf.writeBytes(ttsArray);
}
if (msg.getCityCode().isEmpty()) {
byteBuf.writeByte(0);
} else {
byteBuf.writeByte(msg.getCityCode().size());
for (var c : msg.getCityCode()) {
byteBuf.writeByte(c.byteValue());
}
}
}
byteBuf.writeByte(ControlCommandName.COMMAND_SENSOR.getValue().byteValue()); // 消息类型
// 消息内容长度
byteBuf.writeShort(27 + ttsSize + msg.getCityCode().size() + msg.getDistrictsCode().size() + msg.getSensorId().size() * 2);
// 任务ID, 当前长度 4
byteBuf.writeInt(msg.getTaskId());
// 警报控制, 当前长度 5
byteBuf.writeByte(msg.getAlarmMode().getValue().byteValue());
// 警报模式, 当前长度 6
byteBuf.writeByte(msg.getAlarmControlType().getValue().byteValue());
// 警报类型, 当前长度 7
byteBuf.writeByte(msg.getAlarmType().getValue().byteValue());
// 警报通道, 当前长度 8
byteBuf.writeByte(msg.getControlTunnel().byteValue());
// 警报时间, 当前长度 12
byteBuf.writeInt(msg.getTimeStamp());
// 警报重复次数, 当前长度 13
byteBuf.writeByte(msg.getRepeatTimes().byteValue());
// 北斗ID, 当前长度 21
byteBuf.writeLong(msg.getBeidouCommunicationId());
// 省ID, 当前长度 22(不含可变内容长度)
byteBuf.writeByte(msg.getProviceCode().byteValue());
// TTS 内容长度, 当前长度 24
byteBuf.writeShort((short) ttsSize);
if (ttsSize > 0) {
byteBuf.writeBytes(ttsArray);
}
if (msg.getCityCode().isEmpty()) {
byteBuf.writeByte(0);
} else {
byteBuf.writeByte(msg.getCityCode().size());
for (var c : msg.getCityCode()) {
byteBuf.writeByte(c.byteValue());
}
}
if (msg.getDistrictsCode().isEmpty()) {
byteBuf.writeByte(0);
} else {
byteBuf.writeByte(msg.getDistrictsCode().size());
for (var c : msg.getDistrictsCode()) {
byteBuf.writeByte(c.byteValue());
}
}
if (msg.getSensorId().isEmpty()) {
byteBuf.writeByte(0);
} else {
byteBuf.writeByte(msg.getSensorId().size());
for (var c : msg.getSensorId()) {
SensorId sensor = deviceService.getSensorIdByBeidouId(c);
if (sensor == null) {
byteBuf.writeByte(0xFF);
byteBuf.writeByte(0xFF);
} else {
byteBuf.writeByte(sensor.getCountyId());
byteBuf.writeByte(sensor.getSeqId());
if (msg.getDistrictsCode().isEmpty()) {
byteBuf.writeByte(0);
} else {
byteBuf.writeByte(msg.getDistrictsCode().size());
for (var c : msg.getDistrictsCode()) {
byteBuf.writeByte(c.byteValue());
}
}
}
} else if (msgBody instanceof QuerySensorProtocol msg) {
byteBuf.writeByte(ControlCommandName.COMMAND_QUERY_SENSOR.getValue().byteValue()); // 消息类型
if (msg.getSensorId().isEmpty()) {
byteBuf.writeByte(0);
} else {
byteBuf.writeByte(msg.getSensorId().size());
for (var c : msg.getSensorId()) {
byteBuf.writeByte(c.getCountyId());
byteBuf.writeByte(c.getSeqId());
}
}
} else if (msgBody instanceof QuerySensorProtocol msg) {
byteBuf.writeByte(ControlCommandName.COMMAND_QUERY_SENSOR.getValue().byteValue()); // 消息类型
// if (!msg.getControllContents().isEmpty()) {
// byteBuf.writeShort(2 + 4 * msg.getControllContents().size()); // 消息内容长度
@ -143,10 +122,13 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
// byteBuf.writeShort(c.getSensorId());
// }
// }
} else {
log.info("XXXXX Encode: {}", baseBinaryProtocol.getMsgContent().getMsgBody().getClass());
}
} else {
log.info("XXXXX Encode: {}", baseBinaryProtocol.getMsgContent().getMsgBody().getClass());
}
log.info("\n{}", ByteBufUtil.prettyHexDump(byteBuf));
log.info("\n{}", ByteBufUtil.prettyHexDump(byteBuf));
} catch (Exception e) {
log.error("Protocol Encode Exception: {}", e.getMessage());
}
}
}

View File

@ -23,5 +23,5 @@ public class SensorControlProtocol {
private Long beidouCommunicationId;
private List<Integer> cityCode;
private List<Integer> districtsCode;
private List<Integer> sensorId;
private List<SensorId> sensorId;
}

View File

@ -8,7 +8,9 @@ import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol;
import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol;
import com.zjyr.beidouservice.pojo.vo.binary.MessageContent;
import com.zjyr.beidouservice.pojo.vo.binary.SensorControlProtocol;
import com.zjyr.beidouservice.pojo.vo.binary.SensorId;
import com.zjyr.beidouservice.service.AdapterProtocolService;
import com.zjyr.beidouservice.service.DeviceService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -24,6 +26,9 @@ public class BeidouSocketProtocolServiceImpl implements AdapterProtocolService {
@Resource
CommonConfigure commonConfigure;
@Resource
DeviceService deviceService;
private int currentSeqId = 0;
@Override
@ -44,6 +49,14 @@ public class BeidouSocketProtocolServiceImpl implements AdapterProtocolService {
List<Integer> areas,
List<Integer> sensors) {
long timestamp = new Timestamp(alarmDt.getTime()).getTime() / 1000;
List<SensorId> sensorId = new ArrayList<>();
for (var c : sensors) {
SensorId sensor = deviceService.getSensorIdByBeidouId(c);
if (sensor != null) {
sensorId.add(sensor);
}
}
log.info("Current: proviceCode = {}, beidouId = {}", commonConfigure.getProviceCode(), commonConfigure.getBeidouId());
@ -59,8 +72,8 @@ public class BeidouSocketProtocolServiceImpl implements AdapterProtocolService {
.proviceCode(commonConfigure.getProviceCode())
.beidouCommunicationId(commonConfigure.getBeidouId())
.cityCode(citys == null ? new ArrayList<>() : citys)
.districtsCode(areas == null ? new ArrayList<>() : citys)
.sensorId(sensors == null ? new ArrayList<>() : citys)
.districtsCode(areas == null ? new ArrayList<>() : areas)
.sensorId(sensorId)
.build();
}