pollutant-data/src/test/java/cn/com/tenlion/pollutantdata/HJ212Test.java

118 lines
4.7 KiB
Java
Raw Normal View History

2021-03-19 21:08:24 +08:00
package cn.com.tenlion.pollutantdata;
import com.cm.common.utils.DateUtil;
import com.cm.tenlion.pollutantdata.enums.CnEnum;
import com.cm.tenlion.pollutantdata.enums.DataCpFlagEnum;
import com.cm.tenlion.pollutantdata.enums.DataFlagEnum;
import com.cm.tenlion.pollutantdata.enums.StEnum;
import com.cm.tenlion.pollutantdata.utils.HJ212DataUtil;
2021-03-19 21:08:24 +08:00
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
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.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;
import org.junit.Test;
2021-03-19 21:08:24 +08:00
import java.nio.charset.StandardCharsets;
import java.util.List;
2021-03-26 13:19:16 +08:00
import java.util.Random;
2021-03-19 21:08:24 +08:00
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: Demo
* @Description:
* @Author: wanggeng
* @Date: 2021/3/19 10:41 上午
* @Version: 1.0
*/
public class HJ212Test {
public HJ212DataUtil.Data getData() {
HJ212DataUtil.Data data = new HJ212DataUtil.Data();
2021-03-21 00:23:17 +08:00
data.setQn(DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmssSSS"));
2021-03-19 21:08:24 +08:00
data.setSt(StEnum.AIR.getValue());
data.setCn(CnEnum.REAL_TIME.getValue());
// 采集器密码
data.setPw("123456");
// 采集器编号
2021-03-26 13:19:16 +08:00
data.setMn("SCY00010001");
2021-03-19 21:08:24 +08:00
data.setFlag(DataFlagEnum.HAS_PNUM_ANSWER_V2017.getValue());
HJ212DataUtil.DataCp dataCp = new HJ212DataUtil.DataCp();
2021-03-26 13:19:16 +08:00
Random random = new Random();
2021-03-19 21:08:24 +08:00
// 实时值
2021-03-26 13:19:16 +08:00
dataCp.setRtd((double) random.nextInt(20));
2021-03-19 21:08:24 +08:00
// 折算实时值
2021-03-26 13:19:16 +08:00
dataCp.setZsRtd((double) random.nextInt(20));
2021-03-19 21:08:24 +08:00
// 污染因子
2021-03-26 13:19:16 +08:00
// dataCp.setPollId("w00000");
dataCp.setPollId("w01001");
// dataCp.setSn("CJQ000100010001");
dataCp.setSn("CJQ000100010002");
2021-03-21 00:23:17 +08:00
dataCp.setSampleTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmssSSS"));
dataCp.setFlag(DataCpFlagEnum.N.getValue());
2021-03-19 21:08:24 +08:00
data.setDataCp(dataCp);
return data;
}
@Test
public void hj212ClientTest() {
Bootstrap bootstrap = new Bootstrap();
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
bootstrap.group(eventLoopGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 1024 * 1024);
bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 1024 * 1024);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new MessageToByteEncoder<byte[]>() {
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, byte[] bytes, ByteBuf byteBuf) throws Exception {
byteBuf.writeBytes(bytes);
}
});
socketChannel.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
byte[] dataByte = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(dataByte);
String msg = new String(dataByte, CharsetUtil.UTF_8);
System.out.println(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
});
}
});
2021-03-29 22:39:31 +08:00
ChannelFuture channelFuture = bootstrap.connect("124.67.110.246", 6666).sync();
2021-03-19 21:08:24 +08:00
channelFuture.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
future.channel().pipeline().fireChannelInactive();
}
});
List<String> datas = HJ212DataUtil.respData(getData());
datas.forEach(data -> {
channelFuture.channel().writeAndFlush(data.getBytes(StandardCharsets.UTF_8));
});
2021-03-21 00:23:17 +08:00
channelFuture.channel().closeFuture().sync();
2021-03-19 21:08:24 +08:00
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}