diff --git a/src/IotTelemetrySimulator/PayloadGenerator.cs b/src/IotTelemetrySimulator/PayloadGenerator.cs index 1e55f51..1bcea5b 100644 --- a/src/IotTelemetrySimulator/PayloadGenerator.cs +++ b/src/IotTelemetrySimulator/PayloadGenerator.cs @@ -21,14 +21,9 @@ public PayloadGenerator(IEnumerable payloads, IRandomizer randomize } this.Payloads = payloads.OrderByDescending(x => x.Distribution).ToArray(); - this.payloadsPerDevice = new Dictionary(); - foreach (var payload in this.Payloads) - { - if (!string.IsNullOrEmpty(payload.DeviceId)) - { - this.payloadsPerDevice[payload.DeviceId] = payload; - } - } + this.payloadsPerDevice = this.Payloads + .Where(x => !string.IsNullOrEmpty(x.DeviceId)) + .ToDictionary(x => x.DeviceId); } public (byte[], Dictionary) Generate(string deviceId, Dictionary variableValues) @@ -36,7 +31,7 @@ public PayloadGenerator(IEnumerable payloads, IRandomizer randomize if (this.Payloads.Length == 1) return this.Payloads[0].Generate(variableValues); - if (!string.IsNullOrEmpty(deviceId) && this.payloadsPerDevice.TryGetValue(deviceId, out var payloadForDevice)) + if (deviceId != null && this.payloadsPerDevice.TryGetValue(deviceId, out var payloadForDevice)) return payloadForDevice.Generate(variableValues); var random = this.randomizer.Next(1, 101); diff --git a/src/IotTelemetrySimulator/Program.cs b/src/IotTelemetrySimulator/Program.cs index eb9bc2c..6f7618b 100644 --- a/src/IotTelemetrySimulator/Program.cs +++ b/src/IotTelemetrySimulator/Program.cs @@ -24,7 +24,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) var fileConfig = tempConfig["File"]; if (!string.IsNullOrEmpty(fileConfig)) { - builder.AddJsonFile(fileConfig, false, false); + builder.AddJsonFile(fileConfig, optional: false, reloadOnChange: false); } if (tempConfig is IDisposable diposableConfig) diff --git a/src/IotTelemetrySimulator/RunnerConfiguration.cs b/src/IotTelemetrySimulator/RunnerConfiguration.cs index 60a65cf..6e2cfb3 100644 --- a/src/IotTelemetrySimulator/RunnerConfiguration.cs +++ b/src/IotTelemetrySimulator/RunnerConfiguration.cs @@ -231,9 +231,16 @@ private static List LoadPayloads(IConfiguration configuration, Runn payloads = LoadPayloadsSimple(configuration, config, logger, futureVariableNames); } - if (payloads.GroupBy(x => x.DeviceId).Any(g => g.Select(x => x.Distribution).Sum() != 100)) + foreach (var group in payloads.GroupBy(x => x.DeviceId)) { - logger.LogWarning("Payload percentage distribution is not equal 100"); + var totalDistribution = group.Select(x => x.Distribution).Sum(); + if (totalDistribution != 100) + { + logger.LogWarning( + "Payload percentage distribution is {Total} != 100 for device {DeviceId}", + totalDistribution, + group.Key); + } } return payloads; @@ -391,13 +398,8 @@ private static string GetTemplateFromSection(IConfigurationSection subSection, s return JsonConvert.SerializeObject(dictionaryVals); } - static void ConvertToDictionary(IConfiguration configuration, Dictionary data = null, IConfigurationSection top = null) + static void ConvertToDictionary(IConfiguration configuration, Dictionary data, IConfigurationSection top = null) { - if (data == null) - { - data = new Dictionary(); - } - var children = configuration.GetChildren(); foreach (var child in children) { diff --git a/src/IotTelemetrySimulator/TelemetryValues.cs b/src/IotTelemetrySimulator/TelemetryValues.cs index a09ee11..1658a01 100644 --- a/src/IotTelemetrySimulator/TelemetryValues.cs +++ b/src/IotTelemetrySimulator/TelemetryValues.cs @@ -110,27 +110,41 @@ public Dictionary NextValues(Dictionary previous next[seqVar.Name] = value; } - var referencedVariables = seqVar.GetReferenceVariableNames(); - foreach (var referenceVariable in referencedVariables) - { - if (referenceVariable != usedVariable) - { - if (previous != null && previous.TryGetValue(referenceVariable, out var previousValue)) - { - next[referenceVariable] = previousValue; - } - else - { - next.Remove(referenceVariable); - } - } - } + ResetNonUsedReferencedVariables(previous, next, seqVar, usedVariable); } } return next; } + /// + /// Removes non-used variables in a sequence. + /// This way we can keep the a counter variable incrementally correctly if the sequence did not use it in current iteration. + /// + private static void ResetNonUsedReferencedVariables( + Dictionary previous, + Dictionary next, + TelemetryVariable sequenceVariable, + string usedVariable) + { + var referencedVariables = sequenceVariable.GetReferenceVariableNames(); + foreach (var referencedVariable in referencedVariables) + { + if (referencedVariable != usedVariable) + { + // Restore it from the previous value. + if (previous != null && previous.TryGetValue(referencedVariable, out var previousValue)) + { + next[referencedVariable] = previousValue; + } + else + { + next.Remove(referencedVariable); + } + } + } + } + /// /// All possible variable names this object can produce. ///