Merge remote-tracking branch 'origin/test-training2' into test

This commit is contained in:
joylink_zhangsai 2023-03-20 10:15:59 +08:00
commit 5a9b7ea436
2 changed files with 18 additions and 49 deletions

View File

@ -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));

View File

@ -57,9 +57,9 @@ public class AfcTransferServer implements ApplicationRunner {
ChannelFuture future = b.bind(this.modbusTcpConfig.getAfcTransferPort()).sync();
if(future.isSuccess()) {
log.info(String.format("modbus-tcp server start on port [%s]", this.modbusTcpConfig.getAfcTransferPort()));
log.info(String.format("modbus-tcp AFC server start on port [%s]", this.modbusTcpConfig.getAfcTransferPort()));
} else {
log.error("modbus-tcp server start failed", future.cause());
log.error("modbus-tcp AFC server start failed", future.cause());
}
}