Search in sources :

Example 1 with MemoryArea

use of org.apache.plc4x.java.s7.readwrite.MemoryArea in project plc4x by apache.

the class S7Field method of.

public static S7Field of(String fieldString) {
    Matcher matcher;
    if ((matcher = DATA_BLOCK_STRING_ADDRESS_PATTERN.matcher(fieldString)).matches()) {
        TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE));
        int stringLength = Integer.parseInt(matcher.group(STRING_LENGTH));
        MemoryArea memoryArea = MemoryArea.DATA_BLOCKS;
        int blockNumber = checkDatablockNumber(Integer.parseInt(matcher.group(BLOCK_NUMBER)));
        Short transferSizeCode = getSizeCode(matcher.group(TRANSFER_SIZE_CODE));
        int byteOffset = checkByteOffset(Integer.parseInt(matcher.group(BYTE_OFFSET)));
        byte bitOffset = 0;
        if (matcher.group(BIT_OFFSET) != null) {
            bitOffset = Byte.parseByte(matcher.group(BIT_OFFSET));
        } else if (dataType == TransportSize.BOOL) {
            throw new PlcInvalidFieldException("Expected bit offset for BOOL parameters.");
        }
        int numElements = 1;
        if (matcher.group(NUM_ELEMENTS) != null) {
            numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
        }
        if ((transferSizeCode != null) && (dataType.getShortName() != transferSizeCode)) {
            throw new PlcInvalidFieldException("Transfer size code '" + transferSizeCode + "' doesn't match specified data type '" + dataType.name() + "'");
        }
        return new S7StringField(dataType, memoryArea, blockNumber, byteOffset, bitOffset, numElements, stringLength);
    } else if ((matcher = DATA_BLOCK_STRING_SHORT_PATTERN.matcher(fieldString)).matches()) {
        TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE));
        int stringLength = Integer.parseInt(matcher.group(STRING_LENGTH));
        MemoryArea memoryArea = MemoryArea.DATA_BLOCKS;
        int blockNumber = checkDatablockNumber(Integer.parseInt(matcher.group(BLOCK_NUMBER)));
        int byteOffset = checkByteOffset(Integer.parseInt(matcher.group(BYTE_OFFSET)));
        byte bitOffset = 0;
        int numElements = 1;
        if (matcher.group(NUM_ELEMENTS) != null) {
            numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
        }
        return new S7StringField(dataType, memoryArea, blockNumber, byteOffset, bitOffset, numElements, stringLength);
    } else if ((matcher = DATA_BLOCK_ADDRESS_PATTERN.matcher(fieldString)).matches()) {
        TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE));
        MemoryArea memoryArea = MemoryArea.DATA_BLOCKS;
        Short transferSizeCode = getSizeCode(matcher.group(TRANSFER_SIZE_CODE));
        int blockNumber = checkDatablockNumber(Integer.parseInt(matcher.group(BLOCK_NUMBER)));
        int byteOffset = checkByteOffset(Integer.parseInt(matcher.group(BYTE_OFFSET)));
        byte bitOffset = 0;
        if (matcher.group(BIT_OFFSET) != null) {
            bitOffset = Byte.parseByte(matcher.group(BIT_OFFSET));
        } else if (dataType == TransportSize.BOOL) {
            throw new PlcInvalidFieldException("Expected bit offset for BOOL parameters.");
        }
        int numElements = 1;
        if (matcher.group(NUM_ELEMENTS) != null) {
            numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
        }
        if ((transferSizeCode != null) && (dataType.getShortName() != transferSizeCode)) {
            throw new PlcInvalidFieldException("Transfer size code '" + transferSizeCode + "' doesn't match specified data type '" + dataType.name() + "'");
        }
        return new S7Field(dataType, memoryArea, blockNumber, byteOffset, bitOffset, numElements);
    } else if ((matcher = DATA_BLOCK_SHORT_PATTERN.matcher(fieldString)).matches()) {
        TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE));
        MemoryArea memoryArea = MemoryArea.DATA_BLOCKS;
        int blockNumber = checkDatablockNumber(Integer.parseInt(matcher.group(BLOCK_NUMBER)));
        int byteOffset = checkByteOffset(Integer.parseInt(matcher.group(BYTE_OFFSET)));
        byte bitOffset = 0;
        if (matcher.group(BIT_OFFSET) != null) {
            bitOffset = Byte.parseByte(matcher.group(BIT_OFFSET));
        } else if (dataType == TransportSize.BOOL) {
            throw new PlcInvalidFieldException("Expected bit offset for BOOL parameters.");
        }
        int numElements = 1;
        if (matcher.group(NUM_ELEMENTS) != null) {
            numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
        }
        return new S7Field(dataType, memoryArea, blockNumber, byteOffset, bitOffset, numElements);
    } else if (PLC_PROXY_ADDRESS_PATTERN.matcher(fieldString).matches()) {
        try {
            byte[] addressData = Hex.decodeHex(fieldString.replaceAll("[-]", ""));
            ReadBuffer rb = new ReadBufferByteBased(addressData);
            final S7Address s7Address = S7Address.staticParse(rb);
            if (s7Address instanceof S7AddressAny) {
                S7AddressAny s7AddressAny = (S7AddressAny) s7Address;
                if ((s7AddressAny.getTransportSize() != TransportSize.BOOL) && s7AddressAny.getBitAddress() != 0) {
                    throw new PlcInvalidFieldException("A bit offset other than 0 is only supported for type BOOL");
                }
                return new S7Field(s7AddressAny.getTransportSize(), s7AddressAny.getArea(), s7AddressAny.getDbNumber(), s7AddressAny.getByteAddress(), s7AddressAny.getBitAddress(), s7AddressAny.getNumberOfElements());
            } else {
                throw new PlcInvalidFieldException("Unsupported address type.");
            }
        } catch (ParseException | DecoderException e) {
            throw new PlcInvalidFieldException("Unable to parse address: " + fieldString);
        }
    } else if ((matcher = ADDRESS_PATTERN.matcher(fieldString)).matches()) {
        TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE));
        MemoryArea memoryArea = getMemoryAreaForShortName(matcher.group(MEMORY_AREA));
        Short transferSizeCode = getSizeCode(matcher.group(TRANSFER_SIZE_CODE));
        int byteOffset = checkByteOffset(Integer.parseInt(matcher.group(BYTE_OFFSET)));
        byte bitOffset = 0;
        if (matcher.group(BIT_OFFSET) != null) {
            bitOffset = Byte.parseByte(matcher.group(BIT_OFFSET));
        } else if (dataType == TransportSize.BOOL) {
            throw new PlcInvalidFieldException("Expected bit offset for BOOL parameters.");
        }
        int numElements = 1;
        if (matcher.group(NUM_ELEMENTS) != null) {
            numElements = Integer.parseInt(matcher.group(NUM_ELEMENTS));
        }
        if ((transferSizeCode != null) && (dataType.getShortName() != transferSizeCode)) {
            throw new PlcInvalidFieldException("Transfer size code '" + transferSizeCode + "' doesn't match specified data type '" + dataType.name() + "'");
        }
        if ((dataType != TransportSize.BOOL) && bitOffset != 0) {
            throw new PlcInvalidFieldException("A bit offset other than 0 is only supported for type BOOL");
        }
        return new S7Field(dataType, memoryArea, (short) 0, byteOffset, bitOffset, numElements);
    }
    throw new PlcInvalidFieldException("Unable to parse address: " + fieldString);
}
Also used : Matcher(java.util.regex.Matcher) S7AddressAny(org.apache.plc4x.java.s7.readwrite.S7AddressAny) S7Address(org.apache.plc4x.java.s7.readwrite.S7Address) DecoderException(org.apache.commons.codec.DecoderException) MemoryArea(org.apache.plc4x.java.s7.readwrite.MemoryArea) PlcInvalidFieldException(org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException) TransportSize(org.apache.plc4x.java.s7.readwrite.TransportSize)

Aggregations

Matcher (java.util.regex.Matcher)1 DecoderException (org.apache.commons.codec.DecoderException)1 PlcInvalidFieldException (org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException)1 MemoryArea (org.apache.plc4x.java.s7.readwrite.MemoryArea)1 S7Address (org.apache.plc4x.java.s7.readwrite.S7Address)1 S7AddressAny (org.apache.plc4x.java.s7.readwrite.S7AddressAny)1 TransportSize (org.apache.plc4x.java.s7.readwrite.TransportSize)1