Skip to content

partector_ble_decoder_size

PartectorBleDecoderSize

Bases: PartectorBleDecoderBlueprint

Decode the std advertisement data from the Partector device.

Source code in src/naneos/partector_ble/decoder/partector_ble_decoder_size.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class PartectorBleDecoderSize(PartectorBleDecoderBlueprint):
    """
    Decode the std advertisement data from the Partector device.
    """

    # == External used methods =====================================================================
    @classmethod
    def decode(
        cls, data: bytes, data_structure: Optional[NaneosDeviceDataPoint] = None
    ) -> NaneosDeviceDataPoint:
        """
        Decode the auxiliary characteristic data from the Partector device.
        """
        decoded_data = NaneosDeviceDataPoint(
            particle_number_10nm=cls._get_10nm(data),
            particle_number_16nm=cls._get_10nm(data),
            particle_number_26nm=cls._get_26nm(data),
            particle_number_43nm=cls._get_43nm(data),
            particle_number_70nm=cls._get_70nm(data),
            particle_number_114nm=cls._get_114nm(data),
            particle_number_185nm=cls._get_185nm(data),
            particle_number_300nm=cls._get_300nm(data),
        )

        if not data_structure:
            return decoded_data

        # Fill the given data_structure with the decoded data
        for field in NaneosDeviceDataPoint.BLE_SIZE_DIST_FIELD_NAMES:
            setattr(data_structure, field, getattr(decoded_data, field))
        return data_structure

    # == Helpers ===================================================================================
    @staticmethod
    def _get_10nm(data: bytes) -> float:
        """
        Get the 10nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[0], data[1], data[2] & 0x0F]), byteorder="little")
        )
        return val

    @staticmethod
    def _get_16nm(data: bytes) -> float:
        """
        Get the 16nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[2] & 0xF0, data[3], data[4]]), byteorder="little") >> 4
        )
        return val

    @staticmethod
    def _get_26nm(data: bytes) -> float:
        """
        Get the 26nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[5], data[6], data[7] & 0x0F]), byteorder="little")
        )
        return val

    @staticmethod
    def _get_43nm(data: bytes) -> float:
        """
        Get the 43nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[7] & 0xF0, data[8], data[9]]), byteorder="little") >> 4
        )
        return val

    @staticmethod
    def _get_70nm(data: bytes) -> float:
        """
        Get the 70nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[10], data[11], data[12] & 0x0F]), byteorder="little")
        )
        return val

    @staticmethod
    def _get_114nm(data: bytes) -> float:
        """
        Get the 114nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[12] & 0xF0, data[13], data[14]]), byteorder="little")
            >> 4
        )
        return val

    @staticmethod
    def _get_185nm(data: bytes) -> float:
        """
        Get the 185nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[15], data[16], data[17] & 0x0F]), byteorder="little")
        )
        return val

    @staticmethod
    def _get_300nm(data: bytes) -> float:
        """
        Get the 300nm value from the size characteristic data.
        """
        val = float(
            int.from_bytes(bytearray([data[17] & 0xF0, data[18], data[19]]), byteorder="little")
            >> 4
        )
        return val

decode(data, data_structure=None) classmethod

Decode the auxiliary characteristic data from the Partector device.

Source code in src/naneos/partector_ble/decoder/partector_ble_decoder_size.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@classmethod
def decode(
    cls, data: bytes, data_structure: Optional[NaneosDeviceDataPoint] = None
) -> NaneosDeviceDataPoint:
    """
    Decode the auxiliary characteristic data from the Partector device.
    """
    decoded_data = NaneosDeviceDataPoint(
        particle_number_10nm=cls._get_10nm(data),
        particle_number_16nm=cls._get_10nm(data),
        particle_number_26nm=cls._get_26nm(data),
        particle_number_43nm=cls._get_43nm(data),
        particle_number_70nm=cls._get_70nm(data),
        particle_number_114nm=cls._get_114nm(data),
        particle_number_185nm=cls._get_185nm(data),
        particle_number_300nm=cls._get_300nm(data),
    )

    if not data_structure:
        return decoded_data

    # Fill the given data_structure with the decoded data
    for field in NaneosDeviceDataPoint.BLE_SIZE_DIST_FIELD_NAMES:
        setattr(data_structure, field, getattr(decoded_data, field))
    return data_structure