lib/client/proxy.js
/**
     * construct a callable method
     *
     * @param obj
     * @param reference
     * @param callback
     */
construct a callable method
@param obj
@param reference
@param callback
construct a callable method @param obj @param reference @param callback
function add_method(obj, reference, callback) {
        var name = lowerFirstLetter(reference.browseName.name);
        obj[name] = function functionCaller(inputArgs, callback) {
            assert(_.isFunction(callback));
            var inputArgsDef = obj[name].inputArguments;
            var inputArguments = inputArgsDef.map(function (arg) {
                var dataType = convertNodeIdToDataType(arg.dataType);
                var arrayType = ( arg.valueRank === 1) ? VariantArrayType.Array : VariantArrayType.Scalar;
                var propName = lowerFirstLetter(arg.name);
                var value = inputArgs[propName];
                if (value === undefined) {
                    throw new Error("expecting input argument " + propName);
                }
                if (arrayType === VariantArrayType.Array) {
                    if (!_.isArray(value)) {
                        throw new Error("expecting value to be an Array or a TypedArray");
                    }
                }
                return new Variant({arrayType: arrayType, dataType: dataType, value: value});
            });
            var methodToCall = new call_service.CallMethodRequest({
                objectId: obj.nodeId,
                methodId: reference.nodeId,
                inputArguments: inputArguments
            });
            var methodsToCall = [methodToCall];
            session.call(methodsToCall, function (err, result ) {
                if (err) {
                    return callback(err);
                }
                if (result[0].statusCode !== StatusCodes.Good) {
                    return callback(new Error("Error " + result[0].statusCode.toString()));
                }
                assert(result[0].outputArguments.length === obj[name].outputArguments.length);
                var outputArgs = {};
                var outputArgsDef = obj[name].outputArguments;
                _.zip(outputArgsDef, result[0].outputArguments).forEach(function (pair) {
                    var arg = pair[0];
                    var variant = pair[1];
                    var propName = lowerFirstLetter(arg.name);
                    outputArgs[propName] = variant.value;
                });
                callback(err, outputArgs);
            });
        };
        function extractDataType(arg, callback) {
            if (arg.dataType._dataType) {
                return callback(); 
            }
            convertNodeIdToDataTypeAsync(session, arg.dataType, function (err, dataType) {
                if (!err) {
                    assert(dataType.hasOwnProperty("value"));
                    arg.dataType._dataType = dataType;
                }
                callback(err);
            });
        }
        session.getArgumentDefinition(reference.nodeId, function (err, inputArguments, outputArguments) {
            if (err) {
                return callback(err);
            }
            obj[name].inputArguments = inputArguments;
            obj[name].outputArguments = outputArguments;
            async.series([
                function (callback) {
                    async.each(obj[name].inputArguments, extractDataType, callback);
                },
                function (callback) {
                    async.each(obj[name].outputArguments, extractDataType, callback);
                }
            ], callback)
        });
        var methodObj = {
            nodeId: reference.nodeId,
            executableFlag: false,
            browseName: name,
            func: obj[name]
        };
        obj.$methods[name] = methodObj;
        proxyManager._monitor_execution_flag(methodObj, function () {
        });
    }
function add_method ( obj , reference , callback ) { var name = lowerFirstLetter ( reference . browseName . name ) ; obj [ name ] = function functionCaller ( inputArgs , callback ) { assert ( _ . isFunction ( callback ) ) ; var inputArgsDef = obj [ name ] . inputArguments ; var inputArguments = inputArgsDef . map ( function ( arg ) { var dataType = convertNodeIdToDataType ( arg . dataType ) ; var arrayType = ( arg . valueRank === 1 ) ? VariantArrayType . Array : VariantArrayType . Scalar ; var propName = lowerFirstLetter ( arg . name ) ; var value = inputArgs [ propName ] ; if ( value === undefined ) { throw new Error ( "expecting input argument " + propName ) ; } if ( arrayType === VariantArrayType . Array ) { if ( ! _ . isArray ( value ) ) { throw new Error ( "expecting value to be an Array or a TypedArray" ) ; } } return new Variant ( { arrayType : arrayType , dataType : dataType , value : value } ) ; } ) ; var methodToCall = new call_service . CallMethodRequest ( { objectId : obj . nodeId , methodId : reference . nodeId , inputArguments : inputArguments } ) ; var methodsToCall = [ methodToCall ] ; session . call ( methodsToCall , function ( err , result ) { if ( err ) { return callback ( err ) ; } if ( result [ 0 ] . statusCode !== StatusCodes . Good ) { return callback ( new Error ( "Error " + result [ 0 ] . statusCode . toString ( ) ) ) ; } assert ( result [ 0 ] . outputArguments . length === obj [ name ] . outputArguments . length ) ; var outputArgs = { } ; var outputArgsDef = obj [ name ] . outputArguments ; _ . zip ( outputArgsDef , result [ 0 ] . outputArguments ) . forEach ( function ( pair ) { var arg = pair [ 0 ] ; var variant = pair [ 1 ] ; var propName = lowerFirstLetter ( arg . name ) ; outputArgs [ propName ] = variant . value ; } ) ; callback ( err , outputArgs ) ; } ) ; } ; function extractDataType ( arg , callback ) { if ( arg . dataType . _dataType ) { return callback ( ) ; } convertNodeIdToDataTypeAsync ( session , arg . dataType , function ( err , dataType ) { if ( ! err ) { assert ( dataType . hasOwnProperty ( "value" ) ) ; arg . dataType . _dataType = dataType ; } callback ( err ) ; } ) ; } session . getArgumentDefinition ( reference . nodeId , function ( err , inputArguments , outputArguments ) { if ( err ) { return callback ( err ) ; } obj [ name ] . inputArguments = inputArguments ; obj [ name ] . outputArguments = outputArguments ; async . series ( [ function ( callback ) { async . each ( obj [ name ] . inputArguments , extractDataType , callback ) ; } , function ( callback ) { async . each ( obj [ name ] . outputArguments , extractDataType , callback ) ; } ] , callback ) } ) ; var methodObj = { nodeId : reference . nodeId , executableFlag : false , browseName : name , func : obj [ name ] } ; obj . $methods [ name ] = methodObj ; proxyManager . _monitor_execution_flag ( methodObj , function ( ) { } ) ; }
construct a callable method
 @param obj
 @param reference
 @param callback
construct a callable method @param obj @param reference @param callback
// convert input arguments into Variants //xx console.log("xxx ",arg.toString()); //xx console.log(" calling ",methodToCall.toString()); /*, diagInfo */ // istanbul ignore next // already converted // istanbul ignore next