gprs_pulse_json_codec.js script

The following is an example script which encodes or decodes a JSON data format. This is an example implementation of http://cascademic.com/wp-content/uploads/2017/04/Pulse-metering-adapter-with-GPRS-spec.pdf.
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');

function GprsPulseCodec() {

    const useJson = true;

    const RESRC_METER_ID = "meter_id";
    const RESRC_TIMESTAMP = "timestamp";
    const RESRC_METER_READING = "meter_reading";
    const RESRC_SIGNAL_STRENGTH = "signal_strength";

    ///////////////////////////////
    // Encoder function

    this.encode = function (impactRequest) {
        console.log(impactRequest, typeof(impactRequest), impactRequest.toString());

        throw new Error(`downlink encoding not supported by the GPRS pulse metering device`);
    }

    ///////////////////////////////
    // Decoder Function

    this.decode = function (decodeCtx) {

        var bytes = decodeCtx.getUplinkMessage();

        var text = bytes2String(bytes);
        console.log("input to decoder", text);

        var json = JSON.parse(text);

        var resources = [];

        resources = parseRequestMsg(json);

        console.log("response", JSON.stringify(resources));

        return formImpactNotifyFromJson(decodeCtx, resources);
    }

    // private functions used by this.decode()

    function parseRequestMsg(json) {

        var resp = [];

        if (json.meter_id) {
            resp.push({ name: RESRC_METER_ID, value: json.meter_id });
        }

        if (json.timestamp) {
            resp.push({ name: RESRC_TIMESTAMP, value: json.timestamp });
        }

        if (json.meter_reading) {
            resp.push({ name: RESRC_METER_READING, value: json.meter_reading });
        }

        if (json.signal_strength) {
            resp.push({ name: RESRC_SIGNAL_STRENGTH, value: json.signal_strength });
        }

        return resp;
    }

}

var codec = new GprsPulseCodec();

(codec);
The following is an example script which encodes or decodes a JSON data format. This is an example implementation of http://cascademic.com/wp-content/uploads/2017/04/Pulse-metering-adapter-with-GPRS-spec.pdf.
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');

function GprsPulseCodec() {

    const useJson = true;

    const RESRC_METER_ID = "meter_id";
    const RESRC_TIMESTAMP = "timestamp";
    const RESRC_METER_READING = "meter_reading";
    const RESRC_SIGNAL_STRENGTH = "signal_strength";

    ///////////////////////////////
    // Encoder function

    this.encode = function (impactRequest) {
        console.log(impactRequest, typeof(impactRequest), impactRequest.toString());

        throw new Error(`downlink encoding not supported by the GPRS pulse metering device`);
    }

    ///////////////////////////////
    // Decoder Function

    this.decode = function (decodeCtx) {

        var bytes = decodeCtx.getUplinkMessage();

        var text = bytes2String(bytes);
        console.log("input to decoder", text);

        var json = JSON.parse(text);

        var resources = [];

        resources = parseRequestMsg(json);

        console.log("response", JSON.stringify(resources));

        return formImpactNotifyFromJson(decodeCtx, resources);
    }

    // private functions used by this.decode()

    function parseRequestMsg(json) {

        var resp = [];

        if (json.meter_id) {
            resp.push({ name: RESRC_METER_ID, value: json.meter_id });
        }

        if (json.timestamp) {
            resp.push({ name: RESRC_TIMESTAMP, value: json.timestamp });
        }

        if (json.meter_reading) {
            resp.push({ name: RESRC_METER_READING, value: json.meter_reading });
        }

        if (json.signal_strength) {
            resp.push({ name: RESRC_SIGNAL_STRENGTH, value: json.signal_strength });
        }

        return resp;
    }

}

var codec = new GprsPulseCodec();

(codec);