Search in sources :

Example 1 with PlcSubscriptionEvent

use of org.apache.plc4x.java.api.messages.PlcSubscriptionEvent in project plc4x by apache.

the class GenericCANDriverTest method testSubscribeAndWrite.

@Test
@Disabled("This test requires working virtual CAN transport to be truly platform independent")
void testSubscribeAndWrite() throws Exception {
    // PlcConnection connection1 = new PlcDriverManager().getConnection("genericcan:socketcan://vcan0");
    // PlcConnection connection2 = new PlcDriverManager().getConnection("genericcan:socketcan://vcan0");
    PlcConnection connection1 = new PlcDriverManager().getConnection("genericcan:virtualcan://");
    PlcConnection connection2 = connection1;
    CountDownLatch latch = new CountDownLatch(1);
    Byte field1 = 0x55;
    short field2 = 10;
    short field3 = 50;
    final AtomicReference<PlcSubscriptionEvent> plcEvent = new AtomicReference<>();
    connection1.subscriptionRequestBuilder().addEventField("field1", "200:BYTE").addEventField("field2", "200:UNSIGNED8").addEventField("field3", "200:UNSIGNED8").build().execute().whenComplete((reply, error) -> {
        if (error != null) {
            fail(error);
            return;
        }
        reply.getSubscriptionHandle("field1").register(new Consumer<PlcSubscriptionEvent>() {

            @Override
            public void accept(PlcSubscriptionEvent event) {
                plcEvent.set(event);
                latch.countDown();
            }
        });
    });
    connection2.writeRequestBuilder().addItem("f1", "200:BYTE", field1).addItem("f2", "200:UNSIGNED8", field2).addItem("f3", "200:UNSIGNED8", field3).build().execute().whenComplete((reply, error) -> {
        if (error != null) {
            fail(error);
        }
    }).get();
    latch.await();
    PlcSubscriptionEvent event = plcEvent.get();
    assertEquals(field1, event.getByte("field1"));
    assertEquals(field2, event.getShort("field2"));
    assertEquals(field3, event.getShort("field3"));
}
Also used : Consumer(java.util.function.Consumer) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) Assertions(org.junit.jupiter.api.Assertions) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with PlcSubscriptionEvent

use of org.apache.plc4x.java.api.messages.PlcSubscriptionEvent in project plc4x by apache.

the class OpcuaSubscriptionHandle method onSubscriptionValue.

/**
 * Receive the returned values from the OPCUA server and format it so that it can be received by the PLC4X client.
 *
 * @param values - array of data values to be sent to the client.
 */
private void onSubscriptionValue(MonitoredItemNotification[] values) {
    LinkedHashSet<String> fieldList = new LinkedHashSet<>();
    List<DataValue> dataValues = new ArrayList<>(values.length);
    for (MonitoredItemNotification value : values) {
        fieldList.add(fieldNames.get((int) value.getClientHandle() - 1));
        dataValues.add(value.getValue());
    }
    Map<String, ResponseItem<PlcValue>> fields = plcSubscriber.readResponse(fieldList, dataValues);
    final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), fields);
    consumers.forEach(plcSubscriptionEventConsumer -> plcSubscriptionEventConsumer.accept(event));
}
Also used : PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 3 with PlcSubscriptionEvent

use of org.apache.plc4x.java.api.messages.PlcSubscriptionEvent in project plc4x by apache.

the class CANOpenProtocolLogic method publishEvent.

private void publishEvent(CANOpenService service, int nodeId, CANOpenPayload payload) {
    DefaultPlcSubscriptionHandle dispatchedHandle = null;
    for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
        DefaultPlcConsumerRegistration registration = entry.getKey();
        Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
        for (PlcSubscriptionHandle handler : registration.getSubscriptionHandles()) {
            CANOpenSubscriptionHandle handle = (CANOpenSubscriptionHandle) handler;
            if (payload instanceof CANOpenPDOPayload) {
                if (handle.matches(service, nodeId)) {
                    logger.trace("Dispatching notification {} for node {} to {}", service, nodeId, handle);
                    dispatchedHandle = handle;
                    CANOpenPDOField field = (CANOpenPDOField) handle.getField();
                    byte[] data = ((CANOpenPDOPayload) payload).getPdo().getData();
                    try {
                        PlcValue value = DataItem.staticParse(new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN), field.getCanOpenDataType(), data.length);
                        DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.OK, value)));
                        consumer.accept(event);
                    } catch (ParseException e) {
                        logger.warn("Could not parse data to desired type: {}", field.getCanOpenDataType(), e);
                        DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.INVALID_DATA, new PlcNull())));
                        consumer.accept(event);
                    }
                }
            } else if (payload instanceof CANOpenHeartbeatPayload) {
                if (handle.matches(service, nodeId)) {
                    logger.trace("Dispatching notification {} for node {} to {}", service, nodeId, handle);
                    dispatchedHandle = handle;
                    final NMTState state = ((CANOpenHeartbeatPayload) payload).getState();
                    Map<String, PlcValue> fields = new HashMap<>();
                    fields.put("state", new PlcUSINT(state.getValue()));
                    fields.put("node", new PlcUSINT(nodeId));
                    PlcStruct struct = new PlcStruct(fields);
                    DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.OK, struct)));
                    consumer.accept(event);
                }
            } else if (payload instanceof CANOpenNetworkPayload) {
                if (handle.matches(service, nodeId)) {
                    logger.trace("Dispatching notification {} for node {} to {}", service, nodeId, handle);
                    dispatchedHandle = handle;
                    final NMTStateRequest state = ((CANOpenNetworkPayload) payload).getRequest();
                    Map<String, PlcValue> fields = new HashMap<>();
                    fields.put("state", new PlcUSINT(state.getValue()));
                    fields.put("node", new PlcUSINT(nodeId));
                    PlcStruct struct = new PlcStruct(fields);
                    DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.OK, struct)));
                    consumer.accept(event);
                }
            }
        }
    }
    if (dispatchedHandle == null) {
        logger.trace("Could not find subscription matching {} and node {}", service, nodeId);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PlcUSINT(org.apache.plc4x.java.spi.values.PlcUSINT) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) Consumer(java.util.function.Consumer) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) NMTState(org.apache.plc4x.java.canopen.readwrite.NMTState) CANOpenPDOPayload(org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) NMTStateRequest(org.apache.plc4x.java.canopen.readwrite.NMTStateRequest) CANOpenNetworkPayload(org.apache.plc4x.java.canopen.readwrite.CANOpenNetworkPayload) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CANOpenPDOField(org.apache.plc4x.java.canopen.field.CANOpenPDOField) CANOpenHeartbeatPayload(org.apache.plc4x.java.canopen.readwrite.CANOpenHeartbeatPayload) PlcNull(org.apache.plc4x.java.spi.values.PlcNull) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

PlcSubscriptionEvent (org.apache.plc4x.java.api.messages.PlcSubscriptionEvent)3 Consumer (java.util.function.Consumer)2 DefaultPlcSubscriptionEvent (org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent)2 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)1 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 CANOpenPDOField (org.apache.plc4x.java.canopen.field.CANOpenPDOField)1 CANOpenHeartbeatPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenHeartbeatPayload)1 CANOpenNetworkPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenNetworkPayload)1 CANOpenPDOPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload)1 NMTState (org.apache.plc4x.java.canopen.readwrite.NMTState)1