Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using RabbitMQ.Client;
- using System.Text;
- using System.Threading;
- using System.Globalization;
- class Program
- {
- public static void Main()
- {
- var factory = new ConnectionFactory() { HostName = "localhost" };
- factory.Port = 5672;
- //factory.UserName = "userTest";
- //factory.Password = "userTest";
- factory.Protocol = Protocols.DefaultProtocol;
- using (var connection = factory.CreateConnection())
- using(var channel = connection.CreateModel())
- {
- channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
- Console.WriteLine(" Press [enter] to exit.");
- Console.ReadLine();
- string message = string.Empty;
- Random rnd = new Random();
- for (int i = 0; i < 10; i++)
- {
- var Timestamp = new DateTimeOffset(DateTime.UtcNow).ToString();
- //message = "{ \"type\": \"HPS\", \"sensorid\" : 1, \"user\": \"b8-27-eb-97-3d-b6\", \"value\":[" +
- // Convert.ToString(i, CultureInfo.InvariantCulture) + "," +
- // Convert.ToString(i + rnd.Next(-10, 11), CultureInfo.InvariantCulture) + "],\"ts\": \"" + Timestamp + "\" }";
- long unixTime = UnixTimeNow();
- //Thread.Sleep(300);
- long unixTime2 = UnixTimeNow();
- //Thread.Sleep(300);
- long unixTime3 = UnixTimeNow();
- message = "{" +
- "\"viewer\": \"drawin\", " +
- "\"values\": [" +
- "[\"Sensor Name 1\"," + unixTime +"," + Convert.ToString(rnd.Next(0, i+3), CultureInfo.InvariantCulture) +", \"temperature\", \"running\"], " +
- "[\"Sensor Name 2\", " + unixTime2 + ", " + Convert.ToString(rnd.Next(0, i + 4), CultureInfo.InvariantCulture) + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 3\", " + unixTime3 + ", " + Convert.ToString(rnd.Next(0, i + 5), CultureInfo.InvariantCulture) + ", \"temperature\", \"running\"]" +
- "]" +
- "}";
- string r = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"-0.153"; //Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- string g = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"-0.091";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- string b = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.010";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- string a = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.009";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- string c = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.112";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- string d = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"0.190";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- string e = Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);//"-0.074";//Convert.ToString(rnd.NextDouble() * (1 - (-1)) + (-1), CultureInfo.InvariantCulture);
- message = "{" +
- "\"viewer\": \"drawin\", " +
- "\"values\": [" +
- "[\"Sensor Name 1\"," + unixTime + "," + r + ", \"temperature\", \"running\"], " +
- "[\"Sensor Name 1\"," + unixTime + "," + 10 + ", \"direction\", \"running\"], " +
- "[\"Sensor Name 2\", " + unixTime2 + ", " + g + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 2\", " + unixTime2 + ", " + 100 + ", \"direction\", \"running\"]," +
- "[\"Sensor Name 3\", " + unixTime3 + ", " + b + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 3\", " + unixTime3 + ", " + 55 + ", \"direction\", \"running\"]," +
- "[\"Sensor Name 4\", " + unixTime3 + ", " + a + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 4\", " + unixTime3 + ", " + 75 + ", \"direction\", \"running\"]," +
- "[\"Sensor Name 5\", " + unixTime3 + ", " + c + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 5\", " + unixTime3 + ", " + 180 + ", \"direction\", \"running\"]," +
- "[\"Sensor Name 6\", " + unixTime3 + ", " + d + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 6\", " + unixTime3 + ", " + 75 + ", \"direction\", \"running\"]," +
- "[\"Sensor Name 7\", " + unixTime3 + ", " + e + ", \"temperature\", \"running\"]," +
- "[\"Sensor Name 7\", " + unixTime3 + ", " + 240 + ", \"direction\", \"running\"]" +
- "]" +
- "}";
- var body = Encoding.UTF8.GetBytes(message);
- channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
- //Console.WriteLine(" [x] Sent {0}", message);
- //Console.ReadLine();
- //message = "{ \"type\": \"HPS\", \"sensorid\" : 2, \"user\": \"b8-27-eb-97-3d-b6\", \"value\":[" +
- // Convert.ToString(i , CultureInfo.InvariantCulture) + "," +
- // Convert.ToString(i + rnd.Next(-10, 11), CultureInfo.InvariantCulture) + "],\"ts\": \"" +
- // Timestamp + "\" }";
- //body = Encoding.UTF8.GetBytes(message);
- //channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
- //message = "{ \"type\": \"HPS\", \"sensorid\" : 3, \"user\": \"b8-27-eb-97-3d-b6\", \"value\":[" +
- // Convert.ToString(i, CultureInfo.InvariantCulture) + "," +
- // Convert.ToString(i + rnd.Next(-10, 11), CultureInfo.InvariantCulture) + "],\"ts\": \"" +
- // Timestamp + "\" }";
- //body = Encoding.UTF8.GetBytes(message);
- //channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
- //Console.ReadLine();
- Thread.Sleep(100);
- //Console.ReadLine();
- }
- Console.WriteLine(" [x] Sent {0}", message);
- }
- Console.WriteLine(" Press [enter] to exit.");
- Console.ReadLine();
- }
- public static long UnixTimeNow()
- {
- var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
- return (long)timeSpan.TotalMilliseconds;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement