Search in sources :

Example 16 with PlcResponseCode

use of org.apache.plc4x.java.api.types.PlcResponseCode in project plc4x by apache.

the class OpcuaProtocolLogic method readResponse.

public Map<String, ResponseItem<PlcValue>> readResponse(LinkedHashSet<String> fieldNames, List<DataValue> results) {
    PlcResponseCode responseCode = PlcResponseCode.OK;
    Map<String, ResponseItem<PlcValue>> response = new HashMap<>();
    int count = 0;
    for (String field : fieldNames) {
        PlcValue value = null;
        if (results.get(count).getValueSpecified()) {
            Variant variant = results.get(count).getValue();
            LOGGER.trace("Response of type {}", variant.getClass().toString());
            if (variant instanceof VariantBoolean) {
                byte[] array = ((VariantBoolean) variant).getValue();
                int length = array.length;
                Boolean[] tmpValue = new Boolean[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = array[i] != 0;
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantSByte) {
                byte[] array = ((VariantSByte) variant).getValue();
                value = IEC61131ValueHandler.of(array);
            } else if (variant instanceof VariantByte) {
                List<Short> array = ((VariantByte) variant).getValue();
                Short[] tmpValue = array.toArray(new Short[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantInt16) {
                List<Short> array = ((VariantInt16) variant).getValue();
                Short[] tmpValue = array.toArray(new Short[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantUInt16) {
                List<Integer> array = ((VariantUInt16) variant).getValue();
                Integer[] tmpValue = array.toArray(new Integer[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantInt32) {
                List<Integer> array = ((VariantInt32) variant).getValue();
                Integer[] tmpValue = array.toArray(new Integer[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantUInt32) {
                List<Long> array = ((VariantUInt32) variant).getValue();
                Long[] tmpValue = array.toArray(new Long[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantInt64) {
                List<Long> array = ((VariantInt64) variant).getValue();
                Long[] tmpValue = array.toArray(new Long[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantUInt64) {
                value = IEC61131ValueHandler.of(((VariantUInt64) variant).getValue());
            } else if (variant instanceof VariantFloat) {
                List<Float> array = ((VariantFloat) variant).getValue();
                Float[] tmpValue = array.toArray(new Float[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantDouble) {
                List<Double> array = ((VariantDouble) variant).getValue();
                Double[] tmpValue = array.toArray(new Double[0]);
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantString) {
                int length = ((VariantString) variant).getValue().size();
                List<PascalString> stringArray = ((VariantString) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = stringArray.get(i).getStringValue();
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantDateTime) {
                List<Long> array = ((VariantDateTime) variant).getValue();
                int length = array.size();
                LocalDateTime[] tmpValue = new LocalDateTime[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = LocalDateTime.ofInstant(Instant.ofEpochMilli(getDateTime(array.get(i))), ZoneOffset.UTC);
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantGuid) {
                List<GuidValue> array = ((VariantGuid) variant).getValue();
                int length = array.size();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    // These two data section aren't little endian like the rest.
                    byte[] data4Bytes = array.get(i).getData4();
                    int data4 = 0;
                    for (byte data4Byte : data4Bytes) {
                        data4 = (data4 << 8) + (data4Byte & 0xff);
                    }
                    byte[] data5Bytes = array.get(i).getData5();
                    long data5 = 0;
                    for (byte data5Byte : data5Bytes) {
                        data5 = (data5 << 8) + (data5Byte & 0xff);
                    }
                    tmpValue[i] = Long.toHexString(array.get(i).getData1()) + "-" + Integer.toHexString(array.get(i).getData2()) + "-" + Integer.toHexString(array.get(i).getData3()) + "-" + Integer.toHexString(data4) + "-" + Long.toHexString(data5);
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantXmlElement) {
                int length = ((VariantXmlElement) variant).getValue().size();
                List<PascalString> strings = ((VariantXmlElement) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = strings.get(i).getStringValue();
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantLocalizedText) {
                int length = ((VariantLocalizedText) variant).getValue().size();
                List<LocalizedText> strings = ((VariantLocalizedText) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = "";
                    tmpValue[i] += strings.get(i).getLocaleSpecified() ? strings.get(i).getLocale().getStringValue() + "|" : "";
                    tmpValue[i] += strings.get(i).getTextSpecified() ? strings.get(i).getText().getStringValue() : "";
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantQualifiedName) {
                int length = ((VariantQualifiedName) variant).getValue().size();
                List<QualifiedName> strings = ((VariantQualifiedName) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = "ns=" + strings.get(i).getNamespaceIndex() + ";s=" + strings.get(i).getName().getStringValue();
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantExtensionObject) {
                int length = ((VariantExtensionObject) variant).getValue().size();
                List<ExtensionObject> strings = ((VariantExtensionObject) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = strings.get(i).toString();
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantNodeId) {
                int length = ((VariantNodeId) variant).getValue().size();
                List<NodeId> strings = ((VariantNodeId) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = strings.get(i).toString();
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantStatusCode) {
                int length = ((VariantStatusCode) variant).getValue().size();
                List<StatusCode> strings = ((VariantStatusCode) variant).getValue();
                String[] tmpValue = new String[length];
                for (int i = 0; i < length; i++) {
                    tmpValue[i] = strings.get(i).toString();
                }
                value = IEC61131ValueHandler.of(tmpValue);
            } else if (variant instanceof VariantByteString) {
                PlcList plcList = new PlcList();
                List<ByteStringArray> array = ((VariantByteString) variant).getValue();
                for (int k = 0; k < array.size(); k++) {
                    int length = array.get(k).getValue().size();
                    Short[] tmpValue = new Short[length];
                    for (int i = 0; i < length; i++) {
                        tmpValue[i] = array.get(k).getValue().get(i);
                    }
                    plcList.add(IEC61131ValueHandler.of(tmpValue));
                }
                value = plcList;
            } else {
                responseCode = PlcResponseCode.UNSUPPORTED;
                LOGGER.error("Data type - " + variant.getClass() + " is not supported ");
            }
        } else {
            if (results.get(count).getStatusCode().getStatusCode() == OpcuaStatusCode.BadNodeIdUnknown.getValue()) {
                responseCode = PlcResponseCode.NOT_FOUND;
            } else {
                responseCode = PlcResponseCode.UNSUPPORTED;
            }
            LOGGER.error("Error while reading value from OPC UA server error code:- " + results.get(count).getStatusCode().toString());
        }
        count++;
        response.put(field, new ResponseItem<>(responseCode, value));
    }
    return response;
}
Also used : PlcList(org.apache.plc4x.java.spi.values.PlcList) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PlcValue(org.apache.plc4x.java.api.value.PlcValue) LocalDateTime(java.time.LocalDateTime) PlcList(org.apache.plc4x.java.spi.values.PlcList) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) BigInteger(java.math.BigInteger)

Example 17 with PlcResponseCode

use of org.apache.plc4x.java.api.types.PlcResponseCode in project plc4x by apache.

the class KnxNetIpProtocolLogic method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
    DefaultPlcWriteRequest request = (DefaultPlcWriteRequest) writeRequest;
    // As the KNX driver is using the SingleFieldOptimizer, each request here will have
    // only one item.
    final Optional<String> first = request.getFieldNames().stream().findFirst();
    if (first.isPresent()) {
        String fieldName = first.get();
        final KnxNetIpField field = (KnxNetIpField) request.getField(fieldName);
        byte[] destinationAddress = toKnxAddressData(field);
        if (sequenceCounter.get() == Short.MAX_VALUE) {
            sequenceCounter.set(0);
        }
        // Convert the PlcValue to byte data.
        final PlcValue value = request.getPlcValue(fieldName);
        byte dataFirstByte = 0;
        byte[] data = null;
        final EtsModel etsModel = knxNetIpDriverContext.getEtsModel();
        if (etsModel != null) {
            final String destinationAddressString = etsModel.parseGroupAddress(destinationAddress);
            final GroupAddress groupAddress = etsModel.getGroupAddresses().get(destinationAddressString);
            if ((groupAddress == null) || (groupAddress.getType() == null)) {
                future.completeExceptionally(new PlcRuntimeException("ETS5 model didn't specify group address '" + destinationAddressString + "' or didn't define a type for it."));
                return future;
            }
            // Use the data in the ets model to correctly check and serialize the PlcValue
            try {
                final WriteBufferByteBased writeBuffer = new WriteBufferByteBased(KnxDatapoint.getLengthInBytes(value, groupAddress.getType()));
                KnxDatapoint.staticSerialize(writeBuffer, value, groupAddress.getType());
                final byte[] serialized = writeBuffer.getData();
                dataFirstByte = serialized[0];
                data = new byte[serialized.length - 1];
                System.arraycopy(serialized, 1, data, 0, serialized.length - 1);
            } catch (SerializationException e) {
                future.completeExceptionally(new PlcRuntimeException("Error serializing PlcValue.", e));
                return future;
            }
        } else {
            if (value.isByte()) {
                if ((value.getByte() > 63) || (value.getByte() < 0)) {
                    future.completeExceptionally(new PlcRuntimeException("If no ETS5 model is provided, value of the first byte must be between 0 and 63."));
                    return future;
                }
                dataFirstByte = value.getByte();
            } else if (value.isList()) {
                // Check each item of the list, if it's also a byte.
                List<? extends PlcValue> list = value.getList();
                // TODO: This could cause an exception.
                data = new byte[list.size() - 1];
                boolean allValuesAreBytes = !list.isEmpty();
                int numByte = 0;
                for (PlcValue plcValue : list) {
                    if (numByte == 0) {
                        if (!plcValue.isByte() && (plcValue.getByte() > 63) || (plcValue.getByte() < 0)) {
                            allValuesAreBytes = false;
                            break;
                        }
                        dataFirstByte = plcValue.getByte();
                    } else {
                        if (!plcValue.isByte()) {
                            allValuesAreBytes = false;
                            break;
                        }
                        data[numByte - 1] = plcValue.getByte();
                    }
                    numByte++;
                }
                if (!allValuesAreBytes) {
                    future.completeExceptionally(new PlcRuntimeException("If no ETS5 model is provided, the only supported type for writing data is writing of single byte or list of bytes and the value of the first byte must be between 0 and 63."));
                    return future;
                }
            } else {
                future.completeExceptionally(new PlcRuntimeException("If no ETS5 model is provided, the only supported type for writing data is writing of single byte or list of bytes."));
                return future;
            }
        }
        final short communicationChannelId = knxNetIpDriverContext.getCommunicationChannelId();
        // Prepare the knx request message.
        TunnelingRequest knxRequest = new TunnelingRequest(new TunnelingRequestDataBlock(communicationChannelId, (short) sequenceCounter.getAndIncrement()), new LDataReq((short) 0, new ArrayList<>(0), new LDataExtended(false, false, CEMIPriority.LOW, false, false, true, (byte) 6, (byte) 0, knxNetIpDriverContext.getClientKnxAddress(), destinationAddress, new ApduDataContainer(true, (byte) 0, new ApduDataGroupValueWrite(dataFirstByte, data, (short) -1), (short) -1)), -1), -1);
        // Start a new request-transaction (Is ended in the response-handler)
        RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
        // Start a new request-transaction (Is ended in the response-handler)
        transaction.submit(() -> context.sendRequest(knxRequest).expectResponse(KnxNetIpMessage.class, REQUEST_TIMEOUT).onTimeout(future::completeExceptionally).onError((tr, e) -> future.completeExceptionally(e)).check(tr -> tr instanceof TunnelingResponse).unwrap(tr -> ((TunnelingResponse) tr)).check(tr -> tr.getTunnelingResponseDataBlock().getCommunicationChannelId() == knxRequest.getTunnelingRequestDataBlock().getCommunicationChannelId()).check(tr -> tr.getTunnelingResponseDataBlock().getSequenceCounter() == knxRequest.getTunnelingRequestDataBlock().getSequenceCounter()).handle(tr -> {
            PlcResponseCode responseCode;
            // In this case all went well.
            if (tr.getTunnelingResponseDataBlock().getStatus() == Status.NO_ERROR) {
                responseCode = PlcResponseCode.OK;
            } else // TODO: Should probably differentiate a bit on this and not treat everything as internal error.
            {
                responseCode = PlcResponseCode.INTERNAL_ERROR;
            }
            // Prepare the response.
            PlcWriteResponse response = new DefaultPlcWriteResponse(request, Collections.singletonMap(fieldName, responseCode));
            future.complete(response);
            // Finish the request-transaction.
            transaction.endRequest();
        }));
    }
    return future;
}
Also used : org.apache.plc4x.java.api.value(org.apache.plc4x.java.api.value) EtsModel(org.apache.plc4x.java.knxnetip.ets.model.EtsModel) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) java.util(java.util) DriverContext(org.apache.plc4x.java.spi.context.DriverContext) LoggerFactory(org.slf4j.LoggerFactory) KnxGroupAddress3Level(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress3Level) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) CompletableFuture(java.util.concurrent.CompletableFuture) Hex(org.apache.commons.codec.binary.Hex) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) KnxNetIpDriverContext(org.apache.plc4x.java.knxnetip.context.KnxNetIpDriverContext) GroupAddress(org.apache.plc4x.java.knxnetip.ets.model.GroupAddress) KnxNetIpSubscriptionHandle(org.apache.plc4x.java.knxnetip.model.KnxNetIpSubscriptionHandle) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) DatagramChannel(io.netty.channel.socket.DatagramChannel) PlcSTRING(org.apache.plc4x.java.spi.values.PlcSTRING) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) KnxDatapoint(org.apache.plc4x.java.knxnetip.readwrite.KnxDatapoint) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) Logger(org.slf4j.Logger) KnxGroupAddress2Level(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress2Level) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KnxGroupAddressFreeLevel(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddressFreeLevel) org.apache.plc4x.java.spi.messages(org.apache.plc4x.java.spi.messages) Instant(java.time.Instant) KnxNetIpField(org.apache.plc4x.java.knxnetip.field.KnxNetIpField) InetSocketAddress(java.net.InetSocketAddress) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) Consumer(java.util.function.Consumer) KnxGroupAddress(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) org.apache.plc4x.java.knxnetip.readwrite(org.apache.plc4x.java.knxnetip.readwrite) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) KnxNetIpField(org.apache.plc4x.java.knxnetip.field.KnxNetIpField) CompletableFuture(java.util.concurrent.CompletableFuture) GroupAddress(org.apache.plc4x.java.knxnetip.ets.model.GroupAddress) KnxGroupAddress(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) EtsModel(org.apache.plc4x.java.knxnetip.ets.model.EtsModel)

Example 18 with PlcResponseCode

use of org.apache.plc4x.java.api.types.PlcResponseCode in project plc4x by apache.

the class ModbusTcpProtocolLogic method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
    DefaultPlcWriteRequest request = (DefaultPlcWriteRequest) writeRequest;
    // 2. Split up into multiple sub-requests
    if (request.getFieldNames().size() == 1) {
        String fieldName = request.getFieldNames().iterator().next();
        PlcField field = request.getField(fieldName);
        final ModbusPDU requestPdu = getWriteRequestPdu(field, writeRequest.getPlcValue(fieldName));
        int transactionIdentifier = transactionIdentifierGenerator.getAndIncrement();
        // If we've reached the max value for a 16 bit transaction identifier, reset back to 1
        if (transactionIdentifierGenerator.get() == 0xFFFF) {
            transactionIdentifierGenerator.set(1);
        }
        ModbusTcpADU modbusTcpADU = new ModbusTcpADU(transactionIdentifier, unitIdentifier, requestPdu, false);
        RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
        transaction.submit(() -> context.sendRequest(modbusTcpADU).expectResponse(ModbusTcpADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).check(p -> p.getTransactionIdentifier() == transactionIdentifier).unwrap(ModbusTcpADU::getPdu).handle(responsePdu -> {
            // Try to decode the response data based on the corresponding request.
            PlcResponseCode responseCode;
            // Check if the response was an error response.
            if (responsePdu instanceof ModbusPDUError) {
                ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
                responseCode = getErrorCode(errorResponse);
            } else {
                responseCode = PlcResponseCode.OK;
                // TODO: Check the correct number of elements were written.
                if (responsePdu instanceof ModbusPDUWriteSingleCoilResponse) {
                    ModbusPDUWriteSingleCoilResponse response = (ModbusPDUWriteSingleCoilResponse) responsePdu;
                    ModbusPDUWriteSingleCoilRequest requestSingleCoil = (ModbusPDUWriteSingleCoilRequest) requestPdu;
                    if (!((response.getValue() == requestSingleCoil.getValue()) && (response.getAddress() == requestSingleCoil.getAddress()))) {
                        responseCode = PlcResponseCode.REMOTE_ERROR;
                    }
                }
            }
            // Prepare the response.
            PlcWriteResponse response = new DefaultPlcWriteResponse(request, Collections.singletonMap(fieldName, responseCode));
            // Pass the response back to the application.
            future.complete(response);
            // Finish the request-transaction.
            transaction.endRequest();
        }));
    } else {
        future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
    }
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) ParseException(org.apache.plc4x.java.spi.generation.ParseException) ModbusTcpConfiguration(org.apache.plc4x.java.modbus.tcp.config.ModbusTcpConfiguration) ModbusProtocolLogic(org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic) org.apache.plc4x.java.modbus.readwrite(org.apache.plc4x.java.modbus.readwrite) PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcField(org.apache.plc4x.java.api.model.PlcField) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ModbusRtuConfiguration(org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) Collections(java.util.Collections) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcField(org.apache.plc4x.java.api.model.PlcField) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)

Example 19 with PlcResponseCode

use of org.apache.plc4x.java.api.types.PlcResponseCode in project plc4x by apache.

the class Plc4xProtocolLogic method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest apiReadRequest) {
    CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
    // Prepare the request.
    List<Plc4xFieldRequest> plc4xFields = new ArrayList<>(apiReadRequest.getNumberOfFields());
    for (String fieldName : apiReadRequest.getFieldNames()) {
        final org.apache.plc4x.java.plc4x.field.Plc4xField plc4xField = (org.apache.plc4x.java.plc4x.field.Plc4xField) apiReadRequest.getField(fieldName);
        Plc4xFieldRequest plc4xFieldRequest = new Plc4xFieldRequest(new Plc4xField(fieldName, plc4xField.getAddress() + ":" + plc4xField.getPlcDataType()));
        plc4xFields.add(plc4xFieldRequest);
    }
    final int requestId = txIdGenerator.getAndIncrement();
    Plc4xReadRequest plc4xReadRequest = new Plc4xReadRequest(requestId, connectionId, plc4xFields);
    // Send the request and await a response.
    RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    context.sendRequest(plc4xReadRequest).expectResponse(Plc4xMessage.class, requestTimeout).onTimeout(future::completeExceptionally).check(plc4xMessage -> plc4xMessage.getRequestId() == requestId).unwrap(plc4xMessage -> (Plc4xReadResponse) plc4xMessage).check(plc4xReadResponse -> plc4xReadResponse.getConnectionId() == connectionId).handle(plc4xReadResponse -> {
        Map<String, ResponseItem<PlcValue>> apiResponses = new HashMap<>();
        // Create the API response from the incoming message.
        for (Plc4xFieldValueResponse plc4xField : plc4xReadResponse.getFields()) {
            final Plc4xResponseCode plc4xResponseCode = plc4xField.getResponseCode();
            final PlcResponseCode apiResponseCode = PlcResponseCode.valueOf(plc4xResponseCode.name());
            apiResponses.put(plc4xField.getField().getName(), new ResponseItem<>(apiResponseCode, plc4xField.getValue()));
        }
        // Send it back to the calling process.
        future.complete(new DefaultPlcReadResponse(apiReadRequest, apiResponses));
        // Finish the request-transaction.
        transaction.endRequest();
    });
    return future;
}
Also used : Logger(org.slf4j.Logger) Plc4xConfiguration(org.apache.plc4x.java.plc4x.config.Plc4xConfiguration) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ArrayList(java.util.ArrayList) org.apache.plc4x.java.plc4x.readwrite(org.apache.plc4x.java.plc4x.readwrite) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) Duration(java.time.Duration) Map(java.util.Map) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)

Example 20 with PlcResponseCode

use of org.apache.plc4x.java.api.types.PlcResponseCode in project plc4x by apache.

the class Plc4xProtocolLogic method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
    // Prepare the request.
    List<Plc4xFieldValueRequest> fields = new ArrayList<>(writeRequest.getNumberOfFields());
    for (String fieldName : writeRequest.getFieldNames()) {
        final org.apache.plc4x.java.plc4x.field.Plc4xField plc4xField = (org.apache.plc4x.java.plc4x.field.Plc4xField) writeRequest.getField(fieldName);
        final Plc4xValueType plc4xValueType = plc4xField.getValueType();
        final PlcValue plcValue = writeRequest.getPlcValue(fieldName);
        Plc4xFieldValueRequest fieldRequest = new Plc4xFieldValueRequest(new Plc4xField(fieldName, plc4xField.getAddress() + ":" + plc4xField.getPlcDataType()), plc4xValueType, plcValue);
        fields.add(fieldRequest);
    }
    final int requestId = txIdGenerator.getAndIncrement();
    Plc4xWriteRequest write = new Plc4xWriteRequest(requestId, connectionId, fields);
    // Send the request and await a response.
    RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    context.sendRequest(write).expectResponse(Plc4xMessage.class, requestTimeout).onTimeout(future::completeExceptionally).check(p -> p.getRequestId() == requestId).unwrap(plc4xMessage -> (Plc4xWriteResponse) plc4xMessage).check(plc4xReadResponse -> plc4xReadResponse.getConnectionId() == connectionId).handle(plc4xWriteResponse -> {
        Map<String, PlcResponseCode> apiResponses = new HashMap<>();
        // Create the API response from the incoming message.
        for (Plc4xFieldResponse plc4xField : plc4xWriteResponse.getFields()) {
            final Plc4xResponseCode plc4xResponseCode = plc4xField.getResponseCode();
            final PlcResponseCode apiResponseCode = PlcResponseCode.valueOf(plc4xResponseCode.name());
            apiResponses.put(plc4xField.getField().getName(), apiResponseCode);
        }
        // Send it back to the calling process.
        future.complete(new DefaultPlcWriteResponse(writeRequest, apiResponses));
        // Finish the request-transaction.
        transaction.endRequest();
    });
    return future;
}
Also used : Logger(org.slf4j.Logger) Plc4xConfiguration(org.apache.plc4x.java.plc4x.config.Plc4xConfiguration) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ArrayList(java.util.ArrayList) org.apache.plc4x.java.plc4x.readwrite(org.apache.plc4x.java.plc4x.readwrite) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) Duration(java.time.Duration) Map(java.util.Map) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcValue(org.apache.plc4x.java.api.value.PlcValue)

Aggregations

PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)31 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)20 PlcValue (org.apache.plc4x.java.api.value.PlcValue)19 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)16 CompletableFuture (java.util.concurrent.CompletableFuture)15 DefaultPlcWriteResponse (org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)15 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)14 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)13 Duration (java.time.Duration)11 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)11 PlcField (org.apache.plc4x.java.api.model.PlcField)11 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)10 HasConfiguration (org.apache.plc4x.java.spi.configuration.HasConfiguration)10 DefaultPlcReadRequest (org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest)9 DefaultPlcWriteRequest (org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest)9 Collections (java.util.Collections)8 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)8 org.apache.plc4x.java.modbus.readwrite (org.apache.plc4x.java.modbus.readwrite)8 ParseException (org.apache.plc4x.java.spi.generation.ParseException)8 List (java.util.List)7