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

118 lines
4.7 KiB
Java

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;
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;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Random;
/**
* 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(), "yyyyMMddHHmmssSSS"));
data.setSt(StEnum.AIR.getValue());
data.setCn(CnEnum.REAL_TIME.getValue());
// 采集器密码
data.setPw("123456");
// 采集器编号
data.setMn("SCY00010001");
data.setFlag(DataFlagEnum.HAS_PNUM_ANSWER_V2017.getValue());
HJ212DataUtil.DataCp dataCp = new HJ212DataUtil.DataCp();
Random random = new Random();
// 实时值
dataCp.setRtd((double) random.nextInt(20));
// 折算实时值
dataCp.setZsRtd((double) random.nextInt(20));
// 污染因子
// dataCp.setPollId("w00000");
dataCp.setPollId("w01001");
// dataCp.setSn("CJQ000100010001");
dataCp.setSn("CJQ000100010002");
dataCp.setSampleTime(DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmssSSS"));
dataCp.setFlag(DataCpFlagEnum.N.getValue());
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("124.67.110.246", 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().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}