-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
75 lines (63 loc) · 2.58 KB
/
Program.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Newtonsoft.Json.Linq;
namespace datagentest
{
class Program
{
private static EventHubClient eventHubClient;
private const string EhConnectionString = "Endpoint=sb://myeventhub112233.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=mykey112233;EntityPath=myeventhub";
private static int[] deviceIds = new int[] { 1, 2, 3 };
private static int[] tags = new int[] { 1, 2, 3, 4, 5 };
private static int[] minVals = new int[] { 10, 30, 60, 80, 100 };
private static int[] maxVals = new int[] { 15, 40, 65, 85, 110 };
private static int numMsgs = 100;
private static int delay = 2000;
public static void Main(string[] args)
{
while(true)
{
MainAsync(args).GetAwaiter().GetResult();
}
}
private static async Task SendMessagesToEventHub(int numMessagesToSend)
{
for (var i = 0; i < numMessagesToSend; i++)
{
try
{
var rnd = new Random();
var deviceId = deviceIds[rnd.Next(deviceIds.Length)];
var tagIdx = rnd.Next(tags.Length);
var tag = tags[tagIdx];
var val = Math.Round(minVals[tagIdx] + rnd.NextDouble() * (maxVals[tagIdx] - minVals[tagIdx]), 2);
var ts = DateTime.Now.ToUniversalTime();
var j = new JObject();
j["TS"] = ts;
j["Device"] = deviceId;
j["Tag"] = tag;
j["Value"] = val;
var message = j.ToString();
Console.WriteLine($"Sending message: {message}");
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
}
await Task.Delay(delay);
}
Console.WriteLine($"{numMessagesToSend} messages sent.");
}
private static async Task MainAsync(string[] args)
{
eventHubClient = EventHubClient.CreateFromConnectionString(EhConnectionString);
await SendMessagesToEventHub(numMsgs);
await eventHubClient.CloseAsync();
}
}
}