Ejemplo n.º 1
0
        public void Register(ContainerBuilder builder)
        {
            const int ConnectionPoolSize = 10;

            string edgeHubConnectionString = $"{this.configuration[Service.Constants.ConfigKey.IotHubConnectionString]};ModuleId=$edgeHub";

            Client.IotHubConnectionStringBuilder iotHubConnectionStringBuilder = Client.IotHubConnectionStringBuilder.Create(edgeHubConnectionString);
            var topics = new MessageAddressConversionConfiguration(this.inboundTemplates, this.outboundTemplates);

            builder.RegisterModule(new LoggingModule());

            var mqttSettingsConfiguration = new Mock <IConfiguration>();

            mqttSettingsConfiguration.Setup(c => c.GetSection(It.IsAny <string>())).Returns(Mock.Of <IConfigurationSection>(s => s.Value == null));

            builder.RegisterBuildCallback(
                c =>
            {
                // set up loggers for dotnetty
                var loggerFactory = c.Resolve <ILoggerFactory>();
                InternalLoggerFactory.DefaultFactory = loggerFactory;

                var eventListener = new LoggerEventListener(loggerFactory.CreateLogger("ProtocolGateway"));
                eventListener.EnableEvents(CommonEventSource.Log, EventLevel.Informational);
            });

            var versionInfo = new VersionInfo("v1", "b1", "c1");
            var storeAndForwardConfiguration = new StoreAndForwardConfiguration(-1);

            builder.RegisterModule(
                new CommonModule(
                    string.Empty,
                    iotHubConnectionStringBuilder.HostName,
                    iotHubConnectionStringBuilder.DeviceId,
                    iotHubConnectionStringBuilder.ModuleId,
                    string.Empty,
                    Option.None <string>(),
                    AuthenticationMode.CloudAndScope,
                    Option.Some(edgeHubConnectionString),
                    false,
                    false,
                    string.Empty,
                    Option.None <string>(),
                    TimeSpan.FromHours(1),
                    false,
                    this.trustBundle));

            builder.RegisterModule(
                new RoutingModule(
                    iotHubConnectionStringBuilder.HostName,
                    iotHubConnectionStringBuilder.DeviceId,
                    iotHubConnectionStringBuilder.ModuleId,
                    Option.Some(edgeHubConnectionString),
                    this.routes,
                    false,
                    storeAndForwardConfiguration,
                    ConnectionPoolSize,
                    false,
                    versionInfo,
                    Option.Some(UpstreamProtocol.Amqp),
                    TimeSpan.FromSeconds(5),
                    101,
                    TimeSpan.FromSeconds(3600),
                    true,
                    TimeSpan.FromSeconds(20)));

            builder.RegisterModule(new HttpModule());
            builder.RegisterModule(new MqttModule(mqttSettingsConfiguration.Object, topics, this.serverCertificate, false, false, false));
            builder.RegisterModule(new AmqpModule("amqps", 5671, this.serverCertificate, iotHubConnectionStringBuilder.HostName, true));
        }
Ejemplo n.º 2
0
            async Task StartProtocolHead()
            {
                const int ConnectionPoolSize = 10;
                string    certificateValue   = await SecretsHelper.GetSecret("IotHubMqttHeadCert");

                byte[] cert        = Convert.FromBase64String(certificateValue);
                var    certificate = new X509Certificate2(cert);

                string edgeDeviceConnectionString = await SecretsHelper.GetSecretFromConfigKey("edgeCapableDeviceConnStrKey");

                // TODO - After IoTHub supports MQTT, remove this and move to using MQTT for upstream connections
                await ConnectToIotHub(edgeDeviceConnectionString);

                string edgeHubConnectionString = $"{edgeDeviceConnectionString};ModuleId=$edgeHub";

                Client.IotHubConnectionStringBuilder iotHubConnectionStringBuilder = Client.IotHubConnectionStringBuilder.Create(edgeHubConnectionString);
                var topics = new MessageAddressConversionConfiguration(this.inboundTemplates, this.outboundTemplates);

                var builder = new ContainerBuilder();

                builder.RegisterModule(new LoggingModule());

                var mqttSettingsConfiguration = new Mock <IConfiguration>();

                mqttSettingsConfiguration.Setup(c => c.GetSection(It.IsAny <string>())).Returns(Mock.Of <IConfigurationSection>(s => s.Value == null));

                builder.RegisterBuildCallback(
                    c =>
                {
                    // set up loggers for dotnetty
                    var loggerFactory = c.Resolve <ILoggerFactory>();
                    InternalLoggerFactory.DefaultFactory = loggerFactory;

                    var eventListener = new LoggerEventListener(loggerFactory.CreateLogger("ProtocolGateway"));
                    eventListener.EnableEvents(CommonEventSource.Log, EventLevel.Informational);
                });

                var versionInfo = new VersionInfo("v1", "b1", "c1");
                var storeAndForwardConfiguration = new StoreAndForwardConfiguration(-1);

                builder.RegisterModule(new CommonModule(string.Empty, iotHubConnectionStringBuilder.HostName, iotHubConnectionStringBuilder.DeviceId));
                builder.RegisterModule(
                    new RoutingModule(
                        iotHubConnectionStringBuilder.HostName,
                        iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.ModuleId,
                        Option.Some(edgeHubConnectionString),
                        this.routes, false, false, storeAndForwardConfiguration,
                        string.Empty, ConnectionPoolSize, false, versionInfo, Option.Some(UpstreamProtocol.Amqp),
                        true, TimeSpan.FromSeconds(5), 101, false, Option.None <string>(), Option.None <string>())
                    );
                builder.RegisterModule(new HttpModule());
                builder.RegisterModule(new MqttModule(mqttSettingsConfiguration.Object, topics, certificate, false, false, string.Empty, false));
                builder.RegisterModule(new AmqpModule("amqps", 5671, certificate, iotHubConnectionStringBuilder.HostName));
                this.container = builder.Build();

                IConfigSource configSource = await this.container.Resolve <Task <IConfigSource> >();

                ConfigUpdater configUpdater = await this.container.Resolve <Task <ConfigUpdater> >();

                await configUpdater.Init(configSource);

                ILogger          logger           = this.container.Resolve <ILoggerFactory>().CreateLogger("EdgeHub");
                MqttProtocolHead mqttProtocolHead = await this.container.Resolve <Task <MqttProtocolHead> >();

                AmqpProtocolHead amqpProtocolHead = await this.container.Resolve <Task <AmqpProtocolHead> >();

                this.protocolHead = new EdgeHubProtocolHead(new List <IProtocolHead> {
                    mqttProtocolHead, amqpProtocolHead
                }, logger);
                await this.protocolHead.StartAsync();
            }