|
@@ -45,12 +45,12 @@ public class ModbusDataHandlerAdapter extends ChannelInboundHandlerAdapter {
|
|
|
@Override
|
|
@Override
|
|
|
public void channelActive(ChannelHandlerContext ctx) {
|
|
public void channelActive(ChannelHandlerContext ctx) {
|
|
|
Channel channel = ctx.channel();
|
|
Channel channel = ctx.channel();
|
|
|
- log.info("客户端连接:" + channel.remoteAddress());
|
|
|
|
|
|
|
+ log.info("客户端连接:remote={},local={}" ,channel.remoteAddress(),channel.localAddress());
|
|
|
// 启动定时询问任务(首次延迟1秒,之后每隔QUERY_INTERVAL秒执行)
|
|
// 启动定时询问任务(首次延迟1秒,之后每隔QUERY_INTERVAL秒执行)
|
|
|
startQueryTask(channel);
|
|
startQueryTask(channel);
|
|
|
//设备上线
|
|
//设备上线
|
|
|
MonitorDevice device = monitorDeviceService.getOne(new LambdaQueryWrapper<MonitorDevice>()
|
|
MonitorDevice device = monitorDeviceService.getOne(new LambdaQueryWrapper<MonitorDevice>()
|
|
|
- .eq(MonitorDevice::getIp, getIP(ctx)),false);
|
|
|
|
|
|
|
+ .eq(MonitorDevice::getIp, getLocalAddress(ctx)),false);
|
|
|
if(device!=null){
|
|
if(device!=null){
|
|
|
meilingColdChainDataHandler.login(new MeilingColdChainMessageData(device.getDeviceCode()));
|
|
meilingColdChainDataHandler.login(new MeilingColdChainMessageData(device.getDeviceCode()));
|
|
|
}
|
|
}
|
|
@@ -60,19 +60,19 @@ public class ModbusDataHandlerAdapter extends ChannelInboundHandlerAdapter {
|
|
|
// 收到客户端消息时触发
|
|
// 收到客户端消息时触发
|
|
|
@Override
|
|
@Override
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
|
|
- Channel channel = ctx.channel();
|
|
|
|
|
// 只处理客户端的响应指令(0x02)
|
|
// 只处理客户端的响应指令(0x02)
|
|
|
- log.info("收到客户端[" + channel.remoteAddress() + "]响应:" + msg);
|
|
|
|
|
|
|
+ String localAddress = getLocalAddress(ctx);
|
|
|
|
|
+ log.info("收到客户端[" + localAddress + "]响应:" + msg);
|
|
|
// 取消当前超时任务(响应正常,无需断开连接)
|
|
// 取消当前超时任务(响应正常,无需断开连接)
|
|
|
if (queryTimeout != null && !queryTimeout.isCancelled()) {
|
|
if (queryTimeout != null && !queryTimeout.isCancelled()) {
|
|
|
queryTimeout.cancel();
|
|
queryTimeout.cancel();
|
|
|
}
|
|
}
|
|
|
MeilingColdChainMessageData meilingColdChainMessageData = new MeilingColdChainMessageData();
|
|
MeilingColdChainMessageData meilingColdChainMessageData = new MeilingColdChainMessageData();
|
|
|
- meilingColdChainMessageData.setHost(getIP(ctx));
|
|
|
|
|
|
|
+ meilingColdChainMessageData.setHost(localAddress);
|
|
|
String data = String.valueOf(msg);
|
|
String data = String.valueOf(msg);
|
|
|
MonitorDevice device = monitorDeviceService.getOne(new LambdaQueryWrapper<MonitorDevice>()
|
|
MonitorDevice device = monitorDeviceService.getOne(new LambdaQueryWrapper<MonitorDevice>()
|
|
|
.eq(MonitorDevice::getIp, meilingColdChainMessageData.getHost()),false);
|
|
.eq(MonitorDevice::getIp, meilingColdChainMessageData.getHost()),false);
|
|
|
- if(device!=null){
|
|
|
|
|
|
|
+ if(device!=null && StrUtil.isNotBlank(data)){
|
|
|
meilingColdChainMessageData.setHexData(data);
|
|
meilingColdChainMessageData.setHexData(data);
|
|
|
meilingColdChainMessageData.setDeviceId(device.getDeviceCode());
|
|
meilingColdChainMessageData.setDeviceId(device.getDeviceCode());
|
|
|
meilingColdChainMessageData.setModelName(MonitorDataProcessor.ML_WD_MODBUS);
|
|
meilingColdChainMessageData.setModelName(MonitorDataProcessor.ML_WD_MODBUS);
|
|
@@ -83,10 +83,9 @@ public class ModbusDataHandlerAdapter extends ChannelInboundHandlerAdapter {
|
|
|
/**
|
|
/**
|
|
|
* 获取IP地址
|
|
* 获取IP地址
|
|
|
*/
|
|
*/
|
|
|
- private String getIP(ChannelHandlerContext ctx) {
|
|
|
|
|
- String socketString = ctx.channel().remoteAddress().toString();
|
|
|
|
|
- int index = socketString.indexOf(":");
|
|
|
|
|
- return socketString.substring(1, index);
|
|
|
|
|
|
|
+ private String getLocalAddress(ChannelHandlerContext ctx) {
|
|
|
|
|
+ String socketString = ctx.channel().localAddress().toString();
|
|
|
|
|
+ return socketString;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -94,7 +93,7 @@ public class ModbusDataHandlerAdapter extends ChannelInboundHandlerAdapter {
|
|
|
@Override
|
|
@Override
|
|
|
public void channelInactive(ChannelHandlerContext ctx) {
|
|
public void channelInactive(ChannelHandlerContext ctx) {
|
|
|
Channel channel = ctx.channel();
|
|
Channel channel = ctx.channel();
|
|
|
- log.warn("客户端断开连接:" + channel.remoteAddress());
|
|
|
|
|
|
|
+ log.warn("客户端断开连接:" + channel.localAddress());
|
|
|
// 取消定时任务,避免内存泄漏
|
|
// 取消定时任务,避免内存泄漏
|
|
|
if (queryTimeout != null && !queryTimeout.isCancelled()) {
|
|
if (queryTimeout != null && !queryTimeout.isCancelled()) {
|
|
|
queryTimeout.cancel();
|
|
queryTimeout.cancel();
|
|
@@ -107,7 +106,7 @@ public class ModbusDataHandlerAdapter extends ChannelInboundHandlerAdapter {
|
|
|
if (channel.isActive()) { // 连接仍活跃时发送询问
|
|
if (channel.isActive()) { // 连接仍活跃时发送询问
|
|
|
// 美菱冰箱固定 发送固定询问指令(0x01)
|
|
// 美菱冰箱固定 发送固定询问指令(0x01)
|
|
|
channel.writeAndFlush("01 03 00 00 00 04 44 09");
|
|
channel.writeAndFlush("01 03 00 00 00 04 44 09");
|
|
|
- log.info("向客户端[" + channel.remoteAddress() + "]发送询问");
|
|
|
|
|
|
|
+ log.info("向客户端[" + channel.localAddress() + "]发送询问");
|
|
|
|
|
|
|
|
// 启动响应超时任务:若RESPONSE_TIMEOUT秒内未收到响应,断开连接
|
|
// 启动响应超时任务:若RESPONSE_TIMEOUT秒内未收到响应,断开连接
|
|
|
// queryTimeout = timer.newTimeout(timeout1 -> {
|
|
// queryTimeout = timer.newTimeout(timeout1 -> {
|