物联网通讯协议 iot-modbus V3.2.6 发布

  • 更新内容

iot-modbus 本次发布的 V3.2.6 版本支持设备上线、掉线、处理业务异常监听处理,请看下面的源码解读。

  • 设备上线监听源码解读

主要是在 MiiListenerHandler 重写了 channelActive 方法

@Override  public void channelActive(ChannelHandlerContext ctx) throws Exception {    Channel channel = ctx.channel();    if(!JudgeEmptyUtils.isEmpty(channel.remoteAddress())){      String address = channel.remoteAddress().toString().substring(1,channel.remoteAddress().toString().length());      ChannelConnectData connectServerData = new ChannelConnectData(this, DeviceConnectEnum.ON_LINE.getKey(), address);      if(!JudgeEmptyUtils.isEmpty(connectServerData) && !JudgeEmptyUtils.isEmpty(getApplicationContext)){        getApplicationContext.publishEvent(connectServerData);      }    }  }
  • 设备掉线监听源码解读

主要是在 MiiListenerHandler 重写了 channelInactive 方法。

@Override  public void channelInactive(ChannelHandlerContext ctx) throws Exception {    Channel channel = ctx.channel();    if(!JudgeEmptyUtils.isEmpty(channel.remoteAddress())){      String address = channel.remoteAddress().toString().substring(1,channel.remoteAddress().toString().length());      ChannelConnectData connectServerData = new ChannelConnectData(this, DeviceConnectEnum.BREAK_OFF.getKey(), address);      if(!JudgeEmptyUtils.isEmpty(connectServerData) && !JudgeEmptyUtils.isEmpty(getApplicationContext)){        getApplicationContext.publishEvent(connectServerData);      }    }    //连接断开后的最后处理    ctx.pipeline().remove(this);    ctx.deregister();    ctx.close();  }
  • 处理业务异常监听源码解读

主要是增加了 MiiExceptionHandler 处理器,并在服务端 MiiServer 和客户端 MiiClient 初始化 initChannel 时添加进入。​​​​​​​

package com.takeoff.iot.modbus.netty.handle;
import com.takeoff.iot.modbus.common.entity.ChannelConnectData;import com.takeoff.iot.modbus.common.enums.DeviceConnectEnum;import com.takeoff.iot.modbus.common.utils.JudgeEmptyUtils;import com.takeoff.iot.modbus.common.utils.SpringContextUtil;import org.springframework.context.ApplicationContext;
import io.netty.channel.Channel;import io.netty.channel.ChannelDuplexHandler;import io.netty.channel.ChannelHandlerContext;import lombok.extern.slf4j.Slf4j;
@Slf4jpublic class MiiExceptionHandler extends ChannelDuplexHandler {    private ApplicationContext getApplicationContext = SpringContextUtil.applicationContext;    public MiiExceptionHandler(){      }
  @Override    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {       Channel channel = ctx.channel();      if(!JudgeEmptyUtils.isEmpty(channel.remoteAddress())){        String address = channel.remoteAddress().toString().substring(1,channel.remoteAddress().toString().length());          ChannelConnectData connectServerData = new ChannelConnectData(this, DeviceConnectEnum.ABNORMAL.getKey(), address);            if(!JudgeEmptyUtils.isEmpty(connectServerData) && !JudgeEmptyUtils.isEmpty(getApplicationContext)){              getApplicationContext.publishEvent(connectServerData);            }            //由Tail节点对异常进行统一处理            if(cause instanceof RuntimeException){                log.info("处理业务异常:"+channel.remoteAddress());            }            super.exceptionCaught(ctx, cause);      }    }}

  • 连接监听器源码解读

主要通过 spring 的发布时间监听来处理,增加连接监听器 ChannelConnectListener。​​​​​​​

package com.takeoff.iot.modbus.test.listener;
import com.takeoff.iot.modbus.common.entity.ChannelConnectData;import com.takeoff.iot.modbus.common.utils.JudgeEmptyUtils;import org.springframework.context.event.EventListener;import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Slf4j@Componentpublic class ChannelConnectListener {
  @EventListener  public void handleReceiveDataEvent(ChannelConnectData data) {    if(JudgeEmptyUtils.isEmpty(data.getDeviceConnect())){      log.info("设备连接状态码:"+data.getDeviceConnect()+" ---> "+data.getConnectMsg());    }  }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾飞开源

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值