Search in sources :

Example 1 with GenericFrame

use of org.apache.plc4x.java.can.generic.transport.GenericFrame 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

Instant (java.time.Instant)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Consumer (java.util.function.Consumer)1 org.apache.plc4x.java.api.messages (org.apache.plc4x.java.api.messages)1 PlcConsumerRegistration (org.apache.plc4x.java.api.model.PlcConsumerRegistration)1 PlcField (org.apache.plc4x.java.api.model.PlcField)1 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 PlcSubscriptionType (org.apache.plc4x.java.api.types.PlcSubscriptionType)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 Plc4xCANProtocolBase (org.apache.plc4x.java.can.adapter.Plc4xCANProtocolBase)1 GenericCANField (org.apache.plc4x.java.can.generic.field.GenericCANField)1 GenericFrame (org.apache.plc4x.java.can.generic.transport.GenericFrame)1 DataItem (org.apache.plc4x.java.genericcan.readwrite.DataItem)1 ConversationContext (org.apache.plc4x.java.spi.ConversationContext)1 DriverContext (org.apache.plc4x.java.spi.context.DriverContext)1 org.apache.plc4x.java.spi.generation (org.apache.plc4x.java.spi.generation)1