Skip to content

Commit

Permalink
Merge pull request #507 from telefonicaid/task/use_mqtt_retain_from_c…
Browse files Browse the repository at this point in the history
…ommand

check first and use mqtt {retain, qos} properties from command in command execution
  • Loading branch information
fgalan authored Oct 27, 2020
2 parents 37836a7 + 2286ee2 commit c4a81d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Add: use mqtt.qos and mqtt.retain values from command for command execution (#504)
Add: log in info level command and configuration MQTT
FIX: check ngsi version in configuration handler (#500)
Add missed global config env vars (IOTA_CONFIG_RETRIEVAL, IOTA_DEFAULT_KEY, IOTA_DEFAULT_TRANSPORT)
Expand Down
22 changes: 21 additions & 1 deletion docs/usermanual.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ following query parameters:
- **k (API Key)**: API Key for the service the device is registered on.
- **t (timestamp)**: Timestamp of the measure. Will override the automatic IoTAgent timestamp (optional).

#### Commands
#### Sending Commands

MQTT devices commands are always push. For HTTP Devices commands to be push they **must** be provisioned with the
`endpoint` attribute, that will contain the URL where the IoT Agent will send the received commands. Otherwise the
Expand All @@ -111,6 +111,26 @@ Some additional remarks regarding polling commands:
- Commands can be also retrieved without needed of sending a mesaure. In other words, the device is not forced to send
a measure in order to get the accumulated commands. However, in this case note that `GET` method is used to carry
the `getCmd=1` query parameter (as they are no actual payload for measures, `POST` wouldn't make too much sense).
- MQTT devices can configure (at provisioning and updating time) each command with different values of MQTT QoS and MQTT retain values, which will be used only by a command. Moreover, in the same MQTT device different commands can be configured to use different MQTT options related with QoS level and Retain message policy. I.E:

```json
{

"commands": [
{
"type": "command",
"name": "a_command_name_A",
"mqtt": { "qos": 2, "retain": true }
},
{
"type": "command",
"name": "a_command_name_B",
"mqtt": { "qos": 1, "retain": false }
}
]

}
```

#### Configuration retrieval

Expand Down
37 changes: 29 additions & 8 deletions lib/bindings/MQTTBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,37 @@ function stop(callback) {
*/
function executeCommand(apiKey, device, serializedPayload, callback) {
const options = {};
if (config.getConfig().mqtt.qos) {
options.qos = parseInt(config.getConfig().mqtt.qos) || 0;
}
if (config.getConfig().mqtt.retain === true) {
options.retain = config.getConfig().mqtt.retain;
}
config.getLogger().debug(context, 'Sending cmd to the device:\n %j', serializedPayload);
// retrieve command mqtt options from device
var cmdName = Object.keys(JSON.parse(serializedPayload))[0];
var commands = Object.assign({}, ...device.commands.map((c) => ({[c.name]: c})));

options.qos =
commands[cmdName].mqtt && commands[cmdName].mqtt.qos
? commands[cmdName].mqtt.qos
: config.getConfig().mqtt.qos
? parseInt(config.getConfig().mqtt.qos)
: 0;
options.retain =
commands[cmdName].mqtt && commands[cmdName].mqtt.retain
? commands[cmdName].mqtt.retain
: config.getConfig().mqtt.retain
? config.getConfig().mqtt.retain
: false;

const commandTopic = '/' + apiKey + '/' + device.id + '/cmd';
config.getLogger().debug(
context,
'Sending command execution to [%s] with payload [%s] and with mqtt options [%j]',
commandTopic,
serializedPayload,
options);
mqttClient.publish(commandTopic, serializedPayload, options);
config.getLogger().info(context, 'Cmd:\n %j was sent to the device %s', serializedPayload, commandTopic);
config.getLogger().info(
context,
'Cmd:\n %j was sent to the device %s with mqtt options %j',
serializedPayload,
commandTopic,
options);
callback();
}

Expand Down

0 comments on commit c4a81d2

Please sign in to comment.