|
@@ -0,0 +1,246 @@
|
|
|
+package com.hb.proj.gather.scheduler;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import com.hb.proj.gather.model.LiquidParam;
|
|
|
+import com.hb.proj.gather.protocol.ChannelGroupMgr;
|
|
|
+import com.hb.proj.gather.protocol.ZLOpdProtCMDEnum;
|
|
|
+import com.hb.proj.gather.rep.RedisRepComponent;
|
|
|
+import com.hb.proj.gather.utils.Crc16Utils;
|
|
|
+import com.hb.xframework.util.ApplicationContextUtils;
|
|
|
+import com.hb.xframework.util.MapUtils;
|
|
|
+
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.ByteBufAllocator;
|
|
|
+import io.netty.buffer.ByteBufUtil;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
+import io.netty.channel.Channel;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 手动控制动液面 任务(参数读取、设置;启动测试、读取数据)
|
|
|
+ * @author cwen
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ManualLiquidTask implements Runnable {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(ManualLiquidTask.class);
|
|
|
+
|
|
|
+ public static final int ACTION_READ=0;
|
|
|
+
|
|
|
+ public static final int ACTION_SET=1;
|
|
|
+
|
|
|
+ public static final int ACTION_BOOT_TEST=2;
|
|
|
+
|
|
|
+ private Channel channel;
|
|
|
+
|
|
|
+ private int action=ACTION_READ;
|
|
|
+
|
|
|
+ private LiquidParam liquidParam;
|
|
|
+
|
|
|
+ private RedisRepComponent repRedis;
|
|
|
+
|
|
|
+ private String redisKey;
|
|
|
+
|
|
|
+ private long cmdTimeout=25*1000;
|
|
|
+
|
|
|
+ public ManualLiquidTask(Channel channel,int action) {
|
|
|
+ this.channel=channel;
|
|
|
+ this.action=action;
|
|
|
+ this.repRedis=ApplicationContextUtils.getBean("redisRepComponent", RedisRepComponent.class);
|
|
|
+ this.redisKey="liquid_"+channel.attr(ChannelGroupMgr.ATTR_KEY_SERIAL).get();
|
|
|
+ }
|
|
|
+
|
|
|
+ public ManualLiquidTask(Channel channel,int action,LiquidParam liquidParam) {
|
|
|
+ this(channel,action);
|
|
|
+ this.liquidParam=liquidParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ ByteBufAllocator alloc=channel.alloc();
|
|
|
+ synchronized(channel) {
|
|
|
+ logger.info("手动操作动液面设备...");
|
|
|
+ if(this.action==ACTION_READ) {
|
|
|
+ readParams(alloc);
|
|
|
+ }
|
|
|
+ else if(this.action==ACTION_SET) {
|
|
|
+ setParams(alloc);
|
|
|
+ }
|
|
|
+ else if(this.action==ACTION_BOOT_TEST) {
|
|
|
+ bootTest(alloc);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取参数
|
|
|
+ * @param alloc
|
|
|
+ */
|
|
|
+ private void readParams(ByteBufAllocator alloc) {
|
|
|
+ try {
|
|
|
+ ZLOpdProtCMDEnum[] cmds= {ZLOpdProtCMDEnum.LIQUID_READ_PIPE_SOUND,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_READ_SENSORTIME,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_READ_TEST_INTERVAL,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_READ_SENSITI,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_READ_CASING_PRE
|
|
|
+ };
|
|
|
+ ByteBuf byteBuf=null;
|
|
|
+
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","start","action","read"),true);
|
|
|
+
|
|
|
+ for(ZLOpdProtCMDEnum cmd : cmds) {
|
|
|
+
|
|
|
+ byteBuf=alloc.directBuffer();
|
|
|
+ byteBuf.writeBytes(cmd.getCmd());
|
|
|
+ channel.writeAndFlush(byteBuf);
|
|
|
+ channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name());
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","running"));
|
|
|
+ channel.wait(cmdTimeout);
|
|
|
+ }
|
|
|
+
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","complete"));
|
|
|
+ }
|
|
|
+ catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error("读取动液面设备参数异常:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置参数
|
|
|
+ * @param alloc
|
|
|
+ */
|
|
|
+ private void setParams(ByteBufAllocator alloc) {
|
|
|
+ logger.info("动液面远程设置参数...");
|
|
|
+ try {
|
|
|
+ ZLOpdProtCMDEnum[] cmds= {
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_AVG_PIPE,
|
|
|
+
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_SOUND_SPEED,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_SOUND_MARK,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TEST_INTERVAL,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TIME_YYYY,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TIME_MM,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TIME_DD,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TIME_HR,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TIME_MI,
|
|
|
+ ZLOpdProtCMDEnum.LIQUID_SET_TIME_SS
|
|
|
+
|
|
|
+ };
|
|
|
+ ByteBuf byteBuf=null;
|
|
|
+
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","start","action","set"),true);
|
|
|
+
|
|
|
+ for(ZLOpdProtCMDEnum cmd : cmds) {
|
|
|
+ byteBuf=alloc.directBuffer();
|
|
|
+ byteBuf.writeBytes(bindCMDVal(cmd));
|
|
|
+ channel.writeAndFlush(byteBuf);
|
|
|
+ channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name());
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","running"));
|
|
|
+ channel.wait(cmdTimeout);
|
|
|
+ }
|
|
|
+
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","complete"));
|
|
|
+ }
|
|
|
+ catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error("读取动液面设备参数异常:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动测试
|
|
|
+ * @param alloc
|
|
|
+ */
|
|
|
+ private void bootTest(ByteBufAllocator alloc) {
|
|
|
+ try {
|
|
|
+ ZLOpdProtCMDEnum cmd=ZLOpdProtCMDEnum.LIQUID_BOOT_TEST;
|
|
|
+
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","start","action","boot"),true);
|
|
|
+
|
|
|
+ ByteBuf byteBuf=alloc.directBuffer();
|
|
|
+ byteBuf.writeBytes(bindCMDVal(cmd));
|
|
|
+ channel.writeAndFlush(byteBuf);
|
|
|
+ channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name());
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","running"));
|
|
|
+ channel.wait(cmdTimeout);
|
|
|
+
|
|
|
+ repRedis.put(redisKey, MapUtils.build("status","complete"));
|
|
|
+ }
|
|
|
+ catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error("启动动液面测试异常:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //绑定指令的参数值,2字节,对应无符号的short
|
|
|
+ private byte[] bindCMDVal(ZLOpdProtCMDEnum cmd) {
|
|
|
+ try {
|
|
|
+ logger.info("动液面指令构建:{}",cmd.name());
|
|
|
+ ByteBuf byteBuf=Unpooled.buffer(8);
|
|
|
+ byteBuf.writeBytes(cmd.getCmd());
|
|
|
+ int[] times=liquidParam.getTimeParts();
|
|
|
+ switch (cmd.name()) {
|
|
|
+ case "LIQUID_SET_AVG_PIPE":
|
|
|
+ byteBuf.writeShort(liquidParam.getAvgLenPipe());
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_SOUND_SPEED":
|
|
|
+ byteBuf.writeShort(liquidParam.getSoundSpeed());
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_SOUND_MARK":
|
|
|
+ byteBuf.writeShort(liquidParam.getSoundMarkDepth());
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TEST_INTERVAL":
|
|
|
+ byteBuf.writeShort(liquidParam.getTestInterval());
|
|
|
+ break;
|
|
|
+ case "LIQUID_BOOT_TEST":
|
|
|
+ byteBuf.writeShort(liquidParam.getBootCMDVal());
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TIME_YYYY":
|
|
|
+ byteBuf.writeShort(times[0]);
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TIME_MM":
|
|
|
+ byteBuf.writeShort(times[1]);
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TIME_DD":
|
|
|
+ byteBuf.writeShort(times[2]);
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TIME_HR":
|
|
|
+ byteBuf.writeShort(times[3]);
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TIME_MI":
|
|
|
+ byteBuf.writeShort(times[4]);
|
|
|
+ break;
|
|
|
+ case "LIQUID_SET_TIME_SS":
|
|
|
+ byteBuf.writeShort(times[5]);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ int crc16=Crc16Utils.getCRC(ByteBufUtil.getBytes(byteBuf,0,byteBuf.readableBytes()));
|
|
|
+ byteBuf.writeShort(crc16);
|
|
|
+ logger.info("参数设置指令:{}",ByteBufUtil.hexDump(byteBuf));
|
|
|
+ return byteBuf.array();
|
|
|
+ }
|
|
|
+ catch(Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ public static void main(String[] args) {
|
|
|
+ LiquidParam param=new LiquidParam();
|
|
|
+ param.setSensorTime(new Date());
|
|
|
+ param.setAvgLenPipe(960);
|
|
|
+ ManualLiquidTask task=new ManualLiquidTask(null,ManualLiquidTask.ACTION_SET,param);
|
|
|
+ task.bindCMDVal(ZLOpdProtCMDEnum.LIQUID_SET_AVG_PIPE);
|
|
|
+ System.out.println("over");
|
|
|
+ }*/
|
|
|
+
|
|
|
+}
|