Beispiel #1
0
        public static PSDeviceTracing GetDeviceTracing(string DeviceId, Twin deviceTwin)
        {
            PSDeviceTracing psDeviceTracing = new PSDeviceTracing
            {
                DeviceId      = DeviceId,
                TracingOption = new PSDistributedTracing()
                {
                    SamplingMode = PSDistributedTracingSamplingMode.Disabled, SamplingRate = 0
                },
                IsSynced = false
            };

            if (deviceTwin.Properties.Desired.Contains(TracingProperty))
            {
                psDeviceTracing.TracingOption = JsonConvert.DeserializeObject <PSDistributedTracing>(deviceTwin.Properties.Desired[TracingProperty].ToString());
            }

            if (deviceTwin.Properties.Reported.Contains(TracingProperty))
            {
                PSDistributedTracing psReportedDistributedTracing = JsonConvert.DeserializeObject <PSDistributedTracing>(deviceTwin.Properties.Reported[TracingProperty].ToString());
                if (psReportedDistributedTracing != null)
                {
                    psDeviceTracing.IsSynced = (psDeviceTracing.TracingOption.SamplingMode.Equals(psReportedDistributedTracing.SamplingMode) && psDeviceTracing.TracingOption.SamplingRate.Equals(psReportedDistributedTracing.SamplingRate));
                }
            }

            return(psDeviceTracing);
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DeviceId, Properties.Resources.SetIotHubDistributedTracing))
            {
                IotHubDescription iotHubDescription;
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName = this.InputObject.Resourcegroup;
                    this.IotHubName        = this.InputObject.Name;
                    iotHubDescription      = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject);
                }
                else
                {
                    if (ParameterSetName.Equals(ResourceIdParameterSet))
                    {
                        this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
                        this.IotHubName        = IotHubUtils.GetIotHubName(this.ResourceId);
                    }

                    iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName);
                }

                IEnumerable <SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName);
                SharedAccessSignatureAuthorizationRule policy     = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite);
                PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
                RegistryManager          registryManager          = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);

                PSDistributedTracing psDistributedTracing = new PSDistributedTracing();
                Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult();

                IotHubDataPlaneUtils.ValidateDeviceTracing(this.DeviceId, iotHubDescription.Sku.Tier.Value.ToString(), iotHubDescription.Location, deviceTwin.Capabilities.IotEdge);

                if (deviceTwin.Properties.Desired.Contains(IotHubDataPlaneUtils.TracingProperty))
                {
                    psDistributedTracing = JsonConvert.DeserializeObject <PSDistributedTracing>(deviceTwin.Properties.Desired[IotHubDataPlaneUtils.TracingProperty].ToString());
                }

                psDistributedTracing.SamplingMode = this.SamplingMode;

                if (this.SamplingMode.Equals(PSDistributedTracingSamplingMode.Enabled))
                {
                    if (this.IsParameterBound(c => c.SamplingRate))
                    {
                        psDistributedTracing.SamplingRate = this.SamplingRate;
                    }
                }

                deviceTwin.Properties.Desired[IotHubDataPlaneUtils.TracingProperty] = psDistributedTracing;

                deviceTwin = registryManager.UpdateTwinAsync(this.DeviceId, deviceTwin, deviceTwin.ETag).GetAwaiter().GetResult();

                this.WriteObject(IotHubDataPlaneUtils.GetDeviceTracing(this.DeviceId, deviceTwin));
            }
        }