Skip to content

Commit

Permalink
Add state and logical comparators
Browse files Browse the repository at this point in the history
This commit never should have been this big, but .NET 6 got bundled in and here we are.
  • Loading branch information
Tyler Reynolds committed Jan 12, 2022
1 parent 6c857c5 commit f5d10d6
Show file tree
Hide file tree
Showing 35 changed files with 771 additions and 624 deletions.
15 changes: 0 additions & 15 deletions Configuration/Comparator.cs

This file was deleted.

127 changes: 63 additions & 64 deletions Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,91 @@
using Newtonsoft.Json;
using Virpil.Communicator;

namespace Configuration
namespace Configuration;

public class Config
{
public class Config
{
public LogLevel? LogLevel { get; set; }
public bool CheckUpdates { get; set; } = true;
public bool CheckPrerelease { get; set; } = false;
public HashSet<string> DisabledPlugins { get; set; } = new();
public LogLevel? LogLevel { get; set; }
public bool CheckUpdates { get; set; } = true;
public bool CheckPrerelease { get; set; } = false;
public HashSet<string> DisabledPlugins { get; set; } = new();

public Dictionary<string, Device>? Devices { get; set; }
public Dictionary<string, Device>? Devices { get; set; }

/// <summary>
/// Appends the devices of the provided config to this config.
/// </summary>
/// <param name="otherConfig"></param>
public Config Append(Config otherConfig)
/// <summary>
/// Appends the devices of the provided config to this config.
/// </summary>
/// <param name="otherConfig"></param>
public Config Append(Config otherConfig)
{
if (otherConfig.LogLevel is not null)
{
if (otherConfig.LogLevel is not null)
{
LogLevel = (LogLevel) Math.Min((int) (LogLevel ?? Microsoft.Extensions.Logging.LogLevel.None),
(int) otherConfig.LogLevel);
}
LogLevel = (LogLevel) Math.Min((int) (LogLevel ?? Microsoft.Extensions.Logging.LogLevel.None),
(int) otherConfig.LogLevel);
}

if (otherConfig.CheckPrerelease) CheckPrerelease = otherConfig.CheckPrerelease;
if (otherConfig.CheckUpdates) CheckUpdates = otherConfig.CheckUpdates;
DisabledPlugins.UnionWith(otherConfig.DisabledPlugins);
if (otherConfig.CheckPrerelease) CheckPrerelease = otherConfig.CheckPrerelease;
if (otherConfig.CheckUpdates) CheckUpdates = otherConfig.CheckUpdates;
DisabledPlugins.UnionWith(otherConfig.DisabledPlugins);

if (otherConfig.Devices != null)
if (otherConfig.Devices != null)
{
if (Devices is null) Devices = new Dictionary<string, Device>();
foreach (var (deviceId, device) in otherConfig.Devices)
{
if (Devices is null) Devices = new Dictionary<string, Device>();
foreach (var (deviceId, device) in otherConfig.Devices)
if (Devices.TryGetValue(deviceId, out var thisDevice))
{
if (Devices.TryGetValue(deviceId, out var thisDevice))
foreach (var (boardType, boardActions) in device)
{
foreach (var (boardType, boardActions) in device)
if (Devices[deviceId].TryGetValue(boardType, out var thisBoardActions))
{
if (Devices[deviceId].TryGetValue(boardType, out var thisBoardActions))
foreach (var (ledNumber, ledActions) in boardActions)
{
foreach (var (ledNumber, ledActions) in boardActions)
if (Devices[deviceId][boardType].TryGetValue(ledNumber, out var thisLedActions))
{
if (Devices[deviceId][boardType].TryGetValue(ledNumber, out var thisLedActions))
{
thisLedActions.AddRange(ledActions);
}
else
{
thisLedActions = ledActions;
}

Devices[deviceId][boardType][ledNumber] = thisLedActions;
thisLedActions.AddRange(ledActions);
}
else
{
thisLedActions = ledActions;
}
}
else
{
thisBoardActions = boardActions;
}

Devices[deviceId][boardType] = thisBoardActions;
Devices[deviceId][boardType][ledNumber] = thisLedActions;
}
}
else
{
thisBoardActions = boardActions;
}
}
else
{
thisDevice = device;
}

Devices[deviceId] = thisDevice;
Devices[deviceId][boardType] = thisBoardActions;
}
}
else
{
thisDevice = device;
}
}

return this;
Devices[deviceId] = thisDevice;
}
}

public static Config? GetAllConfiguration()
{
return Directory.EnumerateFiles("Configuration", "*.json", SearchOption.AllDirectories).AsParallel()
.Select(f => JsonConvert.DeserializeObject<Config>(File.ReadAllText(f)))
.Where(c => c != null)
.Aggregate((s, t) => s!.Append(t!));
}
return this;
}

public class Device : Dictionary<BoardType, BoardActions>
public static Config? GetAllConfiguration()
{
return Directory.EnumerateFiles("Configuration", "*.json", SearchOption.AllDirectories).AsParallel()
.Select(f => JsonConvert.DeserializeObject<Config>(File.ReadAllText(f)))
.Where(c => c != null)
.Aggregate((s, t) => s!.Append(t!));
}
}

public class BoardActions : Dictionary<int, List<LedAction>>
{
}
}
public class Device : Dictionary<BoardType, BoardActions>
{
}

public class BoardActions : Dictionary<int, List<LedAction>>
{
}
7 changes: 6 additions & 1 deletion Configuration/Configuration.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,4 +17,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

</Project>
16 changes: 9 additions & 7 deletions Configuration/LedAction.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable ClassNeverInstantiated.Global

using Core;

#pragma warning disable 8618

namespace Configuration
namespace Configuration;

public class LedAction
{
public class LedAction
{
public string Color { get; set; }
public Trigger Trigger { get; set; }
}
}
public string Color { get; set; }
public BaseTrigger Trigger { get; set; }
}
36 changes: 31 additions & 5 deletions Configuration/Schema/ActionConfiguration.json.schema
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"title": "LED Action",
"description": "An action to take for a USB LED",
"type": "array",

"definitions": {
"action": {
"title": "Action",
Expand All @@ -29,12 +29,18 @@
},
"value": {
"description": "The fixed value to compare the sent value to",
"type": ["number", "string", "boolean"]
"oneOf": [
{"type": "number"},
{"type": "string"},
{"type": "boolean"},
{"$ref": "#/definitions/ledAction/definitions/action/definitions/trigger"},
{"type": "array", "items": {"$ref": "#/definitions/ledAction/definitions/action/definitions/trigger"}}
]
},
"comparator": {
"description": "The method to compare the dcs-bios value with. The equation should read as 'biosValue comparator value'",
"type": "string",
"enum": ["None", "GreaterThan", "LessThan", "EqualTo", "NotEqualTo", "GreaterThanOrEqualTo", "LessThanOrEqualTo", "RegexMatch", "RegexNoMatch"]
"enum": ["None", "GreaterThan", "LessThan", "EqualTo", "NotEqualTo", "GreaterThanOrEqualTo", "LessThanOrEqualTo", "RegexMatch", "RegexNoMatch", "And", "Or", "Xor", "Not"]
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -71,11 +77,31 @@
},
{
"if": {
"required": ["comparator"]
"properties": { "value": { "$ref": "#/definitions/ledAction/definitions/action/definitions/trigger" } },
"required": ["value"]
},
"then": {
"properties": { "comparator": { "enum": ["Not"] } },
"required": ["id", "value", "comparator"]
}
},
{
"if": {
"properties": { "value": { "type": "array", "items": {"$ref": "#/definitions/ledAction/definitions/action/definitions/trigger"}}},
"required": ["value"]
},
"then": {
"properties": { "comparator": { "enum": ["And", "Or", "Xor"] } },
"required": ["value", "comparator"]
}
},
{
"if": {
"required": ["comparator"]
},
"then": {
"required": ["value", "comparator"]
}
}
],
"required": ["id"]
Expand Down Expand Up @@ -174,4 +200,4 @@
},

"required": ["devices"]
}
}
14 changes: 0 additions & 14 deletions Configuration/Trigger.cs

This file was deleted.

42 changes: 0 additions & 42 deletions Configuration/TriggerBase.cs

This file was deleted.

18 changes: 18 additions & 0 deletions Core/AndTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Core;

/**
* Triggers if all of the child triggers pass.
*/
public class AndTrigger : BaseTrigger<List<BaseTrigger>>
{
public AndTrigger(IReadOnlyCollection<BaseTrigger> value) : base(value, Comparator.And)
{
}

public override bool ShouldTrigger(State state)
{
return Value.All(t => t.ShouldTrigger(state));
}

protected override HashSet<Comparator> AllowedComparators { get; } = new(Enum.GetValues<Comparator>());
}
Loading

0 comments on commit f5d10d6

Please sign in to comment.