async def getPowerUsage(self) -> float: """ @return: Power usage in Watt. """ stateVal = await self._getState(StateType.POWER_USAGE) powerUsage = Conversion.uint8_array_to_int32(stateVal) / 1000.0 return powerUsage
def filterByNameWithWildcards(self, name: str, complete: bool = True): """ Assets are filtered by their name (case sensitive). :param name: Name, with wildcards, to be added filter. '?' matches any single character '*' matches zero or more characters, only at the end of a name. Example: "??_dev*" will match: "my_dev", and "01_device" won't match: "your_device", or "_dev" :param complete: Whether to look for the complete or shortened name. """ self._resetCache() if len(name) > 31: raise CrownstoneException(CrownstoneError.INVALID_SIZE, f"Name is too long: {name}") bitmask = 0 asset_name = "" for i in range(0, len(name)): if name[i] != '?': bitmask = set_bit(bitmask, i) asset_name += name[i] if name[-1] == '*': bitmask = set_bit(bitmask, len(name) - 1, False) asset_name = asset_name[:-1] else: # Set all remaining bits. If there is more data, it will be used as input. for i in range(len(name), 32): bitmask = set_bit(bitmask, i) _LOGGER.info( f"name={name} bitmask={bitmask:032b} asset_name={asset_name}") adType = 0x09 if complete else 0x08 self._input = InputDescriptionMaskedAdData(adType, bitmask) self._assets = [Conversion.string_to_uint8_array(asset_name)] return self
async def getTime(self) -> int: """ @return: posix timestamp (uint32) """ # TODO: check result code stateVal = await self._getState(StateType.TIME) return Conversion.uint8_array_to_uint32(stateVal)
def filterByMacAddress(self, macAddresses: List[str]): """ Assets are filtered by their MAC address. :param macAddresses: List of mac addresses to be added to the filter, in the form of "12:34:56:78:AB:CD". """ self._resetCache() self._input = InputDescriptionMacAddress() self._assets = [] for mac in macAddresses: self._assets.append(Conversion.address_to_uint8_array(mac)) return self
def filterByCompanyId(self, companyIds: List[int]): """ Assets are filtered by their 16 bit company ID. :param companyIds: A list of 16 bit company IDs. As can be found on https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers/ """ self._resetCache() self._input = InputDescriptionMaskedAdData(0xFF, get_bitmask([0, 1])) self._assets = [] for companyId in companyIds: self._assets.append(Conversion.uint16_to_uint8_array(companyId)) return self
def filterByName(self, names: List[str], complete: bool = True): """ Assets are filtered by their name (case sensitive). :param names: List of names to be added filter. :param complete: Whether to look for the complete or shortened name. """ self._resetCache() adType = 0x09 if complete else 0x08 self._input = InputDescriptionFullAdData(adType) self._assets = [] for name in names: self._assets.append(Conversion.string_to_uint8_array(name)) return self
async def set_ibeacon_uuid(self, crownstone_id: int, uuid: str, index: int = 0) -> MeshResult: """ :param crownstone_id: int crownstoneUid, 1-255 :param uuid: string: "d8b094e7-569c-4bc6-8637-e11ce4221c18" :param index: for the normal uuid, index = 0, when alternating you also need to define 1 in a followup command. Usually 0 has already been set by the setup procedure. :return: """ statePacket = ControlStateSetPacket(StateType.IBEACON_UUID, index) statePacket.loadByteArray( Conversion.ibeaconUUIDString_to_reversed_uint8_array(uuid)) return await self._set_state_via_mesh_acked(crownstone_id, statePacket.getPacket())
def setup() -> bool: logging.log(logging.INFO, "Perform setup") try: controlPacket = ControlPacketsGenerator.getSetupPacket( crownstoneId=crownstoneId, sphereId=sphereId, adminKey=Conversion.ascii_or_hex_string_to_16_byte_array(adminKey), memberKey=Conversion.ascii_or_hex_string_to_16_byte_array( memberKey), basicKey=Conversion.ascii_or_hex_string_to_16_byte_array(basicKey), serviceDataKey=Conversion.ascii_or_hex_string_to_16_byte_array( serviceDataKey), localizationKey=Conversion.ascii_or_hex_string_to_16_byte_array( localizationKey), meshDeviceKey=Conversion.ascii_or_hex_string_to_16_byte_array( meshDeviceKey), meshAppKey=Conversion.ascii_or_hex_string_to_16_byte_array( meshAppKey), meshNetworkKey=Conversion.ascii_or_hex_string_to_16_byte_array( meshNetworkKey), ibeaconUUID=ibeaconUUID, ibeaconMajor=ibeaconMajor, ibeaconMinor=ibeaconMinor) uartMessage = UartMessagePacket(UartTxType.CONTROL, controlPacket).getPacket() uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE, uartMessage).getPacket() result = UartWriter(uartPacket).write_with_result_sync( [ResultValue.SUCCESS, ResultValue.WAIT_FOR_SUCCESS]) if result.resultCode is ResultValue.SUCCESS: return True if result.resultCode is ResultValue.WAIT_FOR_SUCCESS: # Actually we should wait for the next result code.. time.sleep(3.0) return True logging.log(logging.WARN, f"Setup failed, result={result}") return False except CrownstoneException as e: logging.log(logging.WARN, f"Failed to setup: {e}") return False
def test_crc32(): data = '00ff01ff00000200000000c04e0000' bytes = Conversion.hex_string_to_uint8_array(data) assert (crc32(bytes) == 560669980)
async def getChipTemperature(self) -> float: """ @return: Chip temperature in °C. """ stateVal = await self._getState(StateType.TEMPERATURE) return Conversion.uint8_to_int8(stateVal[0])
async def getErrors(self) -> CrownstoneErrors: """ @return: Errors """ stateVal = await self._getState(StateType.ERROR_BITMASK) return CrownstoneErrors(Conversion.uint8_array_to_uint32(stateVal))
async def getCurrentThresholdDimmer(self) -> float: rawState = await self.core.state._getState( StateType.CURRENT_CONSUMPTION_THRESHOLD_DIMMER) currentThresholdMilliAmp = Conversion.uint8_array_to_uint16(rawState) return currentThresholdMilliAmp / 1000.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()