-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiveService.cs
53 lines (42 loc) · 1.78 KB
/
ReceiveService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace Tatweer.Traffic
{
public static class ReceiveService
{
private static List<Message> _logMessages = new List<Message>();
private static readonly int DangerSpeed = int.Parse(ConfigurationManager.AppSettings["speed"]);
public static void ReceiveMessages(IModel channel, Enums.Exchange exchange, bool isLogClear = true)
{
if (isLogClear)
_logMessages = new List<Message>();
channel.BasicQos(0, 1, false);
var eventingBasicConsumer = new EventingBasicConsumer(channel);
eventingBasicConsumer.Received += (sender, delivery) =>
{
var messages = MessageService.DeserializeMessages(delivery.Body);
foreach (var message in messages)
{
Debug.WriteLine(MessageService.SerializeMessages(message));
if (message.Speed > DangerSpeed)
{
EmitService.EmitMessage(Enums.Exchange.Alerts, MessageService.SerializeMessages(message));
Console.WriteLine(string.Concat("Alerts: ", MessageService.SerializeMessages(message)));
Console.WriteLine("\n\n");
}
_logMessages.Add(message);
}
channel.BasicAck(delivery.DeliveryTag, false);
};
channel.BasicConsume(exchange.ToString(), false, eventingBasicConsumer);
}
public static List<Message> GetMessages()
{
return _logMessages;
}
}
}