Thingsboard Manager

tb_manager.on_connect(client, userdata, flags, rc)[source]
class tb_manager.ThingsboardThread(tb, data_to_tb, mqtt_logger)[source]

Bases: Thread

A thread to handle ThingsBoard-related operations, including managing devices, telemetry, and relations between devices.

__init__(tb, data_to_tb, mqtt_logger)[source]

Initialize the ThingsboardThread.

Parameters:
  • tb (ThingsBoardAPI) – An instance of the ThingsBoardAPI class.

  • data_to_tb (queue.Queue) – A queue for receiving data to send to ThingsBoard.

  • mqtt_logger (logging.Logger) – Logger for MQTT-related operations.

run()[source]

Main loop for processing messages from the data queue.

mission_message(message)[source]

Handle mission-related messages.

Parameters:

message (dict) – The mission message to process.

telemetry_message(message)[source]

Handle telemetry-related messages.

Parameters:

message (dict) – The telemetry message to process.

sv_message(message)[source]

Handle state vector messages.

Parameters:

message (dict) – The state vector message to process.

finish_message(message)[source]

Handle finish messages to clean up mission-related data.

Parameters:

message (dict) – The finish message to process.

create_device(device_name, device_type)[source]

Create a device in ThingsBoard.

Parameters:
  • device_name (str) – The name of the device.

  • device_type (str) – The type of the device.

Returns:

A tuple containing the device ID and access token.

Return type:

tuple

create_client(access_token)[source]

Create an MQTT client for a device.

Parameters:

access_token (str) – The access token for the device.

Returns:

The created MQTT client.

Return type:

mqtt.Client

delete_client(client)[source]

Safely disconnect and clean up an MQTT client.

Parameters:

client (mqtt.Client) – The MQTT client to delete.

send_telemetry(client, telemetry)[source]

Send telemetry data to ThingsBoard.

Parameters:
  • client (mqtt.Client) – The MQTT client to use for sending telemetry.

  • telemetry (dict) – The telemetry data to send.

cleanup_mission(mission_id)[source]

Clean up mission-related clients and devices.

Parameters:

mission_id (str) – The ID of the mission to clean up.

cleanup_mission_fields(mission_id)[source]

Clean up all field-related clients and devices for a mission.

Parameters:

mission_id (str) – The ID of the mission to clean up.

remove_mission_data(mission_id)[source]

Remove mission data from internal tracking.

Parameters:

mission_id (str) – The ID of the mission to remove.

delete_device_with_retry(device_id, max_retries=3)[source]

Delete a device with retries.

Parameters:
  • device_id (str) – The ID of the device to delete.

  • max_retries (int) – The maximum number of retries.

Returns:

True if the device was successfully deleted, False otherwise.

Return type:

bool

class tb_manager.ThingsBoardAPI(host)[source]

Bases: object

A class to interact with the ThingsBoard REST API.

__init__(host)[source]

Initialize the ThingsBoardAPI.

Parameters:

host (str) – The host URL of the ThingsBoard server.

login(username, password)[source]

Log in to ThingsBoard.

Parameters:
  • username (str) – The username for authentication.

  • password (str) – The password for authentication.

Returns:

True if login was successful, False otherwise.

Return type:

bool

ensure_auth()[source]

Ensure the API is authenticated.

Returns:

True if authentication is valid, False otherwise.

Return type:

bool

create_device(device_name, device_type='default')[source]

Create a device in ThingsBoard.

Parameters:
  • device_name (str) – The name of the device.

  • device_type (str) – The type of the device.

Returns:

The ID of the created device.

Return type:

str

get_device_access_token(device_id)[source]

Get the access token for a device.

Parameters:

device_id (str) – The ID of the device.

Returns:

The access token for the device.

Return type:

str

delete_device(device_id)[source]

Delete a device in ThingsBoard.

Parameters:

device_id (str) – The ID of the device to delete.

Returns:

True if the device was successfully deleted, False otherwise.

Return type:

bool

logout()[source]

Log out from ThingsBoard.

Returns:

True if logout was successful, False otherwise.

Return type:

bool

get_devices_by_type(device_type)[source]

Get all devices of a specific type.

Parameters:

device_type (str) – The type of devices to retrieve.

Returns:

A list of devices of the specified type.

Return type:

list

delete_all_devices_by_type(device_type)[source]

Delete all devices of a specific type.

Parameters:

device_type (str) – The type of devices to delete.

get_device_by_name(device_name)[source]

Get a device by its name.

Parameters:

device_name (str) – The name of the device to retrieve.

Returns:

The device details if found, None otherwise.

Return type:

dict

create_relation(from_id, to_id, relation_type)[source]

Create a relation between two devices.

Parameters:
  • from_id (str) – The ID of the source device.

  • to_id (str) – The ID of the target device.

  • relation_type (str) – The type of relation to create.

Returns:

True if the relation was successfully created, False otherwise.

Return type:

bool

delete_relation(from_id, to_id, relation_type)[source]

Delete a relation between two devices.

Parameters:
  • from_id (str) – The ID of the source device.

  • to_id (str) – The ID of the target device.

  • relation_type (str) – The type of relation to delete.

Returns:

True if the relation was successfully deleted, False otherwise.

Return type:

bool

set_server_attribute(device_id, key, value)[source]

Set a server attribute for a device.

Parameters:
  • device_id (str) – The ID of the device.

  • key (str) – The key of the attribute to set.

  • value (str) – The value of the attribute to set.

Returns:

True if the attribute was successfully set, False otherwise.

Return type:

bool

tb_manager.start_tb_manager(data_to_tb: Queue, mqtt_logger: Logger)[source]