def fromData(self, data):
        if len(data) != 13:
            self.valid = False
            return self

        payload = BufferReader(data)
        firstByte = payload.getUInt8()
        if not BehaviourPresenceType.has_value(firstByte):
            self.valid = False
            return self

        self.behaviourType = BehaviourPresenceType(firstByte)
        self.locationIds = self.unpackMask(payload.getUInt64())
        self.delayInSeconds = payload.getUInt32()

        return self
Example #2
0
    def _getElementValues(
            self,
            elementType: int,  # TODO: make enum
            elementSize: int,
            elementData: list) -> list:
        bufferReader = BufferReader(elementData)
        dataSize = len(elementData)
        if dataSize % elementSize != 0:
            _LOGGER.warning(
                f"Remaining data with element size of {elementSize} and element data of size {dataSize}"
            )
            return []

        vals = []
        numElements = int(dataSize / elementSize)
        _LOGGER.debug(
            f"dataSize={dataSize} elementSize={elementSize} numElements={numElements}"
        )
        for i in range(0, numElements):
            if elementType == 0:
                # Signed integer
                if elementSize == 1:
                    vals.append(bufferReader.getInt8())
                elif elementSize == 2:
                    vals.append(bufferReader.getInt16())
                elif elementSize == 4:
                    vals.append(bufferReader.getInt32())
                elif elementSize == 8:
                    vals.append(bufferReader.getInt64())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []

            elif elementType == 1:
                # Unsigned integer
                if elementSize == 1:
                    vals.append(bufferReader.getUInt8())
                elif elementSize == 2:
                    vals.append(bufferReader.getUInt16())
                elif elementSize == 4:
                    vals.append(bufferReader.getUInt32())
                elif elementSize == 8:
                    vals.append(bufferReader.getUInt64())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []

            elif elementType == 2:
                # Floating point
                if elementSize == 4:
                    vals.append(bufferReader.getFloat())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []
        return vals