Device ID and Payload Mapping in Downlink
Mapping Device ID in Topic downlink
Device ID can be mapped within an MQTT topic to send downlink data when performing read and write requests. You can include the keyword "#deviceid" anywhere in the mapped topic name.
Topic Handling in Application downlink
To interwork with generally available MQTT devices it is possible to use arbitrary topic names with no design limitation or predefine nomenclature rules when sending read or write downlink messages.
Read request
While performing a read request, you must map the MQTT topics for both the device bound and the server bound responses to the same IMPACT resource. This means that a single IMPACT resource can be mapped to one topic for Reading and one for the Read response.
Topic names can be mapped within an MQTT topic for MQTT devices to send a read message from an application.
Example
- The MQTT device is pre-provisioned with Manufacturer and Model of the device.
- Script files are uploaded that support the free format topic and mapping.Note: Script has encode and decode function. For more information, refer to Scripting examples.
- The device SUBSCRIBE to topic
devices/11223344/messages/devicebound/PackageSize
The topic devices/11223344/messages/devicebound/PackageSize is mapped to Resource /topics/0/PkgSize.
- The Application initiates the following downlink request:
/m2m/endpoints/112223344/messages/devicebound/PackageSize
- IMPACT performs PUBLISH:
devices/112223344/messages/devicebound/PackageSize
- The Device receives the PUBLISH request and respond back with the PackageSize:
device/device01/response {"correlatorId":"f4e45596-a481-4b1b-8f45-57985c89b4e9","type":"read","topic":"messages/devicebound/PackageSize=1024v"}
- The Application receives notification response:
{"reports":[], "registrations":[], "deregistrations":[], "updates":[], "expirations":[], "responses":[{"serialNumber":"11223344", "timestamp":1644821094851, "requestId":"9f14586c-5027-4045-b162-ce52a92c9987", "protocol":"MQTT", "resources":[{"resourcePath":"messages/devicebound/PackageSize", "value":1024v{color}}], "result":{"code":0,"subCode":null, "reason":"Success"}, "operationType":"read"}]}
Write request
It is possible to map the Device ID within an MQTT topic to send downlink requests when performing a write request.
Topic name can be mapped within an MQTT topic to send downlink requests when performing a write request.
After the device is configured with an MQTT device and SUBSCRIBED to the mapped topic in the script, the application initiates a downlink write by issuing a PUBLISH message to an MQTT topic.
Example
- The MQTT device is pre-provisioned with Manufacturer and Model of the device.
- Script files are uploaded that support the free format topic and mapping.Note: Script has encode and decode function. For more information refer to Scripting examples.
- The topic devices/11223344/messages/devicebound/PackageSize is mapped to Resource /topics/0/PkgSize.
- The MQTT device SUBSCRIBE to the topic:
devices/112223344/messages/devicebound/PackageSize
- The Application initiates a downlink write with Resource Name:
/m2m/endpoints/112223344/messages/devicebound/PackageSize=1024
- IMPACT PUBLISH the following:
devices/112223344/messages/devicebound/PackageSize=1024v
- The Device receives the PUBLISH and sets the PackageSize to 1024 and responds back
to application.
device/device01/response {"correlatorId":"965404d9-6e20-4d14-980a-2465a638c05f","type":"write", "topic":"messages/devicebound/PackageSize=1024"}
-
The Application receives notification response:
{"reports":[], "registrations":[], "deregistrations":[], "updates":[], "expirations":[], "responses":[{"serialNumber":"112223344", "timestamp":1619015762517, "requestId":"400067ca-e401-4976-81a7-f1526d5082b8", "protocol":"MQTT-SN", "resources":[{"resourcePath":"messages/devicebound/PackageSize", "value":null}], "result":{"code":0,"subCode":null, "reason":"success"}, "operationType":"write"}]}
Read and Write request using IMPACT Console
Read request
- In the Devices > View Device page, pre-provision the device with the Manufacturer as Generic and the Model as Sensor.
- Configure the corresponding script files for the Make and Model in
Settings > Protocol Setups page.Note: Script has encode and decode function. For more information refer to Scripting examples.
- The read resource request is sent to the device using the action created in the
View Device > Operations
page.
Example: In the action, Sensor/0/temperature is the topic that will be converted to the topic which is subscribed by the device. The topic subscribed is devices/mqttDevice11/sensor/0/temperature.
On sending the action, the payload is received on the device.
- The device initiates the PUBLISH.
- If the job is successful, click on the Job Name in the
View Device > Jobs page to
view the request and the response sent by the device.
In the example, request sent is "sensor/0/temperature" and the response received is "{"sensor/0/temperature" :"25"}"
Write request
- In the Devices > View Device page, pre-provision the device with Manufacturer as Generic and Model as Sensor.
- Configure the corresponding script for the Make and Model in
Settings > Protocol Setups
page.
Script file supports the free format topic and mapping. The script has encode and decode functions. For more information on these functions, see Scripting examples.
- Write resource request is sent to the device using the action created in the
View Device > Operations
page.
Example: In the action, Sensor/0/temperature is the topic that will be converted to the topic which is subscribed by the device. The topic subscribed is devices/mqttDevice10/sensor/0/temperature.
On sending the action, the payload is received on the device.
- The device initiates the PUBLISH.
- If the job is successful, click on the Job Name in the
View Device > Jobs page to
view the request and the response sent by the device.
In the example, request sent is "sensor/0/temperature" and the response received is "{"sensor/0/temperature" :"25"}"
Sample script
Following is a sample script file which parses the payload.
var utils = Java.type('com.nokia.adapter.transformation.utils.TranformerUtil');
function DownlinkRequestCodec() {
const MANUFACTURER = 'device/0/manufacturer';
const MODEL = 'device/0/model';
const FIRMWARE_VERSION = 'device/0/firmwareVersion';
const TEMPERATURE = 'sensor/0/temperature';
const PACKAGE_SIZE = 'topics/0/PkgSize';
this.encode = function (impactRequest) {
console.log("input to DownlinkRequestCodec encoder", impactRequest, typeof(impactRequest), impactRequest.toString());
var requestType = utils.getRequestType(impactRequest);
console.log('request type is ' + requestType);
var payload = {correlatorId: impactRequest.getCorrelatorId(), type: 'read'};
var resources = {resource: 'devices/'+impactRequest.getSerialNumber()+'/messages/devicebound/PackageSize', payload: JSON.stringify(payload)};
return constructImpactWriteRequest(resources);
}
this.decode = function (decodeCtx) {
var bytes = decodeCtx.getUplinkMessage();
var request = bytes2String(bytes);
console.log("input to DownlinkRequestCodec decoder", request);
var requestData = JSON.parse(request);
if(requestData.topic.includes("response")){
var payload = JSON.parse(requestData.payload);
var res = {};
res[PACKAGE_SIZE] = payload.value;
var readResponse = {type: 'READ_RESPONSE', resources: res, correlatorid: payload.correlatorId, status: 200};
return formImpactNotifyFromJson(decodeCtx, readResponse);
} else {
var resources = constructResourceMapping(requestData);
var notifyResponse = {type: 'NOTIFY', resources: resources};
return formImpactNotifyFromJson(decodeCtx, notifyResponse);
}
}
function constructResourceMapping(json) {
var payload = JSON.parse(json.payload);
var response = {};
if (payload.manufacturer) {
response[MANUFACTURER] = payload.manufacturer;
}
if (payload.model) {
response[MODEL] = payload.model;
}
if (payload.firmwareVersion) {
response[FIRMWARE_VERSION] = payload.firmwareVersion;
}
if (payload.temperature) {
response[TEMPERATURE] = payload.temperature;
}
return response;
}
}
var codec = new DownlinkRequestCodec();
(codec);