API¶
State Diagram¶
stateDiagram-v2
disconnected: Disconnected
stu_connected: STU Connected
sensor_node_connected: Sensor Node Connected
disconnected --> stu_connected: connect_stu
stu_connected --> disconnected: disconnect_stu
stu_connected --> stu_connected: enable_ota, collect_sensor_nodes, rename, reset
stu_connected --> sensor_node_connected: connect_sensor_node_mac
sensor_node_connected --> sensor_node_connected: rename
sensor_node_connected --> stu_connected: disconnect_sensor_node
System¶
- class icostate.ICOsystem¶
Stateful access to ICOtronic system
- async collect_sensor_nodes()¶
Get available sensor nodes
This coroutine collects sensor node information until either
no new sensor node was found or
until the given timeout, if no sensor node was found.
- Return type:
list[SensorNodeInfo]- Returns:
A list containing information about the available sensor nodes
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
Examples
Import necessary code
>>> from asyncio import run
Collect sensor nodes
>>> async def collect_sensor_nodes(icosystem: ICOsystem ... ) -> list[SensorNodeInfo]: ... await icosystem.connect_stu() ... nodes = await icosystem.collect_sensor_nodes() ... await icosystem.disconnect_stu() ... return nodes >>> sensor_nodes = run(collect_sensor_nodes(ICOsystem())) >>> # We assume that at least one sensor node is available >>> len(sensor_nodes) >= 1 True
- async connect_sensor_node_mac(mac_address)¶
Connect to the node with the specified MAC address
- Parameters:
mac_address (
str) – The MAC address of the sensor node- Raises:
ArgumentError – If the specified MAC address is not valid
NoResponseError – If there was no response to an request made by this coroutine
- Return type:
None
Examples
Import necessary code
>>> from asyncio import run
Connect to a disconnect from sensor node
>>> async def connect_sensor_node(icosystem: ICOsystem, ... mac_address: str): ... await icosystem.connect_stu() ... await icosystem.connect_sensor_node_mac(mac_address) ... print(icosystem.state) ... await icosystem.disconnect_sensor_node() ... print(icosystem.state) ... await icosystem.disconnect_stu() >>> mac_address = ( ... "08-6B-D7-01-DE-81") # Change to MAC address of your node >>> run(connect_sensor_node(ICOsystem(), mac_address)) State.SENSOR_NODE_CONNECTED State.STU_CONNECTED
- async connect_stu()¶
Connect to STU
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
- Return type:
None
Examples
Import necessary code
>>> from asyncio import run
Connect and disconnect from STU
>>> async def connect_disconnect_stu(icosystem: ICOsystem): ... states = [icosystem.state] ... await icosystem.connect_stu() ... states.append(icosystem.state) ... await icosystem.disconnect_stu() ... states.append(icosystem.state) ... return states >>> run(connect_disconnect_stu(ICOsystem())) [Disconnected, STU Connected, Disconnected]
- async disconnect_sensor_node()¶
Disconnect from current sensor node
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
- Return type:
None
- async disconnect_stu()¶
Disconnect from STU
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
- Return type:
None
- async enable_ota()¶
Enable OTA (Over The Air) update mode
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
- Return type:
None
Examples
Import necessary code
>>> from asyncio import run
Enable OTA update mode
>>> async def enable_ota(icosystem: ICOsystem): ... await icosystem.connect_stu() ... await icosystem.enable_ota() ... await icosystem.disconnect_stu() >>> run(enable_ota(ICOsystem()))
- async is_sensor_node_connected()¶
Check if the STU is connected to a sensor node
- Return type:
bool- Returns:
True, if the STU is connected to a sensor nodeFalse, otherwise
Examples
Import necessary code
>>> from asyncio import run
Check if a sensor node is connected
>>> async def connect_sensor_node(icosystem: ICOsystem, ... mac_address: str): ... print("Before connection to STU:", ... await icosystem.is_sensor_node_connected()) ... await icosystem.connect_stu() ... print("After connection to STU:", ... await icosystem.is_sensor_node_connected()) ... await icosystem.connect_sensor_node_mac(mac_address) ... print("After connection to sensor node:", ... await icosystem.is_sensor_node_connected()) ... await icosystem.disconnect_sensor_node() ... print("After disconnection from sensor node:", ... await icosystem.is_sensor_node_connected()) ... await icosystem.disconnect_stu() >>> mac_address = ( ... "08-6B-D7-01-DE-81") # Change to MAC address of your node >>> run(connect_sensor_node(ICOsystem(), mac_address)) Before connection to STU: False After connection to STU: False After connection to sensor node: True After disconnection from sensor node: False
- async rename(new_name, mac_address=None)¶
Set the name of the sensor node with the specified MAC address
Depending on the state the system is in this coroutine will either:
connect to the sensor device with the given MAC address, if there is no connection yet and disconnect afterwards or
just use the current connection and rename the current sensor device. In this case the given MAC address will be ignored!
- Parameters:
new_name (
str) – The new name of the sensor devicemac_address (
str|None) – The MAC address of the sensor device that should be renamed
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
ValueError – If you call this method without specifying the MAC address while the system is not connected to a sensor node
Examples
Import necessary code :rtype:
None>>> from asyncio import run
Rename a disconnected sensor node
>>> async def rename_disconnected(icosystem: ICOsystem, ... mac_address: str, ... name: str): ... await icosystem.connect_stu() ... print(f"Before renaming: {icosystem.state!r}") ... await icosystem.rename(name, mac_address) ... print(f"After renaming: {icosystem.state!r}") ... await icosystem.disconnect_stu() >>> mac_address = ( ... "08-6B-D7-01-DE-81") # Change to MAC address of your node >>> name = "Test-STH" >>> run(rename_disconnected(ICOsystem(), mac_address, name)) Before renaming: STU Connected After renaming: STU Connected
Rename a connected sensor node
>>> async def rename_connected(icosystem: ICOsystem, ... mac_address: str, ... name: str): ... await icosystem.connect_stu() ... await icosystem.connect_sensor_node_mac(mac_address) ... print(f"Before renaming: {icosystem.state!r}") ... await icosystem.rename(name, None) ... print(f"After renaming: {icosystem.state!r}") ... await icosystem.disconnect_sensor_node() ... await icosystem.disconnect_stu() >>> mac_address = ( ... "08-6B-D7-01-DE-81") # Change to MAC address of your node >>> name = "Test-STH" >>> run(rename_connected(ICOsystem(), mac_address, name)) Before renaming: Sensor Node Connected After renaming: Sensor Node Connected
- async reset_stu()¶
Reset STU
- Raises:
NoResponseError – If there was no response to an request made by this coroutine
- Return type:
None
Examples
Import necessary code
>>> from asyncio import run
Reset a connected STU
>>> async def reset_stu(icosystem: ICOsystem): ... await icosystem.connect_stu() ... await icosystem.reset_stu() ... await icosystem.disconnect_stu() >>> run(reset_stu(ICOsystem()))
Resetting the STU will not work if the STU is not connected
>>> async def reset_stu_without_connection(icosystem: ICOsystem): ... await icosystem.reset_stu() >>> run(reset_stu_without_connection( ... ICOsystem())) Traceback (most recent call last): ... icostate.error.IncorrectStateError: Resetting STU only allowed in the state: STU Connected