use of org.apache.plc4x.java.api.exceptions.PlcConnectionException in project plc4x by apache.
the class OpcuaPlcDriverTest method connectionNoParams.
@Test
public void connectionNoParams() {
connectionStringValidSet.forEach(connectionString -> {
try {
PlcConnection opcuaConnection = new PlcDriverManager().getConnection(connectionString);
Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
assertThat(opcuaConnection).is(is_connected);
opcuaConnection.close();
assertThat(opcuaConnection).isNot(is_connected);
} catch (PlcConnectionException e) {
fail("Exception during connectionNoParams while connecting Test EXCEPTION: " + e.getMessage());
} catch (Exception e) {
fail("Exception during connectionNoParams while closing Test EXCEPTION: " + e.getMessage());
}
});
}
use of org.apache.plc4x.java.api.exceptions.PlcConnectionException in project plc4x by apache.
the class SimulatedDriver method getConnection.
@Override
public PlcConnection getConnection(String url) throws PlcConnectionException {
// TODO: perform further checks
String deviceName = url.substring(getProtocolCode().length() + 1);
if (deviceName.isEmpty()) {
throw new PlcConnectionException("Invalid URL: no device name given.");
}
SimulatedDevice device = new SimulatedDevice(deviceName);
return new SimulatedConnection(device);
}
use of org.apache.plc4x.java.api.exceptions.PlcConnectionException in project plc4x by apache.
the class WaterTankService method connectToDevice.
protected void connectToDevice() {
try {
// Connect to the device
connection = new PlcDriverManager().getConnection(connectionString);
// Check if subscriptions are supported by this connection.
if (!connection.getMetadata().canSubscribe()) {
throw new PlcRuntimeException("This driver doesn't support subscribing");
}
// Prepare a subscription request.
final PlcSubscriptionRequest subscriptionRequest = connection.subscriptionRequestBuilder().addChangeOfStateField(WATER_LEVEL, addressStringWaterLevel).build();
// Execute the request.
PlcSubscriptionResponse syncResponse = subscriptionRequest.execute().get();
// Attach handlers for the incoming data.
for (String subscriptionName : syncResponse.getFieldNames()) {
final PlcSubscriptionHandle subscriptionHandle = syncResponse.getSubscriptionHandle(subscriptionName);
subscriptionHandle.register(new WaterLevelHandler());
}
} catch (PlcConnectionException e) {
throw new PlcRuntimeException("Error connecting to device", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PlcRuntimeException("Error subscribing for data", e);
} catch (ExecutionException e) {
throw new PlcRuntimeException("Error subscribing for data", e);
}
}
use of org.apache.plc4x.java.api.exceptions.PlcConnectionException in project plc4x by apache.
the class PlcEntityManagerComplexTest method getPlcEntityManager.
private PlcEntityManager getPlcEntityManager(final Map<String, PlcValue> responses) throws PlcConnectionException {
driverManager = Mockito.mock(PlcDriverManager.class);
PlcDriverManager mock = driverManager;
PlcConnection connection = Mockito.mock(PlcConnection.class);
when(mock.getConnection(ArgumentMatchers.anyString())).thenReturn(connection);
when(connection.getMetadata()).thenReturn(new PlcConnectionMetadata() {
@Override
public boolean canRead() {
return true;
}
@Override
public boolean canWrite() {
return true;
}
@Override
public boolean canSubscribe() {
return true;
}
});
PlcReader reader = readRequest -> {
Map<String, ResponseItem<PlcValue>> map = readRequest.getFieldNames().stream().collect(Collectors.toMap(Function.identity(), s -> new ResponseItem<>(PlcResponseCode.OK, Objects.requireNonNull(responses.get(s), s + " not found"))));
return CompletableFuture.completedFuture(new DefaultPlcReadResponse(readRequest, map));
};
when(connection.readRequestBuilder()).then(invocation -> new DefaultPlcReadRequest.Builder(reader, getFieldHandler()));
PlcWriter writer = writeRequest -> {
Map<String, PlcResponseCode> map = writeRequest.getFieldNames().stream().collect(Collectors.toMap(Function.identity(), s -> PlcResponseCode.OK));
return CompletableFuture.completedFuture(new DefaultPlcWriteResponse(writeRequest, map));
};
when(connection.writeRequestBuilder()).then(invocation -> new DefaultPlcWriteRequest.Builder(writer, getFieldHandler(), getValueHandler()));
return new PlcEntityManager(mock);
}
use of org.apache.plc4x.java.api.exceptions.PlcConnectionException in project plc4x by apache.
the class PooledPlcDriverManagerTest method cleanupOfBrokenConnections.
@Test
void cleanupOfBrokenConnections() throws Exception {
AtomicBoolean failNow = new AtomicBoolean(false);
when(plcDriver.getConnection(anyString())).then(invocationOnMock -> {
DummyPlcConnection dummyPlcConnection = spy(new DummyPlcConnection(invocationOnMock.getArgument(0)));
// we fake an connection which breaks at this call
doAnswer(invocation -> {
if (failNow.get()) {
throw new PlcConnectionException("blub");
}
return invocation.callRealMethod();
}).when(dummyPlcConnection).connect();
return dummyPlcConnection;
});
assertThat(SUT.getStatistics()).containsOnly(entry("pools.count", 0), entry("numActive", 0), entry("numIdle", 0));
PlcConnection connection = SUT.getConnection("dummydummy:breakIt");
assertThat(SUT.getStatistics()).containsOnly(entry("pools.count", 1), entry("numActive", 1), entry("numIdle", 0), entry("PoolKey{url='dummydummy:breakIt'}.numActive", 1));
failNow.set(true);
try {
connection.connect();
fail("This should throw an exception");
} catch (Exception e) {
// TODO: currently UndeclaredThrowableException is the top one which should be InvocationTargetException
// assertThat(e).isInstanceOf(InvocationTargetException.class);
assertThat(e).hasRootCauseInstanceOf(PlcConnectionException.class);
}
// Faulty connection should have been discarded
assertThat(SUT.getStatistics()).containsOnly(entry("pools.count", 0), entry("numActive", 0), entry("numIdle", 0));
}
Aggregations