Search in sources :

Example 1 with GenericCANField

use of org.apache.plc4x.java.can.generic.field.GenericCANField in project plc4x by apache.

the class GenericCANProtocolLogic 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);
    Map<Integer, GenericCANSubscriptionHandle> handles = new HashMap<>();
    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 GenericCANField) {
            GenericCANField canField = (GenericCANField) subscription.getPlcField();
            GenericCANSubscriptionHandle subscriptionHandle = handles.computeIfAbsent(canField.getNodeId(), node -> new GenericCANSubscriptionHandle(this, node));
            answers.put(key, new ResponseItem<>(PlcResponseCode.OK, subscriptionHandle));
            subscriptionHandle.add(key, canField);
        } else {
            answers.put(key, new ResponseItem<>(PlcResponseCode.INVALID_ADDRESS, null));
        }
    }
    return CompletableFuture.completedFuture(response);
}
Also used : PlcSubscriptionType(org.apache.plc4x.java.api.types.PlcSubscriptionType) java.util(java.util) DriverContext(org.apache.plc4x.java.spi.context.DriverContext) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) CompletableFuture(java.util.concurrent.CompletableFuture) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Logger(org.slf4j.Logger) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) org.apache.plc4x.java.spi.messages(org.apache.plc4x.java.spi.messages) Instant(java.time.Instant) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) PlcField(org.apache.plc4x.java.api.model.PlcField) DataItem(org.apache.plc4x.java.genericcan.readwrite.DataItem) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) Consumer(java.util.function.Consumer) Plc4xCANProtocolBase(org.apache.plc4x.java.can.adapter.Plc4xCANProtocolBase) GenericFrame(org.apache.plc4x.java.can.generic.transport.GenericFrame) GenericCANField(org.apache.plc4x.java.can.generic.field.GenericCANField) Entry(java.util.Map.Entry) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) GenericCANField(org.apache.plc4x.java.can.generic.field.GenericCANField) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 2 with GenericCANField

use of org.apache.plc4x.java.can.generic.field.GenericCANField in project plc4x by apache.

the class GenericCANProtocolLogic method decode.

@Override
public void decode(ConversationContext<GenericFrame> context, GenericFrame msg) throws Exception {
    for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
        DefaultPlcConsumerRegistration registration = entry.getKey();
        Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
        for (PlcSubscriptionHandle handle : registration.getSubscriptionHandles()) {
            GenericCANSubscriptionHandle subscription = (GenericCANSubscriptionHandle) handle;
            Map<String, ResponseItem<PlcValue>> fields = new LinkedHashMap<>();
            ReadBuffer buffer = new ReadBufferByteBased(msg.getData(), ByteOrder.LITTLE_ENDIAN);
            buffer.pullContext("readFields");
            if (subscription.matches(msg.getNodeId())) {
                for (Entry<String, GenericCANField> field : subscription.getFields().entrySet()) {
                    try {
                        PlcValue value = read(buffer, field.getValue());
                        if (value == null) {
                            fields.put(field.getKey(), new ResponseItem<>(PlcResponseCode.INTERNAL_ERROR, null));
                        } else {
                            fields.put(field.getKey(), new ResponseItem<>(PlcResponseCode.OK, value));
                        }
                    } catch (ParseException e) {
                        fields.put(field.getKey(), new ResponseItem<>(PlcResponseCode.INVALID_DATA, null));
                    }
                }
                consumer.accept(new DefaultPlcSubscriptionEvent(Instant.now(), fields));
            }
            buffer.closeContext("readFields");
        }
    }
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) PlcValue(org.apache.plc4x.java.api.value.PlcValue) Consumer(java.util.function.Consumer) GenericCANField(org.apache.plc4x.java.can.generic.field.GenericCANField) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with GenericCANField

use of org.apache.plc4x.java.can.generic.field.GenericCANField in project plc4x by apache.

the class GenericCANProtocolLogic method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    final RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    CompletableFuture<PlcWriteResponse> response = new CompletableFuture<>();
    transaction.submit(() -> {
        Map<Integer, WriteBufferByteBased> messages = new LinkedHashMap<>();
        Map<Integer, Map<String, PlcResponseCode>> responses = new HashMap<>();
        for (String field : writeRequest.getFieldNames()) {
            PlcField plcField = writeRequest.getField(field);
            if (!(plcField instanceof GenericCANField)) {
                responses.computeIfAbsent(-1, (node) -> new HashMap<>()).put(field, PlcResponseCode.UNSUPPORTED);
                continue;
            }
            GenericCANField canField = (GenericCANField) plcField;
            WriteBuffer buffer = messages.computeIfAbsent(canField.getNodeId(), (node) -> new WriteBufferByteBased(8, ByteOrder.LITTLE_ENDIAN));
            Map<String, PlcResponseCode> statusMap = responses.computeIfAbsent(canField.getNodeId(), (node) -> new HashMap<>());
            PlcValue value = writeRequest.getPlcValue(field);
            try {
                write(buffer, canField, value);
                statusMap.put(field, PlcResponseCode.OK);
            } catch (SerializationException e) {
                statusMap.put(field, PlcResponseCode.INVALID_DATA);
            }
        }
        Map<String, PlcResponseCode> codes = new HashMap<>();
        for (Map.Entry<Integer, WriteBufferByteBased> message : messages.entrySet()) {
            boolean discarded = false;
            for (Map.Entry<String, PlcResponseCode> entry : responses.get(message.getKey()).entrySet()) {
                codes.put(entry.getKey(), entry.getValue());
                if (!discarded && entry.getValue() != PlcResponseCode.OK) {
                    logger.info("Discarding writing of frame with field {}. Node {} will not be communicated.", entry.getKey(), message.getKey());
                    discarded = true;
                }
            }
            if (!discarded) {
                byte[] data = message.getValue().getData();
                logger.debug("Writing message with id {} and {} bytes of data", message.getKey(), data.length);
                context.sendToWire(new GenericFrame(message.getKey(), data));
            }
        }
        response.complete(new DefaultPlcWriteResponse(writeRequest, codes));
        transaction.endRequest();
    });
    return response;
}
Also used : PlcSubscriptionType(org.apache.plc4x.java.api.types.PlcSubscriptionType) java.util(java.util) DriverContext(org.apache.plc4x.java.spi.context.DriverContext) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) CompletableFuture(java.util.concurrent.CompletableFuture) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Logger(org.slf4j.Logger) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) org.apache.plc4x.java.spi.messages(org.apache.plc4x.java.spi.messages) Instant(java.time.Instant) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) PlcField(org.apache.plc4x.java.api.model.PlcField) DataItem(org.apache.plc4x.java.genericcan.readwrite.DataItem) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) Consumer(java.util.function.Consumer) Plc4xCANProtocolBase(org.apache.plc4x.java.can.adapter.Plc4xCANProtocolBase) GenericFrame(org.apache.plc4x.java.can.generic.transport.GenericFrame) GenericCANField(org.apache.plc4x.java.can.generic.field.GenericCANField) Entry(java.util.Map.Entry) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PlcField(org.apache.plc4x.java.api.model.PlcField) CompletableFuture(java.util.concurrent.CompletableFuture) GenericCANField(org.apache.plc4x.java.can.generic.field.GenericCANField) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcValue(org.apache.plc4x.java.api.value.PlcValue) GenericFrame(org.apache.plc4x.java.can.generic.transport.GenericFrame) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Consumer (java.util.function.Consumer)3 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)3 PlcValue (org.apache.plc4x.java.api.value.PlcValue)3 GenericCANField (org.apache.plc4x.java.can.generic.field.GenericCANField)3 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)3 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)3 DefaultPlcSubscriptionHandle (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)3 Instant (java.time.Instant)2 java.util (java.util)2 Entry (java.util.Map.Entry)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 org.apache.plc4x.java.api.messages (org.apache.plc4x.java.api.messages)2 PlcConsumerRegistration (org.apache.plc4x.java.api.model.PlcConsumerRegistration)2 PlcField (org.apache.plc4x.java.api.model.PlcField)2 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)2 PlcSubscriptionType (org.apache.plc4x.java.api.types.PlcSubscriptionType)2 Plc4xCANProtocolBase (org.apache.plc4x.java.can.adapter.Plc4xCANProtocolBase)2 GenericFrame (org.apache.plc4x.java.can.generic.transport.GenericFrame)2 DataItem (org.apache.plc4x.java.genericcan.readwrite.DataItem)2