-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DummyProducerService.cs
72 lines (59 loc) · 2.61 KB
/
DummyProducerService.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
namespace MultiEventsConsumer;
internal partial class DummyProducerService(IEventPublisher publisher, ILogger<DummyProducerService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Wait for bus to be ready
await Task.Delay(TimeSpan.FromSeconds(8), stoppingToken);
// Generate random vehicle Ids
var rnd = new Random(DateTimeOffset.UtcNow.Millisecond);
var vehicles = Enumerable.Range(0, 5).Select(_ => GenerateRandomString(rnd)).ToList();
// Find the number of possible combinations
var kinds = Enum.GetValues<DoorKind>().ToList();
var states = Enum.GetValues<DoorState>().ToList();
var combinations = vehicles.Count * kinds.Count * states.Count;
// Select a portion of the combinations
var times = rnd.Next(1, combinations);
logger.LogInformation("Selected {Times} of {Combinations} combinations.", times, combinations);
var delay = TimeSpan.FromSeconds(15);
for (var i = 0; i < times; i++)
{
// Select vehicle, door kind and door state randomly
var vehicle = vehicles[rnd.Next(0, combinations) * 1 % vehicles.Count];
var kind = kinds[rnd.Next(0, combinations) * i % kinds.Count];
var state = states[rnd.Next(0, combinations) * i % states.Count];
// Publish event depending on the door state
if (state == DoorState.Closed)
{
var evt = new DoorClosed
{
VehicleId = vehicle,
Closed = DateTimeOffset.UtcNow,
Kind = kind,
};
await publisher.PublishAsync(evt, cancellationToken: stoppingToken);
}
else
{
var evt = new DoorOpened
{
VehicleId = vehicle,
Opened = DateTimeOffset.UtcNow,
Kind = kind,
};
await publisher.PublishAsync(evt, cancellationToken: stoppingToken);
}
await Task.Delay(delay, stoppingToken);
}
logger.LogInformation("Finished producing dummy data!");
}
private static string GenerateRandomString(Random random)
{
var bys = new byte[20];
random.NextBytes(bys);
var result = Convert.ToBase64String(bys);
return AlphaNumeric().Replace(result, "");
}
[System.Text.RegularExpressions.GeneratedRegex("[^A-Za-z0-9]")]
private static partial System.Text.RegularExpressions.Regex AlphaNumeric();
}