MQTT Example Get command
Introduction
When the user executes Get command
, the device service sends a read request to one MQTT topic and listens for the results of another MQTT topic.
Required attribute
reqTopic
(request topic) is used for publishing a request to a specified device. The device receives a request and returns a response.resTopic
(response topic) is used for subscribing to the response and parsing the message using the specified message format.reqTemp
(request template) is to generate the request body. As we used the MQTT protocol, we can receive multiple responses fromresTopic
, so we define aUUID
field to match the response and request.
Device Profile Example
name: "MQTT-Device-Simulator"
manufacturer: "IOTech"
model: "MQTT-Device"
deviceResources:
- name: "switchbutton"
isHidden: true
attributes:
reqTopic: "${deviceName}/Switch/Req"
reqTemp: '{ "cmd":"number","reqId":"${uuid}" }'
resTopic: "${deviceName}/Switch/Res"
msgFormat: "json"
jsonPath: "switch"
qos: "0"
properties:
valueType: "String"
readWrite: "RW"
deviceCommands:
- name: "switch"
readWrite: "R"
resourceOperations:
- { deviceResource: "switchbutton", mappings: {"true": "ON", "false": "OFF"}}
The complete sequence flow is as follows:
- User executes a
GET command
- Device Service receives the request and subscribes to the
${deviceName}/Switch/Res
topic - Device Service replaces the
${uuid}
in thereqTemp
and publishes the Request to${deviceName}/Switch/Req
with the message{"cmd":"switch",”reqId”:”d1f8dc93-9b40-42c0” }
- MQTT device receives the Request from the
${deviceName}/Switch/Req
topic - MQTT device publishes the Response to the
${deviceName}/Switch/Res
topic with the message:{"switch":"true",”reqId”:”d1f8dc93-9b40-42c0” }
- Device Service receives the Response from
${deviceName}/Switch/Res
and parses "true" as the switch value, and map true to "ON" according to mappings:{"true": "ON", "false": "OFF"}
, then returns "ON" as the command value