package com.hb.proj.gather.scheduler; 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.channel.Channel; /** * 功图采集任务 * 关键点:1、先检测功图点数是否为255,是:功图就绪,否:不可采集功图; * 2、功图中的位移、载荷、电流、功率 每项是分3次获取。 * @author cwen * */ public class GatherMultiTask implements Runnable { private final static Logger logger = LoggerFactory.getLogger(GatherMultiTask.class); private Channel channel; public GatherMultiTask(Channel channel) { this.channel=channel; } /** * 约8s采集完 用时不稳定,有时很慢 */ @Override public void run() { logger.info("多值采集开始..."); ZLOpdProtCMDEnum preCmd=ZLOpdProtCMDEnum.DIAGRAM_POINT_COUNT; //前置命令,返回数据为250 才继续后面的 ZLOpdProtCMDEnum[] cmds= { ZLOpdProtCMDEnum.DIAGRAM_DISP_1, ZLOpdProtCMDEnum.DIAGRAM_DISP_2, ZLOpdProtCMDEnum.DIAGRAM_DISP_3, ZLOpdProtCMDEnum.DIAGRAM_LOAD_1, ZLOpdProtCMDEnum.DIAGRAM_LOAD_2, ZLOpdProtCMDEnum.DIAGRAM_LOAD_3, ZLOpdProtCMDEnum.DIAGRAM_CURR_1, ZLOpdProtCMDEnum.DIAGRAM_CURR_2, ZLOpdProtCMDEnum.DIAGRAM_CURR_3, ZLOpdProtCMDEnum.DIAGRAM_POWER_1, ZLOpdProtCMDEnum.DIAGRAM_POWER_2, ZLOpdProtCMDEnum.DIAGRAM_POWER_3 }; ByteBufAllocator alloc=channel.alloc(); ByteBuf byteBuf=null; synchronized(channel) { try { byteBuf=alloc.directBuffer(); checkDiagramPoint(byteBuf,preCmd); if(!channel.attr(ChannelGroupMgr.ATTR_KEY_DIAGRAM_READY).get()) { logger.info("功图数据还未准备就绪,准备重试一次"); Thread.sleep(500); //重试一次 byteBuf=alloc.directBuffer(); checkDiagramPoint(byteBuf,preCmd); } if(!channel.attr(ChannelGroupMgr.ATTR_KEY_DIAGRAM_READY).get()) { logger.info("功图数据还未准备就绪"); return; } } catch (InterruptedException e) { e.printStackTrace(); } //开始功图采集 for(ZLOpdProtCMDEnum cmd : cmds) { byteBuf=alloc.directBuffer(); byteBuf.writeBytes(cmd.getCmd()); try { channel.writeAndFlush(byteBuf); channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name()); logger.info("发送完后指令:{}",cmd.name()); channel.wait(10*1000); //等待接收返回数据后继续,最多等待10s } catch (InterruptedException e) { e.printStackTrace(); } } } } private void checkDiagramPoint(ByteBuf byteBuf,ZLOpdProtCMDEnum preCmd) throws InterruptedException { byteBuf.writeBytes(preCmd.getCmd()); channel.writeAndFlush(byteBuf); channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(preCmd.name()); channel.wait(10*1000); } }