1. 修正多SensorId组合出错问题

This commit is contained in:
黄昕 2023-11-20 15:26:46 +08:00
parent 14665dc896
commit a6a55309e2
3 changed files with 18 additions and 26 deletions

View File

@ -7,12 +7,10 @@ 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 +21,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,
@ -59,16 +54,6 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
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);
@ -119,14 +104,8 @@ public class YuanRongProtocolEncode<T> extends MessageToByteEncoder<BaseBinaryPr
} 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());
}
byteBuf.writeByte(c.getCountyId());
byteBuf.writeByte(c.getSeqId());
}
}
} else if (msgBody instanceof QuerySensorProtocol msg) {

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