def parseExternalStatePacket(reader: BufferReader):
    packet = AdvExternalCrownstoneState()
    _parseStatePacket(packet, reader)
    # position 13 on the state packet is the global flags, it is used in the external state for rssi
    packet.rssiOfExternalCrownstone = reader.getInt8()
    packet.validation = reader.getUInt8()
    return packet
コード例 #2
0
    def _deserialize(self, reader: BufferReader):
        self.assetId = reader.getUInt8() + (reader.getUInt8() << 8) + (
            reader.getUInt8() << 16)
        self.crownstoneId = reader.getUInt8()

        self.passedFilterIds.clear()
        passedFilterBitmask = reader.getUInt8()
        for i in range(0, 8):
            if is_bit_set(passedFilterBitmask, i):
                self.passedFilterIds.append(i)

        self.rssi = reader.getInt8()
        self.channel = reader.getUInt8()
コード例 #3
0
def _parseErrorPacket(packet, reader: BufferReader):
    packet.crownstoneId     = reader.getUInt8()
    packet.errorsBitmask    = AdvCrownstoneErrorBitmask(reader.getUInt32())
    packet.errorTimestamp   = reader.getUInt32()
    packet.flags            = AdvFlags(reader.getUInt8())
    packet.temperature      = reader.getInt8()
    partialTimestamp        = reader.getUInt16()
    packet.uniqueIdentifier = partialTimestamp

    if packet.flags.timeIsSet:
        packet.timestamp = reconstructTimestamp(time.time(), partialTimestamp)
    else:
        packet.timestamp = partialTimestamp  # this is now a counter
コード例 #4
0
def parseSetupState(reader: BufferReader):
    packet = AdvCrownstoneSetupState()

    packet.switchState = SwitchState(reader.getUInt8())
    packet.flags = AdvFlags(reader.getUInt8())
    packet.temperature = reader.getInt8()
    powerFactor = reader.getInt8()
    realPower = reader.getInt16()

    packet.powerFactor = float(powerFactor) / 127.0

    # we cannot have a 0 for a power factor.To avoid division by 0, we set it to be either 0.01 or -0.01
    if 0 <= packet.powerFactor < 0.01:
        packet.powerFactor = 0.01
    elif -0.01 < packet.powerFactor < 0:
        packet.powerFactor = -0.01

    packet.powerUsageReal = float(realPower) / 8.0
    packet.powerUsageApparent = packet.powerUsageReal / packet.powerFactor
    packet.errorBitmask = reader.getInt32()
    packet.uniqueIdentifier = reader.getUInt8()

    return packet
コード例 #5
0
 def _deserialize(self, reader: BufferReader):
     self.assetMacAddress = Conversion.uint8_array_to_address(
         bytearray(reader.getBytes(6)))
     self.crownstoneId = reader.getUInt8()
     self.rssi = reader.getInt8()
     self.channel = reader.getUInt8()
コード例 #6
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