-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceCacheManager.cs
More file actions
269 lines (231 loc) · 8.69 KB
/
DeviceCacheManager.cs
File metadata and controls
269 lines (231 loc) · 8.69 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
namespace MidiForwarder
{
/// <summary>
/// 设备缓存管理类 - 统一管理MIDI设备列表缓存
/// 支持 MIDI 1.0 和 MIDI 2.0 API,通过事件通知其他组件
/// </summary>
public class DeviceCacheManager : IDisposable
{
// 缓存数据
private List<MidiDeviceInfo> cachedInputDevices = [];
private List<MidiDeviceInfo> cachedOutputDevices = [];
private readonly object cacheLock = new();
// 定时器
private System.Windows.Forms.Timer? autoRefreshTimer;
private const int AutoRefreshInterval = 1000; // 每秒检测一次
// 状态标记
private bool isDisposed = false;
private MidiApiType currentApiType;
/// <summary>
/// 设备列表变更事件(输入或输出设备有变更时触发)
/// </summary>
public event EventHandler? DevicesChanged;
/// <summary>
/// 当前使用的 MIDI API 类型
/// </summary>
public MidiApiType CurrentApiType => currentApiType;
/// <summary>
/// 当前缓存的输入设备列表(只读副本)
/// </summary>
public IReadOnlyList<MidiDeviceInfo> CachedInputDevices
{
get
{
lock (cacheLock)
{
return cachedInputDevices.AsReadOnly();
}
}
}
/// <summary>
/// 当前缓存的输出设备列表(只读副本)
/// </summary>
public IReadOnlyList<MidiDeviceInfo> CachedOutputDevices
{
get
{
lock (cacheLock)
{
return cachedOutputDevices.AsReadOnly();
}
}
}
public DeviceCacheManager()
{
// 检测当前可用的 MIDI API
currentApiType = MidiApiFactory.IsMidi20Available ? MidiApiType.Midi20 : MidiApiType.Midi10;
Logger.Info($"DeviceCacheManager: 使用 {(currentApiType == MidiApiType.Midi20 ? "MIDI 2.0" : "MIDI 1.0")} API 获取设备列表");
}
/// <summary>
/// 获取当前缓存的设备列表
/// </summary>
public (List<MidiDeviceInfo> inputDevices, List<MidiDeviceInfo> outputDevices) GetCachedDevices()
{
lock (cacheLock)
{
return (new List<MidiDeviceInfo>(cachedInputDevices),
new List<MidiDeviceInfo>(cachedOutputDevices));
}
}
/// <summary>
/// 启动自动刷新定时器
/// </summary>
public void StartAutoRefresh()
{
if (autoRefreshTimer?.Enabled == true) return;
autoRefreshTimer?.Dispose();
autoRefreshTimer = new System.Windows.Forms.Timer
{
Interval = AutoRefreshInterval
};
autoRefreshTimer.Tick += (s, e) => PerformAutoRefresh();
autoRefreshTimer.Start();
Logger.Info("DeviceCacheManager: 自动刷新已启动");
}
/// <summary>
/// 停止自动刷新定时器
/// </summary>
public void StopAutoRefresh()
{
autoRefreshTimer?.Stop();
autoRefreshTimer?.Dispose();
autoRefreshTimer = null;
Logger.Info("DeviceCacheManager: 自动刷新已停止");
}
/// <summary>
/// 手动刷新 - 立即刷新缓存并返回最新设备列表
/// </summary>
public (List<MidiDeviceInfo> inputDevices, List<MidiDeviceInfo> outputDevices) ManualRefresh()
{
Logger.Info("DeviceCacheManager: 执行手动刷新");
// 根据当前 API 类型获取设备列表
var (newInputDevices, newOutputDevices) = GetDevicesFromCurrentApi();
// 更新缓存
lock (cacheLock)
{
cachedInputDevices = newInputDevices;
cachedOutputDevices = newOutputDevices;
}
Logger.Info($"DeviceCacheManager: 手动刷新完成 - 输入设备: {newInputDevices.Count}, 输出设备: {newOutputDevices.Count}");
return (newInputDevices, newOutputDevices);
}
/// <summary>
/// 根据当前 API 类型获取设备列表
/// </summary>
private (List<MidiDeviceInfo> inputDevices, List<MidiDeviceInfo> outputDevices) GetDevicesFromCurrentApi()
{
try
{
if (currentApiType == MidiApiType.Midi20 && MidiApiFactory.IsMidi20Available)
{
#if WINDOWS_MIDI_SERVICES
var inputDevices = Midi20Manager.GetInputDevices();
var outputDevices = Midi20Manager.GetOutputDevices();
return (inputDevices, outputDevices);
#endif
}
}
catch (Exception ex)
{
Logger.Error($"使用 MIDI 2.0 API 获取设备失败,回退到 MIDI 1.0: {ex.Message}");
currentApiType = MidiApiType.Midi10;
}
// 默认使用 MIDI 1.0
var midi10InputDevices = Midi10Manager.GetInputDevices();
var midi10OutputDevices = Midi10Manager.GetOutputDevices();
return (midi10InputDevices, midi10OutputDevices);
}
/// <summary>
/// 自动刷新 - 静默检测变更,有变更时触发事件通知UI主动获取
/// </summary>
private void PerformAutoRefresh()
{
var previousInputDevices = GetCachedDevices().inputDevices;
var previousOutputDevices = GetCachedDevices().outputDevices;
// 获取最新设备列表
var (newInputDevices, newOutputDevices) = GetDevicesFromCurrentApi();
// 检测是否有变更
bool inputChanged = !AreDeviceListsEqual(previousInputDevices, newInputDevices);
bool outputChanged = !AreDeviceListsEqual(previousOutputDevices, newOutputDevices);
// 如果没有变更,直接返回
if (!inputChanged && !outputChanged)
{
return;
}
// 更新缓存
lock (cacheLock)
{
cachedInputDevices = newInputDevices;
cachedOutputDevices = newOutputDevices;
}
Logger.Info($"DeviceCacheManager: 自动刷新检测到变更 - 输入设备变更: {inputChanged}, 输出设备变更: {outputChanged}");
// 触发设备变更事件,通知UI主动获取缓存
DevicesChanged?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// 比较两个设备列表是否相等
/// </summary>
private static bool AreDeviceListsEqual(List<MidiDeviceInfo> list1, List<MidiDeviceInfo> list2)
{
if (list1.Count != list2.Count)
return false;
for (int i = 0; i < list1.Count; i++)
{
if (list1[i].Id != list2[i].Id || list1[i].Name != list2[i].Name)
return false;
}
return true;
}
/// <summary>
/// 根据设备名称查找设备ID
/// </summary>
public string? FindInputDeviceIdByName(string deviceName)
{
lock (cacheLock)
{
var device = cachedInputDevices.FirstOrDefault(d => d.Name == deviceName);
return device?.Id;
}
}
/// <summary>
/// 根据设备名称查找设备ID
/// </summary>
public string? FindOutputDeviceIdByName(string deviceName)
{
lock (cacheLock)
{
var device = cachedOutputDevices.FirstOrDefault(d => d.Name == deviceName);
return device?.Id;
}
}
/// <summary>
/// 根据设备ID查找设备名称
/// </summary>
public string? FindInputDeviceNameById(string deviceId)
{
lock (cacheLock)
{
var device = cachedInputDevices.FirstOrDefault(d => d.Id == deviceId);
return device?.Name;
}
}
/// <summary>
/// 根据设备ID查找设备名称
/// </summary>
public string? FindOutputDeviceNameById(string deviceId)
{
lock (cacheLock)
{
var device = cachedOutputDevices.FirstOrDefault(d => d.Id == deviceId);
return device?.Name;
}
}
public void Dispose()
{
if (isDisposed) return;
StopAutoRefresh();
isDisposed = true;
GC.SuppressFinalize(this);
}
}
}