You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
193 lines
7.3 KiB
193 lines
7.3 KiB
//package com.mh.user.netty; |
|
// |
|
//import com.mh.user.constants.Constant; |
|
//import com.mh.user.entity.DeviceManageEntity; |
|
//import com.mh.user.service.DeviceManageService; |
|
//import com.mh.user.utils.*; |
|
//import io.netty.buffer.ByteBuf; |
|
//import io.netty.channel.Channel; |
|
//import io.netty.channel.ChannelHandlerAdapter; |
|
//import io.netty.channel.ChannelHandlerContext; |
|
//import io.netty.handler.timeout.IdleState; |
|
//import io.netty.handler.timeout.IdleStateEvent; |
|
//import io.netty.util.ReferenceCountUtil; |
|
//import lombok.extern.slf4j.Slf4j; |
|
//import org.springframework.context.ApplicationContext; |
|
// |
|
//import java.text.SimpleDateFormat; |
|
//import java.util.Date; |
|
//import java.util.List; |
|
// |
|
///** |
|
// * @author ljf |
|
// * @title : |
|
// * @description :客户端异步消息处理机制 |
|
// * @updateTime 2020-05-13 |
|
// * @throws : |
|
// */ |
|
//@Slf4j |
|
//public class NettyMeterAndCloudClientHandler extends ChannelHandlerAdapter { |
|
// |
|
// |
|
// private int num = 0; |
|
// private int size = 0; |
|
// private String receiveStr = null; |
|
// private int idle_count = 0; |
|
// private String IP = ""; |
|
// private String port = ""; |
|
// |
|
// List<DeviceManageEntity> deviceManageEntityList; |
|
// |
|
// // 调用service |
|
// ApplicationContext context = SpringBeanUtil.getApplicationContext(); |
|
// DeviceManageService deviceManageService = context.getBean(DeviceManageService.class); |
|
// |
|
// AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485(); |
|
// |
|
// @Override |
|
// public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { |
|
// log.info("当前channel从EventLoop取消注册"); |
|
// super.channelUnregistered(ctx); |
|
// } |
|
// |
|
// /** |
|
// * 超时处理 |
|
// * 如果120秒没有接受客户端的心跳,就触发; |
|
// * 如果超过3次,则直接关闭; |
|
// */ |
|
// @Override |
|
// public void userEventTriggered(ChannelHandlerContext ctx, Object obj) throws Exception { |
|
// if (obj instanceof IdleStateEvent) { |
|
// IdleStateEvent event = (IdleStateEvent) obj; |
|
// if (IdleState.READER_IDLE.equals(event.state())) { //如果读通道处于空闲状态,说明没有接收到心跳命令 |
|
// System.out.println("第" + idle_count + "已经20秒没有接收到服务器的信息了,发送的第" + num + "条数据"); |
|
// if (deviceManageEntityList.get(num) == null) { |
|
// ctx.channel().close(); |
|
// } else { |
|
// if (idle_count > 3 || num > size - 1) { |
|
// System.out.println("关闭这个不活跃的channel"); |
|
// ctx.channel().close(); |
|
// } |
|
// |
|
// SendOrderUtils.sendMeterOrCloud(deviceManageEntityList.get(num), num, IP, port, ctx); |
|
// idle_count++; |
|
// } |
|
// } |
|
// } else { |
|
// super.userEventTriggered(ctx, obj); |
|
// } |
|
// } |
|
// |
|
// @Override |
|
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
|
// log.info("通信异常!!"); |
|
// Channel incoming = ctx.channel(); |
|
// if (incoming.isActive()) { |
|
// log.info("SimpleClient: " + incoming.remoteAddress() + "异常"); |
|
// receiveStr = null; |
|
// cause.printStackTrace(); |
|
// ctx.close(); |
|
// } |
|
// } |
|
// |
|
// |
|
// @Override |
|
// public void channelActive(ChannelHandlerContext ctx) throws Exception { |
|
//// super.channelActive(ctx); |
|
// // 添加一个状态值,判断是否继续发送指令 update by ljf on 2020-08-07 |
|
// if (Constant.WEB_FLAG) { |
|
// num = 0; |
|
// // 关闭连接 |
|
// receiveStr = null; |
|
// ctx.close(); |
|
// } else { |
|
// SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); |
|
// Date date = new Date(); |
|
// log.info(ctx.channel().remoteAddress() + " " + sdf1.format(date) + "链接服务端成功!"); |
|
// |
|
// // 截取IP地址 |
|
// IP = ExchangeStringUtil.getMidString(ctx.channel().remoteAddress() + "", "/", ":"); |
|
// // 截取端口号 |
|
// port = ExchangeStringUtil.getMidString(ctx.channel().remoteAddress() + "", ":", ""); |
|
// log.info("IP: " + IP + ",端口号: " + port); |
|
// |
|
// // 生成对应的采集指令 |
|
// deviceManageEntityList = deviceManageService.queryDevicesByType(null); |
|
// size = deviceManageEntityList.size(); |
|
// |
|
// log.info("初始连接报文: " + receiveStr); |
|
// receiveStr = ""; |
|
// // 保持长连接,封装发送电表工具方法 update by ljf on 2021-01-26 |
|
// SendOrderUtils.sendMeterOrCloud(deviceManageEntityList.get(0), 0, IP, port, ctx); |
|
// } |
|
// } |
|
// |
|
// @Override |
|
// public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
|
// Thread.sleep(500); |
|
// receiveStr = null; |
|
// ctx.close(); |
|
// log.info(ctx.channel().localAddress() + "退出链接!!"); |
|
// } |
|
// |
|
// @Override |
|
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
|
// try { |
|
// ByteBuf buf = (ByteBuf) msg; |
|
// byte[] bytes = new byte[buf.readableBytes()]; |
|
// buf.readBytes(bytes);//复制内容到字节数组bytes |
|
// buf.clear(); |
|
// log.info("获取到的值: " + ExchangeStringUtil.bytesToHexString(bytes)); |
|
// if (bytes.length <= 62) { |
|
//// if (bytes.length <= 142) { |
|
//// receiveStr = receiveStr.replace("null", ""); |
|
//// receiveStr = receiveStr + ExchangeStringUtil.bytesToHexString(bytes);//将接收到的数据转为字符串,此字符串就是客户端发送的字符串 |
|
//// log.info(ctx.channel().remoteAddress() + " " + ctx.channel().localAddress() + " 接受服务器数据:" + receiveStr + ",大小: " + receiveStr.length()); |
|
// receiveStr = receiveStr + ExchangeStringUtil.bytesToHexString(bytes);//将接收到的数据转为字符串,此字符串就是客户端发送的字符串 |
|
// receiveStr = receiveStr.replace("null", ""); |
|
// log.info("接受服务器数据:" + receiveStr + ",大小: " + receiveStr.length()); |
|
// } |
|
// } catch (Exception e) { |
|
// e.printStackTrace(); |
|
// } finally { |
|
// ReferenceCountUtil.release(msg); |
|
// } |
|
// } |
|
// |
|
// @Override |
|
// public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
|
// log.info("采集电表或者冷量计-数据读取接收完成: " + receiveStr); |
|
// if ((receiveStr.length() == 36) && (deviceManageEntityList.get(num).getParamId() == 3)) { |
|
//// analysisReceiveOrder485.analysisCloudOrder485(receiveStr); // 解析冷量计 |
|
// } else if ((receiveStr.length() == 62) && (deviceManageEntityList.get(num).getParamId() == 1)) { |
|
//// 把receiveStr的"null"值去掉 |
|
//// a9fec2c71f9002fefefefe6839025007000068810643c3bb446c338d16c2b8 |
|
//// A9 FE C2 C7 1F 90 02 FE FE FE FE 68 39 02 50 07 00 00 68 81 06 43 C3 5B 38 6C 33 21 16 F8 12 |
|
// analysisReceiveOrder485.analysisMeterOrder485(receiveStr,"","","", null); // 解析电表 |
|
// } |
|
// receiveStr = ""; |
|
// num = num + 1; |
|
// Thread.sleep(600); |
|
// if (num > size - 1) { |
|
// num = 0; |
|
// receiveStr = null; |
|
// // 关闭连接 |
|
// ctx.close(); |
|
//// // 保持长连接,封装发送电表工具方法 update by ljf on 2021-01-26 |
|
//// SendOrderUtils.sendMeterOrder(deviceManageEntityList.get(num),num,IP,port,ctx); |
|
// } else { |
|
// // 添加一个状态值,判断是否继续发送指令 update by ljf on 2020-08-07 |
|
// if (Constant.WEB_FLAG) { |
|
// log.info("有指令下发退出定时采集DDC参数"); |
|
// num = 0; |
|
// // 关闭连接 |
|
// receiveStr = null; |
|
// ctx.close(); |
|
// } else { |
|
// // 封装发送电表工具方法 update by ljf on 2021-01-26 |
|
// SendOrderUtils.sendMeterOrCloud(deviceManageEntityList.get(num), num, IP, port, ctx); |
|
// } |
|
// } |
|
// ctx.flush(); |
|
// } |
|
//}
|
|
|