public async Task DoWork(CancellationToken stoppingToken) { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); if (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); ConnectionFactory factory = new ConnectionFactory() { HostName = "rabbitmq", Port = 5672 }; factory.UserName = "******"; factory.Password = "******"; IConnection conn = factory.CreateConnection(); IModel channel = conn.CreateModel(); channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += async(model, ea) => { var body = ea.Body.ToArray(); string message = System.Text.Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received from Rabbit data reader: {0}", message); double temp = double.Parse(message); SmartHomeTemperature sht = new SmartHomeTemperature(); sht.temperature = temp; await _repository.AddDataFromSensors(sht); await sendDataToSinddhiApp(message); }; channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer); } }
public async Task AddDataFromSensors(SmartHomeTemperature sht) { await _smartHome.InsertOneAsync(sht); }