public static MqttClientOptionsBuilder DefaultMqttClient(MqttNetOptions mqttSettings)
        {
            var brokerHost = !string.IsNullOrEmpty(mqttSettings.MqttBroker) ? mqttSettings.MqttBroker : MqttConstants.MqttServer.LocalHost;
            var tcpPort    = mqttSettings.MqttPort > 0 ? mqttSettings.MqttPort : MqttConstants.MqttServer.DefaultTcpPort;
            var socketPort = mqttSettings.MqttPort != null && mqttSettings.MqttSocketPort > 0 ? (int)mqttSettings.MqttSocketPort : MqttConstants.MqttServer.DefaultSocketPort;
            var socketPath = !string.IsNullOrEmpty(mqttSettings.MqttSocketPath) ? mqttSettings.MqttSocketPath : "mqtt";

            return(new MqttClientOptionsBuilder()
                   .WithClientId("IreckonCityApi")
                   //.RunWithTcpClient(brokerHost, tcpPort)
                   .RunWithWebSocket(brokerHost, socketPort, socketPath));
        }
예제 #2
0
        public static ManagedMqttClientOptionsBuilder DefaultManagedMqttClient(MqttNetOptions options)
        {
            //var defaultClient = MqttClientBuilderExtensions.DefaultMqttClient(dependencyResolver).Build();
            var defaultClient = new MqttClientOptionsBuilder()
                                .RunWithWebSocket(options.MqttBroker, (int)options.MqttSocketPort, options.MqttSocketPath)
                                .WithClientId("ElsaMqttClient");

            if (options.MqttSocketPort == 443)
            {
                defaultClient.WithTls();
            }

            var socketClient = defaultClient.Build();

            return(new ManagedMqttClientOptionsBuilder()
                   .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
                   .WithClientOptions(socketClient)
                   .WithStorage(new ManagedMessageHandler()));
        }
        //public static async Task<IDependencyResolver> SetupMqttClientAsync(this IDependencyResolver dependencyResolver, SensorDataCallback callback)
        public static async Task <IManagedMqttClient> SetupMqttClientAsync(MqttNetOptions options)
        {
            //var receiver = dependencyResolver.Resolve<IGenericDataReceiverService>();

            //if (!dependencyResolver.IsRegistered<MqttNetOptions>())
            //{
            //    dependencyResolver.ConfigureSettingsPerType<MqttNetOptions>(false);
            //}

            // Setup and start a managed MQTT client.
            //var options = ManagedMqttClientBuilderExtensions.DefaultManagedMqttClient(dependencyResolver).Build();
            var _options = ManagedMqttClientBuilderExtensions.DefaultManagedMqttClient(options).Build();

            var mqttClient = new MqttFactory()
                             .CreateManagedMqttClient()
                             .SetupConnectionHandler(_options)
                             //.UseApplicationMessageReceivedHandler(new ApplicationMessageReceiverHandler(receiver, callback));
                             .UseApplicationMessageReceivedHandler(new ApplicationMessageReceiverHandler(options.Topic ?? new string[] { "$sys" }));

            //Todo: Get topics from somewhere (DB or Config?)
            //var topics = new List<string> {
            //    MqttConstants.MqttClient.Topic.SensorData
            //};

            await mqttClient.SubscribeAsync(GetTopicBuilder(options.Topic).Build());

            await mqttClient.StartAsync(_options);

            // StartAsync returns immediately, as it starts a new thread using Task.Run,
            // and so the calling thread needs to wait.
            //Console.ReadLine();
            //dependencyResolver.RegisterInstance(mqttClient);

            //return dependencyResolver;
            return(mqttClient);
        }
예제 #4
0
 public MqttService(IOptions <MqttNetOptions> options, ILogger <MqttService> logger)
 {
     this.options = options.Value;
     this.logger  = logger;
 }
        public static IServiceCollection SetupMqttEventBus(this IServiceCollection services, MqttNetOptions mqttSettings)
        {
            var retryCount = 5;

            services.AddMqttEventBus(cfg =>
            {
                cfg.WithClientId("Api");

                if (mqttSettings.MqttSocketPort > 0 && !string.IsNullOrEmpty(mqttSettings.MqttSocketPath))
                {
                    cfg.WithWebSocketServer($"{mqttSettings.MqttBroker}:{mqttSettings.MqttSocketPort}/{mqttSettings.MqttSocketPath}");

                    if (mqttSettings.MqttSocketPort == 443)
                    {
                        cfg.WithTcpServer(mqttSettings.MqttBroker, port: mqttSettings.MqttPort);
                    }
                }
            }, retryCount);
            services.AddTransient <MyEventHandler>();

            return(services);
        }