Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string StartPublishingInactiveVehicles(int timePeriodInSeconds, string address)
- {
- string random = RandomString(16);
- string queueName = "OBU1_InactiveVehicles_" + random;
- Task.Run(() => publishInactiveVehiclesAsync(queueName, timePeriodInSeconds, address));
- return queueName;
- }
- private async Task publishInactiveVehiclesAsync(string queueName, int timePeriodInSeconds, string address)
- {
- if (string.IsNullOrEmpty(address))
- address = "http://127.0.0.1:8080/TRAAS_WS"; //default address
- TraasReference.ServiceImplClient client = new TraasReference.ServiceImplClient();
- client.Endpoint.Address = new EndpointAddress(address);
- ConnectionFactory factory = new ConnectionFactory();
- IProtocol protocol = Protocols.AMQP_0_9_1;
- factory.VirtualHost = "/";
- factory.UserName = "soa";
- factory.Password = "soasoa";
- factory.HostName = "164.8.251.96";
- factory.Port = 5672;
- factory.Protocol = protocol;
- try
- {
- using (IConnection conn = factory.CreateConnection())
- {
- using (IModel ch = conn.CreateModel())
- {
- ch.QueueDeclare(queue: queueName,
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null);
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- Dictionary<string, bool> seznamVozil = new Dictionary<string, bool>();
- client.Open();
- while (stopwatch.Elapsed.TotalSeconds < timePeriodInSeconds)
- {
- string[] activeVehicles = client.Vehicle_getIDList();
- foreach (string s in activeVehicles)
- {
- if (seznamVozil.ContainsKey(s))
- seznamVozil[s] = true;
- else
- seznamVozil.Add(s, true);
- }
- List<string> inactiveVehicles = new List<string>();
- foreach (KeyValuePair<string, bool> entry in seznamVozil)
- {
- if (!entry.Value)
- inactiveVehicles.Add(entry.Key);
- }
- string msg = "";
- foreach (string s in inactiveVehicles)
- {
- msg += s + " ";
- seznamVozil.Remove(s);
- }
- if (msg != "")
- {
- var body = Encoding.UTF8.GetBytes(msg);
- ch.BasicPublish(exchange: "",
- routingKey: queueName,
- basicProperties: null,
- body: body);
- }
- foreach (var key in seznamVozil.Keys.ToList())
- {
- seznamVozil[key] = false;
- }
- System.Threading.Thread.Sleep(5000);
- //client.Close();
- }
- client.Close();
- }
- }
- }
- catch (FaultException e)
- {
- client.Abort();
- }
- catch (Exception e)
- {
- client.Abort();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement