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

112 lines
4.5 KiB
Java
Raw Normal View History

2021-03-19 21:08:24 +08:00
package cn.com.tenlion.pollutantdata;
import cn.com.tenlion.pollutantdata.enums.CnEnum;
import cn.com.tenlion.pollutantdata.enums.DataFlagEnum;
import cn.com.tenlion.pollutantdata.enums.StEnum;
import cn.com.tenlion.pollutantdata.utils.HJ212DataUtil;
import cn.com.tenlion.pollutantdata.utils.core.T212Parser;
import ink.wgink.util.date.DateUtil;
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.jupiter.api.Test;
import org.springframework.util.unit.DataUnit;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 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();
data.setQn(DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmsszzz"));
data.setSt(StEnum.AIR.getValue());
data.setCn(CnEnum.REAL_TIME.getValue());
// 采集器密码
data.setPw("123456");
// 采集器编号
data.setMn("41018311021510");
data.setFlag(DataFlagEnum.HAS_PNUM_ANSWER_V2017.getValue());
HJ212DataUtil.DataCp dataCp = new HJ212DataUtil.DataCp();
// 实时值
dataCp.setRtd(18.0D);
// 折算实时值
dataCp.setZsRtd(6.0D);
// 污染因子
dataCp.setPollId("");
dataCp.setDataTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmsszzz"));
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();
}
});
}
});
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6666).sync();
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));
});
channelFuture.channel().closeFuture();
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}