CommonQmDevice.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.mes.util;
  2. import com.mes.ui.ConfigParam;
  3. import com.mes.ui.MesClient;
  4. import com.mes.util.ytnetty.YtTcpClient;
  5. import com.mes.util.ytnetty.YtUtil;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10. /**
  11. * 气密仪统一入口:innomatec/ateq 走串口,INNTAI(中皖英泰)走网口 TCP(原生 Socket)。
  12. */
  13. public class CommonQmDevice {
  14. public static final Logger log = LoggerFactory.getLogger(CommonQmDevice.class);
  15. public String device;
  16. public SerialPortUtils serialPortUtils;
  17. public YtTcpClient ytClient;
  18. public boolean isOnline;
  19. ConfigParam configParam;
  20. public Timer workTimer;
  21. public CommonQmDevice(ConfigParam configParam) {
  22. this.configParam = configParam;
  23. this.device = configParam.getDevice();
  24. connect();
  25. }
  26. public static boolean isInntai(String device) {
  27. if (device == null) {
  28. return false;
  29. }
  30. return "INNTAI".equalsIgnoreCase(device.trim()) || "中皖英泰".equals(device.trim());
  31. }
  32. public boolean isInntai() {
  33. return isInntai(device);
  34. }
  35. private void connect() {
  36. this.device = configParam.getDevice();
  37. WorkTimer.stop();
  38. if (isInntai()) {
  39. try {
  40. if (serialPortUtils != null) {
  41. serialPortUtils.closePort();
  42. serialPortUtils = null;
  43. }
  44. MesClient.serialPortUtils = null;
  45. String ip = configParam.getSerialPort();
  46. String port = configParam.getBaudRate();
  47. YtTcpClient.run(ip, Integer.parseInt(port));
  48. ytClient = YtTcpClient.getInstance();
  49. workTimerTask();
  50. log.info("INNTAI气密仪TCP连接 {}:{}", ip, port);
  51. } catch (Exception e) {
  52. log.error("INNTAI气密仪连接失败", e);
  53. }
  54. } else {
  55. serialPortUtils = new SerialPortUtils(configParam.getSerialPort(),
  56. MesClient.getEffectiveOprno(),
  57. configParam.getOprnoTitle(),
  58. configParam.getDevice());
  59. MesClient.serialPortUtils = serialPortUtils;
  60. }
  61. }
  62. public boolean isOnline() {
  63. isOnline = false;
  64. if (isInntai()) {
  65. isOnline = YtTcpClient.getTcpOnline() == 1;
  66. } else if (serialPortUtils != null) {
  67. isOnline = serialPortUtils.isOpen;
  68. }
  69. return isOnline;
  70. }
  71. public void reconnect() {
  72. this.device = configParam.getDevice();
  73. if (workTimer != null) {
  74. workTimer.cancel();
  75. workTimer = null;
  76. }
  77. connect();
  78. }
  79. public void workTimerTask() {
  80. if (workTimer != null) {
  81. workTimer.cancel();
  82. }
  83. workTimer = new Timer();
  84. workTimer.schedule(new TimerTask() {
  85. public void run() {
  86. try {
  87. YtTcpClient client = YtTcpClient.getInstance();
  88. if (MesClient.work_status == 1 && client != null && client.isConnected()) {
  89. client.send(YtUtil.getCurDataCommon());
  90. }
  91. } catch (Exception e) {
  92. log.error("INNTAI实时查询失败", e);
  93. }
  94. }
  95. }, 500, 500);
  96. }
  97. public boolean start() {
  98. try {
  99. if (isInntai()) {
  100. YtTcpClient client = YtTcpClient.getInstance();
  101. if (client == null || !client.isConnected()) {
  102. MesClient.setMenuStatus("INNTAI气密仪未连接,请检查IP/端口后重试", 1);
  103. return false;
  104. }
  105. log.info("INNTAI点击启动");
  106. client.send(YtUtil.getStartCommon());
  107. } else {
  108. if (serialPortUtils == null) {
  109. MesClient.setMenuStatus("成品气密仪连接失败,请检查设备后重试", 1);
  110. return false;
  111. }
  112. serialPortUtils.start();
  113. }
  114. } catch (Exception e) {
  115. log.error("气密仪启动失败", e);
  116. return false;
  117. }
  118. return true;
  119. }
  120. public boolean changeProgram() {
  121. try {
  122. if (isInntai()) {
  123. YtTcpClient client = YtTcpClient.getInstance();
  124. if (client == null || !client.isConnected()) {
  125. MesClient.setMenuStatus("INNTAI气密仪未连接,请检查IP/端口后重试", 1);
  126. return false;
  127. }
  128. String programNo = MesClient.configParam.getProgramNo();
  129. log.info("INNTAI切换程序 {}", programNo);
  130. client.send(YtUtil.getSelectProgramCommon(programNo));
  131. } else {
  132. if (serialPortUtils == null) {
  133. MesClient.setMenuStatus("成品气密仪连接失败,请检查设备后重试", 1);
  134. return false;
  135. }
  136. serialPortUtils.changeProgram();
  137. }
  138. } catch (Exception e) {
  139. log.error("气密仪切换程序失败", e);
  140. return false;
  141. }
  142. return true;
  143. }
  144. }