Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            MQTTnet.MqttFactory factory = new MQTTnet.MqttFactory();
            var client  = factory.CreateMqttClient();
            var options = new MqttClientOptionsBuilder()
                          .WithClientId(Guid.NewGuid().ToString())
                          .WithTcpServer("localhost")
                          .WithCredentials("3cb97cd31fbc40b08d12ec47a6fad622")//token
                          .Build();

            Task.Run(async() =>
            {
                await client.ConnectAsync(options);
                do
                {
                    var message = new MqttApplicationMessageBuilder()
                                  .WithTopic("/devices/me/telemetry")
                                  .WithPayload(JsonConvert.SerializeObject(new
                    {
                        RandomString = Guid.NewGuid().ToString(),
                        NowTime      = DateTime.Now
                    }))
                                  .Build();
                    Console.WriteLine(message.ConvertPayloadToString());
                    await client.PublishAsync(message);
                    await Task.Delay(TimeSpan.FromSeconds(10));
                } while (Console.ReadKey().Key != ConsoleKey.Escape);
                await client.DisconnectAsync();
            }).Wait();
        }
        public MqttClient(ILogger <MqttClient> logger, IOptions <MqttSettings> settings, IHostApplicationLifetime applicationLifetime)
        {
            this.logger              = logger;
            this.settings            = settings;
            this.applicationLifetime = applicationLifetime;
            var factory = new MQTTnet.MqttFactory();

            client = factory.CreateMqttClient();

            options = new MqttClientOptionsBuilder()
                      .WithTcpServer(settings.Value.HostName)
                      .WithCredentials(settings.Value.Username, settings.Value.Password)
                      .Build();
        }
Exemplo n.º 3
0
        public Connector(string server, string measurementsTopic, string resultsTopic, int?port = null)
        {
            var factory = new MQTTnet.MqttFactory();

            _client = factory.CreateMqttClient();

            _managedClient = factory.CreateManagedMqttClient();
            _managedClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(ConnectedHandler);
            _managedClient.ApplicationMessageProcessedHandler =
                new ApplicationMessageProcessedHandlerDelegate(ApplicationMessageProcessedHandler);
            _managedClient.ApplicationMessageSkippedHandler =
                new ApplicationMessageSkippedHandlerDelegate(ApplicationMessageSkippedHandler);

            _options = new MqttClientOptionsBuilder()
                       .WithClientId(nameof(Connector))
                       .WithTcpServer(server, port)
                       .Build();

            _measurementsTopic = measurementsTopic;
            _resultsTopic      = resultsTopic;
        }
Exemplo n.º 4
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            MQTTnet.MqttFactory factory = new MQTTnet.MqttFactory();
            var client  = factory.CreateMqttClient();
            var options = new MqttClientOptionsBuilder()
                          .WithClientId(Guid.NewGuid().ToString())
                          .WithTcpServer("localhost")
                          .WithCredentials("3cb97cd31fbc40b08d12ec47a6fad622", "")//token
                          .Build();

            client.UseApplicationMessageReceivedHandler(ax =>
            {
                Console.WriteLine($"ClientId{ ax.ClientId},msg={ax.ApplicationMessage.ConvertPayloadToString()}");
            });

            Task.Run(async() =>
            {
                await client.ConnectAsync(options);
                do
                {
                    var message = new MqttApplicationMessageBuilder()
                                  .WithTopic("/devices/me/telemetry")
                                  .WithPayload(JsonConvert.SerializeObject(new
                    {
                        RandomString = Guid.NewGuid().ToString(),
                        NowTime      = DateTime.Now
                    }))
                                  .Build();
                    Console.WriteLine(message.ConvertPayloadToString());
                    await client.PublishAsync(message);
                    await Task.Delay(TimeSpan.FromSeconds(10));
                    await client.SubscribeAsync("/devices/me/attributes/response/+");
                    await client.PublishAsync("/devices/me/attributes/request/1", "{\"anySide\":\"Doublevalue,longvalue,Doublevalue,longvalue\"}");
                } while (Console.ReadKey().Key != ConsoleKey.Escape);
                await client.DisconnectAsync();
            }).Wait();
        }
Exemplo n.º 5
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public SnipsClient()
 {
     _logger  = LogProvider.GetCurrentClassLogger();
     _factory = new MQTTnet.MqttFactory();
 }