Skip to content

Commit 07b27fb

Browse files
committed
Error states were incorrectly handled in ADS. There is also a library bug complicating things
1 parent f66448f commit 07b27fb

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/impl/AbstractPlc4xAdapter.java

+23-24
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
import org.slf4j.LoggerFactory;
4646

4747
import java.util.Collections;
48-
import java.util.HashMap;
4948
import java.util.List;
5049
import java.util.Map;
5150
import java.util.Objects;
5251
import java.util.function.Function;
5352
import java.util.stream.Collectors;
5453

5554
import static com.hivemq.adapter.sdk.api.state.ProtocolAdapterState.ConnectionStatus.CONNECTED;
55+
import static com.hivemq.adapter.sdk.api.state.ProtocolAdapterState.ConnectionStatus.ERROR;
5656

5757
/**
5858
* Abstract PLC4X implementation. Exposes core abstractions of the underlying framework so instances can be exposes
@@ -135,9 +135,28 @@ public void poll(final @NotNull BatchPollingInput pollingInput, final @NotNull B
135135
public void start(
136136
final @NotNull ProtocolAdapterStartInput input, final @NotNull ProtocolAdapterStartOutput output) {
137137
try {
138-
// we do not subscribe anymore as no current adapter type supports it anyway
139-
initConnection();
140-
output.startedSuccessfully();
138+
if (connection == null) {
139+
synchronized (lock) {
140+
if (connection == null) {
141+
// we do not subscribe anymore as no current adapter type supports it anyway
142+
if (log.isTraceEnabled()) {
143+
log.trace("Creating new instance of Plc4x connector with {}.", adapterConfig);
144+
}
145+
final Plc4xConnection<T> connection = createConnection();
146+
if(connection.isConnected()) {
147+
protocolAdapterState.setConnectionStatus(CONNECTED);
148+
output.startedSuccessfully();
149+
} else {
150+
protocolAdapterState.setConnectionStatus(ERROR);
151+
output.failStart(new Plc4xException("Unable to connect to device"), "Unable to connect to device");
152+
}
153+
154+
this.connection = connection;
155+
}
156+
}
157+
} else {
158+
output.startedSuccessfully();
159+
}
141160
} catch (final Exception e) {
142161
output.failStart(e, null);
143162
}
@@ -164,26 +183,6 @@ public void stop(final @NotNull ProtocolAdapterStopInput input, final @NotNull P
164183
return adapterInformation;
165184
}
166185

167-
private Plc4xConnection<T> initConnection() {
168-
if (connection == null) {
169-
synchronized (lock) {
170-
if (connection == null) {
171-
try {
172-
if (log.isTraceEnabled()) {
173-
log.trace("Creating new instance of Plc4x connector with {}.", adapterConfig);
174-
}
175-
connection = createConnection();
176-
protocolAdapterState.setConnectionStatus(CONNECTED);
177-
return connection;
178-
} catch (final Plc4xException e) {
179-
throw new RuntimeException(e);
180-
}
181-
}
182-
}
183-
}
184-
return connection;
185-
}
186-
187186
protected @NotNull Plc4xConnection<T> createConnection() throws Plc4xException {
188187
return new Plc4xConnection<>(driverManager,
189188
adapterConfig,

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/impl/Plc4xConnection.java

+26-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
import org.slf4j.LoggerFactory;
3333

3434
import java.util.List;
35+
import java.util.Optional;
3536
import java.util.concurrent.CompletableFuture;
37+
import java.util.concurrent.ExecutionException;
38+
import java.util.concurrent.Executors;
39+
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.TimeoutException;
3641
import java.util.function.Consumer;
3742

3843
public abstract class Plc4xConnection<T extends Plc4XSpecificAdapterConfig<?>> {
@@ -76,23 +81,30 @@ public Plc4xConnection(
7681
}
7782

7883
protected void initConnection() throws Plc4xException {
79-
try {
80-
if (plcConnection == null) {
81-
synchronized (lock) {
82-
if (plcConnection == null) {
83-
final String connectionString = createConnectionString(config);
84-
if (log.isTraceEnabled()) {
85-
log.trace("Connecting via PLC4X to {}.", connectionString);
86-
}
87-
plcConnection = plcDriverManager.getConnectionManager().getConnection(connectionString);
84+
if (plcConnection == null) {
85+
synchronized (lock) {
86+
if (plcConnection == null) {
87+
final String connectionString = createConnectionString(config);
88+
if (log.isTraceEnabled()) {
89+
log.trace("Connecting via PLC4X to {}.", connectionString);
90+
}
91+
try {
92+
plcConnection = CompletableFuture.supplyAsync(() -> {
93+
try {
94+
return Optional.of(plcDriverManager.getConnectionManager().getConnection(connectionString));
95+
} catch (Throwable e) {
96+
log.info("Error encountered connecting to external device", e);
97+
}
98+
return Optional.<PlcConnection>empty();
99+
})
100+
.get(2_000, TimeUnit.MILLISECONDS)
101+
.orElseThrow(() -> new Plc4xException("Unable to connect to device."));
102+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
103+
log.error("Error encountered connecting to external device", e);
104+
throw new Plc4xException(e);
88105
}
89106
}
90107
}
91-
} catch (PlcConnectionException e) {
92-
if (log.isInfoEnabled()) {
93-
log.info("Error encountered connecting to external device.", e);
94-
}
95-
throw new Plc4xException("Error connecting", e);
96108
}
97109
}
98110

0 commit comments

Comments
 (0)