/// <summary> /// Invoke a method on a module. /// </summary> /// <param name="deviceId">The unique identifier of the device.</param> /// <param name="moduleId">The unique identifier of the module identity to invoke the method on.</param> /// <param name="directMethodRequest">The details of the method to invoke.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The result of the method invocation and the http response <see cref="Response{T}"/>.</returns> public virtual Response <CloudToDeviceMethodResponse> InvokeMethod( string deviceId, string moduleId, CloudToDeviceMethodRequest directMethodRequest, CancellationToken cancellationToken = default) { return(_deviceMethodClient.InvokeModuleMethod(deviceId, moduleId, directMethodRequest, cancellationToken)); }
/// <summary> /// Invoke a method on a module. /// </summary> /// <param name="deviceId">The unique identifier of the device.</param> /// <param name="moduleId">The unique identifier of the module identity to invoke the method on.</param> /// <param name="directMethodRequest">The details of the method to invoke.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The result of the method invocation and the http response <see cref="Response{T}"/>.</returns> public virtual Task <Response <CloudToDeviceMethodResponse> > InvokeMethodAsync( string deviceId, string moduleId, CloudToDeviceMethodRequest directMethodRequest, CancellationToken cancellationToken = default) { return(_modulesRestClient.InvokeMethodAsync(deviceId, moduleId, directMethodRequest, cancellationToken)); }
internal HttpMessage CreateInvokeDeviceMethodRequest(string deviceId, CloudToDeviceMethodRequest directMethodRequest) { var message = _pipeline.CreateMessage(); var request = message.Request; request.Method = RequestMethod.Post; var uri = new RawRequestUriBuilder(); uri.Reset(endpoint); uri.AppendPath("/twins/", false); uri.AppendPath(deviceId, true); uri.AppendPath("/methods", false); uri.AppendQuery("api-version", apiVersion, true); request.Uri = uri; request.Headers.Add("Content-Type", "application/json"); var content = new Utf8JsonRequestContent(); content.JsonWriter.WriteObjectValue(directMethodRequest); request.Content = content; return(message); }
public async Task ModulesClient_InvokeMethodOnModule() { if (!this.IsAsync) { // TODO: Tim: The module client doesn't appear to open a connection to iothub or start // listening for method invocations when this test is run in Sync mode. Not sure why though. // calls to track 1 library don't throw, but seem to silently fail return; } string testDeviceId = $"InvokeMethodDevice{GetRandom()}"; string testModuleId = $"InvokeMethodModule{GetRandom()}"; DeviceIdentity device = null; ModuleIdentity module = null; ModuleClient moduleClient = null; IotHubServiceClient serviceClient = GetClient(); try { // Create a device to house the modules device = (await serviceClient.Devices .CreateOrUpdateIdentityAsync( new DeviceIdentity { DeviceId = testDeviceId }) .ConfigureAwait(false)) .Value; // Create the module on the device module = (await serviceClient.Modules.CreateOrUpdateIdentityAsync( new ModuleIdentity { DeviceId = testDeviceId, ModuleId = testModuleId }).ConfigureAwait(false)).Value; // Method expectations string expectedMethodName = "someMethodToInvoke"; int expectedStatus = 222; object expectedRequestPayload = null; // Create module client instance to receive the method invocation string moduleClientConnectionString = $"HostName={GetHostName()};DeviceId={testDeviceId};ModuleId={testModuleId};SharedAccessKey={module.Authentication.SymmetricKey.PrimaryKey}"; moduleClient = ModuleClient.CreateFromConnectionString(moduleClientConnectionString, TransportType.Mqtt_Tcp_Only); // These two methods are part of our track 1 device client. When the test fixture runs when isAsync = true, // these methods work. When isAsync = false, these methods silently don't work. await moduleClient.OpenAsync().ConfigureAwait(false); await moduleClient.SetMethodHandlerAsync( expectedMethodName, (methodRequest, userContext) => { return(Task.FromResult(new MethodResponse(expectedStatus))); }, null).ConfigureAwait(false); // Invoke the method on the module CloudToDeviceMethodRequest methodRequest = new CloudToDeviceMethodRequest() { MethodName = expectedMethodName, Payload = expectedRequestPayload, ConnectTimeoutInSeconds = 5, ResponseTimeoutInSeconds = 5 }; var methodResponse = (await serviceClient.Modules.InvokeMethodAsync(testDeviceId, testModuleId, methodRequest).ConfigureAwait(false)).Value; Assert.AreEqual(expectedStatus, methodResponse.Status); } finally { if (moduleClient != null) { await moduleClient.CloseAsync().ConfigureAwait(false); } await CleanupAsync(serviceClient, device).ConfigureAwait(false); } }
public static JobResponse JobResponse(string jobId = default, string queryCondition = default, DateTimeOffset?createdTime = default, DateTimeOffset?startTime = default, DateTimeOffset?endTime = default, long?maxExecutionTimeInSeconds = default, JobResponseType?type = default, CloudToDeviceMethodRequest cloudToDeviceMethod = default, TwinData updateTwin = default, JobResponseStatus?status = default, string failureReason = default, string statusMessage = default, DeviceJobStatistics deviceJobStatistics = default) { return(new JobResponse(jobId, queryCondition, createdTime, startTime, endTime, maxExecutionTimeInSeconds, type, cloudToDeviceMethod, updateTwin, status, failureReason, statusMessage, deviceJobStatistics)); }
public async Task <Response <CloudToDeviceMethodResponse> > InvokeDeviceMethodAsync(string deviceId, CloudToDeviceMethodRequest directMethodRequest, CancellationToken cancellationToken = default) { if (deviceId == null) { throw new ArgumentNullException(nameof(deviceId)); } if (directMethodRequest == null) { throw new ArgumentNullException(nameof(directMethodRequest)); } using var message = CreateInvokeDeviceMethodRequest(deviceId, directMethodRequest); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { CloudToDeviceMethodResponse value = default; using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); if (document.RootElement.ValueKind == JsonValueKind.Null) { value = null; } else { value = CloudToDeviceMethodResponse.DeserializeCloudToDeviceMethodResponse(document.RootElement); } return(Response.FromValue(value, message.Response)); }