GatherLiquidTask.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.hb.proj.gather.scheduler;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import com.hb.proj.gather.protocol.ChannelGroupMgr;
  8. import com.hb.proj.gather.protocol.ZLOpdProtCMDEnum;
  9. import io.netty.buffer.ByteBuf;
  10. import io.netty.buffer.ByteBufAllocator;
  11. import io.netty.channel.Channel;
  12. /**
  13. * 动液面采集任务 具体执行采集任务,下发采集指令
  14. * @author cwen
  15. *
  16. */
  17. public class GatherLiquidTask implements Runnable {
  18. private final static Logger logger = LoggerFactory.getLogger(GatherLiquidTask.class);
  19. private Channel channel;
  20. private long cmdTimeout=25*1000; //指令等待回复超时时间
  21. private int timeoutCount=0; //超时发送指令次数
  22. public GatherLiquidTask(Channel channel) {
  23. this.channel=channel;
  24. }
  25. /**
  26. * 约2-5s采集完
  27. */
  28. @Override
  29. public void run() {
  30. logger.info("动液面采集开始...");
  31. List<ZLOpdProtCMDEnum> cmds=new ArrayList<>(41);
  32. cmds.add(ZLOpdProtCMDEnum.LIQUID_OTHER);
  33. for(int i=1;i<41;i++) {
  34. cmds.add(ZLOpdProtCMDEnum.valueOf("LIQUID_SERIAL_"+i));
  35. }
  36. try {
  37. ByteBufAllocator alloc=channel.alloc();
  38. ByteBuf byteBuf=null;
  39. synchronized(channel) {
  40. channel.attr(ChannelGroupMgr.ATTR_KEY_STOP_NEXT).set(false); //每次任务开始前重置
  41. for(ZLOpdProtCMDEnum cmd : cmds) {
  42. if(needCloseChannel()) {
  43. ChannelGroupMgr.disconnect(channel);
  44. logger.error("设备多次未对指令及时响应,准备关闭连接,等待重连");
  45. return;
  46. }
  47. byteBuf=alloc.directBuffer();
  48. byteBuf.writeBytes(cmd.getCmd());
  49. channel.writeAndFlush(byteBuf);
  50. channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name());
  51. channel.attr(ChannelGroupMgr.ATTR_KEY_PRE_TIME).set((new Date()).getTime());
  52. logger.info("发送完指令:{}",cmd.name());
  53. channel.wait(cmdTimeout); //等待接收返回数据后继续,最多等待cmdTimeout,此处释放锁,回复还未收到就被多值任务获得锁并发指令,会导致两个指令间隔很短
  54. if(channel.attr(ChannelGroupMgr.ATTR_KEY_STOP_NEXT).get()) {
  55. logger.info("本次动液面采集取消(数据已采集过)");
  56. return;
  57. }
  58. if(checkCancel(cmd.name())) {
  59. logger.info("本次动液面采集取消(中途有数据未采到)");
  60. return;
  61. }
  62. }
  63. }
  64. }
  65. catch (InterruptedException e) {
  66. e.printStackTrace();
  67. logger.error("定时采集出现异常:{}",e.getMessage());
  68. }
  69. }
  70. private boolean checkCancel(String cmdName) {
  71. Long pre=channel.attr(ChannelGroupMgr.ATTR_KEY_PRE_TIME).get();
  72. if(pre==null||pre==0) {
  73. return false;
  74. }
  75. boolean isTimeout=((new Date()).getTime()-pre.longValue())>(cmdTimeout-1000);
  76. if(!isTimeout) {
  77. return false;
  78. }
  79. //超时后的处理
  80. if(cmdName.startsWith("LIQUID_OTHER")) {
  81. return true;
  82. }
  83. else if(cmdName.startsWith("LIQUID_SERIAL")){
  84. int num=Integer.parseInt((cmdName.split("_"))[2]);
  85. if(num<30) { //只要前30包数据有效就可以继续
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. private boolean needCloseChannel() {
  92. Long pre=channel.attr(ChannelGroupMgr.ATTR_KEY_PRE_TIME).get();
  93. if(pre==null||pre==0) {
  94. return false;
  95. }
  96. boolean isTimeout= ((new Date()).getTime()-pre.longValue())>(cmdTimeout-1000);
  97. if(!isTimeout) {
  98. timeoutCount=0; //有一次不超时就清空超时次数
  99. }
  100. else {
  101. timeoutCount+=1; //记录一次任务中连续超时次数
  102. }
  103. return timeoutCount>1; //多次超时(2次及以上)需要关闭通道,等待重连
  104. }
  105. }