| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package com.mes.util;
- import com.mes.ui.ConfigParam;
- import com.mes.ui.MesClient;
- import com.mes.util.ytnetty.YtTcpClient;
- import com.mes.util.ytnetty.YtUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.util.Timer;
- import java.util.TimerTask;
- /**
- * 气密仪统一入口:innomatec/ateq 走串口,INNTAI(中皖英泰)走网口 TCP(原生 Socket)。
- */
- public class CommonQmDevice {
- public static final Logger log = LoggerFactory.getLogger(CommonQmDevice.class);
- public String device;
- public SerialPortUtils serialPortUtils;
- public YtTcpClient ytClient;
- public boolean isOnline;
- ConfigParam configParam;
- public Timer workTimer;
- public CommonQmDevice(ConfigParam configParam) {
- this.configParam = configParam;
- this.device = configParam.getDevice();
- connect();
- }
- public static boolean isInntai(String device) {
- if (device == null) {
- return false;
- }
- return "INNTAI".equalsIgnoreCase(device.trim()) || "中皖英泰".equals(device.trim());
- }
- public boolean isInntai() {
- return isInntai(device);
- }
- private void connect() {
- this.device = configParam.getDevice();
- WorkTimer.stop();
- if (isInntai()) {
- try {
- if (serialPortUtils != null) {
- serialPortUtils.closePort();
- serialPortUtils = null;
- }
- MesClient.serialPortUtils = null;
- String ip = configParam.getSerialPort();
- String port = configParam.getBaudRate();
- YtTcpClient.run(ip, Integer.parseInt(port));
- ytClient = YtTcpClient.getInstance();
- workTimerTask();
- log.info("INNTAI气密仪TCP连接 {}:{}", ip, port);
- } catch (Exception e) {
- log.error("INNTAI气密仪连接失败", e);
- }
- } else {
- serialPortUtils = new SerialPortUtils(configParam.getSerialPort(),
- MesClient.getEffectiveOprno(),
- configParam.getOprnoTitle(),
- configParam.getDevice());
- MesClient.serialPortUtils = serialPortUtils;
- }
- }
- public boolean isOnline() {
- isOnline = false;
- if (isInntai()) {
- isOnline = YtTcpClient.getTcpOnline() == 1;
- } else if (serialPortUtils != null) {
- isOnline = serialPortUtils.isOpen;
- }
- return isOnline;
- }
- public void reconnect() {
- this.device = configParam.getDevice();
- if (workTimer != null) {
- workTimer.cancel();
- workTimer = null;
- }
- connect();
- }
- public void workTimerTask() {
- if (workTimer != null) {
- workTimer.cancel();
- }
- workTimer = new Timer();
- workTimer.schedule(new TimerTask() {
- public void run() {
- try {
- YtTcpClient client = YtTcpClient.getInstance();
- if (MesClient.work_status == 1 && client != null && client.isConnected()) {
- client.send(YtUtil.getCurDataCommon());
- }
- } catch (Exception e) {
- log.error("INNTAI实时查询失败", e);
- }
- }
- }, 500, 500);
- }
- public boolean start() {
- try {
- if (isInntai()) {
- YtTcpClient client = YtTcpClient.getInstance();
- if (client == null || !client.isConnected()) {
- MesClient.setMenuStatus("INNTAI气密仪未连接,请检查IP/端口后重试", 1);
- return false;
- }
- log.info("INNTAI点击启动");
- client.send(YtUtil.getStartCommon());
- } else {
- if (serialPortUtils == null) {
- MesClient.setMenuStatus("成品气密仪连接失败,请检查设备后重试", 1);
- return false;
- }
- serialPortUtils.start();
- }
- } catch (Exception e) {
- log.error("气密仪启动失败", e);
- return false;
- }
- return true;
- }
- public boolean changeProgram() {
- try {
- if (isInntai()) {
- YtTcpClient client = YtTcpClient.getInstance();
- if (client == null || !client.isConnected()) {
- MesClient.setMenuStatus("INNTAI气密仪未连接,请检查IP/端口后重试", 1);
- return false;
- }
- String programNo = MesClient.configParam.getProgramNo();
- log.info("INNTAI切换程序 {}", programNo);
- client.send(YtUtil.getSelectProgramCommon(programNo));
- } else {
- if (serialPortUtils == null) {
- MesClient.setMenuStatus("成品气密仪连接失败,请检查设备后重试", 1);
- return false;
- }
- serialPortUtils.changeProgram();
- }
- } catch (Exception e) {
- log.error("气密仪切换程序失败", e);
- return false;
- }
- return true;
- }
- }
|