Decoding the data

The following section takes an example of Lora Pulse Manual.

Example: IWM counter and status

Uplink: 012B0000123401

Fitting this in the PDU structure we get -

Command Type Command Index Command Data
01 2B 00001234 01

Where, Type (0x01) is Data and command index (0x2B) is "IWM Data Counter Status".

As per the Commands table, the data contains 4 bytes (32bits) of the counter value and 1 byte status as bit mask.

Current counter value is 00001234HEX = 4660. Status bit 0 is set indicating a reset has been detected.

Decode logic

Input:

The payload data received from the device is taken as the input.
var bytes = decodeCtx.getUplinkMessage();

Process:

Since the payload is binary data, IMPACT IoT uses Javascript Uint8Array and DataView to extract the information.
var arrBuffer = new Uint8Array(bytes).buffer;
var view = new DataView(arrBuffer);

The first byte is the command type and the second byte is the command index. IMPACT IoT uses the view.getUint8() method to get these details as follows:

var type = view.getUint8(0);
var index = view.getUint8(1);

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:

After decoding the payload, the extracted information is returned in JSON format, using a utility function createJsonResponse(). The response JSON must include the client endpoint name, which can be obtained using the client.getEndpoint() method.

var resp = {
    details: {
        "water/0/UsageinLiters": counter,
        "device/0/StatusResetDetected": true,
 
        "device/0/endPointClientName": client.getEndpoint()
    }
};
 
return utils.createJsonResponse(JSON.stringify(resp));