Operation JavaScript API

Connector functions - Active Operation JS API guide

This API allows users to read and active operations from a connector function.

operation – Main Object

The operation is the main object. It allows to do request of the Operation-API.

To do the request, the object operation use the HTTP-Client API, you can use all attribute of this interface, for example, to add a certificate http.client.certificate=XXX

operation – Object Properties

Property Type Description
deviceId string Target device id of the operation pending
apiKey string Api-Key to use in the request to the Operations-API
host string Host to use in the request to the Operations-API

By default, the attributes will be setted with the value of the context.

operation – Functions

operation.getAllPending()

Read and return the selected device operations pending (with status WAITING_FOR_CONNECTION) of the user to execute the CFx

This function does not require parameters.

Function operation.getAllPending return object, descript like:

Property Type Attributes Description
error null or string Message Description of the exception error caught, or error sent by the request. will be null, when the request no contains errors
opResult Object statusCode, Object Contains statusCode, and the result list object of the request, when it’s OK

Example of use with default values:

var opResult = operation.getAllPending();
opResult – Object Functions

opResult is a JSON List Object returned by the operation.getAllPending function, you can use the next functions:

Function Return Description
activate null or string Active the target operation. Return null whe the activation is OK, in other case return the cause error message
getRequest Object Build and return the request to send of the device to response the operation

The object -opResult- contains all attributes and functions of the response (operation) object.

activate()

Activate the target operation selected with parameters of the context (Update to IN_PROGRESS the operation)

This function does not require parameters.

Example of use with default values:

var opResult = operation.getAllPending();
if (opResult.error) {
    return error;
}
opResult.forEach(op => op.activate());
getRequest()

Build and return the request to send of the device to response the operation

This function does not require parameters.

reqRes – Object return the getRequest function

reqRes is a JSON Object returned by the getRequest function, descript like:

Property Return Description
operation Object Main objet to do the request

– Attributes of operation

Property Return Description
request Object Secondary main objet to do the request

– Attributes of request

Property Return Description
name Object Operation name target of the request
id Object Operation identifier target of the request
parameters Object Parameters to use in the request
timestamp Object Operation time on do the request

By default, the attributes will be set with the value of the target operation.

Example getRequest object return

{ 
    'operation': {
        'request': {
            'name': 'opName',
            'id': 'ce02792a-3a37-11f0-a48d-52540044ee01',
            'parameters': {},
            'timestamp': Date.now()
        }
    }
}

Example of use getRequest function:

var opResult = operation.getAllPending();
if (opResult.error) {
    return error;
}
opResult.forEach(op => {
        op.activate();
        http.client.body = op.getRequest();
        var httpResp = http.client.post();
        if (httpResp.statusCode != 201) {
            throw Error('Unexpected response:', httpResp);
        }
    }
);