-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceManager.cs
305 lines (267 loc) · 11.5 KB
/
DeviceManager.cs
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using Suconbu.Mobile;
using Suconbu.Toolbox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Collections.Concurrent;
namespace Suconbu.Sumacon
{
public class DeviceManager : IDisposable
{
public event EventHandler ConnectedDevicesChanged = delegate { };
// ConnectedDevicesChangedの後に通知します(対象はConnectedDevicesにすでに存在します)
public event EventHandler<Device/* connectedDevice */> DeviceConnected = delegate { };
// ConnectedDevicesChangedの前に通知します(対象はConnectedDevicesにまだ存在します)
public event EventHandler<Device/* disconnectingDevice */> DeviceDisconnecting = delegate { };
public event EventHandler<Device/* previousDevice */> ActiveDeviceChanged = delegate { };
public event EventHandler<IReadOnlyList<Property>> PropertyChanged = delegate { };
public event EventHandler<TouchProtocolType> TouchProtocolTypeChanged = delegate { };
public Device ActiveDevice
{
get { return this.activeDevice; }
set { this.ChangeActiveDevice(value); }
}
public IReadOnlyList<Device> ConnectedDevices { get { return this.connectedDevices.Values.ToList(); } }
public TouchProtocolType TouchProtocolType
{
get => this.touchProtocolType;
set
{
if(this.touchProtocolType != value)
{
this.touchProtocolType = value;
if(this.activeDevice != null)
{
this.activeDevice.Input.TouchProtocol = value;
}
this.TouchProtocolTypeChanged(this, value);
}
}
}
Device activeDevice;
readonly ConcurrentDictionary<string/*serial*/, Device> connectedDevices = new ConcurrentDictionary<string, Device>();
readonly ConcurrentDictionary<string/*serial*/, int/*port*/> wirelessWaitingDevices = new ConcurrentDictionary<string, int>();
readonly DeviceDetector detector = new DeviceDetector();
readonly Dictionary<string/*serial*/, int> susupendRequestedCount = new Dictionary<string, int>();
readonly Dictionary<string/*serial*/, Dictionary<Device.UpdatableProperties, string>> intervalKeys = new Dictionary<string, Dictionary<Device.UpdatableProperties, string>>();
readonly Dictionary<Device.UpdatableProperties, int> intervalMilliseconds = new Dictionary<Device.UpdatableProperties, int>();
TouchProtocolType touchProtocolType;
readonly int kWirelessPortMin = 5500;
readonly int kWirelessPortMax = 5600;
readonly Random random = new Random();
public DeviceManager()
{
this.detector.Connected += this.Detector_Connected;
this.detector.Disconnected += this.Detector_Disconnected;
this.LoadSettings();
//this.intervalMilliseconds[Device.UpdatableProperties.Component] = 30 * 1000;
this.intervalMilliseconds[Device.UpdatableProperties.ProcessInfo] = 10 * 1000;
}
/// <summary>
/// デバイスの接続状態監視を開始します。
/// 接続状態の変化は下記イベントで通知します。
/// - ConnectedDevicesChanged
/// - DeviceConnected
/// - DeviceDisconnecting
/// </summary>
public void StartDeviceDetection()
{
this.detector.Start();
}
/// <summary>
/// デバイスの接続状態監視を停止します。
/// </summary>
public void StopDeviceDetection()
{
this.detector.Stop();
}
/// <summary>
/// デバイスからの定期的な情報取得を一時的に停止します。
/// 再開するには ResumePropertyUpdate を呼び出します。
/// </summary>
public void SuspendPropertyUpdate(Device device)
{
if (device == null) return;
Trace.TraceInformation($"{Util.GetCurrentMethodName()} - {device.Serial} count:{this.susupendRequestedCount[device.Serial]}->{this.susupendRequestedCount[device.Serial] + 1}");
lock (this.susupendRequestedCount)
{
Debug.Assert(this.susupendRequestedCount[device.Serial] >= 0);
this.susupendRequestedCount[device.Serial]++;
}
}
/// <summary>
/// デバイスからの定期的な情報取得を再開します。
/// </summary>
public void ResumePropertyUpdate(Device device)
{
if (device == null) return;
Trace.TraceInformation($"{Util.GetCurrentMethodName()} - {device.Serial} count:{this.susupendRequestedCount[device.Serial]}->{this.susupendRequestedCount[device.Serial] - 1}");
lock (this.susupendRequestedCount)
{
if (this.susupendRequestedCount[device.Serial] > 0)
{
this.susupendRequestedCount[device.Serial]--;
}
}
}
public void StartWireless(Device device)
{
if (!this.wirelessWaitingDevices.ContainsKey(device.Serial))
{
var port = this.GenerateWirelessPortNo();
this.wirelessWaitingDevices[device.Serial] = port;
device.RunCommandAsync($"tcpip {port}");
// 一旦接続切れてまたつながる
}
}
int GenerateWirelessPortNo()
{
return this.random.Next(this.kWirelessPortMin, this.kWirelessPortMax);
}
void ChangeActiveDevice(Device nextActiveDevice)
{
lock (this)
{
if (this.activeDevice == nextActiveDevice) return;
var previousDevice = this.activeDevice;
foreach (var component in (previousDevice?.Components).OrEmptyIfNull())
{
component.PropertyChanged -= this.DeviceComponent_PropertyChanged;
}
if (previousDevice != null)
{
this.StopPropertyUpdate(previousDevice);
}
this.activeDevice = nextActiveDevice;
if (this.activeDevice != null)
{
this.activeDevice.Input.TouchProtocol = this.touchProtocolType;
}
foreach (var component in (this.activeDevice?.Components).OrEmptyIfNull())
{
component.PropertyChanged += this.DeviceComponent_PropertyChanged;
}
if (this.activeDevice != null)
{
this.StartPropertyUpdate(this.activeDevice);
}
this.ActiveDeviceChanged(this, previousDevice);
}
}
void Detector_Connected(object sender, string serial)
{
if(this.wirelessWaitingDevices.TryRemove(serial, out var port))
{
// ワイヤレス希望なので登録はせず接続試行
this.ReconnectDeviceAsWireless(serial, port);
}
else
{
this.AddConnectedDevice(serial);
}
}
void Detector_Disconnected(object sender, string serial)
{
this.RemoveConnectedDevice(serial);
}
void DeviceComponent_PropertyChanged(object sender, IReadOnlyList<Property> properties)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
foreach (var p in properties) Trace.TraceInformation($"- {p.ToString()}");
this.PropertyChanged(sender, properties);
}
void StartPropertyUpdate(Device device)
{
this.susupendRequestedCount[device.Serial] = 0;
this.intervalKeys[device.Serial] = new Dictionary<Device.UpdatableProperties, string>();
foreach(var updatableProperty in this.intervalMilliseconds.Keys)
{
device.UpdatePropertiesAsync(updatableProperty);
this.intervalKeys[device.Serial][updatableProperty] = Delay.SetInterval(() =>
{
if (this.susupendRequestedCount[device.Serial] == 0)
{
device.UpdatePropertiesAsync(updatableProperty);
}
}, this.intervalMilliseconds[updatableProperty]);
}
}
void StopPropertyUpdate(Device device)
{
foreach (var updatableProperty in this.intervalMilliseconds.Keys)
{
if (this.intervalKeys[device.Serial].TryGetValue(updatableProperty, out var id))
{
Delay.ClearInterval(id);
}
}
this.intervalKeys.Remove(device.Serial);
}
void AddConnectedDevice(string serial)
{
if (this.connectedDevices.ContainsKey(serial)) return;
var device = new Device(serial);
this.connectedDevices[serial] = device;
this.susupendRequestedCount[serial] = 0;
this.ConnectedDevicesChanged(this, EventArgs.Empty);
this.DeviceConnected(this, device);
if (this.activeDevice == null || device.HasWirelessConnection)
{
this.ChangeActiveDevice(device);
}
device.UpdatePropertiesAsync();
}
void RemoveConnectedDevice(string serial)
{
if (!this.connectedDevices.TryGetValue(serial, out var device)) return;
this.DeviceDisconnecting(this, device);
this.connectedDevices.TryRemove(serial, out var removed);
this.ConnectedDevicesChanged(this, EventArgs.Empty);
if (this.activeDevice == device)
{
var nextActiveDevice = this.connectedDevices.Values.FirstOrDefault();
this.ChangeActiveDevice(nextActiveDevice);
}
device.Dispose();
}
void ReconnectDeviceAsWireless(string serial, int port)
{
var device = new Device(serial); // Update掛けるのやめたい
device.UpdatePropertiesAsync(Device.UpdatableProperties.SystemComponent, () =>
{
var wirelessSerial = $"{device.IpAddress}:{port}";
device.RunCommandAsync($"connect {wirelessSerial}");
device.Dispose();
// この後、serial=wirelessSerialでDetector_Connectedくる
});
}
void LoadSettings()
{
this.TouchProtocolType = Properties.Settings.Default.DeviceManagerTouchProtocolType;
this.TouchProtocolTypeChanged(this, this.TouchProtocolType);
}
void SaveSettings()
{
Properties.Settings.Default.DeviceManagerTouchProtocolType = this.TouchProtocolType;
}
#region IDisposable Support
bool disposed = false;
public virtual void Dispose()
{
if (this.disposed) return;
this.SaveSettings();
foreach (var keys in this.intervalKeys.Values)
{
foreach (var key in keys.Values)
{
Delay.ClearInterval(key);
}
}
this.StopDeviceDetection();
foreach (var device in this.connectedDevices.Values) device.Dispose();
this.disposed = true;
}
#endregion
}
}