|
| 1 | +--- |
| 2 | +title: MQTT API |
| 3 | +tags: [Child-Device, Registration] |
| 4 | +sidebar_position: 1 |
| 5 | +description: Register child devices and services with MQTT |
| 6 | +--- |
| 7 | + |
| 8 | +# MQTT API for Entity Management |
| 9 | + |
| 10 | +%%te%% provides an MQTT API to manage all the entities (devices and services) attached to a main device. |
| 11 | +These interfaces let you create, update and delete entities as well as observe changes. |
| 12 | + |
| 13 | +When compared to the HTTP API, the MQTT API excels at notifying subscribers in real-time about entity-related changes |
| 14 | +such as creation, updates, or deletion of child devices and services. |
| 15 | +However, unlike the HTTP API, the MQTT API doesn't provide immediate feedback on whether an operation succeeded or failed, |
| 16 | +making it less suitable for scenarios where confirmation is crucial. |
| 17 | +For example, when an entity registration is attempted by publishing the metadata payload, |
| 18 | +there is no feedback on whether the registration succeeded or not. |
| 19 | + |
| 20 | +## Create a new entity {#create-entity} |
| 21 | + |
| 22 | +A new entity can be registered by publishing the entity definition to the MQTT topic that contains the **entity topic identifier**. |
| 23 | +The payload must contain at least the `@type` of the entity: whether it is a `child-device` or `service`. |
| 24 | + |
| 25 | +:::note |
| 26 | +The supported `@type` values are `device`, `child-device` and `service`. |
| 27 | +The `device` type is reserved for the main %%te%% device which is pre-registered when it is bootstrapped. |
| 28 | +::: |
| 29 | + |
| 30 | +Other supported (optional) fields in the registration payload include: |
| 31 | + |
| 32 | +- `@parent`: Topic ID of the parent entity. |
| 33 | + Required for nested child devices or services where the parent cannot be derived from the topic. |
| 34 | +- `@id`: External ID for the entity. |
| 35 | +- `@health`: Topic ID of the health endpoint service of this entity. |
| 36 | + Valid only for `child-device` entities. |
| 37 | + By default, it is the `tedge-agent` service on that device. |
| 38 | + |
| 39 | +Any additional fields included in the payload are considered as initial [twin data](../../references/mqtt-api.md#twin-metadata) for that entity, |
| 40 | +and they are re-published to the corresponding twin topics. |
| 41 | + |
| 42 | +### Example: Create a new child device |
| 43 | + |
| 44 | +Register as a child device with the name "child0". Assign the child device to the main device (`device/main//`) and give it the topic-id of `device/child0//` which will be used to reference it in all other API calls (both for REST and MQTT). |
| 45 | + |
| 46 | +```sh te2mqtt formats=v1 |
| 47 | +tedge mqtt pub -r 'te/device/child0//' '{ |
| 48 | + "@type": "child-device" |
| 49 | +}' |
| 50 | +``` |
| 51 | + |
| 52 | +### Example: Create a service under a child device |
| 53 | + |
| 54 | +Register `device/child0/service/nodered` as a service of `device/child0//`: |
| 55 | + |
| 56 | +```sh te2mqtt formats=v1 |
| 57 | +tedge mqtt pub -r 'te/device/child0/service/nodered' '{ |
| 58 | + "@type": "service" |
| 59 | +}' |
| 60 | +``` |
| 61 | + |
| 62 | +When the topic id of the service follows the default topic scheme (`te/device/<device_id>/service/<service_id>`), |
| 63 | +the parent of the service is derived from the topic itself (`device/child0//` in this case). |
| 64 | +If a custom topic scheme is used, then the parent is assumed to be `device/main//` by default, when not specified. |
| 65 | + |
| 66 | +### Example: Create a nested child device |
| 67 | + |
| 68 | +Register `device/child01//` as a child device of `device/child0//` which is specified as the `@parent`: |
| 69 | + |
| 70 | +```sh te2mqtt formats=v1 |
| 71 | +tedge mqtt pub -r 'te/device/child01//' '{ |
| 72 | + "@type": "child-device", |
| 73 | + "@parent": "device/child0", |
| 74 | +}' |
| 75 | +``` |
| 76 | + |
| 77 | +When the `@parent` is not specified for a child device, it is assumed to be the main device. |
| 78 | + |
| 79 | +### Example: Create a child device with an external id |
| 80 | + |
| 81 | +Register `device/child01//` as a child device of the main device with a unique id: `XYZ001:child01`: |
| 82 | + |
| 83 | +```sh te2mqtt formats=v1 |
| 84 | +tedge mqtt pub -r 'te/device/child01//' '{ |
| 85 | + "@type": "child-device", |
| 86 | + "@id": "XYZ001:child01" |
| 87 | +}' |
| 88 | +``` |
| 89 | + |
| 90 | +This `@id` is used to uniquely identify an entity from others. |
| 91 | +For example, the cloud mappers use this unique id to register these entities with the cloud. |
| 92 | +When an `@id` is not provided, the mappers are free to compute a unique id from their topic id, which is also unique. |
| 93 | + |
| 94 | +### Example: Create a child device with initial twin data |
| 95 | + |
| 96 | +Register `device/child01//` with initial twin values: `name` and `type`. |
| 97 | + |
| 98 | +```sh te2mqtt formats=v1 |
| 99 | +tedge mqtt pub -r 'te/device/child01//' '{ |
| 100 | + "@type": "child-device", |
| 101 | + "name": "Child 01", |
| 102 | + "type": "Raspberry Pi 4" |
| 103 | +}' |
| 104 | +``` |
| 105 | + |
| 106 | +Unlike the reserved keys like `@type`, %%te%% does not interpret these twin keys in any special way. |
| 107 | +For example, the `type` value here captures the type of the device and has nothing to do with the entity `@type`. |
| 108 | +They are just re-published to their corresponding twin topics (`twin/name` and `twin/type`) as-is. |
| 109 | +So, the values can be any valid twin values. |
| 110 | + |
| 111 | +:::warning |
| 112 | +Using the `@` prefix for such twin data keys is discouraged as this prefix is reserved by %%te%%. |
| 113 | +::: |
| 114 | + |
| 115 | +## Get an entity {#get-entity} |
| 116 | + |
| 117 | +Get an entity's metadata (e.g. default name and type, and parent). |
| 118 | + |
| 119 | +### Example: Get a child device |
| 120 | + |
| 121 | +Get the child device `device/child0//`: |
| 122 | + |
| 123 | +```sh te2mqtt formats=v1 |
| 124 | +tedge mqtt sub 'te/device/child0//' --count 1 |
| 125 | +``` |
| 126 | + |
| 127 | +:::note |
| 128 | +The `--count` value of `1` is used to exit the `tedge mqtt sub` program once the metadata message is received. |
| 129 | +::: |
| 130 | + |
| 131 | +### Example: Get all devices/child-devices/services |
| 132 | + |
| 133 | +Fetch the metadata of all registered entities: |
| 134 | + |
| 135 | +```sh te2mqtt formats=v1 |
| 136 | +tedge mqtt sub 'te/+/+/+/+' --duration 1s |
| 137 | +``` |
| 138 | + |
| 139 | +:::note |
| 140 | +The `--duration` value of `1s` is used to exit the `tedge mqtt sub` program once all the entity metadata messages are received. |
| 141 | +If the number of entities are too high, adjust this timeout accordingly to ensure that all messages are received. |
| 142 | +::: |
| 143 | + |
| 144 | +## Update an entity {#update-entity} |
| 145 | + |
| 146 | +An entity definition can be updated by publishing the new entity definition, replacing the existing one. |
| 147 | +Updates are limited to the `@parent` and `@health` properties only, |
| 148 | +so other properties like `@type` and `@id` cannot be updated after the registration. |
| 149 | + |
| 150 | +:::note |
| 151 | +The complete definition of the new entity must be provided in the payload |
| 152 | +unlike the [HTTP PATCH API](./http_api.md#update-entity), that accepts the specific fragments to be updated. |
| 153 | +:::note |
| 154 | + |
| 155 | +### Example: Update the parent of an entity |
| 156 | + |
| 157 | +Update the parent of the entity `device/child01//` by making it a child of `device/child0//`: |
| 158 | + |
| 159 | +```sh te2mqtt formats=v1 |
| 160 | +tedge mqtt pub -r 'te/device/child01//' '{ |
| 161 | + "@type": "child-device", |
| 162 | + "@parent": "device/child0", |
| 163 | +}' |
| 164 | +``` |
| 165 | + |
| 166 | +## Delete an entity {#delete-entity} |
| 167 | + |
| 168 | +An entity and its descendants (immediate children and nested children) can be deregistered by publishing |
| 169 | +an empty retained messages to the MQTT topic corresponding to its entity topic identifier. |
| 170 | + |
| 171 | +### Example: Delete a child device |
| 172 | + |
| 173 | +Remove a child device and any of its children. |
| 174 | + |
| 175 | +```sh te2mqtt formats=v1 |
| 176 | +tedge mqtt pub -r 'te/device/child01//' '' |
| 177 | +``` |
| 178 | + |
| 179 | +:::note |
| 180 | +The deregistration of descendant entities is done asynchronously by the `tedge-agent` |
| 181 | +and hence the completion time would vary based on how deep the hierarchy is. |
| 182 | +Using the [HTTP API](./http_api.md#delete-entity) is recommended for deregistration as it provides clear feedback on completion. |
| 183 | +::: |
0 commit comments