示例#1
0
    def getAllowDimmingPacket(allow: bool):
        """

        :param allow: bool
        :return:
        """

        allowByte = 0
        if allow:
            allowByte = 1

        return ControlPacket(
            ControlType.ALLOW_DIMMING).loadUInt8(allowByte).serialize()
示例#2
0
def getRamStats():
    controlPacket = ControlPacket(ControlType.GET_RAM_STATS).getPacket()
    uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                    controlPacket).getPacket()
    uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                   uartMessage).getPacket()
    try:
        result = UartWriter(uartPacket).write_with_result_sync()
        if result.resultCode is ResultValue.SUCCESS:
            return RamStatsPacket(result.payload)
        logging.log(logging.WARN, f"Get ram stats failed, result={result}")
    except CrownstoneException as e:
        logging.log(logging.WARN, f"Get ram stats failed: {e}")
    return None
 def validate_microapp(self, index: int):
     """
     validate the binary of a microapp saved in Bluenet flash memory. Return True if message is
     send correctly, it doesn't check if the command is set correctly in Bluenet. For now only
     an index of 0 can be given, since Bluenet only supports a single app to run.
     """
     packet = MicroappHeaderPacket(index)
     controlPacket = ControlPacket(
         ControlType.MICROAPP_VALIDATE).loadByteArray(
             packet.serialize()).serialize()
     uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                     controlPacket).serialize()
     uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                    uartMessage).serialize()
     UartWriter(uartPacket).write_sync()
 def disable_microapp(self, index: int):
     """
     Disable a running microapp in Bluenet by removing it from RAM. Return True if message is
     send correctly, it doesn't check if the command is set correctly in Bluenet. For now only
     an index of 0 can be given, since Bluenet only supports a single app to run.
     """
     packet = MicroappHeaderPacket(index)
     controlPacket = ControlPacket(
         ControlType.MICROAPP_DISABLE).loadByteArray(
             packet.serialize()).serialize()
     uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                     controlPacket).serialize()
     uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                    uartMessage).serialize()
     UartWriter(uartPacket).write_sync()
示例#5
0
    def uart_echo(self, payloadString):
        # wrap that in a control packet
        controlPacket = ControlPacket(
            ControlType.UART_MESSAGE).loadString(payloadString).getPacket()

        # wrap that in a uart message
        uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                        controlPacket).getPacket()

        # finally, wrap it in an uart wrapper packet
        uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                       uartMessage).getPacket()

        # send over uart
        result = UartWriter(uartPacket).write_sync()
 def enable_microapp(self, index: int):
     """
     Enable microapp in Bluenet and load it in RAM. Return True if message is send correctly,
     it doesn't check if the command is set correctly in Bluenet. For now only an index of 0
     can be given, since Bluenet only supports a single app to run. It is recommended to run
     the validation function before enabling the microapp to make sure a microapp is able to
     run.
     """
     packet = MicroappHeaderPacket(index)
     controlPacket = ControlPacket(
         ControlType.MICROAPP_ENABLE).loadByteArray(
             packet.serialize()).serialize()
     uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                     controlPacket).serialize()
     uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                    uartMessage).serialize()
     UartWriter(uartPacket).write_sync()
示例#7
0
    def getSetupPacket(crownstoneId: int, sphereId: int, adminKey, memberKey,
                       basicKey, serviceDataKey, localizationKey,
                       meshDeviceKey, meshAppKey, meshNetworkKey,
                       ibeaconUUID: str, ibeaconMajor: int, ibeaconMinor: int):
        """
        :param crownstoneId:  		uint8 number
        :param sphereId:  	     	uint8 number
        :param adminKey:      		byteString (no conversion required)
        :param memberKey:     		byteString (no conversion required)
        :param basicKey:      		byteString (no conversion required)
        :param serviceDataKey: 	    byteString (no conversion required)
        :param localizationKey: 	byteString (no conversion required)
        :param meshDeviceKey: 	    byteString (no conversion required)
        :param meshAppKey: 	        byteString (no conversion required)
        :param meshNetworkKey: 	    byteString (no conversion required)
        :param ibeaconUUID: 		string  (ie. "1843423e-e175-4af0-a2e4-31e32f729a8a")
        :param ibeaconMajor:        uint16 number
        :param ibeaconMinor:        uint16 number
        :return:
        """
        data = []
        data.append(crownstoneId)
        data.append(sphereId)

        data += list(adminKey)
        data += list(memberKey)
        data += list(basicKey)
        data += list(serviceDataKey)
        data += list(localizationKey)

        MDKey = meshDeviceKey
        if type(meshDeviceKey) is str:
            MDKey = Conversion.ascii_or_hex_string_to_16_byte_array(
                meshDeviceKey)

        data += list(MDKey)
        data += list(meshAppKey)
        data += list(meshNetworkKey)

        data += Conversion.ibeaconUUIDString_to_reversed_uint8_array(
            ibeaconUUID)
        data += Conversion.uint16_to_uint8_array(ibeaconMajor)
        data += Conversion.uint16_to_uint8_array(ibeaconMinor)

        return ControlPacket(ControlType.SETUP).loadByteArray(data).serialize()
示例#8
0
    async def _command_via_mesh_broadcast_acked(
            self, crownstone_uid_array: List[int],
            packet: bytearray) -> MeshResult:
        # this is only for the set_iBeacon_config_id
        # broadcast to all, but retry until ID's in list have acked or timeout
        # value: 3
        corePacket = MeshBroadcastAckedPacket(crownstone_uid_array,
                                              packet).getPacket()
        controlPacket = ControlPacket(
            ControlType.MESH_COMMAND).loadByteArray(corePacket).getPacket()
        uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                        controlPacket).getPacket()
        uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                       uartMessage).getPacket()

        resultCollector = Collector(timeout=2, topic=SystemTopics.resultPacket)
        individualCollector = BatchCollector(
            timeout=15, topic=SystemTopics.meshResultPacket)
        finalCollector = Collector(timeout=15,
                                   topic=SystemTopics.meshResultFinalPacket)

        # send the message to the Crownstone
        UartEventBus.emit(SystemTopics.uartWriteData, uartPacket)

        # wait for the collectors to fill
        commandResultData = await resultCollector.receive()
        if commandResultData is not None:
            if commandResultData.resultCode is ResultValue.BUSY:
                await asyncio.sleep(0.2)
                return await self._command_via_mesh_broadcast_acked(
                    crownstone_uid_array, packet)
            elif commandResultData.resultCode is not ResultValue.SUCCESS:
                raise CrownstoneException(commandResultData.resultCode,
                                          "Command has failed.")

        return await self._handleCollectors(crownstone_uid_array,
                                            individualCollector,
                                            finalCollector)
示例#9
0
    async def _set_state_via_mesh_acked(self, crownstone_id: int,
                                        packet: bytearray) -> MeshResult:
        # 1:1 message to N crownstones with acks (only N = 1 supported for now)
        # flag value: 2
        corePacket = MeshSetStatePacket(crownstone_id, packet).getPacket()
        controlPacket = ControlPacket(
            ControlType.MESH_COMMAND).loadByteArray(corePacket).getPacket()
        uartMessage = UartMessagePacket(UartTxType.CONTROL,
                                        controlPacket).getPacket()
        uartPacket = UartWrapperPacket(UartMessageType.UART_MESSAGE,
                                       uartMessage).getPacket()

        resultCollector = Collector(timeout=2, topic=SystemTopics.resultPacket)
        individualCollector = BatchCollector(
            timeout=15, topic=SystemTopics.meshResultPacket)
        finalCollector = Collector(timeout=15,
                                   topic=SystemTopics.meshResultFinalPacket)

        # send the message to the Crownstone
        UartEventBus.emit(SystemTopics.uartWriteData, uartPacket)

        # wait for the collectors to fill
        commandResultData = await resultCollector.receive()

        if commandResultData is not None:
            if commandResultData.resultCode is ResultValue.BUSY:
                await asyncio.sleep(0.2)
                return await self._set_state_via_mesh_acked(
                    crownstone_id, packet)
            elif commandResultData.resultCode is not ResultValue.SUCCESS:
                raise CrownstoneException(commandResultData.resultCode,
                                          "Command has failed.")

        return await self._handleCollectors([crownstone_id],
                                            individualCollector,
                                            finalCollector)
示例#10
0
 def getDisconnectPacket():
     return ControlPacket(ControlType.DISCONNECT).serialize()
示例#11
0
 async def enableMicroapp(self, index):
     packet = MicroappHeaderPacket(index)
     controlPacket = ControlPacket(
         ControlType.MICROAPP_ENABLE).loadByteArray(
             packet.toBuffer()).getPacket()
     await self._writeControlAndGetResult(controlPacket)
示例#12
0
 def getPutInDFUPacket():
     return ControlPacket(ControlType.GOTO_DFU).getPacket()
示例#13
0
 async def getMicroappInfo(self) -> MicroappInfoPacket:
     resultPacket = await self._writeControlAndGetResult(
         ControlPacket(ControlType.MICROAPP_GET_INFO).getPacket())
     _LOGGER.info(f"getMicroappInfo {resultPacket}")
     infoPacket = MicroappInfoPacket(resultPacket.payload)
     return infoPacket
示例#14
0
 def getGetFilterSummariesPacket() -> [int]:
     return ControlPacket(
         ControlType.ASSET_FILTER_GET_SUMMARIES).serialize()
示例#15
0
 def getRemoveFilterPacket(filterId: int) -> [int]:
     removecommand = RemoveFilterPacket(filterId)
     return ControlPacket(ControlType.ASSET_FILTER_REMOVE).loadByteArray(
         removecommand.serialize()).serialize()
示例#16
0
 def getResetPacket():
     return ControlPacket(ControlType.RESET).getPacket()
示例#17
0
 def getCommitFilterChangesPacket(masterVersion: int,
                                  masterCrc: int) -> [int]:
     commitcommand = CommitFilterChangesPacket(masterVersion, masterCrc)
     return ControlPacket(
         ControlType.ASSET_FILTER_COMMIT_CHANGES).loadByteArray(
             commitcommand.serialize()).serialize()
示例#18
0
 def getUploadFilterPacket(chunk: [int]) -> [int]:
     """
     TODO: type chunk param
     """
     return ControlPacket(
         ControlType.ASSET_FILTER_UPLOAD).loadByteArray(chunk).serialize()
示例#19
0
 async def send_no_op(self):
     no_op_packet = ControlPacket(ControlType.NO_OPERATION)
     await self._command_via_mesh_broadcast(no_op_packet.getPacket())
示例#20
0
 def getPutInDFUPacket():
     return ControlPacket(ControlType.GOTO_DFU).serialize()
示例#21
0
 def getResetPacket():
     return ControlPacket(ControlType.RESET).serialize()
示例#22
0
 def getSwitchCommandPacket(switchVal: int):
     """
     :param switchVal: percentage [0..100] or special value (SwitchValSpecial).
     """
     return ControlPacket(
         ControlType.SWITCH).loadUInt8(switchVal).serialize()
 def echo(self, string):
     controlPacket = ControlPacket(ControlType.UART_MESSAGE).loadString(string).serialize()
     uartMessage   = UartMessagePacket(UartTxType.CONTROL, controlPacket).serialize()
     uartPacket    = UartWrapperPacket(UartMessageType.UART_MESSAGE, uartMessage).serialize()
     UartEventBus.emit(SystemTopics.uartWriteData, uartPacket)
示例#24
0
 def getDimmerSwitchPacket(intensity: int):
     """
     :param intensity: percentage [0..100]
     """
     return ControlPacket(ControlType.PWM).loadUInt8(intensity).serialize()
示例#25
0
 def getResetErrorPacket(errorMask):
     return ControlPacket(
         ControlType.RESET_ERRORS).loadUInt32(errorMask).serialize()
 async def send_no_op(self):
     control_packet = ControlPacket(ControlType.NO_OPERATION)
     await self._command_via_mesh_broadcast(control_packet.serialize())