codec_util.js script
The following is a common utility file which is used by the main codec scripts.
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
// utility functions
//
function buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
function bytes2String(bytes, offset=0) {
if (offset < 0) { offset=0; }
var result = "";
for (var i = offset; i < bytes.length; i++) {
result += String.fromCharCode(bytes[i]);
}
return result;
}
function string2Bytes(str, offset=0) {
if (offset < 0) { offset=0; }
var bytes;
const strLen = str? str.length : 0;
bytes = new Array(strLen + offset);
for (var i = 0; i < strLen; i++) {
bytes[offset+i] = str.charCodeAt(i);
}
return bytes;
}
function isBitSet(byte, bitNum) {
return ((byte >> bitNum) & 0xFF) == 0x01;
}
function getImpactRequestAsJson(impactRequest) {
console.log("input to encoder", impactRequest);
var request = {};
request.type = utils.getRequestType(impactRequest);
request.url = impactRequest.getResource();
if (!request.type || !request.url) {
throw new Error(`request type and resource URL are required in the input`);
}
var resourceItems = request.url.split("/");
request.resource = resourceItems[resourceItems.length - 1];
if (impactRequest.getValue) {
request.resourceValue = impactRequest.getValue();
}
console.log("request to encoder", JSON.stringify(request));
return request;
}
function formImpactResponseOrNotifyFromJson(decodeCtx, resources, success=true) {
if(decodeCtx.getRequest() != null) {
var corrId = decodeCtx.getRequest().getCorrelatorId();
var resp = {responseCode: success? 0: 1, requestStatus: 0, response: [], correlatorId: corrId};
var resourceObj = {"device/0/endPointClientName":client.getEndpoint()};
resources.forEach(resource => {
resourceObj[resource.name] = resource.value;
});
resp.response.push(resourceObj);
console.log('sending read response ' + JSON.stringify(resp));
return utils.createJsonResponse(JSON.stringify(resp));
} else {
var resp = {details: {"device/0/endPointClientName": client.getEndpoint()}};
resources.forEach(resource => {
resp.details[resource.name] = resource.value;
});
console.log('sending notify message ' + JSON.stringify(resp));
return utils.createJsonResponse(JSON.stringify(resp));
}
}
function formImpactResponseFromJson(decodeCtx, resources, success=true) {
if(decodeCtx.getRequest() != null) {
var corrId = decodeCtx.getRequest().getCorrelatorId();
var resp = {responseCode: success? 0: 1, requestStatus: 0, response: [], correlatorId: corrId};
var resourceObj = {"device/0/endPointClientName":client.getEndpoint()};
resources.forEach(resource => {
resourceObj[resource.name] = resource.value;
});
resp.response.push(resourceObj);
console.log('sending read response ' + JSON.stringify(resp));
return utils.createJsonResponse(JSON.stringify(resp));
} else {
throw new Error (`no corresponding request found`);
}
}
function formImpactNotifyFromJson(decodeCtx, resources) {
var resp = {details: {"device/0/endPointClientName": client.getEndpoint()}};
resources.forEach(resource => {
resp.details[resource.name] = resource.value;
});
console.log('sending notify response ' + JSON.stringify(resp));
return utils.createJsonResponse(JSON.stringify(resp));
}