From 0c5a58771fcf86ad0b562829fbfa440da9392703 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期三, 17 十二月 2025 09:08:16 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/network/ZyStationConnectDriver.java | 57 ++++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 42 insertions(+), 15 deletions(-)
diff --git a/src/main/java/com/zy/core/network/ZyStationConnectDriver.java b/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
index d2beb2e..61edc2a 100644
--- a/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
+++ b/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
@@ -1,6 +1,5 @@
package com.zy.core.network;
-import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.core.ThreadHandler;
import com.zy.core.model.CommandResponse;
@@ -11,6 +10,10 @@
import com.zy.core.network.fake.ZyStationFakeConnect;
import com.zy.core.network.real.ZyStationRealConnect;
import lombok.extern.slf4j.Slf4j;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
/**
* 杈撻�佺珯杩炴帴椹卞姩
@@ -21,24 +24,16 @@
private boolean connected = false;
private DeviceConfig deviceConfig;
private ZyStationConnectApi zyStationConnectApi;
+ private volatile boolean closed = false;
+ private ScheduledExecutorService executor;
public ZyStationConnectDriver(DeviceConfig deviceConfig) {
this.deviceConfig = deviceConfig;
}
@Override
- @SuppressWarnings("InfiniteLoopStatement")
public void run() {
- while (true) {
- try {
- if (!connected) {
- connect();
- }
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
+
}
@Override
@@ -56,9 +51,41 @@
@Override
public void close() {
- zyStationConnectApi.disconnect();
- zyStationConnectApi = null;
+ closed = true;
+ ScheduledExecutorService ex = executor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
+ }
+ if (zyStationConnectApi != null) {
+ zyStationConnectApi.disconnect();
+ zyStationConnectApi = null;
+ }
connected = false;
+ }
+
+ public void start() {
+ executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("DevpConnect-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+
+ executor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
+ if (!connected) {
+ connect();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }, 0, 1000, TimeUnit.MILLISECONDS);
}
public List<ZyStationStatusEntity> getStatus() {
@@ -71,4 +98,4 @@
public CommandResponse sendCommand(StationCommand command) {
return zyStationConnectApi.sendCommand(command);
}
-}
\ No newline at end of file
+}
--
Gitblit v1.9.1