Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeltrao committed Sep 23, 2021
1 parent a046b14 commit 48a8656
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
13 changes: 4 additions & 9 deletions src/IotTelemetrySimulator/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@ public PayloadGenerator(IEnumerable<PayloadBase> payloads, IRandomizer randomize
}

this.Payloads = payloads.OrderByDescending(x => x.Distribution).ToArray();
this.payloadsPerDevice = new Dictionary<string, PayloadBase>();
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<string, object>) Generate(string deviceId, Dictionary<string, object> variableValues)
{
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);
Expand Down
2 changes: 1 addition & 1 deletion src/IotTelemetrySimulator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions src/IotTelemetrySimulator/RunnerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,16 @@ private static List<PayloadBase> 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;
Expand Down Expand Up @@ -391,13 +398,8 @@ private static string GetTemplateFromSection(IConfigurationSection subSection, s
return JsonConvert.SerializeObject(dictionaryVals);
}

static void ConvertToDictionary(IConfiguration configuration, Dictionary<string, string> data = null, IConfigurationSection top = null)
static void ConvertToDictionary(IConfiguration configuration, Dictionary<string, string> data, IConfigurationSection top = null)
{
if (data == null)
{
data = new Dictionary<string, string>();
}

var children = configuration.GetChildren();
foreach (var child in children)
{
Expand Down
44 changes: 29 additions & 15 deletions src/IotTelemetrySimulator/TelemetryValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,41 @@ public Dictionary<string, object> NextValues(Dictionary<string, object> 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;
}

/// <summary>
/// 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.
/// </summary>
private static void ResetNonUsedReferencedVariables(
Dictionary<string, object> previous,
Dictionary<string, object> 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);
}
}
}
}

/// <summary>
/// All possible variable names this object can produce.
/// </summary>
Expand Down

0 comments on commit 48a8656

Please sign in to comment.