예제 #1
0
        public static void ListentoCowLocation()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    //channel.QueueDeclare(queue: "location_queue",
                    //                     durable: true,
                    //                     exclusive: false,
                    //                     autoDelete: false,
                    //                     arguments: null);

                    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

                    Console.WriteLine(" [*] Waiting for messages.");

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (sender, ea) =>
                    {
                        var body    = ea.Body.ToArray();
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("Location modtaget [x] Received {0}", message);
                        var dto = JsonConvert.DeserializeObject <LocationCreate>(message);

                        //her burde man bruge Istorage i stedet, men jeg kunne ikke får de til at funke
                        InMemoryStorage inMemory = new InMemoryStorage();
                        inMemory.LocationCreateUpdate(dto.EarTag, dto.Latitude, dto.Longitude);

                        Console.WriteLine(" [x] Done");

                        // Note: it is possible to access the channel via
                        //       ((EventingBasicConsumer)sender).Model here
                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                    };
                    channel.BasicConsume(queue: "location_queue",
                                         autoAck: false,
                                         consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }