GatherMultiTask.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.hb.proj.gather.scheduler;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import com.hb.proj.gather.protocol.ChannelGroupMgr;
  5. import com.hb.proj.gather.protocol.ZLOpdProtCMDEnum;
  6. import io.netty.buffer.ByteBuf;
  7. import io.netty.buffer.ByteBufAllocator;
  8. import io.netty.channel.Channel;
  9. /**
  10. * 功图采集任务
  11. * 关键点:1、先检测功图点数是否为255,是:功图就绪,否:不可采集功图;
  12. * 2、功图中的位移、载荷、电流、功率 每项是分3次获取。
  13. * @author cwen
  14. *
  15. */
  16. public class GatherMultiTask implements Runnable {
  17. private final static Logger logger = LoggerFactory.getLogger(GatherMultiTask.class);
  18. private Channel channel;
  19. public GatherMultiTask(Channel channel) {
  20. this.channel=channel;
  21. }
  22. /**
  23. * 约8s采集完 用时不稳定,有时很慢
  24. */
  25. @Override
  26. public void run() {
  27. logger.info("多值采集开始...");
  28. ZLOpdProtCMDEnum preCmd=ZLOpdProtCMDEnum.DIAGRAM_POINT_COUNT; //前置命令,返回数据为250 才继续后面的
  29. ZLOpdProtCMDEnum[] cmds= {
  30. ZLOpdProtCMDEnum.DIAGRAM_DISP_1,
  31. ZLOpdProtCMDEnum.DIAGRAM_DISP_2,
  32. ZLOpdProtCMDEnum.DIAGRAM_DISP_3,
  33. ZLOpdProtCMDEnum.DIAGRAM_LOAD_1,
  34. ZLOpdProtCMDEnum.DIAGRAM_LOAD_2,
  35. ZLOpdProtCMDEnum.DIAGRAM_LOAD_3,
  36. ZLOpdProtCMDEnum.DIAGRAM_CURR_1,
  37. ZLOpdProtCMDEnum.DIAGRAM_CURR_2,
  38. ZLOpdProtCMDEnum.DIAGRAM_CURR_3,
  39. ZLOpdProtCMDEnum.DIAGRAM_POWER_1,
  40. ZLOpdProtCMDEnum.DIAGRAM_POWER_2,
  41. ZLOpdProtCMDEnum.DIAGRAM_POWER_3
  42. };
  43. ByteBufAllocator alloc=channel.alloc();
  44. ByteBuf byteBuf=null;
  45. synchronized(channel) {
  46. try {
  47. byteBuf=alloc.directBuffer();
  48. checkDiagramPoint(byteBuf,preCmd);
  49. if(!channel.attr(ChannelGroupMgr.ATTR_KEY_DIAGRAM_READY).get()) {
  50. logger.info("功图数据还未准备就绪,准备重试一次");
  51. Thread.sleep(500); //重试一次
  52. byteBuf=alloc.directBuffer();
  53. checkDiagramPoint(byteBuf,preCmd);
  54. }
  55. if(!channel.attr(ChannelGroupMgr.ATTR_KEY_DIAGRAM_READY).get()) {
  56. logger.info("功图数据还未准备就绪");
  57. return;
  58. }
  59. }
  60. catch (InterruptedException e) {
  61. e.printStackTrace();
  62. }
  63. //开始功图采集
  64. for(ZLOpdProtCMDEnum cmd : cmds) {
  65. byteBuf=alloc.directBuffer();
  66. byteBuf.writeBytes(cmd.getCmd());
  67. try {
  68. channel.writeAndFlush(byteBuf);
  69. channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(cmd.name());
  70. logger.info("发送完后指令:{}",cmd.name());
  71. channel.wait(10*1000); //等待接收返回数据后继续,最多等待10s
  72. } catch (InterruptedException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. }
  78. private void checkDiagramPoint(ByteBuf byteBuf,ZLOpdProtCMDEnum preCmd) throws InterruptedException {
  79. byteBuf.writeBytes(preCmd.getCmd());
  80. channel.writeAndFlush(byteBuf);
  81. channel.attr(ChannelGroupMgr.ATTR_KEY_CMD).set(preCmd.name());
  82. channel.wait(10*1000);
  83. }
  84. }