public TwinRepository(IoTHubConfiguration config) { Config = config; if (client == null) { client = new HttpClient(); client.BaseAddress = new Uri("https://" + config.HostName); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } }
/// <summary> /// Called by the service fabric when the listener is to be started /// </summary> /// <param name="cancellationToken">A token to monitor for abort requests</param> /// <returns>A task that completes when the service is openned</returns> public async Task <string> OpenAsync(CancellationToken cancellationToken) { string publishAddress = null; Guid traceId = Guid.NewGuid(); int threadCount = Environment.ProcessorCount; this.serverControl = new CancellationTokenSource(); GatewayConfiguration configuration = this.configurationProvider.Config; this.logger.Informational(traceId, this.componentName, "OpenAsync() invoked."); var iotHubConfigurationProvider = new ConfigurationProvider <IoTHubConfiguration>(traceId, this.serviceContext.ServiceName, this.serviceContext.CodePackageActivationContext, this.logger, "IoTHubClient"); iotHubConfigurationProvider.ConfigurationChangedEvent += (sender, args) => this.unsupportedConfigurationChangeCallback?.Invoke(); iotHubConfigurationProvider.ConfigurationPropertyChangedEvent += (sender, args) => this.unsupportedConfigurationChangeCallback?.Invoke(); IoTHubConfiguration iotHubConfiguration = iotHubConfigurationProvider.Config; X509Certificate2 certificate = this.GetServerCertificate(configuration); this.logger.Informational(traceId, this.componentName, "Certificate retrieved."); var mqttConfigurationProvider = new ConfigurationProvider <MqttServiceConfiguration>(traceId, this.serviceContext.ServiceName, this.serviceContext.CodePackageActivationContext, this.logger, "Mqtt"); mqttConfigurationProvider.ConfigurationChangedEvent += (sender, args) => this.unsupportedConfigurationChangeCallback?.Invoke(); mqttConfigurationProvider.ConfigurationPropertyChangedEvent += (sender, args) => this.unsupportedConfigurationChangeCallback?.Invoke(); MqttServiceConfiguration mqttConfiguration = mqttConfigurationProvider.Config; if (mqttConfiguration != null) { var mqttInboundTemplates = new List <string>(mqttConfiguration.MqttInboundTemplates); var mqttOutboundTemplates = new List <string>(mqttConfiguration.MqttOutboundTemplates); ISessionStatePersistenceProvider qosSessionProvider = await this.GetSessionStateProviderAsync(traceId, mqttConfiguration).ConfigureAwait(false); IQos2StatePersistenceProvider qos2SessionProvider = await this.GetQos2StateProvider(traceId, mqttConfiguration).ConfigureAwait(false); this.logger.Informational(traceId, this.componentName, "QOS Providers instantiated."); var settingsProvider = new ServiceFabricConfigurationProvider(traceId, this.componentName, this.logger, configuration, iotHubConfiguration, mqttConfiguration); this.bootStrapper = new Bootstrapper(settingsProvider, qosSessionProvider, qos2SessionProvider, mqttInboundTemplates, mqttOutboundTemplates); this.runTask = this.bootStrapper.RunAsync(certificate, threadCount, this.serverControl.Token); publishAddress = this.BuildPublishAddress(configuration); this.logger.Informational(traceId, this.componentName, "Bootstrapper instantiated."); } else { this.logger.Critical(traceId, this.componentName, "Failed to start endpoint because Mqtt service configuration is missing."); } return(publishAddress); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { string connectionString = Configuration["IoTStorageConnectionString"]; IoTHubConfiguration iotConfig = new IoTHubConfiguration { HostName = Configuration["IoTHubHostName"], SharedAccessKeyName = Configuration["IoTHubSharedAccessKeyName"], SharedAccessKey = Configuration["IoTHubSharedAccessKey"] }; // Add framework services. services .AddMvc(); services.AddAutoMapper(); services.AddScoped <ITableRepository <DataPointEntity> >(p => new DataPointRepository(connectionString)); services.AddScoped <ITableRepository <AlertEntity> >(p => new AlertRepository(connectionString)); services.AddScoped <IIoTHubSender <SumpPumpSettings> >(p => new SumpPumpService(iotConfig)); services.AddScoped <ITwinRepository <SumpPumpSettingEntity> >(p => new SettingsRepository(iotConfig)); }
/// <summary> /// The settings provider used to bridge Service Fabric configuration information and DotNetty /// </summary> /// <param name="traceId">The unique identifier used to correlate debugging and diagnostics messages</param> /// <param name="componentName">The component name used for debug and diagnostics messages</param> /// <param name="logger">The service fabric logger to be used when writing debug and diagnostics information</param> /// <param name="gatewayConfiguration">The gateway configuration used to get current configuration information for DotNetty</param> /// <param name="iotHubConfiguration">The IotHub Client configuration used to get current configuration information for DotNetty</param> /// <param name="mqttConfiguration">The MQTT configuration used to get current configuration information for DotNetty</param> public ServiceFabricConfigurationProvider(Guid traceId, string componentName, IServiceLogger logger, GatewayConfiguration gatewayConfiguration, IoTHubConfiguration iotHubConfiguration, MqttServiceConfiguration mqttConfiguration) { this.componentName = componentName; this.logger = logger; this.logger.Verbose(traceId, this.componentName, "Initializing configuration provider."); var baseProperties = new Dictionary<string, string>(); foreach (PropertyInfo element in typeof(GatewayConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (!element.PropertyType.IsArray) { object value = element.GetValue(gatewayConfiguration, null); baseProperties.Add(element.Name, element.PropertyType == typeof(string) ? value as string : value.ToString()); } } foreach (PropertyInfo element in typeof(MqttServiceConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (!element.PropertyType.IsArray) { object value = element.GetValue(mqttConfiguration, null); baseProperties.Add(element.Name, element.PropertyType == typeof(string) ? value as string : value.ToString()); } } foreach (PropertyInfo element in typeof(IoTHubConfiguration).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (!element.PropertyType.IsArray) { object value = element.GetValue(iotHubConfiguration, null); baseProperties.Add($"IotHubClient.{element.Name}", value?.ToString()); } } this.configurationValues = baseProperties.ToImmutableDictionary(); this.logger.Informational(traceId, this.componentName, "Initializing configuration provider complete."); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); var iotHubConfiguration = new IoTHubConfiguration { IotHubConnectionString = Configuration["IotHubConnectionString"], EventHubsCompatibleEndpoint = Configuration["EventHubsCompatibleEndpoint"], EventHubsCompatiblePath = Configuration["EventHubsCompatiblePath"], IotHubSasKey = Configuration["IotHubSasKey"], IotHubSasKeyName = Configuration["IotHubSasKeyName"] }; services.AddSingleton(iotHubConfiguration); services.AddSingleton(new DeviceController(iotHubConfiguration)); services.AddHostedService <TelemetryIngesterHostedService>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSignalR(); }
public SettingsRepository(IoTHubConfiguration config) : base(config) { }
public SumpPumpService(IoTHubConfiguration config) { Configuration = config; }
public IoTHubClient(IOptions <IoTHubConfiguration> config) { _config = config.Value; _iotDeviceClient = DeviceClient.CreateFromConnectionString(_config.IoTDeviceConnectionString); }
//private readonly IoTHubConfiguration iotHubConfiguration; public DeviceController(IoTHubConfiguration iotHubConfiguration) { //this.iotHubConfiguration = iotHubConfiguration; serviceClient = ServiceClient.CreateFromConnectionString(iotHubConfiguration.IotHubConnectionString); }