Decoder
Input:
byte[] getUplinkMessage()
- Returns the uplink payload as Int8 byte array.ImpactRequest getRequest()
- Returns the associated downlink request for which this uplink message is received.
In the preceding code snippet, the uplink payload is retrieved using
getUplinkMessage()
. The use of the getRequest()
function can be seen in the following Output section, where the request correlator ID is
obtained.
Output:
The decode function must return the decoded payload details by calling an util function as shown in the following code snippet:
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
function CodecName() {
this.decode = function (decodeCtx) {
return utils.createJsonResponse(JSON.stringify(resp));
}
// ...
In the preceding code, the decode function returns the decoded payload details in JSON string format.
The identity of the device needs to be retrieved and this can be done using the
client.getEndpoint()
built-in function.
function CodecName() {
this.decode = function (decodeCtx) {
var clientName = client.getEndpoint();
Unsolicited Uplink
The majority of traffic for uplinks is unsolicited, meaning that the device sends this spontaneously without request from the Server. To set this up an OBSERVE operation needs to be received from the server with the resources that are to be observed.
The JSON structure for notification from the device into IMPACT IoT is as follows:
{
"details": {
"device/0/endPointClientName": "<output of client.getEndpoint()>",
//Below are name-value pairs as decoded from uplink payload
"resource1": "value1",
"resource2": "value2"
}
}
Under details, the “device/0/endPointClientName” is a mandatory field whose value must
correspond to the device identity. The built-in function
client.getEndpoint()
can be used to retrieve the device id. Other
details include name-value pairs of device resources that are found after decoding the
uplink payload.
Solicited Uplink
For commands such as READ
, DELETE
,
WRITE
, and EXECUTE
a correlator can be used to
link together the request and response such that the status of the request can be
recorded in IMPACT IoT.
The JSON structure for response is common for all solicited uplink messages is as follows:
{
"responseCode": 0, zero means success
"requestStatus": 0,
"response": [
{
"device/0/endPointClientName": "<output of client.getEndpoint()>",
"resource1": "value1", name-value pairs as decoded from uplink payload
"resource2": "value2"
}
],
"correlatorId": "<output of decodeCtx.getRequest().getCorrelatorId()>"
}
In the preceding JSON structure, the correlatorId
is mandatory which
refers to the correlator ID of the IMPACT IoT downlink request
associated with the current uplink response. The value of the
correlatorId
is stored in IMPACT IoT. There can be
only one outstanding downlink request for a particular device. The value of
correlatorId
can be obtained from the input argument as
follows:
function CodecName() {
this.decode = function (decodeCtx) {
var corrId = decodeCtx.getRequest().getCorrelatorId();
Also, the “device/0/endPointClientName”
field is mandatory, whose value
can be obtained by using the built-in function
client.getEndpoint()
.
Processing:
The process of decoding the input payload is specific to the protocol specification of the device. Refer to Sample scripts section.
If there is any error while decoding the uplink payload then the script can throw an exception as shown in the following code snippet:
function CodecName() {
this.decode = function (decodeCtx) {
if (decoding_failure) {
throw new Error(`boot message with invalid reset reason`);
}
// ...