Search in sources :

Example 1 with PlcSubscriber

use of org.apache.plc4x.java.spi.messages.PlcSubscriber 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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) PlcSubscriber(org.apache.plc4x.java.spi.messages.PlcSubscriber) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 DefaultPlcSubscriptionResponse (org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse)1 PlcSubscriber (org.apache.plc4x.java.spi.messages.PlcSubscriber)1