Example #1
0
    def from_bytes(cls, source_bytes: bytes):
        data = bytearray(source_bytes)
        tag = data.pop(0)
        if tag != cls.TAG:
            raise ValueError(
                f"Tag for GET request is not correct. Got {tag}, should be {cls.TAG}"
            )

        type_choice = enums.GetRequestType(data.pop(0))
        if type_choice is not enums.GetRequestType.NORMAL:
            raise ValueError(
                "The data for the GetRequest is not for a GetRequestNormal")

        invoke_id_and_priority = InvokeIdAndPriority.from_bytes(
            data.pop(0).to_bytes(1, "big"))

        cosem_attribute_data = data[:9]
        cosem_attribute = cosem.CosemAttribute.from_bytes(cosem_attribute_data)
        data = data[9:]
        has_access_selection = bool(data.pop(0))
        if has_access_selection:
            access_selection = selective_access.AccessDescriptorFactory.from_bytes(
                data)
        else:
            access_selection = None

        return cls(
            cosem_attribute=cosem_attribute,
            invoke_id_and_priority=invoke_id_and_priority,
            access_selection=access_selection,
        )
Example #2
0
    def from_bytes(cls, source_bytes: bytes):
        data = bytearray(source_bytes)
        tag = data.pop(0)
        if tag != cls.TAG:
            raise ValueError(
                f"Tag for GET request is not correct. Got {tag}, should be {cls.TAG}"
            )

        type_choice = enums.GetRequestType(data.pop(0))
        if type_choice is not enums.GetRequestType.WITH_LIST:
            raise ValueError(
                "The data for the GetRequest is not for a GetRequestWithList")
        invoke_id_and_priority = InvokeIdAndPriority.from_bytes(
            data.pop(0).to_bytes(1, "big"))

        number_of_items = data.pop(0)
        cosem_atts = list()
        for i in range(0, number_of_items):
            # Not really happy with the format of this but it works fine.
            c = cosem.CosemAttributeWithSelection.from_bytes(data)
            cosem_atts.append(c)
            data = data[len(c.to_bytes()):]

        return cls(
            cosem_attributes_with_selection=cosem_atts,
            invoke_id_and_priority=invoke_id_and_priority,
        )
Example #3
0
    def from_bytes(cls, source_bytes: bytes):
        data = bytearray(source_bytes)
        tag = data.pop(0)
        if tag != cls.TAG:
            raise ValueError(
                f"Tag for GET request is not correct. Got {tag}, should be {cls.TAG}"
            )

        type_choice = enums.GetRequestType(data.pop(0))
        if type_choice is not enums.GetRequestType.NORMAL:
            raise ValueError(
                "The data for the GetRequest is not for a GetRequestNormal")
        decoder = AXdrDecoder(encoding_conf=cls.ENCODING_CONF)
        out_dict = decoder.decode(data)
        return cls(**out_dict)
Example #4
0
    def from_bytes(cls, source_bytes: bytes):
        data = bytearray(source_bytes)
        tag = data.pop(0)
        if tag != cls.TAG:
            raise ValueError(
                f"Tag for GET request is not correct. Got {tag}, should be {cls.TAG}"
            )

        type_choice = enums.GetRequestType(data.pop(0))
        if type_choice is not enums.GetRequestType.NEXT:
            raise ValueError(
                "The data for the GetRequest is not for a GetRequestNext")
        invoke_id_and_priority = InvokeIdAndPriority.from_bytes(
            data.pop(0).to_bytes(1, "big"))
        assert len(data) == 4  # should only be block number left.
        block_number = int.from_bytes(data, "big")
        return cls(block_number, invoke_id_and_priority)
Example #5
0
 def from_bytes(source_bytes: bytes):
     data = bytearray(source_bytes)
     tag = data.pop(0)
     if tag != GetRequestFactory.TAG:
         raise ValueError(
             f"Tag for GET request is not correct. Got {tag}, should be "
             f"{GetRequestFactory.TAG}")
     request_type = enums.GetRequestType(data.pop(0))
     if request_type == enums.GetRequestType.NORMAL:
         return GetRequestNormal.from_bytes(source_bytes)
     elif request_type == enums.GetRequestType.NEXT:
         return GetRequestNext.from_bytes(source_bytes)
     elif request_type == enums.GetRequestType.WITH_LIST:
         return GetRequestWithList.from_bytes(source_bytes)
     else:
         raise ValueError(
             f"Received an enum request type that is not valid for "
             f"GetRequest: {request_type}")