示例#1
0
        /// <summary>
        /// Add or get module identity
        /// </summary>
        private static async Task <ConnectionString> AddOrGetAsync(IIoTHubConfig config,
                                                                   string deviceId, string moduleId)
        {
            var logger   = ConsoleLogger.Create(LogEventLevel.Error);
            var registry = new IoTHubServiceHttpClient(new HttpClient(logger),
                                                       config, new NewtonSoftJsonSerializer(), logger);

            try {
                await registry.CreateOrUpdateAsync(new DeviceTwinModel {
                    Id   = deviceId,
                    Tags = new Dictionary <string, VariantValue> {
                        [TwinProperty.Type] = IdentityType.Gateway
                    },
                    Capabilities = new DeviceCapabilitiesModel {
                        IotEdge = true
                    }
                }, false, CancellationToken.None);
            }
            catch (ConflictingResourceException) {
                logger.Information("Gateway {deviceId} exists.", deviceId);
            }
            try {
                await registry.CreateOrUpdateAsync(new DeviceTwinModel {
                    Id       = deviceId,
                    ModuleId = moduleId
                }, false, CancellationToken.None);
            }
            catch (ConflictingResourceException) {
                logger.Information("Module {moduleId} exists...", moduleId);
            }
            var cs = await registry.GetConnectionStringAsync(deviceId, moduleId);

            return(cs);
        }
示例#2
0
        /// <summary>
        /// Clear registry
        /// </summary>
        private static async Task ClearSupervisorsAsync()
        {
            var logger   = ConsoleOutLogger.Create();
            var config   = new IoTHubConfig(null);
            var registry = new IoTHubServiceHttpClient(new HttpClient(logger),
                                                       config, new NewtonSoftJsonSerializer(), logger);

            var query = "SELECT * FROM devices.modules WHERE " +
                        $"properties.reported.{TwinProperty.Type} = '{IdentityType.Supervisor}'";
            var supers = await registry.QueryAllDeviceTwinsAsync(query);

            foreach (var item in supers)
            {
                foreach (var tag in item.Tags.Keys.ToList())
                {
                    item.Tags[tag] = null;
                }
                foreach (var property in item.Properties.Desired.Keys.ToList())
                {
                    item.Properties.Desired[property] = null;
                }
                foreach (var property in item.Properties.Reported.Keys.ToList())
                {
                    if (!item.Properties.Desired.ContainsKey(property))
                    {
                        item.Properties.Desired.Add(property, null);
                    }
                }
                await registry.CreateOrUpdateAsync(item, true, CancellationToken.None);
            }
        }
        /// <summary>
        /// Add or get supervisor identity
        /// </summary>
        private static async Task <ConnectionString> AddOrGetAsync(IIoTHubConfig config,
                                                                   string deviceId, string moduleId)
        {
            var logger   = LogEx.Console(LogEventLevel.Error);
            var registry = new IoTHubServiceHttpClient(new HttpClient(logger),
                                                       config, logger);
            await registry.CreateOrUpdateAsync(new DeviceTwinModel {
                Id           = deviceId,
                ModuleId     = moduleId,
                Capabilities = new DeviceCapabilitiesModel {
                    IotEdge = true
                }
            });

            var cs = await registry.GetConnectionStringAsync(deviceId, moduleId);

            return(cs);
        }
 /// <summary>
 /// Reset supervisor
 /// </summary>
 private static async Task ResetAsync(IoTHubServiceHttpClient registry,
                                      DeviceTwinModel item)
 {
     if (item.Tags != null)
     {
         foreach (var tag in item.Tags.Keys.ToList())
         {
             item.Tags[tag] = null;
         }
     }
     if (item.Properties?.Desired != null)
     {
         foreach (var property in item.Properties.Desired.Keys.ToList())
         {
             if (property.StartsWith('$'))
             {
                 continue;
             }
             item.Properties.Desired[property] = null;
         }
     }
     if (item.Properties?.Reported != null)
     {
         foreach (var property in item.Properties.Reported.Keys.ToList())
         {
             if (property.StartsWith('$'))
             {
                 continue;
             }
             if (!item.Properties.Desired.ContainsKey(property))
             {
                 item.Properties.Desired.Add(property, null);
             }
         }
     }
     await registry.CreateOrUpdateAsync(item);
 }
示例#5
0
        /// <summary>
        /// Create supervisor module identity in device registry
        /// </summary>
        private static async Task MakeSupervisorAsync(string deviceId, string moduleId)
        {
            var logger   = ConsoleOutLogger.Create();
            var config   = new IoTHubConfig(null);
            var registry = new IoTHubServiceHttpClient(new HttpClient(logger),
                                                       config, new NewtonSoftJsonSerializer(), logger);

            await registry.CreateOrUpdateAsync(new DeviceTwinModel {
                Id       = deviceId,
                ModuleId = moduleId
            }, true, CancellationToken.None);

            var module = await registry.GetRegistrationAsync(deviceId, moduleId, CancellationToken.None);

            Console.WriteLine(JsonConvert.SerializeObject(module));
            var twin = await registry.GetAsync(deviceId, moduleId, CancellationToken.None);

            Console.WriteLine(JsonConvert.SerializeObject(twin));
            var cs = ConnectionString.Parse(config.IoTHubConnString);

            Console.WriteLine("Connection string:");
            Console.WriteLine($"HostName={cs.HostName};DeviceId={deviceId};" +
                              $"ModuleId={moduleId};SharedAccessKey={module.Authentication.PrimaryKey}");
        }