|
@@ -0,0 +1,66 @@
|
|
|
+package com.hb.proj.gather.business;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import com.hb.proj.gather.protocol.ChannelGroupMgr;
|
|
|
+import com.hb.proj.gather.protocol.ZLOpdProtCMDEnum;
|
|
|
+
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.ByteBufAllocator;
|
|
|
+import io.netty.buffer.ByteBufUtil;
|
|
|
+import io.netty.channel.Channel;
|
|
|
+import io.netty.channel.ChannelFuture;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 单值采集任务 具体执行采集任务,下发采集指令
|
|
|
+ * @author cwen
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class GatherSingleTask implements Runnable {
|
|
|
+
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(GatherSingleTask.class);
|
|
|
+
|
|
|
+ private Channel channel;
|
|
|
+
|
|
|
+ public GatherSingleTask(Channel channel) {
|
|
|
+ this.channel=channel;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ logger.info("单值采集开始...");
|
|
|
+ ZLOpdProtCMDEnum[] cmds= {ZLOpdProtCMDEnum.PRESS_TEMP_LOAD,ZLOpdProtCMDEnum.CURR_VOL_LOS_PW,ZLOpdProtCMDEnum.FREQ_STROKE};
|
|
|
+ ByteBufAllocator alloc=channel.alloc();
|
|
|
+ ByteBuf byteBuf=alloc.directBuffer(); //
|
|
|
+ //ChannelFuture future=null;
|
|
|
+ for(ZLOpdProtCMDEnum cmd : cmds) {
|
|
|
+ byteBuf=alloc.directBuffer();
|
|
|
+
|
|
|
+ byteBuf.writeBytes(cmd.getCmd());
|
|
|
+ try {
|
|
|
+ //logger.info("准备发送指令:{},{}",ByteBufUtil.hexDump(byteBuf),LocalDateTime.now());
|
|
|
+ channel.writeAndFlush(byteBuf).sync();
|
|
|
+ channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name());
|
|
|
+
|
|
|
+ logger.info("发送完后:{}",LocalDateTime.now());
|
|
|
+ //byteBuf.readerIndex(byteBuf.writerIndex()); //已写入的数据标记为已读,成为可丢弃数据
|
|
|
+ //byteBuf.discardReadBytes();
|
|
|
+ Thread.sleep(1000); //等待0.1秒再发下条指令 时间过短 多条指令的返回消息会粘包半包
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //byteBuf.readerIndex(byteBuf.writerIndex()); //已写入的数据标记为已读,成为可丢弃数据
|
|
|
+ //byteBuf.discardReadBytes(); //丢弃已读数据,使原本的空间可以重新算作可写
|
|
|
+ //byteBuf.clear(); //清空下次复用
|
|
|
+ //byteBuf.resetWriterIndex();
|
|
|
+ //byteBuf.release();
|
|
|
+ }
|
|
|
+
|
|
|
+ //byteBuf.release(); //手动释放
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|