Skip to content

Commit 734b706

Browse files
committed
Fix reconnect issue #34
1 parent 2554434 commit 734b706

4 files changed

Lines changed: 32 additions & 55 deletions

File tree

org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticGenericDevice.java

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public enum ProcessDataResult {
8989
}
9090

9191
private @Nullable ScheduledFuture<?> periodicJob = null;
92-
private @Nullable ScheduledFuture<?> reconnectJob = null;
9392

9493
private final AtomicBoolean reconnecting = new AtomicBoolean(false);
94+
private long lastExecution = 0;
9595

9696
/**
9797
* Constructor
@@ -102,16 +102,16 @@ public SimaticGenericDevice(int pollRate, Charset charset, SimaticUpdateMode upd
102102
this.updateMode = updateMode;
103103
if (pollRate > 0) {
104104
periodicJob = scheduler.scheduleAtFixedRate(() -> {
105-
if (!reconnecting.get()) {
105+
if (System.currentTimeMillis() - lastExecution >= pollRate) {
106+
lastExecution = System.currentTimeMillis();
106107
execute();
107108
}
108109
}, 500, pollRate, TimeUnit.MILLISECONDS);
109110
} else {
110111
scheduler.execute(() -> {
111112
while (!disposed) {
113+
execute();
112114
if (!reconnecting.get()) {
113-
execute();
114-
} else {
115115
try {
116116
Thread.sleep(500);
117117
} catch (InterruptedException e) {
@@ -134,10 +134,6 @@ public void dispose() {
134134
periodicJob.cancel(true);
135135
periodicJob = null;
136136
}
137-
if (reconnectJob != null) {
138-
reconnectJob.cancel(true);
139-
reconnectJob = null;
140-
}
141137
}
142138

143139
/**
@@ -147,7 +143,9 @@ protected void execute() {
147143
if (shouldReconnect()) {
148144
reconnectWithDelaying();
149145
}
150-
checkNewData();
146+
if (!reconnecting.get()) {
147+
checkNewData();
148+
}
151149
}
152150

153151
@Override
@@ -225,44 +223,30 @@ public boolean reconnect() {
225223
* Reconnect device
226224
*/
227225
protected void reconnectWithDelaying() {
228-
if (!reconnecting.compareAndSet(false, true)) {
229-
logger.debug("{} - reconnectJob(): already running", toString());
230-
return;
226+
if (reconnecting.compareAndSet(false, true)) {
227+
logger.debug("{} - reconnectJob(): started...", toString());
231228
}
232229

233-
if (reconnectJob != null) {
234-
logger.debug("{} - reconnectJob(): canceling previous instance", toString());
235-
reconnectJob.cancel(true);
236-
reconnectJob = null;
237-
}
238-
239-
logger.debug("{} - reconnectJob(): create", toString());
230+
logger.debug("{} - reconnectJob(): {}/{}/{}", toString(), rcTest, rcTestMax, RECONNECT_DELAY_MAX);
240231

241-
reconnectJob = scheduler.scheduleAtFixedRate(() -> {
242-
logger.debug("{} - reconnectJob(): {}/{}/{}", toString(), rcTest, rcTestMax, RECONNECT_DELAY_MAX);
243-
244-
if (rcTest < rcTestMax) {
245-
rcTest++;
246-
return;
247-
}
232+
if (rcTest < rcTestMax) {
233+
rcTest++;
234+
return;
235+
}
248236

249-
if (reconnect()) {
250-
rcTest = 0;
251-
rcTestMax = 0;
237+
logger.debug("{} - reconnectJob(): reconnecting...", toString());
238+
if (reconnect()) {
239+
rcTest = 0;
240+
rcTestMax = 0;
252241

253-
logger.debug("{} - reconnectJob(): reconnected", toString());
254-
if (reconnectJob.cancel(false)) {
255-
reconnectJob = null;
256-
logger.debug("{} - reconnectJob(): canceled", toString());
257-
reconnecting.set(false);
258-
}
259-
} else {
260-
if (rcTestMax <= RECONNECT_DELAY_MAX) {
261-
rcTestMax++;
262-
}
263-
rcTest = 0;
242+
logger.debug("{} - reconnectJob(): reconnected", toString());
243+
reconnecting.set(false);
244+
} else {
245+
if (rcTestMax < RECONNECT_DELAY_MAX) {
246+
rcTestMax++;
264247
}
265-
}, 1000, 1000, TimeUnit.MILLISECONDS);
248+
rcTest = 0;
249+
}
266250
}
267251

268252
/**
@@ -428,12 +412,6 @@ protected void checkNewData() {
428412

429413
readed++;
430414
readedBytes += area.getAddressSpaceLength();
431-
432-
/*
433-
* if (logger.isDebugEnabled()) {
434-
* logger.debug("{} - Reading finished. Area={}", toString(), area.toString());
435-
* }
436-
*/
437415
}
438416
} catch (Exception ex) {
439417
logger.error("{} - Read data error", toString(), ex);

org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ public Boolean open() {
149149
tryReconnect.set(true);
150150
return false;
151151
} finally {
152-
if (!isConnected()) {
153-
reconnectWithDelaying();
154-
}
152+
155153
}
156154

157155
return true;
@@ -325,7 +323,10 @@ public void readDataArea(SimaticReadDataArea area) throws SimaticReadException {
325323
}
326324
}
327325

328-
long response = System.currentTimeMillis() - startTime;
326+
if (logger.isDebugEnabled()) {
327+
logger.debug("{} - Reading finished in {}ms. Area={}", toString(), System.currentTimeMillis() - startTime,
328+
area.toString());
329+
}
329330
// get data for all items in area
330331
for (SimaticChannel item : area.getItems()) {
331332
// send value into openHAB

org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simatic/SimaticTCP200.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public Boolean open() {
8585
tryReconnect.set(true);
8686
return false;
8787
} finally {
88-
if (shouldReconnect()) {
89-
reconnectWithDelaying();
90-
}
88+
9189
}
9290

9391
return true;

org.openhab.binding.simatic/src/main/java/org/openhab/binding/simatic/internal/simaticBindingConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
@NonNullByDefault
2626
public class SimaticBindingConstants {
2727

28-
public static final String VERSION = "3.2.0-beta.1";
28+
public static final String VERSION = "3.2.0-beta.2";
2929

3030
private static final String BINDING_ID = "simatic";
3131

0 commit comments

Comments
 (0)