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 b00e9c876..fd2234b3f 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 @@ -2,17 +2,18 @@ package club.joylink.rtss.simulation.cbtc.device.real.afctransfer; import club.joylink.rtss.simulation.cbtc.device.real.modbustcp.PlcGatewayConnectManager; import club.joylink.rtss.simulation.cbtc.device.real.modbustcp.handler.ModbusMessageHandler2; +import com.digitalpetri.modbus.FunctionCode; import com.digitalpetri.modbus.codec.ModbusTcpPayload; -import io.netty.channel.Channel; +import com.digitalpetri.modbus.requests.*; +import com.digitalpetri.modbus.responses.*; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueue; -import io.netty.util.internal.shaded.org.jctools.queues.MpscChunkedArrayQueue; -import io.netty.util.internal.shaded.org.jctools.queues.atomic.MpscAtomicArrayQueue; import lombok.extern.slf4j.Slf4j; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Semaphore; +import java.nio.charset.StandardCharsets; import java.util.concurrent.locks.ReentrantLock; /** @@ -54,17 +55,50 @@ public class AfcMessageHandler extends SimpleChannelInboundHandler future = modbusMessageHandler.sendModbusMessage(channel, query.getPayload()); + // TODO 网关调试通后放开注释 +// 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); +// 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); + } + query.getChannel().writeAndFlush(modbusResponse); } catch (Exception e) { log.error("获取AFC网关响应异常", e); } @@ -83,11 +117,11 @@ public class AfcMessageHandler extends SimpleChannelInboundHandler