【还原modbus服务响应】
This commit is contained in:
parent
05a8045679
commit
60fca3df21
@ -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<ModbusTcpPayl
|
||||
}
|
||||
AfcClientQuery query = AfcQueryQueue.poll();
|
||||
// TODO 网关调试通后放开注释
|
||||
// Channel channel = plcGatewayConnectManager.getChannel(GatewayName);
|
||||
// if(channel == null) {
|
||||
// log.warn("AFC网关不在线,不发送数据");
|
||||
// return;
|
||||
// }
|
||||
// // 发送消息到网关
|
||||
// CompletableFuture<ModbusTcpPayload> future = modbusMessageHandler.sendModbusMessage(channel, query.getPayload());
|
||||
Channel channel = plcGatewayConnectManager.getChannel(GatewayName);
|
||||
if(channel == null) {
|
||||
log.warn("AFC网关不在线,不发送数据");
|
||||
return;
|
||||
}
|
||||
// 发送消息到网关
|
||||
CompletableFuture<ModbusTcpPayload> 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<ModbusTcpPayl
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, ModbusTcpPayload msg) throws Exception {
|
||||
// Channel channel = this.plcGatewayConnectManager.getChannel(GatewayName);
|
||||
// if(channel == null) {
|
||||
// log.debug("AFC网关不在线,不记录数据");
|
||||
// return;
|
||||
// }
|
||||
Channel channel = this.plcGatewayConnectManager.getChannel(GatewayName);
|
||||
if(channel == null) {
|
||||
log.debug("AFC网关不在线,不记录数据");
|
||||
return;
|
||||
}
|
||||
synchronized (AfcQueryQueue) {
|
||||
// 生产客户端查询记录
|
||||
AfcQueryQueue.offer(new AfcClientQuery(ctx.channel(), msg));
|
||||
|
Loading…
Reference in New Issue
Block a user