예제 #1
0
 public Device(string tenantId, string brand, string type, string hardwareId, DeviceRegistrationState deviceRegistrationState)
 {
     DeviceId   = Guid.NewGuid().ToString();
     TenantId   = tenantId;
     Brand      = brand;
     Type       = type;
     HardwareId = hardwareId;
     DeviceRegistrationState = deviceRegistrationState;
 }
예제 #2
0
        public override void ExecuteCmdlet()
        {
            ProvisioningServiceDescription provisioningServiceDescription;

            if (ParameterSetName.Equals(InputObjectParameterSet))
            {
                this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                this.DpsName                   = this.DpsObject.Name;
                provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
            }
            else
            {
                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
            }

            IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
            SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
            PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
            ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

            if ((!this.IsParameterBound(c => c.RegistrationId) && !this.IsParameterBound(c => c.EnrollmentId)) ||
                (this.IsParameterBound(c => c.RegistrationId) && this.IsParameterBound(c => c.EnrollmentId)))
            {
                throw new ArgumentException("Please provide either RegistrationId or EnrollmentId.");
            }

            if (this.IsParameterBound(c => c.RegistrationId))
            {
                DeviceRegistrationState result = client.GetDeviceRegistrationStateAsync(this.RegistrationId).GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSDeviceRegistrationState(result));
            }

            if (this.IsParameterBound(c => c.EnrollmentId))
            {
                string      query   = this.IsParameterBound(c => c.Query) ? this.Query : "select * from enrollments";
                QueryResult results = client.CreateEnrollmentGroupRegistrationStateQuery(new QuerySpecification(query), this.EnrollmentId).NextAsync().GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSDeviceRegistrationStates(results.Items), true);
            }
        }
예제 #3
0
        /// <summary>
        /// デバイス接続情報を取得する。
        /// </summary>
        /// <param name="edgeId">端末ID</param>
        /// <returns>デバイス接続情報を返す。対象デバイスが見つからない場合nullを返す。</returns>
        public async Task <DeviceConnectionInfo> GetDeviceConnectionInfoAsync(Guid edgeId)
        {
            DeviceConnectionInfo result = null;
            string connectionStringDps  = null;

            try
            {
                _logger.Enter($"{nameof(edgeId)}: {{0}}", new object[] { edgeId });

                // DPS接続文字列取得
                connectionStringDps = _appSettings.DpsConnectionString;
                if (string.IsNullOrWhiteSpace(connectionStringDps))
                {
                    throw new RmsInvalidAppSettingException("DpsConnectionString is required.");
                }

                string iotHubName = null;

                using (var service = ProvisioningServiceClient.CreateFromConnectionString(connectionStringDps))
                {
                    await _dpsPolly.ExecuteAsync(
                        async() =>
                    {
                        // 見つからなかったらProvisioningServiceClientHttpExceptionが発生して、
                        // その例外のStatusCodeにHttpStatusCode型のNotFoundになる。
                        // これ以外はとりあえず500にする。
                        DeviceRegistrationState deviceRegistrationState = await service.GetDeviceRegistrationStateAsync(edgeId.ToString());
                        iotHubName = deviceRegistrationState.AssignedHub;
                    });
                }

                string iotHubConnectionStringKey = string.Format(IoTHubNamePrefixOnAppSettings, iotHubName);

                // IoTHub接続文字列取得
                string iotHubConnectionStringValue = _appSettings.GetConnectionString(iotHubConnectionStringKey);
                if (string.IsNullOrWhiteSpace(iotHubConnectionStringValue))
                {
                    throw new RmsInvalidAppSettingException(string.Format("{0} is required.", iotHubConnectionStringKey));
                }

                result = new DeviceConnectionInfo()
                {
                    EdgeId = edgeId,
                    IotHubConnectionString = new KeyValuePair <string, string>(iotHubConnectionStringKey, iotHubConnectionStringValue),
                };
                return(result);
            }
            catch (RmsException)
            {
                throw;
            }
            catch (ArgumentException ex)
            {
                // 接続文字列がフォーマットに則っていない。
                throw new RmsInvalidAppSettingException("DpsConnectionString is invalid format.", ex);
            }
            catch (ProvisioningServiceClientHttpException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                // 対象デバイスが見つからない
                result = null;
                return(result);
            }
            catch (ProvisioningServiceClientHttpException ex) when(ex.StatusCode == HttpStatusCode.Unauthorized)
            {
                // 使用した接続文字列で認証が通らなかった
                throw new RmsException(string.Format("DPSで認証エラーが発生しました。({0})", connectionStringDps), ex);
            }
            catch (Exception ex)
            {
                throw new RmsException("接続先のIoT Hub", ex);
            }
            finally
            {
                _logger.LeaveJson("{0}", new { result });
            }
        }
예제 #4
0
 public static PSDeviceRegistrationState ToPSDeviceRegistrationState(DeviceRegistrationState registration)
 {
     return(ConvertObject <DeviceRegistrationState, PSDeviceRegistrationState>(registration));
 }