コード例 #1
0
        public async Task AddToNameCache(string deviceId)
        {
            var device = await this.GetDeviceAsync(deviceId);

            var twin = device.Twin;

            await _nameCacheLogic.AddNameAsync(nameof(device.Twin.DeviceId));

            await _nameCacheLogic.AddShortNamesAsync(
                NameCacheEntityType.Tag,
                twin.Tags
                .AsEnumerableFlatten()
                .Select(pair => pair.Key));

            await _nameCacheLogic.AddShortNamesAsync(
                NameCacheEntityType.DesiredProperty,
                twin.Properties.Desired
                .AsEnumerableFlatten()
                .Select(pair => pair.Key));

            await _nameCacheLogic.AddShortNamesAsync(
                NameCacheEntityType.ReportedProperty,
                twin.Properties.Reported
                .AsEnumerableFlatten()
                .Select(pair => pair.Key)
                .Where(name => !SupportedMethodsHelper.IsSupportedMethodProperty(name)));

            foreach (var command in device.Commands.Where(c => c.DeliveryType == DeliveryType.Method))
            {
                await _nameCacheLogic.AddMethodAsync(command);
            }
        }
コード例 #2
0
ファイル: DeviceBase.cs プロジェクト: MadhumiPerera/Second
        protected async Task UpdateReportedPropertiesAsync(TwinCollection reported, bool regenerate = false)
        {
            var patch = new TwinCollection();

            CrossSyncProperties(patch, reported, regenerate);
            SupportedMethodsHelper.CreateSupportedMethodReport(patch, Commands, reported);
            AddConfigs(patch);

            // Update ReportedProperties to IoT Hub
            await Transport.UpdateReportedPropertiesAsync(patch);
        }
コード例 #3
0
        private void UpdateNameCache(IEnumerable <Shared.Twin> twins)
        {
            // Reminder: None of the tasks updating the namecache need to be waited for completed

            var tags    = twins.GetNameList(twin => twin.Tags);
            var tagTask = _nameCacheLogic.AddShortNamesAsync(NameCacheEntityType.Tag, tags);

            var desiredProperties   = twins.GetNameList(twin => twin.Properties.Desired);
            var desiredPropertyTask = _nameCacheLogic.AddShortNamesAsync(NameCacheEntityType.DesiredProperty, desiredProperties);

            var reportedProperties = twins.GetNameList(twin => twin.Properties.Reported)
                                     .Where(name => !SupportedMethodsHelper.IsSupportedMethodProperty(name));
            var reportedPropertyTask = _nameCacheLogic.AddShortNamesAsync(NameCacheEntityType.ReportedProperty, reportedProperties);

            // No need to update Method here, since it will not change during device running
        }
コード例 #4
0
        public async Task <DeviceModel> UpdateDeviceFromDeviceInfoPacketAsync(DeviceModel device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            // Get original device document
            DeviceModel existingDevice = await this.GetDeviceAsync(device.IoTHub.ConnectionDeviceId);

            SupportedMethodsHelper.AddSupportedMethodsFromReportedProperty(device, existingDevice.Twin);

            // Save the command history, original created date, and system properties (if any) of the existing device
            if (existingDevice.DeviceProperties != null)
            {
                DeviceProperties deviceProperties = device.DeviceProperties;
                deviceProperties.CreatedTime    = existingDevice.DeviceProperties.CreatedTime;
                existingDevice.DeviceProperties = deviceProperties;
            }

            device.CommandHistory = existingDevice.CommandHistory;

            // Copy the existing system properties, or initialize them if they do not exist
            if (existingDevice.SystemProperties != null)
            {
                device.SystemProperties = existingDevice.SystemProperties;
            }
            else
            {
                device.SystemProperties = null;
            }
            // If there is Telemetry or Command objects from device, replace instead of merge
            if (device.Telemetry != null)
            {
                existingDevice.Telemetry = device.Telemetry;
            }
            if (device.Commands != null)
            {
                existingDevice.Commands = device.Commands;
            }


            return(await _deviceRegistryCrudRepository.UpdateDeviceAsync(existingDevice));
        }
コード例 #5
0
        // Copy values from twin to view model
        // All of the tags, desired and reported properties will be copy to view model. While the reported properties will be marked as non-editable
        //
        // Reminder: Only DeviceModel.Twin and cloud service configuration 'iotHub.HostName' will be read. . DeviceProperties and other properties of class DeviceModel will not be touched
        public override IEnumerable <DevicePropertyValueModel> ExtractDevicePropertyValuesModels(
            DeviceModel device)
        {
            string hostNameValue;
            IEnumerable <DevicePropertyValueModel> propValModels;

            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            var tags = device.Twin.Tags.AsEnumerableFlatten().OrderBy(pair => pair.Key).Select(pair => new DevicePropertyValueModel
            {
                DisplayOrder = 1,
                IsEditable   = true,
                IsIncludedWithUnregisteredDevices = false,
                Name         = FormattableString.Invariant($"tags.{pair.Key}"),
                PropertyType = GetObjectType(pair.Value.Value),
                Value        = pair.Value.Value.ToString()
            });

            var desiredProperties = device.Twin.Properties.Desired.AsEnumerableFlatten().OrderBy(pair => pair.Key).Select(pair => new DevicePropertyValueModel
            {
                DisplayOrder = 2,
                IsEditable   = true,
                IsIncludedWithUnregisteredDevices = false,
                Name           = FormattableString.Invariant($"properties.desired.{pair.Key}"),
                PropertyType   = GetObjectType(pair.Value.Value),
                Value          = pair.Value.Value.ToString(),
                LastUpdatedUtc = pair.Value.LastUpdated
            });

            var reportedProperties = device.Twin.Properties.Reported.AsEnumerableFlatten().Where(pair => !SupportedMethodsHelper.IsSupportedMethodProperty(pair.Key)).OrderBy(pair => pair.Key).Select(pair => new DevicePropertyValueModel
            {
                DisplayOrder = 3,
                IsEditable   = false,
                IsIncludedWithUnregisteredDevices = false,
                Name           = FormattableString.Invariant($"properties.reported.{pair.Key}"),
                PropertyType   = GetObjectType(pair.Value.Value),
                Value          = pair.Value.Value.ToString(),
                LastUpdatedUtc = pair.Value.LastUpdated
            });

            propValModels = tags.Concat(desiredProperties).Concat(reportedProperties);

            hostNameValue = _configProvider.GetConfigurationSettingValue("iotHub.HostName");

            if (!string.IsNullOrEmpty(hostNameValue))
            {
                propValModels = propValModels.Concat(
                    new DevicePropertyValueModel[]
                {
                    new DevicePropertyValueModel()
                    {
                        DisplayOrder = 0,
                        IsEditable   = false,
                        IsIncludedWithUnregisteredDevices = true,
                        Name         = "DeviceID",
                        PropertyType = PropertyType.String,
                        Value        = device.DeviceProperties.DeviceID,
                    },
                    new DevicePropertyValueModel()
                    {
                        DisplayOrder = 0,
                        IsEditable   = false,
                        IsIncludedWithUnregisteredDevices = true,
                        Name         = "HostName",
                        PropertyType = PropertyType.String,
                        Value        = hostNameValue
                    }
                });
            }

            return(propValModels);
        }
コード例 #6
0
        public void GenerateSupportedMethodsReportedPropertyTest()
        {
            var commands = new List <Command>()
            {
                // Method with parameters
                new Command("method1", DeliveryType.Method, "desc1", new List <Parameter>()
                {
                    new Parameter("p1", "string"),
                    new Parameter("p2", "int")
                }),
                // Command, should be ignored
                new Command("command1", DeliveryType.Method, "desc1", new List <Parameter>()
                {
                    new Parameter("p1", "int"),
                    new Parameter("p2", "string")
                }),
                // Method without parameters
                new Command("method2", DeliveryType.Method, "desc2"),
                // Method name with _
                new Command("method_3", DeliveryType.Method, "desc3"),
                // Method without name, should be ignored
                new Command("", DeliveryType.Method, "desc2"),
                // parameter with no type, should be ignored
                new Command("method4", DeliveryType.Method, "desc1", new List <Parameter>()
                {
                    new Parameter("p1", ""),
                    new Parameter("p2", "int")
                }),
            };

            var property = new TwinCollection();

            SupportedMethodsHelper.CreateSupportedMethodReport(property, commands, null);

            JObject supportedMethods = property["SupportedMethods"] as JObject;

            Assert.Equal(supportedMethods.Count, commands.Where(c => c.DeliveryType == DeliveryType.Method).Count() - 2);

            Assert.Equal(supportedMethods["method1--p1-string--p2-int"].ToString(), "desc1");
            Assert.Equal(supportedMethods["command1--p1-int--p2-string"].ToString(), "desc1");
            Assert.Equal(supportedMethods["method2"].ToString(), "desc2");
            Assert.Equal(supportedMethods["method_3"].ToString(), "desc3");

            supportedMethods[""] = "desc2";
            supportedMethods["method4--p1---p2-int"] = "desc1";

            var device = new DeviceModel();
            var twin   = new Twin();

            twin.Properties.Reported["SupportedMethods"] = supportedMethods;

            SupportedMethodsHelper.AddSupportedMethodsFromReportedProperty(device, twin);
            Assert.Equal(supportedMethods.Count - 2, device.Commands.Count);
            foreach (var command in device.Commands)
            {
                var srcCommand = commands.FirstOrDefault(c => c.Name == command.Name);
                Assert.Equal(command.Name, srcCommand.Name);
                Assert.Equal(command.Description, srcCommand.Description);
                Assert.Equal(command.Parameters.Count, srcCommand.Parameters.Count);

                foreach (var parameter in command.Parameters)
                {
                    var srcParameter = srcCommand.Parameters.FirstOrDefault(p => p.Name == parameter.Name);
                    Assert.Equal(parameter.Name, srcParameter.Name);
                    Assert.Equal(parameter.Type, srcParameter.Type);
                }
            }
        }