Advertisement
Guest User

dasdasdasda

a guest
Apr 27th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.19 KB | None | 0 0
  1. using System;
  2. using RabbitMQ.Client;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Globalization;
  6.  
  7. class Program
  8. {
  9.     public static void Main()
  10.     {
  11.         var factory = new ConnectionFactory() { HostName = "localhost" };
  12.         factory.Port = 5672;
  13.         //factory.UserName = "userTest";
  14.         //factory.Password = "userTest";
  15.         factory.Protocol = Protocols.DefaultProtocol;
  16.  
  17.         using (var connection = factory.CreateConnection())
  18.         using(var channel = connection.CreateModel())
  19.         {
  20.             channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
  21.  
  22.             Console.WriteLine(" Press [enter] to exit.");
  23.             Console.ReadLine();
  24.  
  25.             string message = string.Empty;
  26.             Random rnd = new Random();
  27.  
  28.             for (int i = 0; i < 10; i++)
  29.             {
  30.                 var Timestamp = new DateTimeOffset(DateTime.UtcNow).ToString();
  31.  
  32.  
  33.                 //message = "{ \"type\": \"HPS\", \"sensorid\" : 1, \"user\": \"b8-27-eb-97-3d-b6\", \"value\":[" +
  34.                 //        Convert.ToString(i, CultureInfo.InvariantCulture) + "," +
  35.                 //        Convert.ToString(i + rnd.Next(-10, 11), CultureInfo.InvariantCulture) + "],\"ts\": \"" + Timestamp + "\" }";
  36.  
  37.                 long unixTime = UnixTimeNow();
  38.                 //Thread.Sleep(300);
  39.                 long unixTime2 = UnixTimeNow();
  40.                 //Thread.Sleep(300);
  41.                 long unixTime3 = UnixTimeNow();
  42.  
  43.                 message = "{" +
  44.                             "\"viewer\": \"drawin\", " +
  45.                             "\"values\": [" +
  46.                             "[\"Sensor Name 1\"," + unixTime +"," + Convert.ToString(rnd.Next(0, i+3), CultureInfo.InvariantCulture) +", \"temperature\", \"running\"], " +
  47.                             "[\"Sensor Name 2\", " + unixTime2 + ", " + Convert.ToString(rnd.Next(0, i + 4), CultureInfo.InvariantCulture) + ", \"temperature\", \"running\"]," +
  48.                             "[\"Sensor Name 3\", " + unixTime3 + ", " + Convert.ToString(rnd.Next(0, i + 5), CultureInfo.InvariantCulture) + ", \"temperature\", \"running\"]" +
  49.                             "]" +
  50.                           "}";
  51.  
  52.                 string r = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"-0.153"; //Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  53.                 string g = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"-0.091";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  54.                 string b = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.010";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  55.                 string a = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.009";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  56.                 string c = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.112";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  57.                 string d = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.190";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  58.                 string e = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"-0.074";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
  59.  
  60.                 message = "{" +
  61.                           "\"viewer\": \"drawin\", " +
  62.                           "\"values\": [" +
  63.                           "[\"Sensor Name 1\"," + unixTime + "," + r + ", \"temperature\", \"running\"], " +
  64.                           "[\"Sensor Name 1\"," + unixTime + "," + 10 + ", \"direction\", \"running\"], " +
  65.  
  66.                           "[\"Sensor Name 2\", " + unixTime2 + ", " + g + ", \"temperature\", \"running\"]," +
  67.                           "[\"Sensor Name 2\", " + unixTime2 + ", " + 100 + ", \"direction\", \"running\"]," +
  68.  
  69.                           "[\"Sensor Name 3\", " + unixTime3 + ", " + b + ", \"temperature\", \"running\"]," +
  70.                           "[\"Sensor Name 3\", " + unixTime3 + ", " + 55 + ", \"direction\", \"running\"]," +
  71.  
  72.                           "[\"Sensor Name 4\", " + unixTime3 + ", " + a + ", \"temperature\", \"running\"]," +
  73.                           "[\"Sensor Name 4\", " + unixTime3 + ", " + 75 + ", \"direction\", \"running\"]," +
  74.  
  75.                           "[\"Sensor Name 5\", " + unixTime3 + ", " + c + ", \"temperature\", \"running\"]," +
  76.                           "[\"Sensor Name 5\", " + unixTime3 + ", " + 180 + ", \"direction\", \"running\"]," +
  77.  
  78.                           "[\"Sensor Name 6\", " + unixTime3 + ", " + d + ", \"temperature\", \"running\"]," +
  79.                           "[\"Sensor Name 6\", " + unixTime3 + ", " + 75 + ", \"direction\", \"running\"]," +
  80.  
  81.                           "[\"Sensor Name 7\", " + unixTime3 + ", " + e + ", \"temperature\", \"running\"]," +
  82.                           "[\"Sensor Name 7\", " + unixTime3 + ", " + 240 + ", \"direction\", \"running\"]" +
  83.  
  84.                           "]" +
  85.                         "}";
  86.  
  87.  
  88.  
  89.                 var body = Encoding.UTF8.GetBytes(message);
  90.                 channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
  91.  
  92.                 //Console.WriteLine(" [x] Sent {0}", message);
  93.  
  94.                 //Console.ReadLine();
  95.                 //message = "{ \"type\": \"HPS\", \"sensorid\" : 2, \"user\": \"b8-27-eb-97-3d-b6\", \"value\":[" +
  96.                 //    Convert.ToString(i , CultureInfo.InvariantCulture) + "," +
  97.                 //    Convert.ToString(i + rnd.Next(-10, 11), CultureInfo.InvariantCulture) + "],\"ts\": \"" +
  98.                 //    Timestamp + "\" }";
  99.                 //body = Encoding.UTF8.GetBytes(message);
  100.                 //channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
  101.  
  102.                 //message = "{ \"type\": \"HPS\", \"sensorid\" : 3, \"user\": \"b8-27-eb-97-3d-b6\", \"value\":[" +
  103.                 //    Convert.ToString(i, CultureInfo.InvariantCulture) + "," +
  104.                 //    Convert.ToString(i + rnd.Next(-10, 11), CultureInfo.InvariantCulture) + "],\"ts\": \"" +
  105.                 //    Timestamp + "\" }";
  106.                 //body = Encoding.UTF8.GetBytes(message);
  107.                 //channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
  108.  
  109.                 //Console.ReadLine();
  110.                 Thread.Sleep(100);
  111.                 //Console.ReadLine();
  112.             }
  113.             Console.WriteLine(" [x] Sent {0}", message);
  114.         }
  115.  
  116.         Console.WriteLine(" Press [enter] to exit.");
  117.         Console.ReadLine();
  118.     }
  119.  
  120.     public static long UnixTimeNow()
  121.     {
  122.         var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
  123.         return (long)timeSpan.TotalMilliseconds;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement