public static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddEnvironmentVariables("DeviceSimulator:"); Configuration = builder.Build(); using (var cts = new CancellationTokenSource()) { var simulationTasks = new List <Task>(); var eventhubConnectionString = Configuration["EventHubConnectionString"]; var eventhubEventSender = new EventHubDeviceEventSender(eventhubConnectionString); int numberOfDevicesWithPeakStrategy = int.Parse(Configuration["NumberOfDevicesWithPeakStrategy"]); var devicesWithPeakStrategy = SetupDevicesWithPeakStrategy(eventhubEventSender, simulationTasks, numberOfDevicesWithPeakStrategy, cts.Token); simulationTasks.AddRange(devicesWithPeakStrategy); Console.ReadKey(true); cts.Cancel(); Console.WriteLine("Quitting. Waiting for all simulations tasks to finish."); Task.WhenAll(simulationTasks).GetAwaiter().GetResult(); } }
public ChannelSimulation(string deviceId, string channel, string unit, EventHubDeviceEventSender sender) { _deviceId = deviceId; _channel = channel; _unit = unit; _sender = sender; }
private static IEnumerable <Task> SetupDevicesWithPeakStrategy(EventHubDeviceEventSender eventhubEventSender, List <Task> simulationTasks, int numberOfDevicesWithPeakStrategy, CancellationToken cancellationToken) { Console.WriteLine($"Setting up {numberOfDevicesWithPeakStrategy} devices with a peaking strategy"); var devicesWithPeakStrat = LoadDevices(DevicesWithPeakStrategyStateFileName, numberOfDevicesWithPeakStrategy); var peakIntervals = new int[] { 5, 10, 15, 20 }; var random = new Random(); foreach (var device in devicesWithPeakStrat.Take(numberOfDevicesWithPeakStrategy - 1)) { // Randomizing the peak interval for some variation between devices var peakIntervalSeconds = random.Next(0, peakIntervals.Length); var sim = new ChannelSimulation(device, "register://electricity/0/voltage/sumli", "V", eventhubEventSender); var strat = new PeakStrategy() { MinValue = 228, MaxValue = 232, PeakValue = 300, ValueInterval = TimeSpan.FromMilliseconds(100), PeakInterval = TimeSpan.FromSeconds(peakIntervalSeconds) }; var simTask = sim.RunAsync(strat, cancellationToken); yield return(simTask); } var zeroDevice = devicesWithPeakStrat.TakeLast(1).First(); var zeroSim = new ChannelSimulation(zeroDevice, "register://electricity/0/voltage/sumli", "V", eventhubEventSender); var zeroStrat = new PeakStrategy() { MinValue = 0, MaxValue = 0, PeakValue = 230, ValueInterval = TimeSpan.FromMilliseconds(100), PeakInterval = TimeSpan.FromSeconds(60 * 5) }; var zeroSimTask = zeroSim.RunAsync(zeroStrat, cancellationToken); yield return(zeroSimTask); }