Search in sources :

Example 1 with CANOpenPDOField

use of org.apache.plc4x.java.canopen.field.CANOpenPDOField in project plc4x by apache.

the class CANOpenProtocolLogic method write.

public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    CompletableFuture<PlcWriteResponse> response = new CompletableFuture<>();
    if (writeRequest.getFieldNames().size() != 1) {
        response.completeExceptionally(new IllegalArgumentException("You can write only one field at the time"));
        return response;
    }
    PlcField field = writeRequest.getFields().get(0);
    if (!(field instanceof CANOpenField)) {
        response.completeExceptionally(new IllegalArgumentException("Only CANOpenField instances are supported"));
        return response;
    }
    if (field instanceof CANOpenSDOField) {
        writeInternally((DefaultPlcWriteRequest) writeRequest, (CANOpenSDOField) field, response);
        return response;
    }
    if (field instanceof CANOpenPDOField) {
        writeInternally((DefaultPlcWriteRequest) writeRequest, (CANOpenPDOField) field, response);
        return response;
    }
    response.completeExceptionally(new IllegalArgumentException("Only CANOpenSDOField instances are supported"));
    return response;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CANOpenPDOField(org.apache.plc4x.java.canopen.field.CANOpenPDOField) CANOpenField(org.apache.plc4x.java.canopen.field.CANOpenField) CANOpenSDOField(org.apache.plc4x.java.canopen.field.CANOpenSDOField) PlcField(org.apache.plc4x.java.api.model.PlcField) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)

Example 2 with CANOpenPDOField

use of org.apache.plc4x.java.canopen.field.CANOpenPDOField 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)

Example 3 with CANOpenPDOField

use of org.apache.plc4x.java.canopen.field.CANOpenPDOField in project plc4x by apache.

the class CANOpenProtocolLogic method subscribe.

@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest request) {
    DefaultPlcSubscriptionRequest rq = (DefaultPlcSubscriptionRequest) request;
    Map<String, ResponseItem<PlcSubscriptionHandle>> answers = new LinkedHashMap<>();
    DefaultPlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(rq, answers);
    for (String key : rq.getFieldNames()) {
        DefaultPlcSubscriptionField subscription = (DefaultPlcSubscriptionField) rq.getField(key);
        if (subscription.getPlcSubscriptionType() != PlcSubscriptionType.EVENT) {
            answers.put(key, new ResponseItem<>(PlcResponseCode.UNSUPPORTED, null));
        } else if ((subscription.getPlcField() instanceof CANOpenPDOField)) {
            answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenPDOField) subscription.getPlcField())));
        } else if ((subscription.getPlcField() instanceof CANOpenNMTField)) {
            answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenNMTField) subscription.getPlcField())));
        } else if ((subscription.getPlcField() instanceof CANOpenHeartbeatField)) {
            answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenHeartbeatField) subscription.getPlcField())));
        } else {
            answers.put(key, new ResponseItem<>(PlcResponseCode.INVALID_ADDRESS, null));
        }
    }
    return CompletableFuture.completedFuture(response);
}
Also used : DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) CANOpenPDOField(org.apache.plc4x.java.canopen.field.CANOpenPDOField) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) CANOpenNMTField(org.apache.plc4x.java.canopen.field.CANOpenNMTField) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) DefaultPlcSubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest) LinkedHashMap(java.util.LinkedHashMap) CANOpenHeartbeatField(org.apache.plc4x.java.canopen.field.CANOpenHeartbeatField)

Aggregations

CANOpenPDOField (org.apache.plc4x.java.canopen.field.CANOpenPDOField)3 LinkedHashMap (java.util.LinkedHashMap)2 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Consumer (java.util.function.Consumer)1 PlcSubscriptionEvent (org.apache.plc4x.java.api.messages.PlcSubscriptionEvent)1 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)1 PlcField (org.apache.plc4x.java.api.model.PlcField)1 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 CANOpenField (org.apache.plc4x.java.canopen.field.CANOpenField)1 CANOpenHeartbeatField (org.apache.plc4x.java.canopen.field.CANOpenHeartbeatField)1 CANOpenNMTField (org.apache.plc4x.java.canopen.field.CANOpenNMTField)1 CANOpenSDOField (org.apache.plc4x.java.canopen.field.CANOpenSDOField)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