use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class Plc4xReadClient method main.
public static void main(String[] args) throws Exception {
try (final PlcConnection connection = new PlcDriverManager().getConnection("plc4x://localhost?remote-connection-string=simulated%3A%2F%2Flocalhost")) {
final PlcReadRequest.Builder requestBuilder = connection.readRequestBuilder();
requestBuilder.addItem("test-BOOL", "RANDOM/foo:BOOL");
requestBuilder.addItem("test-BYTE", "RANDOM/foo:BYTE");
requestBuilder.addItem("test-WORD", "RANDOM/foo:WORD");
requestBuilder.addItem("test-DWORD", "RANDOM/foo:DWORD");
requestBuilder.addItem("test-USINT", "RANDOM/foo:USINT");
requestBuilder.addItem("test-UINT", "RANDOM/foo:UINT");
requestBuilder.addItem("test-UDINT", "RANDOM/foo:UDINT");
requestBuilder.addItem("test-ULINT", "RANDOM/foo:ULINT");
requestBuilder.addItem("test-SINT", "RANDOM/foo:SINT");
requestBuilder.addItem("test-INT", "RANDOM/foo:INT");
requestBuilder.addItem("test-DINT", "RANDOM/foo:DINT");
requestBuilder.addItem("test-LINT", "RANDOM/foo:LINT");
requestBuilder.addItem("test-REAL", "RANDOM/foo:REAL");
requestBuilder.addItem("test-LREAL", "RANDOM/foo:LREAL");
requestBuilder.addItem("test-CHAR", "RANDOM/foo:CHAR");
requestBuilder.addItem("test-WCHAR", "RANDOM/foo:WCHAR");
final PlcReadRequest readRequest = requestBuilder.build();
final PlcReadResponse readResponse = readRequest.execute().get();
System.out.println(readResponse);
}
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class Plc4xWriteClient method main.
public static void main(String[] args) throws Exception {
try (final PlcConnection connection = new PlcDriverManager().getConnection("plc4x://localhost?remote-connection-string=simulated%3A%2F%2Flocalhost")) {
final PlcWriteRequest.Builder requestBuilder = connection.writeRequestBuilder();
requestBuilder.addItem("test-BOOL", "STDOUT/foo:BOOL", true);
requestBuilder.addItem("test-BYTE", "STDOUT/foo:BYTE", new boolean[] { true, true, false, true, false, true, false, true });
requestBuilder.addItem("test-WORD", "STDOUT/foo:WORD", new boolean[] { true, true, false, true, false, true, false, true, false, false, false, false, true, true, true, true });
requestBuilder.addItem("test-DWORD", "STDOUT/foo:DWORD", new boolean[] { true, true, false, true, false, true, false, true, false, false, false, false, true, true, true, true, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false });
requestBuilder.addItem("test-USINT", "STDOUT/foo:USINT", 12);
requestBuilder.addItem("test-UINT", "STDOUT/foo:UINT", 12345);
requestBuilder.addItem("test-UDINT", "STDOUT/foo:UDINT", 1234567890);
requestBuilder.addItem("test-ULINT", "STDOUT/foo:ULINT", new BigInteger("12345678901234567"));
requestBuilder.addItem("test-SINT", "STDOUT/foo:SINT", -12);
requestBuilder.addItem("test-INT", "STDOUT/foo:INT", -12345);
requestBuilder.addItem("test-DINT", "STDOUT/foo:DINT", -1234567890);
requestBuilder.addItem("test-LINT", "STDOUT/foo:LINT", new BigInteger("-12345678901234567"));
requestBuilder.addItem("test-REAL", "STDOUT/foo:REAL", 3.14159f);
requestBuilder.addItem("test-LREAL", "STDOUT/foo:LREAL", 2.71828182845904523536028747135d);
requestBuilder.addItem("test-CHAR", "STDOUT/foo:CHAR", "P");
requestBuilder.addItem("test-WCHAR", "STDOUT/foo:WCHAR", "Ϡ");
final PlcWriteRequest writeRequest = requestBuilder.build();
final PlcWriteResponse writeResponse = writeRequest.execute().get();
System.out.println(writeResponse);
}
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class ManualProfinetIoTest method main.
public static void main(String[] args) throws Exception {
final PlcConnection connection = new PlcDriverManager().getConnection("profinet://192.168.24.31");
final PlcReadRequest readRequest = connection.readRequestBuilder().addItem("test", "").build();
final PlcReadResponse plcReadResponse = readRequest.execute().get();
System.out.println(plcReadResponse);
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class MockDriver method getConnection.
@Override
public PlcConnection getConnection(String url) throws PlcConnectionException {
// Mock a connection.
PlcConnection plcConnectionMock = mock(PlcConnection.class, RETURNS_DEEP_STUBS);
when(plcConnectionMock.getMetadata().canRead()).thenReturn(true);
when(plcConnectionMock.getMetadata().canWrite()).thenReturn(true);
when(plcConnectionMock.readRequestBuilder()).thenReturn(mock(PlcReadRequest.Builder.class, RETURNS_DEEP_STUBS));
when(plcConnectionMock.writeRequestBuilder()).thenReturn(mock(PlcWriteRequest.Builder.class, RETURNS_DEEP_STUBS));
when(plcConnectionMock.subscriptionRequestBuilder()).thenReturn(mock(PlcSubscriptionRequest.Builder.class, RETURNS_DEEP_STUBS));
when(plcConnectionMock.unsubscriptionRequestBuilder()).thenReturn(mock(PlcUnsubscriptionRequest.Builder.class, RETURNS_DEEP_STUBS));
// Mock a typical subscriber.
PlcSubscriber plcSubscriber = mock(PlcSubscriber.class, RETURNS_DEEP_STUBS);
when(plcSubscriber.subscribe(any(PlcSubscriptionRequest.class))).thenAnswer(invocation -> {
LOGGER.info("Received {}", invocation);
// TODO: Translate this so it actually does something ...
/*PlcSubscriptionRequest subscriptionRequest = invocation.getArgument(0);
List<PlcSubscriptionResponse> responseItems =
subscriptionRequest.getFieldNames().stream().map(
fieldName -> subscriptionRequest.getField(fieldName)).map(field -> {
Consumer consumer = subscriptionRequestItem.getConsumer();
executorService.submit(() -> {
while (!Thread.currentThread().isInterrupted()) {
consumer.accept(new SubscriptionEventItem<>(null, Calendar.getInstance(), Collections.singletonList("HelloWorld")));
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
});
return new SubscriptionResponseItem<>(subscriptionRequestItem,
mock(PlcSubscriptionHandle.class, RETURNS_DEEP_STUBS), PlcResponseCode.OK);
}).collect(Collectors.toList());
PlcSubscriptionResponse response = new PlcSubscriptionResponse(subscriptionRequest, responseItems);*/
PlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(mock(PlcSubscriptionRequest.class), new HashMap<>());
CompletableFuture<PlcSubscriptionResponse> responseFuture = new CompletableFuture<>();
responseFuture.complete(response);
return responseFuture;
});
return plcConnectionMock;
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class Plc4XProducerTest method doStopBadConnection.
@Test
public void doStopBadConnection() throws Exception {
Field openRequests = SUT.getClass().getDeclaredField("plcConnection");
openRequests.setAccessible(true);
PlcConnection plcConnectionMock = mock(PlcConnection.class);
doThrow(new RuntimeException("oh noes")).when(plcConnectionMock).close();
openRequests.set(SUT, plcConnectionMock);
SUT.doStop();
}
Aggregations