Skip to content

Commit

Permalink
Merge branch 'release/0.2.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Reynolds committed May 20, 2021
2 parents c83bc48 + 3c62179 commit 3e7d118
Show file tree
Hide file tree
Showing 17 changed files with 413 additions and 59 deletions.
3 changes: 3 additions & 0 deletions ViLA/Comparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
{
public enum Comparator
{
None,
GreaterThan,
LessThan,
EqualTo,
NotEqualTo,
GreaterThanOrEqualTo,
LessThanOrEqualTo,
RegexMatch,
RegexNoMatch,
}
}
4 changes: 3 additions & 1 deletion ViLA/Config.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable ClassNeverInstantiated.Global
#pragma warning disable 8618

using System.Collections.Generic;
using ViLA.Configuration;

namespace ViLA
{
public class Config
{
public Dictionary<string, List<Action>> Devices { get; set; }
public Dictionary<string, List<LedAction>> Devices { get; set; }
}
}
7 changes: 5 additions & 2 deletions ViLA/Action.cs → ViLA/Configuration/LedAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable ClassNeverInstantiated.Global
#pragma warning disable 8618
namespace ViLA

using ViLA.Triggers;

namespace ViLA.Configuration
{
public class Action
public class LedAction
{
public string Color { get; set; }
public Trigger Trigger { get; set; }
Expand Down
50 changes: 45 additions & 5 deletions ViLA/Configuration/Schema/ActionConfiguration.json.schema
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,61 @@

"properties": {
"id": {
"description": "The string bios code from dcs-bios to act on",
"description": "The string id from the plugin to act on",
"type": "string"
},
"value": {
"description": "The value to compare the dcs-bios value to",
"type": "integer"
"description": "The fixed value to compare the sent value to",
"type": ["number", "string", "boolean"]
},
"comparator": {
"description": "The method to compare the dcs-bios value with. The equation should read as 'biosValue comparator value'",
"type": "string",
"enum": ["GreaterThan", "LessThan", "EqualTo", "NotEqualTo", "GreaterThanOrEqualTo", "LessThanOrEqualTo"]
"enum": ["None", "GreaterThan", "LessThan", "EqualTo", "NotEqualTo", "GreaterThanOrEqualTo", "LessThanOrEqualTo", "RegexMatch", "RegexNoMatch"]
}
},
"additionalProperties": false,
"required": ["id", "value", "comparator"]
"allOf": [
{
"if": {
"properties": { "value": { "type": "number" } },
"required": ["value"]
},
"then": {
"properties": { "comparator": { "enum": ["GreaterThan", "LessThan", "EqualTo", "NotEqualTo", "GreaterThanOrEqualTo", "LessThanOrEqualTo"] } },
"required": ["id", "value", "comparator"]
}
},
{
"if": {
"properties": { "value": { "type": "string" } },
"required": ["value"]
},
"then": {
"properties": { "comparator": { "enum": ["EqualTo", "NotEqualTo", "RegexMatch", "RegexNoMatch"] } },
"required": ["id", "value", "comparator"]
}
},
{
"if": {
"properties": { "value": { "type": "boolean" } },
"required": ["value"]
},
"then": {
"properties": { "comparator": { "enum": ["EqualTo", "NotEqualTo"] } },
"required": ["id", "value", "comparator"]
}
},
{
"if": {
"required": ["comparator"]
},
"then": {
"required": ["id", "value", "comparator"]
}
}
],
"required": ["id"]
}
},

Expand Down
3 changes: 2 additions & 1 deletion ViLA/Target.cs → ViLA/Configuration/Target.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable ClassNeverInstantiated.Global
#pragma warning disable 8618

using Virpil.Communicator;

namespace ViLA
namespace ViLA.Configuration
{
public class Target
{
Expand Down
19 changes: 15 additions & 4 deletions ViLA/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using McMaster.NETCore.Plugins;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ViLA.Configuration;
using ViLA.Triggers;
using Virpil.Communicator;

namespace ViLA
Expand Down Expand Up @@ -41,6 +43,11 @@ public static async Task Main(string[] args)

var devices = DeviceCommunicator.AllConnectedVirpilDevices(loggerFactory).ToList();

foreach (var device in devices)
{
_log.LogInformation("Detected device with PID {Device:x4}", device.PID);
}

var r = new Runner(devices, cfg.Devices, plugins, loggerFactory.CreateLogger<Runner>());

await r.Start(loggerFactory);
Expand Down Expand Up @@ -81,14 +88,18 @@ public static async Task Main(string[] args)
}
}

public class DeviceAction
public class DeviceAction<T> where T : TriggerBase
{
public Action Action { get; }
public ushort Device { get; }
public T Trigger { get; }
public string Color { get; }
public Target Target { get; }

public DeviceAction(Action action, ushort device)
public DeviceAction(string color, T trigger, Target target, ushort device)
{
Action = action;
Color = color;
Trigger = trigger;
Target = target;
Device = device;
}
}
Expand Down
115 changes: 101 additions & 14 deletions ViLA/Runner.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ViLA.Configuration;
using ViLA.Triggers;
using Virpil.Communicator;

namespace ViLA
Expand All @@ -11,10 +14,17 @@ public class Runner
{
private readonly Dictionary<ushort, DeviceCommunicator> _devices;
private readonly List<PluginBase.PluginBase> _plugins;
private readonly Dictionary<string, List<DeviceAction>> _actions = new();

// id => action
private readonly Dictionary<string, List<DeviceAction<TriggerBase<long>>>> _longActions = new();
private readonly Dictionary<string, List<DeviceAction<TriggerBase<string>>>> _stringActions = new();
private readonly Dictionary<string, List<DeviceAction<TriggerBase<double>>>> _doubleActions = new();
private readonly Dictionary<string, List<DeviceAction<TriggerBase<bool>>>> _boolActions = new();
private readonly Dictionary<string, List<DeviceAction<TriggerBase>>> _basicActions = new();

private readonly ILogger<Runner> _log;

public Runner(IEnumerable<DeviceCommunicator> devices, Dictionary<string, List<Action>> deviceActions, List<PluginBase.PluginBase> plugins, ILogger<Runner> log)
public Runner(IEnumerable<DeviceCommunicator> devices, Dictionary<string, List<LedAction>> deviceActions, List<PluginBase.PluginBase> plugins, ILogger<Runner> log)
{
_log = log;
_plugins = plugins;
Expand All @@ -25,22 +35,64 @@ public Runner(IEnumerable<DeviceCommunicator> devices, Dictionary<string, List<A
var deviceShort = ushort.Parse(device, NumberStyles.HexNumber);
foreach (var action in actions)
{
if (!_actions.ContainsKey(action.Trigger.Id))
try
{
if (action.Trigger.TryGetLongTrigger(out var longTrigger))
{
SetUpTrigger(_longActions, longTrigger, action, deviceShort);
}
else if (action.Trigger.TryGetStringTrigger(out var stringTrigger))
{
SetUpTrigger(_stringActions, stringTrigger, action, deviceShort);
}
else if (action.Trigger.TryGetDoubleTrigger(out var doubleTrigger))
{
SetUpTrigger(_doubleActions, doubleTrigger, action, deviceShort);
}
else if (action.Trigger.TryGetBoolTrigger(out var boolTrigger))
{
SetUpTrigger(_boolActions, boolTrigger, action, deviceShort);
}
else if (action.Trigger.TryGetBasicTrigger(out var basicTrigger))
{
SetUpTrigger(_basicActions, basicTrigger, action, deviceShort);
}
else
{
log.LogError("Skipping action for {Id} because no supported trigger configuration was found",
action.Trigger.Id);
}
}
catch (ArgumentException)
{
_actions[action.Trigger.Id] = new List<DeviceAction>();
_log.LogError("Unsupported comparator passed for {Id}. Skipping...", action.Trigger.Id);
}
_actions[action.Trigger.Id].Add(new DeviceAction(action, deviceShort));
}
}
}

private static void SetUpTrigger<T>(IDictionary<string, List<DeviceAction<T>>> dict, T trigger,
LedAction ledAction, ushort deviceShort) where T : TriggerBase
{
if (!dict.ContainsKey(ledAction.Trigger.Id))
{
dict[ledAction.Trigger.Id] = new List<DeviceAction<T>>();
}

dict[ledAction.Trigger.Id].Add(new DeviceAction<T>(ledAction.Color, trigger, ledAction.Target, deviceShort));
}

public async Task Start(ILoggerFactory loggerFactory)
{
_log.LogInformation("Starting plugins...");

foreach (var plugin in _plugins)
{
plugin.SendData += Action;
plugin.SendData += LongAction;
plugin.SendString += StringAction;
plugin.SendFloat += DoubleAction;
plugin.SendBool += BoolAction;
plugin.SendTrigger += TriggerAction;
plugin.LoggerFactory = loggerFactory;

_log.LogDebug("Starting plugin {Plugin}", plugin.GetType().Name);
Expand All @@ -60,18 +112,53 @@ public async Task Start(ILoggerFactory loggerFactory)
_log.LogInformation("Plugins started");
}

private void Action(string id, int value)
private void LongAction(string id, int value)
{
TriggerActionForValue(_longActions, id, value);
}

private void StringAction(string id, string value)
{
TriggerActionForValue(_stringActions, id, value);
}

private void DoubleAction(string id, float value)
{
TriggerActionForValue(_doubleActions, id, value);
}

private void BoolAction(string id, bool value)
{
TriggerActionForValue(_boolActions, id, value);
}

private void TriggerActionForValue<T>(IReadOnlyDictionary<string, List<DeviceAction<TriggerBase<T>>>> typedActions, string id, T value)
{
if (!typedActions.TryGetValue(id, out var actions)) return; // nothing for this bios code, then leave

_log.LogTrace("got {Type} data {Data} for {Id}", nameof(T), value, id);
foreach (var action in actions.Where(action => action.Trigger.ShouldTrigger(value)))
{
if (!_devices.TryGetValue(action.Device, out var device)) continue;

_log.LogDebug("Triggering {Id}", id);
var (red, green, blue) = action.Color.ToLedPowers();
device.SendCommand(action.Target.BoardType, action.Target.LedNumber, red, green, blue);
}
}

private void TriggerAction(string id)
{
if (!_actions.TryGetValue(id, out var actions)) return; // nothing for this bios code, then leave
if (!_basicActions.TryGetValue(id, out var actions)) return; // nothing for this bios code, then leave

_log.LogDebug("got data for {Id}", id);
foreach (var action in actions.Where(action => action.Action.Trigger.ShouldTrigger(value)))
_log.LogTrace("got trigger for {Id}", id);
foreach (var action in actions)
{
if (!_devices.TryGetValue(action.Device, out var device)) continue;

_log.LogDebug("got relevant data for {Id}, sending...", id);
var (red, green, blue) = action.Action.Color.ToLedPowers();
device.SendCommand(action.Action.Target.BoardType, action.Action.Target.LedNumber, red, green, blue);
_log.LogDebug("Triggering {Id}", id);
var (red, green, blue) = action.Color.ToLedPowers();
device.SendCommand(action.Target.BoardType, action.Target.LedNumber, red, green, blue);
}
}
}
Expand Down
28 changes: 0 additions & 28 deletions ViLA/Trigger.cs

This file was deleted.

9 changes: 9 additions & 0 deletions ViLA/Triggers/BasicTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ViLA.Triggers
{
public class BasicTrigger : TriggerBase
{
public BasicTrigger(string id) : base(id)
{
}
}
}
Loading

0 comments on commit 3e7d118

Please sign in to comment.