Skip to content

Commit bdd1aaa

Browse files
author
Aytackydln
committed
add Enable/Disable checkbox for rgb.net devices
1 parent deb192b commit bdd1aaa

12 files changed

Lines changed: 122 additions & 40 deletions

File tree

Project-Aurora/AuroraCommon/Devices/DeviceCommands.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ public static class DeviceCommands
55
public const string Quit = "quit";
66
public const string Enable = "enable";
77
public const string Disable = "disable";
8+
public const string EnableDevice = "enableDevice";
9+
public const string DisableDevice = "disableDevice";
810
public const string Blink = "blink";
911
public const string Remap = "remap";
1012
public const string Unmap = "unmap";
1113
public const string Share = "share";
1214
public const string Recalibrate = "recalibrate";
13-
15+
1416
// Aurora Interface
1517
public const string RemappableDevices = "remappableDevices";
1618
}

Project-Aurora/AuroraCommon/Devices/DeviceConfig.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Common.Devices;
66

7-
public sealed class DeviceConfig : INotifyPropertyChanged, IAuroraConfig
7+
public sealed partial class DeviceConfig : INotifyPropertyChanged, IAuroraConfig
88
{
99
public const string FileName = "DeviceConfig.json";
1010

@@ -32,11 +32,19 @@ public sealed class DeviceConfig : INotifyPropertyChanged, IAuroraConfig
3232

3333
public bool DangerousOpenRgbNonDirectEnable { get; set; }
3434

35-
private ObservableCollection<string>? _enabledDevices;
36-
public ObservableCollection<string> EnabledDevices
35+
private ObservableCollection<string>? _enabledControllers;
36+
[JsonPropertyName("EnabledDevices")]
37+
public ObservableCollection<string> EnabledControllers
3738
{
38-
get => _enabledDevices ??= new ObservableCollection<string>(DefaultEnabledDevices);
39-
set => _enabledDevices = value;
39+
get => _enabledControllers ??= new ObservableCollection<string>(DefaultEnabledControllers);
40+
set => _enabledControllers = value;
41+
}
42+
43+
private ObservableCollection<string>? _disabledControllerDevices;
44+
public ObservableCollection<string> DisabledControllerDevices
45+
{
46+
get => _disabledControllerDevices ??= [];
47+
set => _disabledControllerDevices = value;
4048
}
4149

4250
private static readonly Dictionary<string,string> Migrations = new()
@@ -74,7 +82,7 @@ public ObservableCollection<string> EnabledDevices
7482
{"Wooting", "Wooting (RGB.NET)"},
7583
};
7684

77-
private static HashSet<string> DefaultEnabledDevices =>
85+
private static HashSet<string> DefaultEnabledControllers =>
7886
[
7987
"Corsair (RGB.NET)",
8088
"Logitech (RGB.NET)",
@@ -85,32 +93,32 @@ public ObservableCollection<string> EnabledDevices
8593

8694
public void OnPostLoad()
8795
{
88-
_enabledDevices ??= new ObservableCollection<string>(DefaultEnabledDevices);
96+
_enabledControllers ??= new ObservableCollection<string>(DefaultEnabledControllers);
8997

9098
MigrateDevices();
9199

92100
PrioritizeDevice("Logitech (RGB.NET)", "Logitech");
93101

94-
EnabledDevices.CollectionChanged += (_, _) =>
95-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnabledDevices)));
102+
EnabledControllers.CollectionChanged += (_, _) =>
103+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnabledControllers)));
96104
}
97105

98106
private void MigrateDevices()
99107
{
100-
foreach (var loadedDeviceString in EnabledDevices.ToArray())
108+
foreach (var loadedDeviceString in EnabledControllers.ToArray())
101109
{
102110
var typeName = loadedDeviceString.Split(",")[0];
103111
if (!Migrations.TryGetValue(typeName, out var migratedValue)) continue;
104-
EnabledDevices.Remove(loadedDeviceString);
105-
EnabledDevices.Add(migratedValue);
112+
EnabledControllers.Remove(loadedDeviceString);
113+
EnabledControllers.Add(migratedValue);
106114
}
107115
}
108116

109117
private void PrioritizeDevice(string primary, string secondary)
110118
{
111-
if (EnabledDevices.Contains(primary))
119+
if (EnabledControllers.Contains(primary))
112120
{
113-
EnabledDevices.Remove(secondary);
121+
EnabledControllers.Remove(secondary);
114122
}
115123
}
116124
}

Project-Aurora/AuroraCommon/Devices/RGBNet/RemappableDevice.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
namespace Common.Devices.RGBNet;
55

66
[method: JsonConstructor]
7-
public class RemappableDevice(string deviceId, string deviceSummary, List<LedId> rgbNetLeds, SimpleColor calibration, bool remapEnabled)
7+
public class RemappableDevice(bool isEnabled, string deviceId, string deviceSummary, List<LedId> rgbNetLeds, SimpleColor calibration, bool remapEnabled)
88
{
9+
public bool IsEnabled { get; } = isEnabled;
10+
911
public string DeviceId { get; } = deviceId;
1012
public string DeviceSummary { get; } = deviceSummary;
1113

Project-Aurora/AuroraDeviceManager/AuroraPipe.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ async void ReceiveCommand(IAsyncResult ar)
7575
await _deviceManager.Disable(deviceController);
7676
break;
7777
}
78+
case DeviceCommands.EnableDevice:
79+
{
80+
var deviceId = splits.Next();
81+
await _deviceManager.EnableDevice(deviceId);
82+
break;
83+
}
84+
case DeviceCommands.DisableDevice:
85+
{
86+
var deviceId = splits.Next();
87+
await _deviceManager.DisableDevice(deviceId);
88+
break;
89+
}
7890
case DeviceCommands.Blink:
7991
{
8092
var deviceId = splits.Next();

Project-Aurora/AuroraDeviceManager/ConfigManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private async Task<DeviceConfig> CreateDefaultConfigurationFile()
100100

101101
var migratedConfig = new DeviceConfig
102102
{
103-
EnabledDevices = new ObservableCollection<string>(auroraConfig.EnabledDevices.Values),
103+
EnabledControllers = new ObservableCollection<string>(auroraConfig.EnabledDevices.Values),
104104
DeviceCalibrations = new Dictionary<string, SimpleColor>(auroraConfig.DeviceCalibrations.Values),
105105
AllowPeripheralDevices = auroraConfig.AllowPeripheralDevices,
106106
DevicesDisableHeadset = auroraConfig.DevicesDisableHeadset,
@@ -116,6 +116,11 @@ private async Task<DeviceConfig> CreateDefaultConfigurationFile()
116116
return migratedConfig;
117117
}
118118

119+
public static Task SaveDeviceConfig()
120+
{
121+
return Save(Global.DeviceConfig, DeviceConfig.ConfigFile);
122+
}
123+
119124
private static Task Save(object configuration, string path)
120125
{
121126
var content = JsonSerializer.Serialize(configuration, JsonSerializerOptions);

Project-Aurora/AuroraDeviceManager/Devices/DeviceManager.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,12 @@ public async Task ShareRemappableDevices()
226226
var remappableDevices = (
227227
from rgbNetController in rgbNetControllers
228228
from device in rgbNetController.DeviceList
229-
let calibration = Global.DeviceConfig.DeviceCalibrations.GetValueOrDefault(device.DeviceInfo.DeviceName, SimpleColor.White)
230-
let deviceSummary = $"{rgbNetController.DeviceName}: [{device.DeviceInfo.DeviceType}] {device.DeviceInfo.DeviceName}"
229+
let deviceId = device.DeviceInfo.DeviceName
230+
let isEnabled = !Global.DeviceConfig.DisabledControllerDevices.Contains(deviceId)
231+
let calibration = Global.DeviceConfig.DeviceCalibrations.GetValueOrDefault(deviceId, SimpleColor.White)
232+
let deviceSummary = $"{rgbNetController.DeviceName}: [{device.DeviceInfo.DeviceType}] {deviceId}"
231233
let rgbNetLeds = device.Select(l => l.Id).ToList()
232-
select new RemappableDevice(device.DeviceInfo.DeviceName, deviceSummary, rgbNetLeds, calibration, !rgbNetController.NeedsLayout())
234+
select new RemappableDevice(isEnabled, deviceId, deviceSummary, rgbNetLeds, calibration, !rgbNetController.NeedsLayout())
233235
).ToList();
234236

235237
var currentDevices = new CurrentDevices(remappableDevices);
@@ -299,14 +301,18 @@ public async Task Disable(string deviceController)
299301
await DeviceContainers.First(dc => dc.Device.DeviceName == deviceController).Disable();
300302
}
301303

302-
public async Task EnableDevice(string deviceController, string deviceId)
304+
public async Task EnableDevice(string deviceId)
303305
{
304-
await DeviceContainers.First(dc => dc.Device.DeviceName == deviceController).EnableDevice(deviceId);
306+
Global.DeviceConfig.DisabledControllerDevices.Remove(deviceId);
307+
await ShareRemappableDevices();
308+
await ConfigManager.SaveDeviceConfig();
305309
}
306310

307-
public async Task DisableDevice(string deviceController, string deviceId)
311+
public async Task DisableDevice(string deviceId)
308312
{
309-
await DeviceContainers.First(dc => dc.Device.DeviceName == deviceController).DisableDevice(deviceId);
313+
Global.DeviceConfig.DisabledControllerDevices.Add(deviceId);
314+
await ShareRemappableDevices();
315+
await ConfigManager.SaveDeviceConfig();
310316
}
311317

312318
private readonly byte[] _end = "\n"u8.ToArray();

Project-Aurora/AuroraDeviceManager/Devices/RGBNet/RgbNetDevice.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ protected override async Task<bool> DoInitialize(CancellationToken cancellationT
118118
}
119119
catch (TaskCanceledException)
120120
{
121+
ErrorMessage = null;
121122
return false;
122123
}
123124
}
@@ -222,6 +223,7 @@ protected override Task Shutdown()
222223
Provider.DevicesChanged -= ProviderOnDevicesChanged;
223224

224225
IsInitialized = false;
226+
ErrorMessage = null;
225227
return Task.CompletedTask;
226228
}
227229

@@ -261,6 +263,10 @@ protected override Task<bool> UpdateDevice(Dictionary<DeviceKeys, SimpleColor> k
261263
{
262264
foreach (var device in DeviceList)
263265
{
266+
if (Global.DeviceConfig.DisabledControllerDevices.Contains(device.DeviceInfo.DeviceName))
267+
{
268+
continue;
269+
}
264270
_updater.UpdateDevice(keyColors, device);
265271
}
266272
}

Project-Aurora/Project-Aurora/Controls/Control_DeviceItem.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ private void btnToggleOnOff_Click(object? sender, EventArgs e)
7575

7676
private void btnToggleEnableDisable_Click(object? sender, EventArgs e)
7777
{
78-
var deviceEnabled = !_deviceConfigs.EnabledDevices.Contains(_device.Device.DeviceName);
78+
var deviceEnabled = !_deviceConfigs.EnabledControllers.Contains(_device.Device.DeviceName);
7979
if (deviceEnabled)
8080
{
81-
_deviceConfigs.EnabledDevices.Add(_device.Device.DeviceName);
81+
_deviceConfigs.EnabledControllers.Add(_device.Device.DeviceName);
8282
var device = _device;
8383
Task.Run(async () =>
8484
{
@@ -87,7 +87,7 @@ private void btnToggleEnableDisable_Click(object? sender, EventArgs e)
8787
}
8888
else
8989
{
90-
_deviceConfigs.EnabledDevices.Remove(_device.Device.DeviceName);
90+
_deviceConfigs.EnabledControllers.Remove(_device.Device.DeviceName);
9191
}
9292
UpdateDynamic();
9393

@@ -206,7 +206,7 @@ private void UpdateDynamic()
206206
DeviceDetails.Text = _device.Device.DeviceDetails;
207207
DevicePerformance.Text = _device.Device.DeviceUpdatePerformance;
208208

209-
if (!_deviceConfigs.EnabledDevices.Contains(_device.Device.DeviceName))
209+
if (!_deviceConfigs.EnabledControllers.Contains(_device.Device.DeviceName))
210210
{
211211
BtnEnable.Content = "Enable";
212212
BtnStart.IsEnabled = false;

Project-Aurora/Project-Aurora/Devices/DeviceContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public sealed class DeviceContainer(MemorySharedDevice device, DeviceManager dev
99

1010
public async Task EnableDevice()
1111
{
12-
await deviceManager.DevicesPipe.EnableDevice(Device.DeviceName);
12+
await deviceManager.DevicesPipe.EnableController(Device.DeviceName);
1313
}
1414

1515
public async Task DisableDevice()
1616
{
17-
await deviceManager.DevicesPipe.DisableDevice(Device.DeviceName);
17+
await deviceManager.DevicesPipe.DisableController(Device.DeviceName);
1818
}
1919

2020
public void Dispose()

Project-Aurora/Project-Aurora/Devices/DevicesPipe.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,31 @@ public async Task RemapKey(string deviceId, LedId led, DeviceKeys? newKey)
5757

5858
public async Task RequestRemappableDevices()
5959
{
60-
await SendCommand( DeviceCommands.Share);
60+
await SendCommand(DeviceCommands.Share);
6161
}
6262

63-
public async Task EnableDevice(string deviceDeviceName)
63+
public async Task EnableController(string controllerName)
6464
{
65-
var command = DeviceCommands.Enable + Constants.StringSplit + deviceDeviceName;
66-
await SendCommand( command);
65+
var command = DeviceCommands.Enable + Constants.StringSplit + controllerName;
66+
await SendCommand(command);
6767
}
6868

69-
public async Task DisableDevice(string deviceDeviceName)
69+
public async Task DisableController(string controllerName)
7070
{
71-
var command = DeviceCommands.Disable + Constants.StringSplit + deviceDeviceName;
72-
await SendCommand( command);
71+
var command = DeviceCommands.Disable + Constants.StringSplit + controllerName;
72+
await SendCommand(command);
73+
}
74+
75+
public async Task EnableDevice(string deviceName)
76+
{
77+
var command = DeviceCommands.EnableDevice + Constants.StringSplit + deviceName;
78+
await SendCommand(command);
79+
}
80+
81+
public async Task DisableDevice(string deviceName)
82+
{
83+
var command = DeviceCommands.DisableDevice + Constants.StringSplit + deviceName;
84+
await SendCommand(command);
7385
}
7486

7587
public async Task Recalibrate(string deviceName, SimpleColor color)

0 commit comments

Comments
 (0)