def setup_uart_listeners(manager: CrownstoneEntryManager) -> None: """Set up UART listeners.""" # save subscription id to unsub manager.listeners[UART_LISTENERS] = [ UartEventBus.subscribe( SystemTopics.connectionEstablished, partial(update_uart_state, manager), ), UartEventBus.subscribe( SystemTopics.connectionClosed, partial(update_uart_state, manager), ), UartEventBus.subscribe( UartTopics.newDataAvailable, partial(update_crwn_state_uart, manager), ), ]
def _getMacAddress(): result = [True, None] def handleMessage(container, data): container[0] = False container[1] = data subscriptionId = UartEventBus.subscribe( DevTopics.ownMacAddress, lambda data: handleMessage(result, data)) uart._usbDev.requestMacAddress() counter = 0 while result[0] and counter < 50: counter += 1 time.sleep(0.05) UartEventBus.unsubscribe(subscriptionId) return result[1]
def __init__(self): self._logFormatter = LogFormatter() UartEventBus.subscribe(UartTopics.log, self._onLog) UartEventBus.subscribe(UartTopics.logArray, self._onLogArray) UartEventBus.subscribe(SystemTopics.uartDiscardedData, self._onDiscardedData) # The log string file name. self._logStringsFileName = "" # Modification timestamp of the log string file. self._logStringsFileTimestamp = 0 # Key: filename hash # Value: filename self._fileNames = {} # Key: filename hash # Value: map with: # Key: line number # Value: log string self._logs = {} # Key: filename hash # Value: map with: # Key: line number # Value: (startFormat, endFormat, separationFormat, elementFormat) self._logArrays = {} # Whether to print discarded data. self._printDiscardedData = False
async def async_unload(self) -> bool: """Unload the current config entry.""" # Authentication failed if self.cloud.cloud_data is None: return True self.sse.close_client() for sse_unsub in self.listeners[SSE_LISTENERS]: sse_unsub() if self.uart: self.uart.stop() for subscription_id in self.listeners[UART_LISTENERS]: UartEventBus.unsubscribe(subscription_id) unload_ok = await self.hass.config_entries.async_unload_platforms( self.config_entry, PLATFORMS) if unload_ok: self.hass.data[DOMAIN].pop(self.config_entry.entry_id) return unload_ok
if data.type == AdvType.CROWNSTONE_STATE or data.type == AdvType.EXTERNAL_STATE: print( f"PowerUsage of crownstone {data.crownstoneId} is {data.powerUsageReal}W" ) print("-------------------") uart = CrownstoneUart() # Start up the USB bridge. uart.initialize_usb_sync() # you can alternatively do this async by # await uart.initialize_usb() # Set up event listeners UartEventBus.subscribe(UartTopics.newDataAvailable, showNewData) # Switch this Crownstone on and off. turnOn = True # The try except part is just to catch a control+c, time.sleep does not appreciate being killed. try: for i in range(0, 10): if not uart.running: break if turnOn: print("Switching Crownstone on (iteration: ", i, ")") else: print("Switching Crownstone off (iteration: ", i, ")") uart.switch_crownstone(targetCrownstoneId, on=turnOn)
uart = CrownstoneUart() # Keep up the operation mode. stoneHasBeenSetUp = False def handleHello(data): helloResult = data logging.log(logging.DEBUG, f"flags={helloResult.status.flags}") global stoneHasBeenSetUp stoneHasBeenSetUp = helloResult.status.hasBeenSetUp logging.log(logging.DEBUG, f"stoneHasBeenSetUp={stoneHasBeenSetUp}") UartEventBus.subscribe(UartTopics.hello, handleHello) # Start up the USB bridge. uart.initialize_usb_sync(writeChunkMaxSize=64) # Sphere specific settings: adminKey = "adminKeyForCrown" memberKey = "memberKeyForHome" basicKey = "basicKeyForOther" serviceDataKey = "MyServiceDataKey" localizationKey = "aLocalizationKey" meshAppKey = "MyGoodMeshAppKey" meshNetworkKey = "MyGoodMeshNetKey" ibeaconUUID = "1843423e-e175-4af0-a2e4-31e32f729a8a" sphereId = 1
uart = CrownstoneUart() # Start up the USB bridge. uart.initialize_usb_sync() # you can alternatively do this async by # await uart.initialize_usb() # Function that's called when the power usage is updated. def showUartMessage(data): print("Received payload", data) # Set up event listeners UartEventBus.subscribe(UartTopics.uartMessage, showUartMessage) # the try except part is just to catch a control+c, time.sleep does not appreciate being killed. try: uart.uart_echo("HelloWorld") time.sleep(0.2) uart.uart_echo("HelloWorld") time.sleep(0.2) uart.uart_echo("HelloWorld") time.sleep(0.2) uart.uart_echo("HelloWorld") time.sleep(0.2) uart.uart_echo("HelloWorld") time.sleep(0.2) uart.uart_echo("HelloWorld") time.sleep(0.2)
# Start up the USB bridge. uart.initialize_usb_sync() def showNewData(data): print("New data received!") print(json.dumps(data, indent=2)) print("-------------------") print("PING!") uart.uart_echo("PONG!") def showUartMessage(data): print("Received Uart Message " + data["string"]) # Set up event listeners UartEventBus.subscribe(UartTopics.newDataAvailable, showNewData) UartEventBus.subscribe(UartTopics.uartMessage, showUartMessage) print("Listening for Crownstones on the mesh, this might take a while.") # the try except part is just to catch a control+c, time.sleep does not appreciate being killed. try: while uart.running: time.sleep(1) except KeyboardInterrupt: print("Closing example.... Thanks for your time!") uart.stop()