mh
7 months ago
21 changed files with 84 additions and 2178 deletions
@ -1,41 +0,0 @@ |
|||||||
//package com.mh.user.job;
|
|
||||||
//
|
|
||||||
//import com.mh.user.netty.NettyClient;
|
|
||||||
//import com.mh.user.constants.SocketMessage;
|
|
||||||
//import lombok.SneakyThrows;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//import org.quartz.DisallowConcurrentExecution;
|
|
||||||
//import org.quartz.Job;
|
|
||||||
//import org.quartz.JobExecutionContext;
|
|
||||||
//import org.quartz.JobExecutionException;
|
|
||||||
//import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description : 定时采集冷量计任务
|
|
||||||
// * @updateTime 2020-05-18
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
///**
|
|
||||||
// * :@DisallowConcurrentExecution : 此标记用在实现Job的类上面,意思是不允许并发执行.
|
|
||||||
// * :注意org.quartz.threadPool.threadCount线程池中线程的数量至少要多个,否则@DisallowConcurrentExecution不生效
|
|
||||||
// * :假如Job的设置时间间隔为3秒,但Job执行时间是5秒,设置@DisallowConcurrentExecution以后程序会等任务执行完毕以后再去执行,
|
|
||||||
// * 否则会在3秒时再启用新的线程执行
|
|
||||||
// */
|
|
||||||
//@DisallowConcurrentExecution
|
|
||||||
//@Slf4j
|
|
||||||
//public class JobCloud implements Job {
|
|
||||||
//
|
|
||||||
// @Autowired
|
|
||||||
// private SocketMessage socketMessage;
|
|
||||||
//
|
|
||||||
// @SneakyThrows
|
|
||||||
// @Override
|
|
||||||
// public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
|
||||||
// // 定时采集冷量计
|
|
||||||
// log.info("定时采集冷量计");
|
|
||||||
// NettyClient nettyClient = new NettyClient();
|
|
||||||
// nettyClient.connect(socketMessage.getPort(),socketMessage.getIP());
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,39 +0,0 @@ |
|||||||
//package com.mh.user.job;
|
|
||||||
//
|
|
||||||
//import com.mh.user.netty.NettyMeterClient;
|
|
||||||
//import com.mh.user.constants.SocketMessage;
|
|
||||||
//import lombok.SneakyThrows;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//import org.quartz.DisallowConcurrentExecution;
|
|
||||||
//import org.quartz.Job;
|
|
||||||
//import org.quartz.JobExecutionContext;
|
|
||||||
//import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description : 定时采集电表数据任务
|
|
||||||
// * @updateTime 2020-05-18
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
///**
|
|
||||||
// * :@DisallowConcurrentExecution : 此标记用在实现Job的类上面,意思是不允许并发执行.
|
|
||||||
// * :注意org.quartz.threadPool.threadCount线程池中线程的数量至少要多个,否则@DisallowConcurrentExecution不生效
|
|
||||||
// * :假如Job的设置时间间隔为3秒,但Job执行时间是5秒,设置@DisallowConcurrentExecution以后程序会等任务执行完毕以后再去执行,
|
|
||||||
// * 否则会在3秒时再启用新的线程执行
|
|
||||||
// */
|
|
||||||
//@DisallowConcurrentExecution
|
|
||||||
//@Slf4j
|
|
||||||
//public class JobMeter implements Job {
|
|
||||||
//
|
|
||||||
// @Autowired
|
|
||||||
// private SocketMessage socketMessage;
|
|
||||||
//
|
|
||||||
// @SneakyThrows
|
|
||||||
// @Override
|
|
||||||
// public void execute(JobExecutionContext jobExecutionContext) {
|
|
||||||
// log.info("定时采集电表数据任务开始");
|
|
||||||
// NettyMeterClient nettyMeterClient = new NettyMeterClient();
|
|
||||||
// nettyMeterClient.connect(socketMessage.getPort(), socketMessage.getIP());
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,32 +0,0 @@ |
|||||||
package com.mh.user.job; |
|
||||||
|
|
||||||
import com.mh.user.constants.Constant; |
|
||||||
import lombok.SneakyThrows; |
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
import org.quartz.DisallowConcurrentExecution; |
|
||||||
import org.quartz.Job; |
|
||||||
import org.quartz.JobExecutionContext; |
|
||||||
import org.quartz.JobExecutionException; |
|
||||||
|
|
||||||
/** |
|
||||||
* :@DisallowConcurrentExecution : 此标记用在实现Job的类上面,意思是不允许并发执行. |
|
||||||
* :注意org.quartz.threadPool.threadCount线程池中线程的数量至少要多个,否则@DisallowConcurrentExecution不生效 |
|
||||||
* :假如Job的设置时间间隔为3秒,但Job执行时间是5秒,设置@DisallowConcurrentExecution以后程序会等任务执行完毕以后再去执行, |
|
||||||
* 否则会在3秒时再启用新的线程执行 |
|
||||||
*/ |
|
||||||
@DisallowConcurrentExecution |
|
||||||
@Slf4j |
|
||||||
public class JobTest implements Job { |
|
||||||
@SneakyThrows |
|
||||||
@Override |
|
||||||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { |
|
||||||
for (int i = 0; i < 30; i++) { |
|
||||||
if (Constant.FLAG) { |
|
||||||
break; |
|
||||||
} |
|
||||||
Thread.sleep(1000); |
|
||||||
log.info("第" + i + "个," +jobExecutionContext.getJobDetail()+"---------------------定时任务测试----------------------"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
@ -1,32 +0,0 @@ |
|||||||
package com.mh.user.job; |
|
||||||
|
|
||||||
import com.mh.user.constants.Constant; |
|
||||||
import lombok.SneakyThrows; |
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
import org.quartz.DisallowConcurrentExecution; |
|
||||||
import org.quartz.Job; |
|
||||||
import org.quartz.JobExecutionContext; |
|
||||||
import org.quartz.JobExecutionException; |
|
||||||
|
|
||||||
/** |
|
||||||
* :@DisallowConcurrentExecution : 此标记用在实现Job的类上面,意思是不允许并发执行. |
|
||||||
* :注意org.quartz.threadPool.threadCount线程池中线程的数量至少要多个,否则@DisallowConcurrentExecution不生效 |
|
||||||
* :假如Job的设置时间间隔为3秒,但Job执行时间是5秒,设置@DisallowConcurrentExecution以后程序会等任务执行完毕以后再去执行, |
|
||||||
* 否则会在3秒时再启用新的线程执行 |
|
||||||
*/ |
|
||||||
@DisallowConcurrentExecution |
|
||||||
@Slf4j |
|
||||||
public class JobTest1 implements Job { |
|
||||||
@SneakyThrows |
|
||||||
@Override |
|
||||||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { |
|
||||||
for (int i = 0; i < 30; i++) { |
|
||||||
if (Constant.FLAG) { |
|
||||||
break; |
|
||||||
} |
|
||||||
Thread.sleep(1000); |
|
||||||
log.info("第" + i + "个," +jobExecutionContext.getJobDetail()+"---------------------定时任务测试----------------------"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
@ -1,190 +0,0 @@ |
|||||||
package com.mh.user.job;//package com.mh.quartz.job;
|
|
||||||
//
|
|
||||||
//import com.mh.quartz.manage.SerialPortManager;
|
|
||||||
//import com.mh.quartz.utils.AnalysisReceiveOrder485;
|
|
||||||
//import com.mh.quartz.utils.ExchangeStringUtil;
|
|
||||||
//import com.mh.quartz.utils.GetReadOrder485;
|
|
||||||
//import gnu.io.PortInUseException;
|
|
||||||
//import gnu.io.SerialPort;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
//import org.springframework.stereotype.Component;
|
|
||||||
//
|
|
||||||
//import java.util.Date;
|
|
||||||
//import java.util.concurrent.BlockingQueue;
|
|
||||||
//import java.util.concurrent.LinkedBlockingQueue;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description : 定时采集发送水电表数据
|
|
||||||
// * @updateTime 2020-04-22
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
//@Component
|
|
||||||
//@Slf4j
|
|
||||||
//public class SendMeterWaterJob {
|
|
||||||
//
|
|
||||||
// // 串口对象
|
|
||||||
// private SerialPort mSerialPort;
|
|
||||||
//
|
|
||||||
// AnalysisReceiveOrder485 analysisReceiveOrder485;
|
|
||||||
//
|
|
||||||
// // 测试
|
|
||||||
// @Scheduled(cron = "0,5 * * * * ? ")
|
|
||||||
// public void test() throws InterruptedException {
|
|
||||||
// Thread.sleep(10000);
|
|
||||||
// log.info("延迟10s");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Scheduled(cron = "0,20,40 * * * * ? ")
|
|
||||||
// public void task1() throws PortInUseException, InterruptedException {
|
|
||||||
//
|
|
||||||
// // 堵塞队列用来存放读到的数据
|
|
||||||
// BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();
|
|
||||||
// log.info("每0、20、40秒执行一次!");
|
|
||||||
// if (mSerialPort == null){
|
|
||||||
// // 打开串口
|
|
||||||
// } else {
|
|
||||||
// // 关闭串口
|
|
||||||
// SerialPortManager.closePort(mSerialPort);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// mSerialPort = SerialPortManager.openPort("COM2",1200);
|
|
||||||
//
|
|
||||||
// // 发送数据(采集冷量计累计流量)
|
|
||||||
// String sendMeterOrderStr = "FEFEFE" + GetReadOrder485.createMeterOrder("080140001125","1");
|
|
||||||
// SerialPortManager.sendToPort(mSerialPort, ExchangeStringUtil.hexStrToBinaryStr(sendMeterOrderStr));
|
|
||||||
//
|
|
||||||
// // 添加串口监听
|
|
||||||
// SerialPort finalMSerialPort = mSerialPort;
|
|
||||||
// SerialPortManager.addListener(mSerialPort, () -> {
|
|
||||||
// byte[] data = null;
|
|
||||||
// try {
|
|
||||||
// // 读取串口数据
|
|
||||||
// data = SerialPortManager.readFromPort(finalMSerialPort);
|
|
||||||
// String needData = ExchangeStringUtil.printHexString(data);
|
|
||||||
// msgQueue.add(needData);
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.info(e.getMessage());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// // 创建线程解析队列信息
|
|
||||||
// Thread thread = new Thread(() -> {
|
|
||||||
// try {
|
|
||||||
// log.info("--------------任务处理线程运行了--------------");
|
|
||||||
// String vo = "";
|
|
||||||
// String vos[] = new String[0];
|
|
||||||
// while (true) {
|
|
||||||
// // 判断数组是否完整
|
|
||||||
// // 电表
|
|
||||||
// if (vos.length >= 22) {
|
|
||||||
// log.info("获取到数据长度: " + vos.length);
|
|
||||||
// log.info("电表: " + new Date() + " 完整收到的数据为:-----" + vo);
|
|
||||||
// // 解析接收到的报文
|
|
||||||
// analysisReceiveOrder485.analysisMeterOrder485(vo);
|
|
||||||
// vos = new String[0];
|
|
||||||
// vo = "";
|
|
||||||
//// sendOrder();
|
|
||||||
// } else {
|
|
||||||
// // 如果堵塞队列中存在数据就将其输出
|
|
||||||
// if (msgQueue.size() > 0) {
|
|
||||||
// if (vo == "") {
|
|
||||||
// vo = msgQueue.peek();
|
|
||||||
// } else {
|
|
||||||
// vo = vo + " " + msgQueue.peek();
|
|
||||||
// }
|
|
||||||
// vos = vo.split(" ", -1);
|
|
||||||
//// getData(vos);
|
|
||||||
// msgQueue.take();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// // TODO Auto-generated catch block
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// thread.start();
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Scheduled(cron = "10,30,50 * * * * ? ")
|
|
||||||
// public void task2() throws PortInUseException, InterruptedException {
|
|
||||||
// // 堵塞队列用来存放读到的数据
|
|
||||||
// BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();
|
|
||||||
// log.info("每10、30、50秒执行一次!");
|
|
||||||
// if (mSerialPort == null){
|
|
||||||
// // 打开串口
|
|
||||||
// } else {
|
|
||||||
// // 关闭串口
|
|
||||||
// SerialPortManager.closePort(mSerialPort);
|
|
||||||
// }
|
|
||||||
// mSerialPort = SerialPortManager.openPort("COM3",9600);
|
|
||||||
//
|
|
||||||
// // 发送数据(采集冷量计累计流量)
|
|
||||||
// String sendOrderStr = GetReadOrder485.createCloudOrder("88","34");
|
|
||||||
// SerialPortManager.sendToPort(mSerialPort, ExchangeStringUtil.hexStrToBinaryStr(sendOrderStr));
|
|
||||||
//
|
|
||||||
// // 添加串口监听
|
|
||||||
// SerialPort finalMSerialPort = mSerialPort;
|
|
||||||
// SerialPortManager.addListener(mSerialPort, () -> {
|
|
||||||
// byte[] data = null;
|
|
||||||
// try {
|
|
||||||
// // 读取串口数据
|
|
||||||
// data = SerialPortManager.readFromPort(finalMSerialPort);
|
|
||||||
// String needData = ExchangeStringUtil.printHexString(data);
|
|
||||||
// msgQueue.add(needData);
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.info(e.getMessage());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// // 创建线程解析队列信息
|
|
||||||
// Thread thread = new Thread(() -> {
|
|
||||||
// try {
|
|
||||||
// log.info("--------------任务处理线程运行了--------------");
|
|
||||||
// String vo = "";
|
|
||||||
// String vos[] = new String[0];
|
|
||||||
// while (true) {
|
|
||||||
// // 判断数组是否完整
|
|
||||||
// // 冷量计
|
|
||||||
// if (vos.length >= 9) {
|
|
||||||
// if (vos.length == 9) {
|
|
||||||
// log.info("获取到数据长度: " + vos.length);
|
|
||||||
// log.info("冷量计: " + new Date() + " 完整收到的数据为:-----" + vo);
|
|
||||||
// // 解析接收到的报文
|
|
||||||
// analysisReceiveOrder485.analysisCloudOrder485(vo);
|
|
||||||
// }
|
|
||||||
// vos = new String[0];
|
|
||||||
// vo = "";
|
|
||||||
//// sendOrder();
|
|
||||||
// } else {
|
|
||||||
// // 如果堵塞队列中存在数据就将其输出
|
|
||||||
// if (msgQueue.size() > 0) {
|
|
||||||
// if (vo.equalsIgnoreCase("")){
|
|
||||||
// vo = msgQueue.peek();
|
|
||||||
// } else {
|
|
||||||
// vo = vo + " " + msgQueue.peek();
|
|
||||||
// }
|
|
||||||
// vos = vo.split(" ", -1);
|
|
||||||
//// getData(vos);
|
|
||||||
// msgQueue.take();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// // TODO Auto-generated catch block
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// thread.start();
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
@ -1,98 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import com.mh.user.entity.OrderMessageEntity;
|
|
||||||
//import io.netty.bootstrap.Bootstrap;
|
|
||||||
//import io.netty.channel.*;
|
|
||||||
//import io.netty.channel.nio.NioEventLoopGroup;
|
|
||||||
//import io.netty.channel.socket.SocketChannel;
|
|
||||||
//import io.netty.channel.socket.nio.NioSocketChannel;
|
|
||||||
//import io.netty.handler.timeout.IdleStateHandler;
|
|
||||||
//import io.netty.handler.timeout.ReadTimeoutHandler;
|
|
||||||
//import lombok.Getter;
|
|
||||||
//import lombok.Setter;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//
|
|
||||||
//import java.util.List;
|
|
||||||
//import java.util.concurrent.TimeUnit;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description : 控制冷水机组和其他设备
|
|
||||||
// * @updateTime 2020-05-28
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
//@Setter
|
|
||||||
//@Getter
|
|
||||||
//@Slf4j
|
|
||||||
//public class NettyChillerControlClient {
|
|
||||||
//
|
|
||||||
//// private int port;
|
|
||||||
//// private String host;
|
|
||||||
//// private List<OrderMessageEntity> orderMessageEntityList;
|
|
||||||
//
|
|
||||||
// // 构造函数传递值 继承Thread时需要
|
|
||||||
//// public NettyChillerControlClient(int port, String host) {
|
|
||||||
//// this.port = port;
|
|
||||||
//// this.host = host;
|
|
||||||
//// }
|
|
||||||
//
|
|
||||||
// public void connect(int port, String host, List<OrderMessageEntity> orderMessageEntityList) throws InterruptedException {
|
|
||||||
// // 配置客户端NIO线程组
|
|
||||||
// EventLoopGroup group = new NioEventLoopGroup(1);
|
|
||||||
// try {
|
|
||||||
// Bootstrap bootstrap = new Bootstrap();
|
|
||||||
// bootstrap.group(group).channel(NioSocketChannel.class)
|
|
||||||
// .option(ChannelOption.TCP_NODELAY, true)
|
|
||||||
// .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
|
|
||||||
// .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1024*1024))
|
|
||||||
// .handler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
// @Override
|
|
||||||
// protected void initChannel(SocketChannel socketChannel) {
|
|
||||||
// // 基于换行符号
|
|
||||||
//// socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,4,4,-8,0));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
|
|
||||||
//// socketChannel.pipeline().addLast(new LengthFieldPrepender(4));
|
|
||||||
// // 超过10秒钟没有数据读取自动断开连接
|
|
||||||
//// socketChannel.pipeline().addLast(new ReadTimeoutHandler(30));
|
|
||||||
// // 超过10秒没有返回触发心跳机制 update by ljf on 2021-01-30
|
|
||||||
// socketChannel.pipeline().addLast(new IdleStateHandler(10,10,6, TimeUnit.SECONDS));
|
|
||||||
// // 在管道中添加我们自己的接收数据实现方法
|
|
||||||
// socketChannel.pipeline().addLast(new NettyChillerControlHandler(orderMessageEntityList));
|
|
||||||
//// socketChannel.pipeline().addLast(new NettyMeterClientHandler());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// // 发起异步连接操作
|
|
||||||
// ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
|
|
||||||
// if (channelFuture.isSuccess()) {
|
|
||||||
// log.info("connect server 成功---------");
|
|
||||||
// } else {
|
|
||||||
// log.info("连接失败!");
|
|
||||||
// log.info("准备重连!");
|
|
||||||
//// connect(port, host);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 等待客户端连接链路关闭future.channel().closeFuture().sync(); // 阻塞main线程
|
|
||||||
// channelFuture.channel().closeFuture().sync();
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.error(e.getMessage());
|
|
||||||
// } finally {
|
|
||||||
// group.shutdownGracefully();
|
|
||||||
//// try {
|
|
||||||
//// TimeUnit.SECONDS.sleep(5);
|
|
||||||
//// connect(port, host); // 断线重连
|
|
||||||
//// } catch (InterruptedException e) {
|
|
||||||
//// e.printStackTrace();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//// @SneakyThrows
|
|
||||||
//// @Override
|
|
||||||
//// public void run() {
|
|
||||||
//// connect(port, host);
|
|
||||||
//// }
|
|
||||||
//}
|
|
@ -1,311 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import com.mh.user.entity.OrderMessageEntity;
|
|
||||||
//import com.mh.user.service.impl.DeviceDisplayServiceImpl;
|
|
||||||
//import com.mh.user.constants.Constant;
|
|
||||||
//import com.mh.user.utils.ExchangeStringUtil;
|
|
||||||
//import com.mh.user.utils.SpringBeanUtil;
|
|
||||||
//import io.netty.buffer.ByteBuf;
|
|
||||||
//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 NettyChillerControlHandler extends ChannelHandlerAdapter {
|
|
||||||
//
|
|
||||||
// private int num = 0;
|
|
||||||
// private int size = 0;
|
|
||||||
// private String receiveStr = "";
|
|
||||||
// private int sendNum = 0;
|
|
||||||
// private int idle_count = 1;
|
|
||||||
//
|
|
||||||
// List<OrderMessageEntity> orderMessageEntityList;
|
|
||||||
//
|
|
||||||
// // 调用service
|
|
||||||
// ApplicationContext context = SpringBeanUtil.getApplicationContext();
|
|
||||||
// DeviceDisplayServiceImpl.GatewayManageService gatewayManageService = context.getBean(DeviceDisplayServiceImpl.GatewayManageService.class);
|
|
||||||
// //OrderMessageService orderMessageService = context.getBean(OrderMessageService.class);
|
|
||||||
//
|
|
||||||
// public NettyChillerControlHandler(List<OrderMessageEntity> orderMessageEntityList) {
|
|
||||||
// this.orderMessageEntityList = orderMessageEntityList;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// log.info("当前channel从EventLoop取消注册");
|
|
||||||
//// Constant.SEND_STATUS = false;
|
|
||||||
//// super.channelUnregistered(ctx);
|
|
||||||
// ctx.close();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 超时处理
|
|
||||||
// * 如果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 + "已经10秒没有接收到服务器的信息了,发送第" + num + "条数据");
|
|
||||||
// if (num > size - 1) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// Constant.SEND_STATUS = true;
|
|
||||||
// System.out.println("关闭这个不活跃的channel");
|
|
||||||
// ctx.close();
|
|
||||||
// } else if (idle_count > 3) {
|
|
||||||
// System.out.println("关闭这个不活跃的channel");
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// Constant.SEND_STATUS = false;
|
|
||||||
// ctx.close();
|
|
||||||
// } else {
|
|
||||||
// // 发送采集DDC指令
|
|
||||||
// // 判断空值
|
|
||||||
// if (orderMessageEntityList.get(num).getRegisterAddr() == null ||
|
|
||||||
// orderMessageEntityList.get(num).getRegisterAddr().equalsIgnoreCase("")) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// Constant.SEND_STATUS = true;
|
|
||||||
// ctx.close();
|
|
||||||
// } else {
|
|
||||||
// String sendStr = orderMessageEntityList.get(num).getOrderStr();
|
|
||||||
//// // 获取采集参数个数
|
|
||||||
//// size = orderMessageEntityList.size();
|
|
||||||
// // 2.发送数据
|
|
||||||
// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// idle_count++;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// super.userEventTriggered(ctx, obj);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
||||||
//// super.exceptionCaught(ctx, cause);
|
|
||||||
// log.info("通信异常!!");
|
|
||||||
// cause.printStackTrace();
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// Channel incoming = ctx.channel();
|
|
||||||
//// if (incoming.isActive()) {
|
|
||||||
//// // 重新发送
|
|
||||||
//// if (sendNum > 2) {
|
|
||||||
//// // 通信异常,发送失败
|
|
||||||
//// log.info("SimpleClient: " + incoming.remoteAddress() + "异常");
|
|
||||||
//// cause.printStackTrace();
|
|
||||||
//// Constant.SEND_STATUS = false;
|
|
||||||
//// ctx.close();
|
|
||||||
//// } else {
|
|
||||||
//// // 发送采集DDC指令
|
|
||||||
//// String sendStr = orderMessageEntityList.get(num).getOrderStr();
|
|
||||||
//// // 获取采集参数个数
|
|
||||||
//// size = orderMessageEntityList.size();
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx,sendStr);
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// sendNum += 1;
|
|
||||||
//// }
|
|
||||||
//// }
|
|
||||||
//// // 判断发送的下标,如果不等于指令数组大小
|
|
||||||
//// num = num + 1;
|
|
||||||
//// if (num > size-1) {
|
|
||||||
//// num = 0;
|
|
||||||
//// // 关闭连接
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// Constant.SEND_STATUS = true;
|
|
||||||
//// ctx.close();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// super.channelActive(ctx);
|
|
||||||
// SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
|
|
||||||
// Date date=new Date();
|
|
||||||
// log.info(ctx.channel().remoteAddress() + " " + sdf1.format(date) + "链接服务端成功!");
|
|
||||||
// // 截取IP地址
|
|
||||||
// String IP = ExchangeStringUtil.getMidString(ctx.channel().remoteAddress()+"","/", ":");
|
|
||||||
// // 截取端口号
|
|
||||||
// String port = ExchangeStringUtil.getMidString(ctx.channel().remoteAddress()+"",":", "");
|
|
||||||
// log.info("IP: " + IP + ",端口号: " + port);
|
|
||||||
// // 更新对应的网关在线情况
|
|
||||||
// gatewayManageService.updateGatewayManage(IP, port);
|
|
||||||
//
|
|
||||||
// // 发送控制DDC指令
|
|
||||||
// String sendStr = orderMessageEntityList.get(num).getOrderStr();
|
|
||||||
// // 获取采集参数个数
|
|
||||||
// size = orderMessageEntityList.size();
|
|
||||||
//
|
|
||||||
// // 2.发送数据
|
|
||||||
// ByteBuf buffer = getByteBuf(ctx,sendStr);
|
|
||||||
// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private ByteBuf getByteBuf(ChannelHandlerContext ctx, String sendStr) {
|
|
||||||
// // 申请一个数据结构存储信息
|
|
||||||
// ByteBuf buffer = ctx.alloc().buffer();
|
|
||||||
// // 将信息放入数据结构中
|
|
||||||
// buffer.writeBytes(ExchangeStringUtil.hexStrToBinaryStr(sendStr));//对接需要16进制
|
|
||||||
// return buffer;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
//// Thread.sleep(100);
|
|
||||||
// ctx.close();
|
|
||||||
// log.info(ctx.channel().localAddress() + "退出链接!!");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
||||||
//// super.channelRead(ctx, msg);
|
|
||||||
// // ByteBuf buf = (ByteBuf)msg;
|
|
||||||
//// byte[] req = new byte[buf.readableBytes()];
|
|
||||||
//// buf.readBytes(req);
|
|
||||||
//// String body = new String(req, "UTF-8");
|
|
||||||
// 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 <= 24) {
|
|
||||||
// if (bytes.length != 0) {
|
|
||||||
//// 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() == 24) {
|
|
||||||
// if (receiveStr.equalsIgnoreCase(orderMessageEntityList.get(num).getOrderStr())) {
|
|
||||||
// // 解析采集回来的数据
|
|
||||||
// log.info("采集完整的报文: " + receiveStr + ",指令下标: " + size);
|
|
||||||
// // 解析采集的报文
|
|
||||||
// // 更新发送后的指令
|
|
||||||
// OrderMessageEntity orderMessageEntity = new OrderMessageEntity();
|
|
||||||
// orderMessageEntity.setRegisterAddr(orderMessageEntityList.get(num).getRegisterAddr());
|
|
||||||
// orderMessageEntity.setCreateTime(orderMessageEntityList.get(num).getCreateTime());
|
|
||||||
// orderMessageEntity.setGrade(1);
|
|
||||||
// orderMessageEntity.setSendNum(1);
|
|
||||||
// orderMessageEntity.setStatus(1);
|
|
||||||
// orderMessageEntity.setOrderStr(orderMessageEntityList.get(num).getOrderStr());
|
|
||||||
// //orderMessageService.updateOrderMessage(orderMessageEntity);
|
|
||||||
//
|
|
||||||
//// // 关闭连接
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// Constant.SEND_STATUS = true;
|
|
||||||
//// ctx.close();
|
|
||||||
//
|
|
||||||
// // 清空receiveStr
|
|
||||||
// receiveStr = "";
|
|
||||||
// // 判断发送的下标,如果不等于指令数组大小
|
|
||||||
// num = num + 1;
|
|
||||||
// if (num > size - 1) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// Constant.SEND_STATUS = true;
|
|
||||||
// ctx.close();
|
|
||||||
// } else {
|
|
||||||
// Thread.sleep(4000);
|
|
||||||
// // 继续发送下一个采集DDC设备指令
|
|
||||||
// String sendStr = orderMessageEntityList.get(num).getOrderStr();
|
|
||||||
// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
// // 2.发送数据
|
|
||||||
// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// log.info("客户端再次往服务端发送数据" + num + ",报文: " + sendStr);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } else if ((receiveStr.length() > 24) && (num == 0)) {
|
|
||||||
// // 发送采集DDC指令
|
|
||||||
// String sendStr = orderMessageEntityList.get(num).getOrderStr();
|
|
||||||
// // 获取采集参数个数
|
|
||||||
// size = orderMessageEntityList.size();
|
|
||||||
// // 2.发送数据
|
|
||||||
// ByteBuf buffer = getByteBuf(ctx,sendStr);
|
|
||||||
// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// // 清空receiveStr
|
|
||||||
// receiveStr = "";
|
|
||||||
// sendNum += 1;
|
|
||||||
// } else if (sendNum > 2){
|
|
||||||
// // 更新发送后的指令
|
|
||||||
// OrderMessageEntity orderMessageEntity = new OrderMessageEntity();
|
|
||||||
// orderMessageEntity.setRegisterAddr(orderMessageEntityList.get(num).getRegisterAddr());
|
|
||||||
// orderMessageEntity.setCreateTime(orderMessageEntityList.get(num).getCreateTime());
|
|
||||||
// orderMessageEntity.setGrade(1);
|
|
||||||
// orderMessageEntity.setSendNum(sendNum);
|
|
||||||
// orderMessageEntity.setStatus(0);
|
|
||||||
// orderMessageEntity.setOrderStr(orderMessageEntityList.get(num).getOrderStr());
|
|
||||||
// //orderMessageService.updateOrderMessage(orderMessageEntity);
|
|
||||||
// Constant.SEND_STATUS = false;
|
|
||||||
// ctx.close();
|
|
||||||
// } else if ((receiveStr.length() > 24)) {
|
|
||||||
// // 接收采集DDC的数据
|
|
||||||
// // 解析采集的报文
|
|
||||||
//// AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485();
|
|
||||||
//// analysisReceiveOrder485.analysisChillersDDC(receiveStr);
|
|
||||||
//
|
|
||||||
// // 清空receiveStr
|
|
||||||
// receiveStr = "";
|
|
||||||
// // 更新发送后的指令
|
|
||||||
// OrderMessageEntity orderMessageEntity = new OrderMessageEntity();
|
|
||||||
// orderMessageEntity.setRegisterAddr(orderMessageEntityList.get(num).getRegisterAddr());
|
|
||||||
// orderMessageEntity.setCreateTime(orderMessageEntityList.get(num).getCreateTime());
|
|
||||||
// orderMessageEntity.setGrade(1);
|
|
||||||
// orderMessageEntity.setSendNum(1);
|
|
||||||
// orderMessageEntity.setStatus(1);
|
|
||||||
// orderMessageEntity.setOrderStr(orderMessageEntityList.get(num).getOrderStr());
|
|
||||||
// //orderMessageService.updateOrderMessage(orderMessageEntity);
|
|
||||||
//
|
|
||||||
// // 判断发送的下标,如果不等于指令数组大小
|
|
||||||
// num = num + 1;
|
|
||||||
//// Thread.sleep(500);
|
|
||||||
// if (num > size-1) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// Constant.SEND_STATUS = true;
|
|
||||||
// ctx.close();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// ctx.flush();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
@ -1,90 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import io.netty.bootstrap.Bootstrap;
|
|
||||||
//import io.netty.channel.*;
|
|
||||||
//import io.netty.channel.nio.NioEventLoopGroup;
|
|
||||||
//import io.netty.channel.socket.SocketChannel;
|
|
||||||
//import io.netty.channel.socket.nio.NioSocketChannel;
|
|
||||||
//import io.netty.handler.timeout.ReadTimeoutHandler;
|
|
||||||
//import lombok.Getter;
|
|
||||||
//import lombok.Setter;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description :Netty客户端,采集冷量计
|
|
||||||
// * @updateTime 2020-05-13
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
//@Slf4j
|
|
||||||
//@Setter
|
|
||||||
//@Getter
|
|
||||||
//public class NettyClient {
|
|
||||||
//
|
|
||||||
// private int port;
|
|
||||||
// private String host;
|
|
||||||
//
|
|
||||||
// // 构造函数传递值 继承Thread时需要
|
|
||||||
//// public NettyClient(int port, String host) {
|
|
||||||
//// this.port = port;
|
|
||||||
//// this.host = host;
|
|
||||||
//// }
|
|
||||||
//
|
|
||||||
// public void connect(int port, String host) throws InterruptedException {
|
|
||||||
// // 配置客户端NIO线程组
|
|
||||||
// EventLoopGroup group = new NioEventLoopGroup(1);
|
|
||||||
// try {
|
|
||||||
// Bootstrap bootstrap = new Bootstrap();
|
|
||||||
// bootstrap.group(group).channel(NioSocketChannel.class)
|
|
||||||
// .option(ChannelOption.TCP_NODELAY, true)
|
|
||||||
// .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
|
|
||||||
// .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1024*1024))
|
|
||||||
// .handler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
// @Override
|
|
||||||
// protected void initChannel(SocketChannel socketChannel) {
|
|
||||||
// // 基于换行符号
|
|
||||||
//// socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,4,4,-8,0));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
|
|
||||||
//// socketChannel.pipeline().addLast(new LengthFieldPrepender(4));
|
|
||||||
// // 超过10秒钟没有数据读取自动断开连接
|
|
||||||
// socketChannel.pipeline().addLast(new ReadTimeoutHandler(10));
|
|
||||||
// // 在管道中添加我们自己的接收数据实现方法
|
|
||||||
// socketChannel.pipeline().addLast(new NettyClientHandler());
|
|
||||||
//// socketChannel.pipeline().addLast(new NettyMeterClientHandler());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// // 发起异步连接操作
|
|
||||||
// ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
|
|
||||||
// if (channelFuture.isSuccess()) {
|
|
||||||
// log.info("connect server 成功---------");
|
|
||||||
// } else {
|
|
||||||
// log.info("连接失败!");
|
|
||||||
// log.info("准备重连!");
|
|
||||||
//// connect(port, host);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 等待客户端连接链路关闭future.channel().closeFuture().sync(); // 阻塞main线程
|
|
||||||
// channelFuture.channel().closeFuture().sync();
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.error(e.getMessage());
|
|
||||||
// } finally {
|
|
||||||
// group.shutdownGracefully();
|
|
||||||
//// try {
|
|
||||||
//// TimeUnit.SECONDS.sleep(5);
|
|
||||||
//// connect(port, host); // 断线重连
|
|
||||||
//// } catch (InterruptedException e) {
|
|
||||||
//// e.printStackTrace();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//// @SneakyThrows
|
|
||||||
//// @Override
|
|
||||||
//// public void run() {
|
|
||||||
//// connect(port, host);
|
|
||||||
//// }
|
|
||||||
//}
|
|
@ -1,295 +0,0 @@ |
|||||||
//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.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 NettyClientHandler extends ChannelHandlerAdapter {
|
|
||||||
//
|
|
||||||
// private int num = 0;
|
|
||||||
// private int size = 0;
|
|
||||||
// private String receiveStr = null;
|
|
||||||
// private String IP = null;
|
|
||||||
// private String port = null;
|
|
||||||
// List<DeviceManageEntity> deviceManageEntityList;
|
|
||||||
//
|
|
||||||
// // 调用service
|
|
||||||
// ApplicationContext context = SpringBeanUtil.getApplicationContext();
|
|
||||||
// DeviceManageService deviceManageService = context.getBean(DeviceManageService.class);
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// log.info("当前channel从EventLoop取消注册");
|
|
||||||
// super.channelUnregistered(ctx);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
||||||
//// super.exceptionCaught(ctx, cause);
|
|
||||||
// log.info("通信异常!!");
|
|
||||||
// receiveStr = null;
|
|
||||||
// Channel incoming = ctx.channel();
|
|
||||||
// if (incoming.isActive()){
|
|
||||||
// log.info("SimpleClient: " + incoming.remoteAddress() + "异常");
|
|
||||||
// cause.printStackTrace();
|
|
||||||
// ctx.close();
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// try {
|
|
||||||
//// TimeUnit.SECONDS.sleep(5);
|
|
||||||
//// SocketAddress remoteAddress = ctx.channel().remoteAddress();
|
|
||||||
//// String port = ExchangeStringUtil.endData(remoteAddress.toString(),":");
|
|
||||||
//// String host = ExchangeStringUtil.splitData(remoteAddress.toString(),"/",":");
|
|
||||||
//// NettyClient nettyClient = new NettyClient();
|
|
||||||
//// nettyClient.connect(Integer.parseInt(port), host); // 断线重连
|
|
||||||
//// } catch (InterruptedException e) {
|
|
||||||
//// e.printStackTrace();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelActive(ChannelHandlerContext ctx) {
|
|
||||||
// // 添加一个状态值,判断是否继续发送指令 update by ljf on 2020-08-07
|
|
||||||
// if (Constant.WEB_FLAG) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// ctx.close();
|
|
||||||
// } else {
|
|
||||||
// ctx.channel().read();
|
|
||||||
// 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("3");
|
|
||||||
// size = deviceManageEntityList.size();
|
|
||||||
//
|
|
||||||
// // 封装工具类进行采集,update by ljf on 2021-01-26
|
|
||||||
// SendOrderUtils.sendCloudOrder(deviceManageEntityList.get(0),0,IP,port,ctx);
|
|
||||||
//// // 1.创建将要写出的数据
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(0).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createCloudOrder(IP, port,
|
|
||||||
//// deviceManageEntityList.get(0).getDataCom(),
|
|
||||||
//// collectionNum, "34");
|
|
||||||
////// String sendStr = "5803004900021914";
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//// String sendStr = "5803004900021914"; // 冷量计
|
|
||||||
//// // 申请一个数据结构存储信息
|
|
||||||
//// ByteBuf buffer = ctx.alloc().buffer();
|
|
||||||
//// // 将信息放入数据结构中
|
|
||||||
//// buffer.writeBytes(ExchangeStringUtil.hexStrToBinaryStr(sendStr));//对接需要16进制
|
|
||||||
//// ctx.writeAndFlush(buffer, ctx.newProgressivePromise());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private ByteBuf getByteBuf(ChannelHandlerContext ctx, String sendStr) {
|
|
||||||
// // byte类型的数据
|
|
||||||
//// byte[] bytes = "这里是将要写往服务端的数据".getBytes(Charset.forName("utf-8"));
|
|
||||||
//// String sendStr = "5803004900021914"; // 冷量计
|
|
||||||
// // 申请一个数据结构存储信息
|
|
||||||
// ByteBuf buffer = ctx.alloc().buffer();
|
|
||||||
// // 将信息放入数据结构中
|
|
||||||
// buffer.writeBytes(ExchangeStringUtil.hexStrToBinaryStr(sendStr));//对接需要16进制
|
|
||||||
// return buffer;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// Thread.sleep(100);
|
|
||||||
// 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 <= 36) {
|
|
||||||
//// 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);
|
|
||||||
//// A9 FE C2 C7 1F 90 01 58 03 04 4A 30 00 53 65 1C C4 06
|
|
||||||
// if (receiveStr.length() == 36) {
|
|
||||||
// // 接收到的报文
|
|
||||||
// log.info("接收完整报文: " + receiveStr);
|
|
||||||
// // 解析报文
|
|
||||||
//// AnalysisReceiveOrder485 analysisReceiveOrder485 = new AnalysisReceiveOrder485();
|
|
||||||
//// analysisReceiveOrder485.analysisCloudOrder485(receiveStr); // 解析冷量计
|
|
||||||
// receiveStr = "";
|
|
||||||
// // 1.创建将要写出的数据
|
|
||||||
//// String sendStr = "5803004900021914";
|
|
||||||
// num = num + 1;
|
|
||||||
// Thread.sleep(500);
|
|
||||||
// if (num > size-1) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// receiveStr = null;
|
|
||||||
// ctx.close();
|
|
||||||
//// 保持长连接
|
|
||||||
//// // 封装工具类进行采集,update by ljf on 2021-01-26
|
|
||||||
//// SendOrderUtils.sendCloudOrder(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.sendCloudOrder(deviceManageEntityList.get(num),num,IP,port,ctx);
|
|
||||||
//// // 1.创建将要写出的数据
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createCloudOrder(IP, port,
|
|
||||||
//// deviceManageEntityList.get(num).getDataCom(),
|
|
||||||
//// collectionNum, "34");
|
|
||||||
////// String sendStr = "5803004900021914";
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// log.info("客户端再次往服务端发送数据" + sendStr);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// } else {
|
|
||||||
// log.info(receiveStr);
|
|
||||||
// receiveStr = null;
|
|
||||||
// ctx.flush();
|
|
||||||
// ctx.close();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//// if (receiveStr.contains("c0a801fc")) { // 冷量计
|
|
||||||
////
|
|
||||||
//// // 生成对应的采集指令
|
|
||||||
//// deviceManageEntityList = deviceManageService.queryDevicesByType("3");
|
|
||||||
//// size = deviceManageEntityList.size();
|
|
||||||
////
|
|
||||||
//// log.info("初始连接报文: " + receiveStr);
|
|
||||||
//// IP = receiveStr;
|
|
||||||
//// receiveStr = "";
|
|
||||||
//// // 1.创建将要写出的数据
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(0).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createCloudOrder(collectionNum,"34");
|
|
||||||
////// String sendStr = "5803004900021914";
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx,sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// } else if (receiveStr.contains("c0a801f0")) { // 电表
|
|
||||||
////
|
|
||||||
//// // 生成对应的采集指令
|
|
||||||
//// deviceManageEntityList = deviceManageService.queryDevicesByType("1");
|
|
||||||
//// size = deviceManageEntityList.size();
|
|
||||||
////
|
|
||||||
//// log.info("初始连接报文: " + receiveStr);
|
|
||||||
//// IP = receiveStr;
|
|
||||||
//// receiveStr = "";
|
|
||||||
//// // 1.创建将要写出的数据
|
|
||||||
////// String sendStr = "6830043080000068110432326536C816"; // 网络单相电表
|
|
||||||
////// String sendStr = "FEFEFEFE6880025007000068010243C3B216"; // 广仪三相电表
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(0).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createMeterOrder(collectionNum,"1");
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx,sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// } else if ((receiveStr.length() == 18) && (IP.contains("c0a801fc"))) {
|
|
||||||
//// analysisReceiveOrder485.analysisCloudOrder485(receiveStr); // 解析冷量计
|
|
||||||
//// receiveStr = "";
|
|
||||||
//// // 1.创建将要写出的数据
|
|
||||||
////// String sendStr = "5803004900021914";
|
|
||||||
//// num = num + 1;
|
|
||||||
//// Thread.sleep(1000);
|
|
||||||
//// if (num >= size-1) {
|
|
||||||
//// num = 0;
|
|
||||||
//// // 关闭连接
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// ctx.close();
|
|
||||||
//// } else {
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createCloudOrder(collectionNum, "34");
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// log.info("客户端再次往服务端发送数据" + num);
|
|
||||||
//// }
|
|
||||||
////
|
|
||||||
//// } else if ((receiveStr.length() == 44) && (IP.contains("c0a801f0"))) {
|
|
||||||
//// analysisReceiveOrder485.analysisMeterOrder485(receiveStr); // 解析电表
|
|
||||||
//// receiveStr = "";
|
|
||||||
//// num = num + 1;
|
|
||||||
//// Thread.sleep(1000);
|
|
||||||
//// if (num >= size-1) {
|
|
||||||
//// num = 0;
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// // 关闭连接
|
|
||||||
//// ctx.close();
|
|
||||||
//// } else {
|
|
||||||
//// // 1.创建将要写出的数据
|
|
||||||
//// // fe fe fe fe 68 80 02 50 07 00 00 68 81 06 43 c3 8c 34 33 33 5c 16
|
|
||||||
////// String sendStr = "FEFEFE6880025007000068010243C3B216";
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createMeterOrder(collectionNum, "1");
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// log.info("客户端再次往服务端发送数据" + num);
|
|
||||||
//// }
|
|
||||||
//// } else if ((receiveStr.length() > 44)) {
|
|
||||||
//// log.info(receiveStr);
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// ctx.flush();
|
|
||||||
//// ctx.close();
|
|
||||||
//// }
|
|
||||||
// ctx.flush();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
@ -1,179 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import com.mh.user.utils.ExchangeStringUtil;
|
|
||||||
//import io.netty.bootstrap.ServerBootstrap;
|
|
||||||
//import io.netty.buffer.ByteBuf;
|
|
||||||
//import io.netty.buffer.Unpooled;
|
|
||||||
//import io.netty.channel.*;
|
|
||||||
//import io.netty.channel.nio.NioEventLoopGroup;
|
|
||||||
//import io.netty.channel.socket.SocketChannel;
|
|
||||||
//import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
||||||
//import io.netty.handler.logging.LogLevel;
|
|
||||||
//import io.netty.handler.logging.LoggingHandler;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//import org.springframework.util.StringUtils;
|
|
||||||
//
|
|
||||||
//import java.io.IOException;
|
|
||||||
//import java.net.InetSocketAddress;
|
|
||||||
//
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :Netty
|
|
||||||
// * @description :netty 使用
|
|
||||||
// * @updateTime 2020-04-21
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
//@Slf4j
|
|
||||||
//public class NettyEchoServer {
|
|
||||||
//
|
|
||||||
// public void bind(int port) throws Exception {
|
|
||||||
// // accept线程组,用来接收连接
|
|
||||||
// EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
|
||||||
// // IO 线程组,用来处理业务逻辑
|
|
||||||
// EventLoopGroup workerGroup = new NioEventLoopGroup(1);
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// // 服务端启动引导
|
|
||||||
// ServerBootstrap serverBootstrap = new ServerBootstrap();
|
|
||||||
// serverBootstrap.group(bossGroup,workerGroup) // 绑定两个线程
|
|
||||||
// .channel(NioServerSocketChannel.class) // 指定通道类型
|
|
||||||
// .option(ChannelOption.SO_BACKLOG, 1024) // 设置TCP连接的缓冲区
|
|
||||||
// .handler(new LoggingHandler(LogLevel.INFO)) // 设置日志级别
|
|
||||||
// .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
// @Override
|
|
||||||
// protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
||||||
// ChannelPipeline pipeline = socketChannel.pipeline(); // 获取处理器链
|
|
||||||
// pipeline.addLast(new EchoServerHandler()); // 添加新的事件处理器
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// // 通过bind启动服务
|
|
||||||
// ChannelFuture f = serverBootstrap.bind(port).sync();
|
|
||||||
// // 阻塞主线程,知道网络服务被关闭
|
|
||||||
// f.channel().closeFuture().sync();
|
|
||||||
// } catch (Exception e){
|
|
||||||
// e.printStackTrace();
|
|
||||||
// } finally {
|
|
||||||
// workerGroup.shutdownGracefully();
|
|
||||||
// bossGroup.shutdownGracefully();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// static class EchoServerHandler extends ChannelHandlerAdapter {
|
|
||||||
//
|
|
||||||
// // 每当从客户端收到新的数据时,这个方法会在收到消息时被调用
|
|
||||||
// @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
|
|
||||||
// String receiveStr = ExchangeStringUtil.bytesToHexString(bytes);//将接收到的数据转为字符串,此字符串就是客户端发送的字符串
|
|
||||||
// log.info("接收到的数据: "+ receiveStr);
|
|
||||||
// //返回16进制到客户端
|
|
||||||
// writeToClient(receiveStr,ctx,"测试");
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// // TODO Auto-generated catch block
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
//// ctx.write(Unpooled.wrappedBuffer("Server message".getBytes()));
|
|
||||||
//// ctx.fireChannelRead(msg);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 数据读取完后被调用
|
|
||||||
// @Override
|
|
||||||
// public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// ctx.flush();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 当Netty由于IO错误或者处理器在处理事件时抛出的异常时被调用
|
|
||||||
// @Override
|
|
||||||
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
||||||
// cause.printStackTrace();
|
|
||||||
// ctx.close();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 客户端与服务端第一次建立连接时 执行
|
|
||||||
// *
|
|
||||||
// * @param ctx
|
|
||||||
// * @throws Exception
|
|
||||||
// */
|
|
||||||
// @Override
|
|
||||||
// public void channelActive(ChannelHandlerContext ctx) throws Exception, IOException
|
|
||||||
// {
|
|
||||||
// super.channelActive(ctx);
|
|
||||||
// ctx.channel().read();
|
|
||||||
// InetSocketAddress ifsock = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
||||||
// String clientIp = ifsock.getAddress().getHostAddress();
|
|
||||||
// //此处不能使用ctx.close(),否则客户端始终无法与服务端建立连接
|
|
||||||
// log.info("channelActive: "+clientIp + ctx.name());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 客户端与服务端 断连时 执行
|
|
||||||
// *
|
|
||||||
// * @param ctx
|
|
||||||
// * @throws Exception
|
|
||||||
// */
|
|
||||||
// @Override
|
|
||||||
// public void channelInactive(ChannelHandlerContext ctx) throws Exception, IOException
|
|
||||||
// {
|
|
||||||
// super.channelInactive(ctx);
|
|
||||||
// InetSocketAddress ifsock = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
||||||
// String clientIp = ifsock.getAddress().getHostAddress();
|
|
||||||
// ctx.close(); //断开连接时,必须关闭,否则造成资源浪费,并发量很大情况下可能造成宕机
|
|
||||||
// System.out.println("channelInactive:"+clientIp);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 服务端当read超时, 会调用这个方法
|
|
||||||
// *
|
|
||||||
// * @param ctx
|
|
||||||
// * @param evt
|
|
||||||
// * @throws Exception
|
|
||||||
// */
|
|
||||||
// @Override
|
|
||||||
// public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception, IOException
|
|
||||||
// {
|
|
||||||
// super.userEventTriggered(ctx, evt);
|
|
||||||
// InetSocketAddress ifsock = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
||||||
// String clientIp = ifsock.getAddress().getHostAddress();
|
|
||||||
// ctx.close();//超时时断开连接
|
|
||||||
// System.out.println("userEventTriggered:"+clientIp);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 公用回写数据到客户端的方法
|
|
||||||
// * @param channel
|
|
||||||
// * @param mark 用于打印/log的输出
|
|
||||||
// * <br>//channel.writeAndFlush(msg);//不行
|
|
||||||
// * <br>//channel.writeAndFlush(receiveStr.getBytes());//不行
|
|
||||||
// * <br>在netty里,进出的都是ByteBuf,楼主应确定服务端是否有对应的编码器,将字符串转化为ByteBuf
|
|
||||||
// */
|
|
||||||
// private void writeToClient(final String receiveStr, ChannelHandlerContext channel, final String mark) {
|
|
||||||
// try {
|
|
||||||
// ByteBuf buff = Unpooled.buffer();//netty需要用ByteBuf传输
|
|
||||||
// buff.writeBytes(ExchangeStringUtil.hexStrToBinaryStr(receiveStr));//对接需要16进制
|
|
||||||
// channel.writeAndFlush(buff).addListener((ChannelFutureListener) future -> {
|
|
||||||
// StringBuilder sb = new StringBuilder("");
|
|
||||||
// if(!StringUtils.isEmpty(mark)){
|
|
||||||
// sb.append("【").append(mark).append("】");
|
|
||||||
// }
|
|
||||||
// if (future.isSuccess()) {
|
|
||||||
// System.out.println(sb.toString()+"回写成功"+receiveStr);
|
|
||||||
// log.info(sb.toString()+"回写成功"+receiveStr);
|
|
||||||
// } else {
|
|
||||||
// System.out.println(sb.toString()+"回写失败"+receiveStr);
|
|
||||||
// log.error(sb.toString()+"回写失败"+receiveStr);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// System.out.println("调用通用writeToClient()异常"+e.getMessage());
|
|
||||||
// log.error("调用通用writeToClient()异常:",e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,97 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import io.netty.bootstrap.Bootstrap;
|
|
||||||
//import io.netty.channel.*;
|
|
||||||
//import io.netty.channel.nio.NioEventLoopGroup;
|
|
||||||
//import io.netty.channel.socket.SocketChannel;
|
|
||||||
//import io.netty.channel.socket.nio.NioSocketChannel;
|
|
||||||
//import io.netty.handler.timeout.IdleStateHandler;
|
|
||||||
//import io.netty.handler.timeout.ReadTimeoutHandler;
|
|
||||||
//import lombok.Getter;
|
|
||||||
//import lombok.Setter;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//
|
|
||||||
//import java.util.concurrent.TimeUnit;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description :Netty客户端,采集电表
|
|
||||||
// * @updateTime 2020-05-13
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
//@Slf4j
|
|
||||||
//@Setter
|
|
||||||
//@Getter
|
|
||||||
//public class NettyMeterAndCloudClient {
|
|
||||||
//// implements Runnable {
|
|
||||||
//
|
|
||||||
// private int port;
|
|
||||||
// private String host;
|
|
||||||
//
|
|
||||||
// // 构造函数传递值 继承Thread时需要
|
|
||||||
// public NettyMeterAndCloudClient(int port, String host) {
|
|
||||||
// this.port = port;
|
|
||||||
// this.host = host;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public NettyMeterAndCloudClient() {
|
|
||||||
// super();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public void connect(int port, String host) throws InterruptedException {
|
|
||||||
// // 配置客户端NIO线程组
|
|
||||||
// EventLoopGroup group = new NioEventLoopGroup(1);
|
|
||||||
// try {
|
|
||||||
// Bootstrap bootstrap = new Bootstrap();
|
|
||||||
// bootstrap.group(group).channel(NioSocketChannel.class)
|
|
||||||
// .option(ChannelOption.TCP_NODELAY, true)
|
|
||||||
// .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
|
|
||||||
// .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1024*1024))
|
|
||||||
// .handler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
// @Override
|
|
||||||
// protected void initChannel(SocketChannel socketChannel) {
|
|
||||||
// // 基于换行符号
|
|
||||||
//// socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
|
|
||||||
// // 超过10秒钟没有数据读取自动断开连接
|
|
||||||
// socketChannel.pipeline().addLast(new IdleStateHandler(10,10,10, TimeUnit.SECONDS));
|
|
||||||
// // 在管道中添加我们自己的接收数据实现方法
|
|
||||||
//// socketChannel.pipeline().addLast(new NettyClientHandler());
|
|
||||||
// socketChannel.pipeline().addLast(new NettyMeterAndCloudClientHandler());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// // 发起异步连接操作
|
|
||||||
// ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
|
|
||||||
// if (channelFuture.isSuccess()) {
|
|
||||||
// log.info("connect server 成功---------");
|
|
||||||
// } else {
|
|
||||||
// log.info("连接失败!");
|
|
||||||
// log.info("准备重连!");
|
|
||||||
//// connect(port, host);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 等待客户端连接链路关闭future.channel().closeFuture().sync(); // 阻塞main线程
|
|
||||||
// channelFuture.channel().closeFuture().sync();
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.error(e.getMessage());
|
|
||||||
// } finally {
|
|
||||||
// group.shutdownGracefully();
|
|
||||||
//// try {
|
|
||||||
//// TimeUnit.SECONDS.sleep(5);
|
|
||||||
//// connect(port, host); // 断线重连
|
|
||||||
//// } catch (InterruptedException e) {
|
|
||||||
//// e.printStackTrace();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
////
|
|
||||||
//// @SneakyThrows
|
|
||||||
//// @Override
|
|
||||||
//// public void run() {
|
|
||||||
//// connect(port, host);
|
|
||||||
//// }
|
|
||||||
//}
|
|
@ -1,193 +0,0 @@ |
|||||||
//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();
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,96 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import io.netty.bootstrap.Bootstrap;
|
|
||||||
//import io.netty.channel.*;
|
|
||||||
//import io.netty.channel.nio.NioEventLoopGroup;
|
|
||||||
//import io.netty.channel.socket.SocketChannel;
|
|
||||||
//import io.netty.channel.socket.nio.NioSocketChannel;
|
|
||||||
//import io.netty.handler.timeout.ReadTimeoutHandler;
|
|
||||||
//import lombok.Getter;
|
|
||||||
//import lombok.Setter;
|
|
||||||
//import lombok.SneakyThrows;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * @author ljf
|
|
||||||
// * @title :
|
|
||||||
// * @description :Netty客户端,采集电表
|
|
||||||
// * @updateTime 2020-05-13
|
|
||||||
// * @throws :
|
|
||||||
// */
|
|
||||||
//@Slf4j
|
|
||||||
//@Setter
|
|
||||||
//@Getter
|
|
||||||
//public class NettyMeterClient {
|
|
||||||
//// implements Runnable {
|
|
||||||
//
|
|
||||||
// private int port;
|
|
||||||
// private String host;
|
|
||||||
//
|
|
||||||
// // 构造函数传递值 继承Thread时需要
|
|
||||||
// public NettyMeterClient(int port, String host) {
|
|
||||||
// this.port = port;
|
|
||||||
// this.host = host;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public NettyMeterClient() {
|
|
||||||
// super();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public void connect(int port, String host) throws InterruptedException {
|
|
||||||
// // 配置客户端NIO线程组
|
|
||||||
// // 配置客户端NIO线程组
|
|
||||||
// EventLoopGroup group = new NioEventLoopGroup(1);
|
|
||||||
// try {
|
|
||||||
// Bootstrap bootstrap = new Bootstrap();
|
|
||||||
// bootstrap.group(group).channel(NioSocketChannel.class)
|
|
||||||
// .option(ChannelOption.TCP_NODELAY, true)
|
|
||||||
// .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
|
|
||||||
// .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1024*1024))
|
|
||||||
// .handler(new ChannelInitializer<SocketChannel>() {
|
|
||||||
// @Override
|
|
||||||
// protected void initChannel(SocketChannel socketChannel) {
|
|
||||||
// // 基于换行符号
|
|
||||||
//// socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
|
|
||||||
//// // 解码转String,注意调整自己的编码格式GBK、UTF-8
|
|
||||||
//// socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
|
|
||||||
// // 超过10秒钟没有数据读取自动断开连接
|
|
||||||
// socketChannel.pipeline().addLast(new ReadTimeoutHandler(20));
|
|
||||||
// // 在管道中添加我们自己的接收数据实现方法
|
|
||||||
//// socketChannel.pipeline().addLast(new NettyClientHandler());
|
|
||||||
// socketChannel.pipeline().addLast(new NettyMeterClientHandler1());
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// // 发起异步连接操作
|
|
||||||
// ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
|
|
||||||
// if (channelFuture.isSuccess()) {
|
|
||||||
// log.info("connect server 成功---------");
|
|
||||||
// } else {
|
|
||||||
// log.info("连接失败!");
|
|
||||||
// log.info("准备重连!");
|
|
||||||
//// connect(port, host);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 等待客户端连接链路关闭future.channel().closeFuture().sync(); // 阻塞main线程
|
|
||||||
// channelFuture.channel().closeFuture().sync();
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.error(e.getMessage());
|
|
||||||
// } finally {
|
|
||||||
// group.shutdownGracefully();
|
|
||||||
//// try {
|
|
||||||
//// TimeUnit.SECONDS.sleep(5);
|
|
||||||
//// connect(port, host); // 断线重连
|
|
||||||
//// } catch (InterruptedException e) {
|
|
||||||
//// e.printStackTrace();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
////
|
|
||||||
//// @SneakyThrows
|
|
||||||
//// @Override
|
|
||||||
//// public void run() {
|
|
||||||
//// connect(port, host);
|
|
||||||
//// }
|
|
||||||
//}
|
|
@ -1,143 +0,0 @@ |
|||||||
//package com.mh.user.netty;
|
|
||||||
//
|
|
||||||
//import com.mh.user.entity.DeviceManageEntity;
|
|
||||||
//import com.mh.user.service.DeviceManageService;
|
|
||||||
//import com.mh.user.utils.AnalysisReceiveOrder485;
|
|
||||||
//import com.mh.user.utils.ExchangeStringUtil;
|
|
||||||
//import com.mh.user.utils.GetReadOrder485;
|
|
||||||
//import com.mh.user.utils.SpringBeanUtil;
|
|
||||||
//import io.netty.buffer.ByteBuf;
|
|
||||||
//import io.netty.channel.Channel;
|
|
||||||
//import io.netty.channel.ChannelHandlerAdapter;
|
|
||||||
//import io.netty.channel.ChannelHandlerContext;
|
|
||||||
//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 NettyMeterClientHandler extends ChannelHandlerAdapter {
|
|
||||||
//
|
|
||||||
// private static int num = 0;
|
|
||||||
// private static int size = 0;
|
|
||||||
// private static String receiveStr = "";
|
|
||||||
// private static String IP = "";
|
|
||||||
// List<DeviceManageEntity> deviceManageEntityList;
|
|
||||||
//
|
|
||||||
// // 调用service
|
|
||||||
// ApplicationContext context = SpringBeanUtil.getApplicationContext();
|
|
||||||
// DeviceManageService deviceManageService = context.getBean(DeviceManageService.class);
|
|
||||||
//
|
|
||||||
// AnalysisReceiveOrder485 analysisReceiveOrder485;
|
|
||||||
//
|
|
||||||
// // 到处理IO事件,异常抛出时调用,已丢弃
|
|
||||||
// @Override
|
|
||||||
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
||||||
// Channel incoming = ctx.channel();
|
|
||||||
// if (!incoming.isActive()){
|
|
||||||
// log.info("通信异常!!");
|
|
||||||
// receiveStr = "";
|
|
||||||
// log.info("SimpleClient: " + incoming.remoteAddress() + "异常");
|
|
||||||
// cause.printStackTrace();
|
|
||||||
// ctx.close();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
|
|
||||||
// Date date=new Date();
|
|
||||||
// log.info(sdf1.format(date) + "链接服务端成功!");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private ByteBuf getByteBuf(ChannelHandlerContext ctx, String sendStr) {
|
|
||||||
// // byte类型的数据
|
|
||||||
//// byte[] bytes = "这里是将要写往服务端的数据".getBytes(Charset.forName("utf-8"));
|
|
||||||
//// String sendStr = "5803004900021914"; // 冷量计
|
|
||||||
// // 申请一个数据结构存储信息
|
|
||||||
// ByteBuf buffer = ctx.alloc().buffer();
|
|
||||||
// // 将信息放入数据结构中
|
|
||||||
// buffer.writeBytes(ExchangeStringUtil.hexStrToBinaryStr(sendStr));//对接需要16进制
|
|
||||||
// return buffer;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// Thread.sleep(500);
|
|
||||||
// ctx.close();
|
|
||||||
// log.info("退出链接!!");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
||||||
//// ByteBuf buf = (ByteBuf)msg;
|
|
||||||
//// byte[] req = new byte[buf.readableBytes()];
|
|
||||||
//// buf.readBytes(req);
|
|
||||||
//// String body = new String(req, "UTF-8");
|
|
||||||
// ByteBuf buf = (ByteBuf)msg;
|
|
||||||
// byte [] bytes = new byte[buf.readableBytes()];
|
|
||||||
// buf.readBytes(bytes);//复制内容到字节数组bytes
|
|
||||||
// receiveStr = receiveStr + ExchangeStringUtil.bytesToHexString(bytes);//将接收到的数据转为字符串,此字符串就是客户端发送的字符串
|
|
||||||
// log.info("接受服务器数据:" + receiveStr + ",大小: " + receiveStr.length());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// log.info("数据读取接收完成");
|
|
||||||
// if (receiveStr.contains("c0a801f0")) { // 电表
|
|
||||||
//
|
|
||||||
// // 生成对应的采集指令
|
|
||||||
// deviceManageEntityList = deviceManageService.queryDevicesByType("1");
|
|
||||||
// size = deviceManageEntityList.size();
|
|
||||||
//
|
|
||||||
// log.info("初始连接报文: " + receiveStr);
|
|
||||||
// IP = receiveStr;
|
|
||||||
// receiveStr = "";
|
|
||||||
// num = 0;
|
|
||||||
// // 1.创建将要写出的数据
|
|
||||||
//// String sendStr = "6830043080000068110432326536C816"; // 网络单相电表
|
|
||||||
//// String sendStr = "FEFEFEFE6880025007000068010243C3B216"; // 广仪三相电表
|
|
||||||
// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
// String sendStr = GetReadOrder485.createMeterOrder(collectionNum,"1");
|
|
||||||
// ByteBuf buffer = getByteBuf(ctx,sendStr);
|
|
||||||
// // 2.发送数据
|
|
||||||
// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// } else if ((receiveStr.length() == 44) && (IP.contains("c0a801f0"))) {
|
|
||||||
// analysisReceiveOrder485.analysisMeterOrder485(receiveStr); // 解析电表
|
|
||||||
// receiveStr = "";
|
|
||||||
// num = num + 1;
|
|
||||||
// Thread.sleep(1000);
|
|
||||||
// if (num >= size-1) {
|
|
||||||
// num = 0;
|
|
||||||
// // 关闭连接
|
|
||||||
// ctx.close();
|
|
||||||
// } else {
|
|
||||||
// // 1.创建将要写出的数据
|
|
||||||
// // fe fe fe fe 68 80 02 50 07 00 00 68 81 06 43 c3 8c 34 33 33 5c 16
|
|
||||||
//// String sendStr = "FEFEFE6880025007000068010243C3B216";
|
|
||||||
// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
// String sendStr = GetReadOrder485.createMeterOrder(collectionNum, "1");
|
|
||||||
// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
// // 2.发送数据
|
|
||||||
// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// log.info("客户端再次往服务端发送数据" + num);
|
|
||||||
// }
|
|
||||||
// } else if ((receiveStr.length() > 44)) {
|
|
||||||
// log.info(receiveStr);
|
|
||||||
// receiveStr = "";
|
|
||||||
// ctx.flush();
|
|
||||||
// ctx.close();
|
|
||||||
// }
|
|
||||||
// ctx.flush();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
@ -1,259 +0,0 @@ |
|||||||
//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.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 NettyMeterClientHandler1 extends ChannelHandlerAdapter {
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// private int num = 0;
|
|
||||||
// private int size = 0;
|
|
||||||
// private String receiveStr = null;
|
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
||||||
//// super.exceptionCaught(ctx, cause);
|
|
||||||
// log.info("通信异常!!");
|
|
||||||
//// receiveStr = null;
|
|
||||||
// Channel incoming = ctx.channel();
|
|
||||||
// if (incoming.isActive()) {
|
|
||||||
// log.info("SimpleClient: " + incoming.remoteAddress() + "异常");
|
|
||||||
// receiveStr = null;
|
|
||||||
// cause.printStackTrace();
|
|
||||||
// ctx.close();
|
|
||||||
//// receiveStr = null;
|
|
||||||
//// try {
|
|
||||||
//// TimeUnit.SECONDS.sleep(5);
|
|
||||||
//// SocketAddress remoteAddress = ctx.channel().remoteAddress();
|
|
||||||
//// String port = ExchangeStringUtil.endData(remoteAddress.toString(),":");
|
|
||||||
//// String host = ExchangeStringUtil.splitData(remoteAddress.toString(),"/",":");
|
|
||||||
//// NettyClient nettyClient = new NettyClient();
|
|
||||||
//// nettyClient.connect(Integer.parseInt(port), host); // 断线重连
|
|
||||||
//// } catch (InterruptedException e) {
|
|
||||||
//// e.printStackTrace();
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// @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 {
|
|
||||||
// ctx.channel().read();
|
|
||||||
// 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);
|
|
||||||
//
|
|
||||||
// // 生成对应的采集指令
|
|
||||||
// // 修改生成指令(冷量计和电量一起采集) update by ljf on 2021-01-27
|
|
||||||
// deviceManageEntityList = deviceManageService.queryDevicesByType(null);
|
|
||||||
// size = deviceManageEntityList.size();
|
|
||||||
//
|
|
||||||
// log.info("初始连接报文: " + receiveStr);
|
|
||||||
// receiveStr = "";
|
|
||||||
// // 保持长连接,封装发送电表工具方法 update by ljf on 2021-01-26
|
|
||||||
// SendOrderUtils.sendMeterOrder(deviceManageEntityList.get(0), 0, IP, port, ctx);
|
|
||||||
// // 1.创建将要写出的数据
|
|
||||||
//// String sendStr = "6830043080000068110432326536C816"; // 网络单相电表
|
|
||||||
//// String sendStr = "FEFEFEFE6880025007000068010243C3B216"; // 广仪三相电表
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(0).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createMeterOrder(IP, port,
|
|
||||||
//// deviceManageEntityList.get(0).getDataCom(), collectionNum, "1");
|
|
||||||
////// FileUtils.createFileAndWrite(sendStr, 0);
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private ByteBuf getByteBuf(ChannelHandlerContext ctx, String sendStr) {
|
|
||||||
// // byte类型的数据
|
|
||||||
//// byte[] bytes = "这里是将要写往服务端的数据".getBytes(Charset.forName("utf-8"));
|
|
||||||
//// String sendStr = "5803004900021914"; // 冷量计
|
|
||||||
// // 申请一个数据结构存储信息
|
|
||||||
// ByteBuf buffer = ctx.alloc().buffer();
|
|
||||||
// // 将信息放入数据结构中
|
|
||||||
// buffer.writeBytes(ExchangeStringUtil.hexStrToBinaryStr(sendStr));//对接需要16进制
|
|
||||||
// return buffer;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @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);
|
|
||||||
// }
|
|
||||||
//// super.channelRead(ctx, msg);
|
|
||||||
// // ByteBuf buf = (ByteBuf)msg;
|
|
||||||
//// byte[] req = new byte[buf.readableBytes()];
|
|
||||||
//// buf.readBytes(req);
|
|
||||||
//// String body = new String(req, "UTF-8");
|
|
||||||
//// ByteBuf buf = (ByteBuf)msg;
|
|
||||||
//// byte [] bytes = new byte[buf.readableBytes()];
|
|
||||||
//// buf.readBytes(bytes);//复制内容到字节数组bytes
|
|
||||||
//// log.info("获取到的值: " + ExchangeStringUtil.bytesToHexString(bytes));
|
|
||||||
//// if (bytes.length != 0) {
|
|
||||||
////// 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());
|
|
||||||
//// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
// log.info("采集电表-数据读取接收完成: " + receiveStr);
|
|
||||||
//// 把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
|
|
||||||
// if ((receiveStr.length() == 62)) {
|
|
||||||
//// log.info(receiveStr);
|
|
||||||
// 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.sendMeterOrder(deviceManageEntityList.get(num), num, IP, port, ctx);
|
|
||||||
// // 1.创建将要写出的数据
|
|
||||||
// // fe fe fe fe 68 80 02 50 07 00 00 68 81 06 43 c3 8c 34 33 33 5c 16
|
|
||||||
//// String sendStr = "FEFEFE6880025007000068010243C3B216";
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createMeterOrder(IP, port,
|
|
||||||
//// deviceManageEntityList.get(num).getDataCom(), collectionNum, "1");
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// log.info("客户端再次往服务端发送数据" + num);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } else if ((receiveStr.length() > 62)) {
|
|
||||||
// receiveStr = null;
|
|
||||||
// num = num + 1;
|
|
||||||
// Thread.sleep(500);
|
|
||||||
// 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.sendMeterOrder(deviceManageEntityList.get(num), num, IP, port, ctx);
|
|
||||||
// // 1.创建将要写出的数据
|
|
||||||
// // fe fe fe fe 68 80 02 50 07 00 00 68 81 06 43 c3 8c 34 33 33 5c 16
|
|
||||||
//// String sendStr = "FEFEFE6880025007000068010243C3B216";
|
|
||||||
//// String collectionNum = deviceManageEntityList.get(num).getCollectionNum();
|
|
||||||
//// String sendStr = GetReadOrder485.createMeterOrder(IP, port,
|
|
||||||
//// deviceManageEntityList.get(num).getDataCom(), collectionNum, "1");
|
|
||||||
//// ByteBuf buffer = getByteBuf(ctx, sendStr);
|
|
||||||
//// // 2.发送数据
|
|
||||||
//// ctx.channel().writeAndFlush(buffer);
|
|
||||||
//// log.info("客户端再次往服务端发送数据" + num);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// ctx.flush();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
Loading…
Reference in new issue