/// <summary> /// Clear registry /// </summary> private static async Task CleanupAsync(IIoTHubConfig config, bool includeSupervisors) { var logger = ConsoleLogger.Create(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, new NewtonSoftJsonSerializer(), logger); var result = await registry.QueryAllDeviceTwinsAsync( "SELECT * from devices where IS_DEFINED(tags.DeviceType)"); foreach (var item in result) { Console.WriteLine($"Deleting {item.Id} {item.ModuleId ?? ""}"); await registry.DeleteAsync(item.Id, item.ModuleId, null, CancellationToken.None); } if (!includeSupervisors) { return; } var query = "SELECT * FROM devices.modules WHERE " + $"properties.reported.{TwinProperty.Type} = '{IdentityType.Supervisor}'"; var supers = await registry.QueryAllDeviceTwinsAsync(query); foreach (var item in supers) { Console.WriteLine($"Deleting {item.Id} {item.ModuleId ?? ""}"); await registry.DeleteAsync(item.Id, item.ModuleId, null, CancellationToken.None); } }
/// <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, logger); await registry.CreateAsync(new DeviceTwinModel { Id = deviceId, Tags = new Dictionary <string, JToken> { [TwinProperty.Type] = IdentityType.Gateway }, Capabilities = new DeviceCapabilitiesModel { IotEdge = true } }, true, CancellationToken.None); await registry.CreateAsync(new DeviceTwinModel { Id = deviceId, ModuleId = moduleId }, true, CancellationToken.None); var cs = await registry.GetConnectionStringAsync(deviceId, moduleId); return(cs); }
/// <summary> /// Clear registry /// </summary> private static async Task CleanupAsync(IIoTHubConfig config, bool includeSupervisors) { var logger = LogEx.Console(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, logger); var result = await registry.QueryDeviceTwinsAsync( "SELECT * from devices where IS_DEFINED(tags.DeviceType)"); foreach (var item in result) { Console.WriteLine($"Deleting {item.Id} {item.ModuleId ?? ""}"); await registry.DeleteAsync(item.Id, item.ModuleId); } if (!includeSupervisors) { return; } var query = "SELECT * FROM devices.modules WHERE " + $"properties.reported.{TwinProperty.kType} = 'supervisor'"; var supers = await registry.QueryDeviceTwinsAsync(query); foreach (var item in supers) { Console.WriteLine($"Deleting {item.Id} {item.ModuleId ?? ""}"); await registry.DeleteAsync(item.Id, item.ModuleId); } }
/// <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, logger); await registry.CreateAsync(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}"); }
/// <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, 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.CreateAsync(item, true, CancellationToken.None); } }
/// <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.CreateAsync(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.CreateAsync(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); }
/// <summary> /// Create client /// </summary> private static IoTHubServiceHttpClient CreateClient(IIoTHubConfig config, ILogger logger) { var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, logger); return(registry); }
/// <summary> /// Create client /// </summary> private static IoTHubServiceHttpClient CreateClient(IIoTHubConfig config, ILogger logger) { var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, new NewtonSoftJsonSerializer(), logger); return(registry); }
/// <summary> /// Delete supervisor /// </summary> private static async Task DeleteAsync(IIoTHubConfig config, string deviceId, string moduleId) { var logger = LogEx.Console(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, logger); await registry.DeleteAsync(deviceId, moduleId); }
/// <summary> /// Delete supervisor /// </summary> private static async Task DeleteAsync(IIoTHubConfig config, string deviceId, string moduleId) { var logger = ConsoleLogger.Create(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, new NewtonSoftJsonSerializer(), logger); await registry.DeleteAsync(deviceId, moduleId, null, CancellationToken.None); }
/// <summary> /// Get module connection string /// </summary> private static async Task GetAsync( IIoTHubConfig config, string deviceId, string moduleId) { var logger = ConsoleLogger.Create(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, new NewtonSoftJsonSerializer(), logger); var cs = await registry.GetConnectionStringAsync(deviceId, moduleId); Console.WriteLine(cs); }
/// <summary> /// Reset supervisor /// </summary> private static async Task ResetAsync(IIoTHubConfig config, string deviceId, string moduleId) { var logger = LogEx.Console(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, logger); await ResetAsync(registry, await registry.GetAsync(deviceId, moduleId, CancellationToken.None)); }
public static ITwinModuleApi CreateModueClient(string deviceId, string moduleName) { var logger = ConsoleLogger.Create(LogEventLevel.Error); var iotHubConfig = ConnectionStringEx.ToIoTHubConfig(Environment.GetEnvironmentVariable("IOT_HUB_CONNECTION_STRING")); var httpClient = new HttpClient(logger); var iotHubServiceClient = new IoTHubServiceHttpClient(httpClient, iotHubConfig, new NewtonSoftJsonSerializer(), logger); var methodClient = new ChunkMethodClient(new IoTHubTwinMethodClient(iotHubServiceClient, logger), new NewtonSoftJsonSerializer(), logger); return(new TwinModuleClient(methodClient, deviceId, moduleName)); }
/// <summary> /// Clear registry /// </summary> private static async Task ClearRegistryAsync() { var logger = ConsoleOutLogger.Create(); var config = new IoTHubConfig(null); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, logger); var result = await registry.QueryAllDeviceTwinsAsync( "SELECT * from devices where IS_DEFINED(tags.DeviceType)"); foreach (var item in result) { await registry.DeleteAsync(item.Id, item.ModuleId, null, CancellationToken.None); } }
/// <summary> /// List all twin module identities /// </summary> private static async Task ListAsync(IIoTHubConfig config) { var logger = LogEx.Console(LogEventLevel.Error); var registry = new IoTHubServiceHttpClient(new HttpClient(logger), config, logger); var query = "SELECT * FROM devices.modules WHERE " + $"properties.reported.{TwinProperty.kType} = 'supervisor'"; var supers = await registry.QueryDeviceTwinsAsync(query); foreach (var item in supers) { Console.WriteLine($"{item.Id} {item.ModuleId}"); } }
/// <summary> /// List all twin module identities /// </summary> private static async Task ListAsync(IIoTHubConfig config) { var logger = ConsoleLogger.Create(LogEventLevel.Error); 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) { Console.WriteLine($"{item.Id} {item.ModuleId}"); } }
/// <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.CreateAsync(item, true, CancellationToken.None); }