Skip to content

Commit

Permalink
Fix reconnect issue #34
Browse files Browse the repository at this point in the history
  • Loading branch information
docbender committed Nov 23, 2021
1 parent 2554434 commit 734b706
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public enum ProcessDataResult {
}

private @Nullable ScheduledFuture<?> periodicJob = null;
private @Nullable ScheduledFuture<?> reconnectJob = null;

private final AtomicBoolean reconnecting = new AtomicBoolean(false);
private long lastExecution = 0;

/**
* Constructor
Expand All @@ -102,16 +102,16 @@ public SimaticGenericDevice(int pollRate, Charset charset, SimaticUpdateMode upd
this.updateMode = updateMode;
if (pollRate > 0) {
periodicJob = scheduler.scheduleAtFixedRate(() -> {
if (!reconnecting.get()) {
if (System.currentTimeMillis() - lastExecution >= pollRate) {
lastExecution = System.currentTimeMillis();
execute();
}
}, 500, pollRate, TimeUnit.MILLISECONDS);
} else {
scheduler.execute(() -> {
while (!disposed) {
execute();
if (!reconnecting.get()) {
execute();
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Expand All @@ -134,10 +134,6 @@ public void dispose() {
periodicJob.cancel(true);
periodicJob = null;
}
if (reconnectJob != null) {
reconnectJob.cancel(true);
reconnectJob = null;
}
}

/**
Expand All @@ -147,7 +143,9 @@ protected void execute() {
if (shouldReconnect()) {
reconnectWithDelaying();
}
checkNewData();
if (!reconnecting.get()) {
checkNewData();
}
}

@Override
Expand Down Expand Up @@ -225,44 +223,30 @@ public boolean reconnect() {
* Reconnect device
*/
protected void reconnectWithDelaying() {
if (!reconnecting.compareAndSet(false, true)) {
logger.debug("{} - reconnectJob(): already running", toString());
return;
if (reconnecting.compareAndSet(false, true)) {
logger.debug("{} - reconnectJob(): started...", toString());
}

if (reconnectJob != null) {
logger.debug("{} - reconnectJob(): canceling previous instance", toString());
reconnectJob.cancel(true);
reconnectJob = null;
}

logger.debug("{} - reconnectJob(): create", toString());
logger.debug("{} - reconnectJob(): {}/{}/{}", toString(), rcTest, rcTestMax, RECONNECT_DELAY_MAX);

reconnectJob = scheduler.scheduleAtFixedRate(() -> {
logger.debug("{} - reconnectJob(): {}/{}/{}", toString(), rcTest, rcTestMax, RECONNECT_DELAY_MAX);

if (rcTest < rcTestMax) {
rcTest++;
return;
}
if (rcTest < rcTestMax) {
rcTest++;
return;
}

if (reconnect()) {
rcTest = 0;
rcTestMax = 0;
logger.debug("{} - reconnectJob(): reconnecting...", toString());
if (reconnect()) {
rcTest = 0;
rcTestMax = 0;

logger.debug("{} - reconnectJob(): reconnected", toString());
if (reconnectJob.cancel(false)) {
reconnectJob = null;
logger.debug("{} - reconnectJob(): canceled", toString());
reconnecting.set(false);
}
} else {
if (rcTestMax <= RECONNECT_DELAY_MAX) {
rcTestMax++;
}
rcTest = 0;
logger.debug("{} - reconnectJob(): reconnected", toString());
reconnecting.set(false);
} else {
if (rcTestMax < RECONNECT_DELAY_MAX) {
rcTestMax++;
}
}, 1000, 1000, TimeUnit.MILLISECONDS);
rcTest = 0;
}
}

/**
Expand Down Expand Up @@ -428,12 +412,6 @@ protected void checkNewData() {

readed++;
readedBytes += area.getAddressSpaceLength();

/*
* if (logger.isDebugEnabled()) {
* logger.debug("{} - Reading finished. Area={}", toString(), area.toString());
* }
*/
}
} catch (Exception ex) {
logger.error("{} - Read data error", toString(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ public Boolean open() {
tryReconnect.set(true);
return false;
} finally {
if (!isConnected()) {
reconnectWithDelaying();
}

}

return true;
Expand Down Expand Up @@ -325,7 +323,10 @@ public void readDataArea(SimaticReadDataArea area) throws SimaticReadException {
}
}

long response = System.currentTimeMillis() - startTime;
if (logger.isDebugEnabled()) {
logger.debug("{} - Reading finished in {}ms. Area={}", toString(), System.currentTimeMillis() - startTime,
area.toString());
}
// get data for all items in area
for (SimaticChannel item : area.getItems()) {
// send value into openHAB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public Boolean open() {
tryReconnect.set(true);
return false;
} finally {
if (shouldReconnect()) {
reconnectWithDelaying();
}

}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@NonNullByDefault
public class SimaticBindingConstants {

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

private static final String BINDING_ID = "simatic";

Expand Down

0 comments on commit 734b706

Please sign in to comment.