Encoding the data

This section takes Set the reporting interval data as an example to illustrate how the encoding logic is built.

This is an example of write primitive in terms of IMPACT IoT. Note the switch statement in the process subsection below. Similarly other primitives, such as read, execute can be implemented.

Setting the reporting interval to 1440 minutes = 24 hours

Index Description Datatype Encoding Valid range Access Unsolicited Description Note
0x26 Reporting interval Uint16 Big endian Minutes 1-10080 Query Set No Measurement interval in minutes Setting measurement interval resets the measurement timer.

The command type for Set is 0x01 and the command index for reporting interval is 0x26. The minutes value 1440 in hex is 5A0. The data is encoded as 16 bit value as 0x05A0.

Command type Command index Command data
01 26 05A0

Downlink command: 012605A0

Encode logic

Input:

The input to the encode function is an object, with methods to obtain details of the request.
var request = {
    type = utils.getRequestType(impactRequest),
    resource = impactRequest.getResource()
};
 
if (impactRequest.getValue) {
    request.resourceValue = impactRequest.getValue();
}

Process:

Since the payload is binary data, IMPACT IoT uses Javascript Uint8Array and DataView to extract the information.
var arrBuffer = new ArrayBuffer(MAX_PAYLOADSIZE);
var view = new DataView(arrBuffer);
 
var offset=0;
var dataLen=0;
switch (request.type) {
    case "WRITE":
        view.setUint8(offset++, 0x01); // type = Set
        if (request.resource == "water/0/ReportingInterval") {
            view.setUint8(offset++, 0x26);
        } // else if () { ... } ...
 
        view.setUint16(offset, request.resourceValue);
        offset += 2;
        dataLen = offset;
    case "READ":
        //...
}
 
// Get a typed int8 array
var resBuffer = new Int8Array(arrBuffer.slice(0, dataLen));

Since, there are multiple commands a switch statement can be used to handle them as follows:


switch (index) {
    case 0x2B:
        var counter = view.getUint32(2) ;
        var status = view.getUint8(6) ;

Output:

The script output is the binary data that is sent to the device. IMPACT IoT uses an unitl function createBinaryResponse() to form the binary response.

var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
 return utils.createBinaryResponse(resBuffer, _map, true);

The third argument to createBinaryResponse() indicates whether this message is unidirectional. That is, the device does not send any response or acknowledgement for this request. A read request is a bidirectional message, since there is an associated response from the device, whereas write and execute requests are unidirectional.