Encoder
In this section the term argument is used, this is very similar to a parameter and is used to pass data between functions.
Input:
String getResource()
- Returns the URL to the requested device resource.String getValue()
- Returns the request data (if any).
String getRequestType(ImpactRequest impactRequest)
- Returns the request type, which can be READ, WRITE, EXECUTE, OBSERVE, DELETE
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
function CodecName() {
this.encode = function (impactRequest) {
var type = utils.getRequestType(impactRequest);
var url = impactRequest.getResource();
if (impactRequest.getValue) { // get request data (if defined)
request.resourceValue = impactRequest.getValue();
}
// ...
In the preceding code, the URL for the requested device resource is obtained using the getResource() function.
Output:
-
createBinaryResponse( byte[] bytes, Map<String, Object> scriptCtx, boolean onewaymessaging)
-
createJsonResponse( String json, Map<String, Object> scriptCtx, boolean onewaymessaging)
-
createStringResponse( String str, Map<String, Object> scriptCtx, boolean onewaymessaging)
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
function CodecName() {
this.encode = function (impactRequest) {
return utils.createBinaryResponse(resBuffer, _map, false);
}
// ...
In the preceding code, the encode function returns the encoded payload as a binary byte
array in the first argument to createBinaryResponse
. The second
argument is the script context by name _map. The third argument is a boolean flag
indicating whether this downlink is a one-way messaging. For example: the
READ
operation is a two-way messaging for which the device will
send a response. Operations, such as WRITE
, EXECUTE
,
OBSERVE
, and DELETE
will be considered one-way if
the device does not send the acknowledgement. The third argument (one-way messaging)
equal to false indicates the device will respond to this downlink request.
You can use createJsonResponse
or createStringResponse
if the encoded payload is plain text or JSON.
Processing:
The processing of input request and producing the encoded payload is specific to the protocol specification of the device. Refer to Sample scripts section.
If there is any error while encoding the request, the script can throw an exception as shown in the following code snippet. In this case, the error is recorded in the - transformation gateway pod log.
function CodecName() {
this.encode = function (impactRequest) {
if (encoding_failure) {
throw new Error(`request type ${request.type} not supported`);
}
// ...