Esempio n. 1
0
        /// <summary>
        /// MethodCallAsync
        /// </summary>
        /// <param name="parameterValues"></param>
        /// <param name="nodeId"></param>
        /// <returns>Status</returns>
        public async Task <string> MethodCallAsync(MethodMetadataResponseApiModel parameters, string[] parameterValues,
                                                   string endpointId, string nodeId, CredentialModel credential = null)
        {
            var argumentsList = new List <MethodCallArgumentApiModel>();
            var model         = new MethodCallRequestApiModel()
            {
                MethodId = nodeId,
                ObjectId = parameters.ObjectId
            };

            model.Header = Elevate(new RequestHeaderApiModel(), credential);

            try {
                var count = 0;
                foreach (var item in parameters.InputArguments)
                {
                    var argument = new MethodCallArgumentApiModel {
                        Value    = parameterValues[count] ?? string.Empty,
                        DataType = item.Type.DataType
                    };
                    argumentsList.Add(argument);
                    count++;
                }
                model.Arguments = argumentsList;

                MethodCallResponse = await _twinService.NodeMethodCallAsync(endpointId, model);

                if (MethodCallResponse.ErrorInfo == null)
                {
                    return(null);
                }
                else
                {
                    if (MethodCallResponse.ErrorInfo.Diagnostics != null)
                    {
                        return(MethodCallResponse.ErrorInfo.Diagnostics.ToString());
                    }
                    else
                    {
                        return(MethodCallResponse.ErrorInfo.ToString());
                    }
                }
            }
            catch (Exception e) {
                _logger.Error($"Can not get method parameter from node '{nodeId}'");
                var errorMessage = string.Concat(e.Message, e.InnerException?.Message ?? "--", e?.StackTrace ?? "--");
                _logger.Error(errorMessage);
                string error = JToken.Parse(e.Message).ToString(Formatting.Indented);
                if (error.Contains(StatusCodes.Status401Unauthorized.ToString()))
                {
                    errorMessage = "Unauthorized access: Bad User Access Denied.";
                }
                else
                {
                    errorMessage = error;
                }
                return(errorMessage);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Call
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <MethodCallResponseApiModel> MethodCallAsync(
            MethodCallRequestApiModel request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            var result = await _nodes.NodeMethodCallAsync(
                _twin.Endpoint, request.ToServiceModel());

            return(new MethodCallResponseApiModel(result));
        }
Esempio n. 3
0
        public async Task <MethodCallResponseApiModel> CallMethodAsync(
            string endpointId, [FromBody][Required] MethodCallRequestApiModel request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // TODO: Permissions

            var callresult = await _nodes.NodeMethodCallAsync(
                endpointId, request.ToServiceModel());

            return(callresult.ToApiModel());
        }
Esempio n. 4
0
        /// <summary>
        /// MethodCallAsync
        /// </summary>
        /// <param name="parameterValues"></param>
        /// <param name="nodeId"></param>
        /// <returns>Status</returns>
        public async Task <string> MethodCallAsync(MethodMetadataResponseApiModel parameters, string[] parameterValues,
                                                   string endpointId, string nodeId)
        {
            var argumentsList = new List <MethodCallArgumentApiModel>();
            var model         = new MethodCallRequestApiModel()
            {
                MethodId = nodeId,
                ObjectId = parameters.ObjectId
            };

            try {
                var count = 0;
                foreach (var item in parameters.InputArguments)
                {
                    var argument = new MethodCallArgumentApiModel {
                        Value    = parameterValues[count] ?? string.Empty,
                        DataType = item.Type.DataType
                    };
                    argumentsList.Add(argument);
                    count++;
                }
                model.Arguments = argumentsList;

                MethodCallResponse = await _twinService.NodeMethodCallAsync(endpointId, model);

                if (MethodCallResponse.ErrorInfo == null)
                {
                    return(null);
                }
                else
                {
                    if (MethodCallResponse.ErrorInfo.Diagnostics != null)
                    {
                        return(MethodCallResponse.ErrorInfo.Diagnostics.ToString());
                    }
                    else
                    {
                        return(MethodCallResponse.ErrorInfo.ToString());
                    }
                }
            }
            catch (Exception e) {
                Trace.TraceError("Can not get method parameter from node '{0}'", nodeId);
                var errorMessage = string.Concat(e.Message, e.InnerException?.Message ?? "--", e?.StackTrace ?? "--");
                Trace.TraceError(errorMessage);
                return(errorMessage);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Convert back to service model
 /// </summary>
 /// <returns></returns>
 public static MethodCallRequestModel ToServiceModel(
     this MethodCallRequestApiModel model)
 {
     if (model == null)
     {
         return(null);
     }
     return(new MethodCallRequestModel {
         MethodId = model.MethodId,
         ObjectId = model.ObjectId,
         MethodBrowsePath = model.MethodBrowsePath,
         ObjectBrowsePath = model.ObjectBrowsePath,
         Arguments = model.Arguments?
                     .Select(s => s.ToServiceModel()).ToList(),
         Header = model.Header.ToServiceModel()
     });
 }
        /// <inheritdoc/>
        public async Task <MethodCallResponseApiModel> NodeMethodCallAsync(
            string endpointId, MethodCallRequestApiModel content, CancellationToken ct)
        {
            if (string.IsNullOrEmpty(endpointId))
            {
                throw new ArgumentNullException(nameof(endpointId));
            }
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            var request = _httpClient.NewRequest($"{_serviceUri}/v2/call/{endpointId}",
                                                 _resourceId);

            request.SetContent(content);
            var response = await _httpClient.PostAsync(request, ct).ConfigureAwait(false);

            response.Validate();
            return(response.GetContent <MethodCallResponseApiModel>());
        }
        /// <inheritdoc/>
        public async Task <MethodCallResponseApiModel> NodeMethodCallAsync(
            EndpointApiModel endpoint, MethodCallRequestApiModel request, CancellationToken ct)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (string.IsNullOrEmpty(endpoint.Url))
            {
                throw new ArgumentNullException(nameof(endpoint.Url));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            var response = await _methodClient.CallMethodAsync(_deviceId, _moduleId,
                                                               "MethodCall_V2", _serializer.SerializeToString(new {
                endpoint,
                request
            }), null, ct);

            return(_serializer.Deserialize <MethodCallResponseApiModel>(response));
        }
        public async Task <ActionResult> MethodCall(string jstreeNode, string parameterData, string parameterValues, Session session = null)
        {
            string[] delimiter       = { "__$__" };
            string[] jstreeNodeSplit = jstreeNode.Split(delimiter, 3, StringSplitOptions.None);
            string   node;
            string   parentNode = null;
            string   endpointId = Session["EndpointId"].ToString();

            if (jstreeNodeSplit.Length == 1)
            {
                node       = jstreeNodeSplit[0];
                parentNode = null;
            }
            else
            {
                node       = jstreeNodeSplit[1];
                parentNode = (jstreeNodeSplit[0].Replace(delimiter[0], "")).Replace("__", "");
            }

            string actionResult = "";
            List <MethodCallParameterData> originalData = JsonConvert.DeserializeObject <List <MethodCallParameterData> >(parameterData);
            List <Variant> values = JsonConvert.DeserializeObject <List <Variant> >(parameterValues);
            List <MethodCallArgumentApiModel> argumentsList = new List <MethodCallArgumentApiModel>();

            try
            {
                MethodCallRequestApiModel model = new MethodCallRequestApiModel();
                model.MethodId = node;
                model.ObjectId = originalData[0].ObjectId;

                if (originalData.Count > 1)
                {
                    int count = 0;
                    foreach (var item in originalData)
                    {
                        MethodCallArgumentApiModel argument = new MethodCallArgumentApiModel();
                        argument.Value    = values[count].Value != null ? values[count].Value.ToString() : string.Empty;
                        argument.DataType = item.Datatype;
                        argumentsList.Add(argument);
                        count++;
                    }
                    model.Arguments = argumentsList;
                }

                var data = await TwinService.NodeMethodCallAsync(endpointId, model);

                if (data.ErrorInfo != null)
                {
                    actionResult = Strings.BrowserOpcMethodCallFailed + @"<br/><br/>" +
                                   Strings.BrowserOpcMethodStatusCode + ": " + data.ErrorInfo.StatusCode;
                    if (data.ErrorInfo.Diagnostics != null)
                    {
                        actionResult += @"<br/><br/>" + Strings.BrowserOpcDataDiagnosticInfoLabel + ": " + data.ErrorInfo.Diagnostics;
                    }
                }
                else
                {
                    if (data.Results.Count == 0)
                    {
                        actionResult = Strings.BrowserOpcMethodCallSucceeded;
                    }
                    else
                    {
                        actionResult = Strings.BrowserOpcMethodCallSucceededWithResults + @"<br/><br/>";
                        for (int ii = 0; ii < data.Results.Count; ii++)
                        {
                            actionResult += data.Results[ii].Value + @"<br/>";
                        }
                    }
                }
                return(Content(actionResult));
            }
            catch (Exception exception)
            {
                return(Content(CreateOpcExceptionActionString(exception)));
            }
        }
 /// <summary>
 /// Call a method
 /// </summary>
 /// <remarks>
 /// Invoke method node with specified input arguments. The endpoint must be
 /// activated and connected and the module client and server must trust each
 /// other.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='endpointId'>
 /// The identifier of the activated endpoint.
 /// </param>
 /// <param name='body'>
 /// The method call request
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <MethodCallResponseApiModel> CallMethodAsync(this IAzureOpcTwinClient operations, string endpointId, MethodCallRequestApiModel body, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CallMethodWithHttpMessagesAsync(endpointId, body, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 /// <summary>
 /// Call a method
 /// </summary>
 /// <remarks>
 /// Invoke method node with specified input arguments. The endpoint must be
 /// activated and connected and the module client and server must trust each
 /// other.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='endpointId'>
 /// The identifier of the activated endpoint.
 /// </param>
 /// <param name='body'>
 /// The method call request
 /// </param>
 public static MethodCallResponseApiModel CallMethod(this IAzureOpcTwinClient operations, string endpointId, MethodCallRequestApiModel body)
 {
     return(operations.CallMethodAsync(endpointId, body).GetAwaiter().GetResult());
 }
Esempio n. 11
0
 /// <inheritdoc/>
 public Task <MethodCallResponseApiModel> NodeMethodCallAsync(
     string endpointId, MethodCallRequestApiModel content, CancellationToken ct)
 {
     return(Task.FromException <MethodCallResponseApiModel>(new NotImplementedException()));
 }
Esempio n. 12
0
 /// <summary>
 /// Call method on endpoint
 /// </summary>
 /// <param name="api"></param>
 /// <param name="endpointUrl"></param>
 /// <param name="request"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 public static Task <MethodCallResponseApiModel> NodeMethodCallAsync(
     this ITwinModuleApi api, string endpointUrl, MethodCallRequestApiModel request,
     CancellationToken ct = default)
 {
     return(api.NodeMethodCallAsync(NewEndpoint(endpointUrl), request, ct));
 }
        public MethodCallResponseApiModel NodeMethodCall(string endpoint, MethodCallRequestApiModel content)
        {
            Task <MethodCallResponseApiModel> t = Task.Run(() => _twinServiceHandler.NodeMethodCallAsync(endpoint, content));

            return(t.Result);
        }
        public async Task <MethodCallResponseApiModel> NodeMethodCallAsync(string endpoint, MethodCallRequestApiModel content)
        {
            var applications = await _twinServiceHandler.NodeMethodCallAsync(endpoint, content).ConfigureAwait(false);

            return(applications);
        }