|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +namespace Microsoft.Azure.Commands.Management.IotHub |
| 16 | +{ |
| 17 | + using System; |
| 18 | + using System.Collections.Generic; |
| 19 | + using System.Management.Automation; |
| 20 | + using Microsoft.Azure.Commands.Management.IotHub.Common; |
| 21 | + using Microsoft.Azure.Commands.Management.IotHub.Models; |
| 22 | + using Microsoft.Azure.Devices; |
| 23 | + using Microsoft.Azure.Management.IotHub; |
| 24 | + using Microsoft.Azure.Management.IotHub.Models; |
| 25 | + using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 26 | + using ResourceManager.Common.ArgumentCompleters; |
| 27 | + |
| 28 | + [Cmdlet("Invoke", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceMethod", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)] |
| 29 | + [OutputType(typeof(PSCloudToDeviceMethodResult))] |
| 30 | + public class InvokeAzIotHubDeviceMethod : IotHubBaseCmdlet |
| 31 | + { |
| 32 | + private const string ResourceIdParameterSet = "ResourceIdSet"; |
| 33 | + private const string ResourceParameterSet = "ResourceSet"; |
| 34 | + private const string InputObjectParameterSet = "InputObjectSet"; |
| 35 | + |
| 36 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")] |
| 37 | + [ValidateNotNullOrEmpty] |
| 38 | + public PSIotHub InputObject { get; set; } |
| 39 | + |
| 40 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")] |
| 41 | + [ValidateNotNullOrEmpty] |
| 42 | + [ResourceIdCompleter("Microsoft.Devices/IotHubs")] |
| 43 | + public string ResourceId { get; set; } |
| 44 | + |
| 45 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")] |
| 46 | + [ValidateNotNullOrEmpty] |
| 47 | + [ResourceGroupCompleter] |
| 48 | + public string ResourceGroupName { get; set; } |
| 49 | + |
| 50 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")] |
| 51 | + [ValidateNotNullOrEmpty] |
| 52 | + public string IotHubName { get; set; } |
| 53 | + |
| 54 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Target Device Id.")] |
| 55 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Target Device Id.")] |
| 56 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Target Device Id.")] |
| 57 | + [ValidateNotNullOrEmpty] |
| 58 | + public string DeviceId { get; set; } |
| 59 | + |
| 60 | + [Parameter(Mandatory = true, HelpMessage = "The name of the method to invoke on this device.")] |
| 61 | + public string Name { get; set; } |
| 62 | + |
| 63 | + [Parameter(Mandatory = false, HelpMessage = "The payload for the method to invoke on this device.")] |
| 64 | + public string Payload { get; set; } |
| 65 | + |
| 66 | + [Parameter(Mandatory = false, HelpMessage = "Number of seconds to wait until a result is received from the direct method. Default is 10.")] |
| 67 | + public int ResponseTimeOut { get; set; } |
| 68 | + |
| 69 | + [Parameter(Mandatory = false, HelpMessage = "Number of seconds to wait until a connection is successfully made. Default is 10.")] |
| 70 | + [ValidateRange(10, 300)] |
| 71 | + public int ConnectionTimeOut { get; set; } |
| 72 | + |
| 73 | + public override void ExecuteCmdlet() |
| 74 | + { |
| 75 | + if (ShouldProcess(this.IotHubName, Properties.Resources.InvokeIotHubDeviceMethod)) |
| 76 | + { |
| 77 | + IotHubDescription iotHubDescription; |
| 78 | + if (ParameterSetName.Equals(InputObjectParameterSet)) |
| 79 | + { |
| 80 | + this.ResourceGroupName = this.InputObject.Resourcegroup; |
| 81 | + this.IotHubName = this.InputObject.Name; |
| 82 | + iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + if (ParameterSetName.Equals(ResourceIdParameterSet)) |
| 87 | + { |
| 88 | + this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); |
| 89 | + this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); |
| 90 | + } |
| 91 | + |
| 92 | + iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); |
| 93 | + } |
| 94 | + |
| 95 | + IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); |
| 96 | + SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.ServiceConnect); |
| 97 | + PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); |
| 98 | + ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); |
| 99 | + |
| 100 | + if (!this.IsParameterBound(c => c.ResponseTimeOut)) |
| 101 | + { |
| 102 | + this.ResponseTimeOut = 10; |
| 103 | + } |
| 104 | + |
| 105 | + if (!this.IsParameterBound(c => c.ConnectionTimeOut)) |
| 106 | + { |
| 107 | + this.ConnectionTimeOut = 10; |
| 108 | + } |
| 109 | + |
| 110 | + CloudToDeviceMethod method = new CloudToDeviceMethod(this.Name, new TimeSpan(0, 0, this.ResponseTimeOut), new TimeSpan(0, 0, this.ConnectionTimeOut)); |
| 111 | + if (this.IsParameterBound(c => c.Payload)) |
| 112 | + { |
| 113 | + method = method.SetPayloadJson(this.Payload); |
| 114 | + } |
| 115 | + |
| 116 | + CloudToDeviceMethodResult result = serviceClient.InvokeDeviceMethodAsync(this.DeviceId, method).GetAwaiter().GetResult(); |
| 117 | + PSCloudToDeviceMethodResult psCloudToDeviceMethodResult = new PSCloudToDeviceMethodResult() |
| 118 | + { |
| 119 | + Status = result.Status, |
| 120 | + Payload = result.GetPayloadAsJson() |
| 121 | + }; |
| 122 | + this.WriteObject(psCloudToDeviceMethodResult); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments