diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java index 8f5429e12..68352cbed 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/device/real/afctransfer/AfcMessageHandler.java @@ -8,12 +8,14 @@ import com.digitalpetri.modbus.requests.*; import com.digitalpetri.modbus.responses.*; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueue; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.locks.ReentrantLock; /** @@ -56,50 +58,17 @@ public class AfcMessageHandler extends SimpleChannelInboundHandler future = modbusMessageHandler.sendModbusMessage(channel, query.getPayload()); + Channel channel = plcGatewayConnectManager.getChannel(GatewayName); + if(channel == null) { + log.warn("AFC网关不在线,不发送数据"); + return; + } + // 发送消息到网关 + CompletableFuture future = modbusMessageHandler.sendModbusMessage(channel, query.getPayload()); try { // 获取网关消息并回写给AFC客户端 -// ModbusTcpPayload payload = future.get(); -// query.getChannel().writeAndFlush(payload); - ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", StandardCharsets.UTF_8); - ModbusResponse modbusResponse = null; - FunctionCode functionCode = query.getPayload().getModbusPdu().getFunctionCode(); - switch (functionCode) { - case ReadCoils: - modbusResponse = new ReadCoilsResponse(buf); - break; - case ReadDiscreteInputs: - modbusResponse = new ReadDiscreteInputsResponse(buf); - break; - case ReadHoldingRegisters: - modbusResponse = new ReadHoldingRegistersResponse(buf); - break; - case ReadInputRegisters: - modbusResponse = new ReadInputRegistersResponse(buf); - break; - case WriteSingleCoil: - modbusResponse = new WriteSingleCoilResponse(0, 19); - break; - case WriteSingleRegister: - modbusResponse = new WriteSingleRegisterResponse(0, 19); - break; - case WriteMultipleCoils: - modbusResponse = new WriteMultipleCoilsResponse(0, 19); - break; - case WriteMultipleRegisters: - modbusResponse = new WriteMultipleRegistersResponse(0, 19); - break; - default: - modbusResponse = new ReadCoilsResponse(buf); - } - ModbusTcpPayload modbusTcpPayload = new ModbusTcpPayload(query.getPayload().getTransactionId(), query.getPayload().getUnitId(), modbusResponse); - query.getChannel().writeAndFlush(modbusTcpPayload); + ModbusTcpPayload payload = future.get(); + query.getChannel().writeAndFlush(payload); } catch (Exception e) { log.error("获取AFC网关响应异常", e); } @@ -118,11 +87,11 @@ public class AfcMessageHandler extends SimpleChannelInboundHandler