public Task <DirectMethodResponse> InvokeMethod(DirectMethodRequest methodRequest) { Preconditions.CheckNotNull(methodRequest, nameof(methodRequest)); // Check if a device Proxy is available with the right subscriptions // If so, then invoke the method on the deviceProxy right away. // If not, then add this method invocation to a list. If the client connects // and subscribes to methods invocations before the method invocation expires, // then invoke it, else return a 404 Option <IDeviceProxy> deviceProxy = this.GetDeviceProxyWithSubscription(methodRequest.Id); return(deviceProxy.Map( d => { Events.InvokingMethod(methodRequest); return d.InvokeMethodAsync(methodRequest); }) .GetOrElse( async() => { var taskCompletion = new TaskCompletionSource <DirectMethodResponse>(); ConcurrentDictionary <DirectMethodRequest, TaskCompletionSource <DirectMethodResponse> > clientQueue = this.GetClientQueue(methodRequest.Id); clientQueue.TryAdd(methodRequest, taskCompletion); Task completedTask = await Task.WhenAny(taskCompletion.Task, Task.Delay(methodRequest.ConnectTimeout)); if (completedTask != taskCompletion.Task && clientQueue.TryRemove(methodRequest, out taskCompletion)) { taskCompletion.TrySetResult(new DirectMethodResponse(new EdgeHubTimeoutException($"Client {methodRequest.Id} not found"), HttpStatusCode.NotFound)); } return await taskCompletion.Task; })); }
internal async Task <DirectMethodResponse> HandleMethodInvocation(DirectMethodRequest request) { Preconditions.CheckNotNull(request, nameof(request)); if (request.Name.Equals(Constants.ServiceIdentityRefreshMethodName, StringComparison.OrdinalIgnoreCase)) { RefreshRequest refreshRequest; try { refreshRequest = request.Data.FromBytes <RefreshRequest>(); } catch (Exception e) { Events.ErrorParsingMethodRequest(e); return(new DirectMethodResponse(e, HttpStatusCode.BadRequest)); } try { await this.deviceScopeIdentitiesCache.RefreshServiceIdentities(refreshRequest.DeviceIds); Events.RefreshedServiceIdentities(refreshRequest.DeviceIds); return(new DirectMethodResponse(request.CorrelationId, null, (int)HttpStatusCode.OK)); } catch (Exception e) { Events.ErrorRefreshingServiceIdentities(e); return(new DirectMethodResponse(e, HttpStatusCode.InternalServerError)); } } else { Events.InvalidMethodRequest(request.Name); return(new DirectMethodResponse(new InvalidOperationException($"Method {request.Name} is not supported"), HttpStatusCode.NotFound)); } }
public Task <DirectMethodResponse> InvokeMethodAsync(DirectMethodRequest request) => this.edgeHubConnection.HandleMethodInvocation(request);
public Task <DirectMethodResponse> CallMethodAsync(DirectMethodRequest request) { throw new NotImplementedException(); }