forked from antonpup/Aurora
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathUnifiedHIDDevice.cs
More file actions
164 lines (139 loc) · 5.77 KB
/
UnifiedHIDDevice.cs
File metadata and controls
164 lines (139 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.ComponentModel;
using System.Diagnostics;
using Common;
using Common.Devices;
using Common.Utils;
namespace AuroraDeviceManager.Devices.UnifiedHID;
public class UnifiedHIDDevice : DefaultDevice
{
private Stopwatch sleepWatch = new();
private long lastUpdateTime = 0;
private long lastSleepUpdateTime;
List<UnifiedBase> allDevices = new();
List<UnifiedBase> connectedDevices = new();
public override string DeviceName => "UnifiedHID";
protected override string DeviceInfo => string.Join(", ", connectedDevices.Select(hd => hd.PrettyName));
public override bool IsInitialized => connectedDevices.Count != 0;
protected override void RegisterVariables(VariableRegistry variableRegistry)
{
variableRegistry = new VariableRegistry();
variableRegistry.Register($"{DeviceName}_update_interval", 0, "Update interval", null, 0);
variableRegistry.Register($"{DeviceName}_enable_shutdown_color", false, "Enable shutdown color");
variableRegistry.Register($"{DeviceName}_shutdown_color", SimpleColor.FromRgba(255, 255, 255, 255), "Shutdown color");
foreach (var device in allDevices)
variableRegistry.Register($"UnifiedHID_{device.GetType().Name}_enable", false,
$"Enable {(string.IsNullOrEmpty(device.PrettyName) ? device.GetType().Name : device.PrettyName)} in {DeviceName}");
}
public UnifiedHIDDevice()
{
try
{
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetLoadableTypes())
.Where(type => type.IsSubclassOf(typeof(UnifiedBase))).ToList()
.ForEach(class_ => allDevices.Add((UnifiedBase)Activator.CreateInstance(class_)));
}
catch (Exception exc)
{
Global.Logger.Error(exc, "[UnifiedHID] class could not be constructed:");
}
}
protected override Task<bool> DoInitialize(CancellationToken cancellationToken)
{
if (IsInitialized) return Task.FromResult(IsInitialized);
// Clear list from old data
connectedDevices.Clear();
try
{
foreach (var device in allDevices)
{
// Force disconnection and try a new connection
if (device.Disconnect() && device.Connect())
{
connectedDevices.Add(device);
}
}
}
catch (Exception e)
{
Global.Logger.Error(e, $"[UnifiedHID] device could not be initialized");
}
return Task.FromResult(IsInitialized);
}
protected override Task Shutdown()
{
try
{
if (IsInitialized)
{
var enableShutdownColor = Global.DeviceConfig.VarRegistry.GetVariable<bool>($"{DeviceName}_enable_shutdown_color");
var shutdownColor = Global.DeviceConfig.VarRegistry.GetVariable<SimpleColor>($"{DeviceName}_shutdown_color");
foreach (UnifiedBase dev in connectedDevices)
{
foreach (var map in dev.DeviceFuncMap)
{
if (enableShutdownColor)
map.Value.Invoke(shutdownColor.R, shutdownColor.G, shutdownColor.B);
}
dev.Disconnect();
}
connectedDevices.Clear();
}
}
catch (Exception ex)
{
Global.Logger.Error(ex, "[UnifiedHID] there was an error shutting down devices");
}
return Task.CompletedTask;
}
protected override Task<bool> UpdateDevice(Dictionary<DeviceKeys, SimpleColor> keyColors, DoWorkEventArgs e, bool forced = false)
{
var sleep = Global.DeviceConfig.VarRegistry.GetVariable<int>($"{DeviceName}_update_interval");
sleepWatch.Stop();
lastSleepUpdateTime = sleepWatch.ElapsedMilliseconds;
if (lastSleepUpdateTime > sleep)
{
sleepWatch.Restart();
}
else
{
// Resume stopWatch
sleepWatch.Start();
return Task.FromResult(false);
}
try
{
var results = new Dictionary<UnifiedBase, bool>(connectedDevices.Count);
foreach (var key in keyColors)
{
foreach (var device in connectedDevices)
{
if (!device.DeviceColorMap.TryGetValue(key.Key, out var currentColor) || currentColor == key.Value) continue;
// Apply and strip Alpha
var color = CommonColorUtils.MultiplyColorByScalar(key.Value, key.Value.A / 255.0D);
// Update current color
device.DeviceColorMap[key.Key] = color;
// Set color
results[device] = device.SetColor(key.Key, color.R, color.G, color.B);
}
}
// Check results of connected devices
foreach (var result in results)
{
if (result.Value) continue;
Global.Logger.Error("[UnifiedHID] error when updating device {KeyName}. Restarting...", result.Key.PrettyName);
// Try to restart device
if (!result.Key.Disconnect() || result.Key.Connect()) continue;
Global.Logger.Error("[UnifiedHID] unable to restart device {KeyName}. Removed from connected device!", result.Key.PrettyName);
// Remove device from connected list
connectedDevices.Remove(result.Key);
}
return Task.FromResult(true);
}
catch (Exception ex)
{
Global.Logger.Error(ex, "[UnifiedHID] error when updating device:");
return Task.FromResult(false);
}
}
}