-
Notifications
You must be signed in to change notification settings - Fork 1
/
JoyManager.cs
306 lines (290 loc) · 11.6 KB
/
JoyManager.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
306
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TimeBoxJoy.MapConfig;
using TimeBoxJoy.Utils;
namespace TimeBoxJoy
{
public class JoyManager
{
private List<BTDeviceInfo> timeBoxJoyList;
private BluetoothComponent blueComponent;
private BluetoothClient blueClient;
private DateTime startScan = DateTime.Now;
public TimeSpan ScanBlueTimeOut = TimeSpan.FromSeconds(30);
public int State = 0;//0=STOP,1=SCANNING
public List<string> rememberMac { get; set; }
public List<DefaultMapConfig> mapConfigs { get; set; }
public Action<List<BTDeviceInfo>> OnJoyStateChange { get; set; } = null;
public Action<Byte[]> OnJoyMessageReceive { get; set; } = null;
public Action<string> OnMessage { get; set; } = null;
public Action OnStartScanDevice { get; set; } = null;
public Action OnEndScanDevice { get; set; } = null;
public JoyManager(
Action<List<BTDeviceInfo>> OnJoyStateChange=null,
Action<Byte[]> OnJoyMessageReceive=null,
Action<string> OnMessage=null,
Action OnStartScanDevice=null,
Action OnEndScanDevice=null)
{
timeBoxJoyList = new List<BTDeviceInfo>();
this.OnJoyStateChange = OnJoyStateChange;
this.OnJoyMessageReceive = OnJoyMessageReceive;
this.OnMessage = OnMessage;
this.OnStartScanDevice = OnStartScanDevice;
this.OnEndScanDevice = OnEndScanDevice;
//读取所有配置文件
mapConfigs = new List<DefaultMapConfig>();
if (!Directory.Exists("maps"))
{
Directory.CreateDirectory("maps");
}
mapConfigs.Add(new KeyBoardConfig());
mapConfigs.Add(new XInputConfig());
mapConfigs.Add(new MixModeConfig());
mapConfigs.Add(new ScriptModeConfig());
foreach (var file in Directory.GetFiles("maps"))
{
var _config = DefaultMapConfig.GetConfig(file);
//FileInfo finfo = new FileInfo(file);
_config.Name = System.IO.Path.GetFileNameWithoutExtension(file);
if (!mapConfigs.Any(p => p.Name == _config.Name))
{
mapConfigs.Add(_config);
}
}
//读取已记忆的MAC设备
rememberMac = new List<string>();
if (File.Exists("remember.txt"))
{
FileHelper fh = new FileHelper();
foreach(var addr in fh.readFileLine("remember.txt"))
{
try
{
if (string.IsNullOrWhiteSpace(addr))
{
continue;
}
if (!addr.Contains(":"))
{
continue;
}
string macAddr = addr.Split(':')[0];
string configName = addr.Split(':')[1];
if (rememberMac.Contains(macAddr))
{
continue;
}
rememberMac.Add(macAddr);
BluetoothAddress address = BluetoothAddress.Parse(macAddr);
BluetoothDeviceInfo info = new BluetoothDeviceInfo(address);
var btDevice = new BTDeviceInfo(info);
btDevice.State = deviceState.LOST;
btDevice.mapConfig = mapConfigs.Where(i => i.Name == configName).FirstOrDefault();
this.timeBoxJoyList.Add(btDevice);
}
catch
{
}
}
this.OnJoyStateChange?.Invoke(this.timeBoxJoyList);
}
//初始化蓝牙设备
blueClient = new BluetoothClient();
blueComponent = new BluetoothComponent(blueClient);
string name = "timebox";
blueComponent.DiscoverDevicesProgress += (sender, e) => {
foreach (var device in e.Devices)
{
if (device.DeviceName == name && !this.timeBoxJoyList.Any(p => p.bluetoothDeviceInfo.DeviceAddress.ToString() == device.DeviceAddress.ToString()))
{
this.timeBoxJoyList.Add(new BTDeviceInfo(device));
}
}
this.OnJoyStateChange?.Invoke(this.timeBoxJoyList);
};
blueComponent.DiscoverDevicesComplete += (sender, e) => {
if ((DateTime.Now - startScan) < ScanBlueTimeOut)
{
blueComponent.DiscoverDevicesAsync(30, true, true, true, true, null);
}
else
{
this.State = 0;
this.OnEndScanDevice?.Invoke();
}
};
//启动自动扫描手柄状态线程
Thread tr = new Thread(new ThreadStart(() => {
ScanJoyConnection();
}));
tr.Start();
}
public void ScanBTDevice()
{
if (this.State == 0)
{
OnStartScanDevice?.Invoke();
this.timeBoxJoyList.RemoveAll(i => i.State == deviceState.DISCONNECT);
this.State = 1;
startScan = DateTime.Now;
blueComponent.DiscoverDevicesAsync(30, true, true, true, true, null);
}
}
private void ConnectJoy(BTDeviceInfo joy)
{
string addr = joy.bluetoothDeviceInfo.DeviceAddress.ToString();
JoyStick joyst = new JoyStick(addr);
joy.joyStick = joyst;
if (joy.joyStick.StartConnect(0))
{
joy.State = deviceState.CONNECT;
OnJoyStateChange?.Invoke(this.timeBoxJoyList);
OnMessage?.Invoke(addr+"连接成功!");
joy.joyStick.OnReceive = buffer => {
//string text = HexHelper.byteToHexStr(buffer, 18);
//this.ShowMsg(text);
this.OnJoyMessageReceive?.Invoke(buffer);
};
//joy.joyStick.SetJoyMap(new KeyBoardJoyMap());
joy.SetJoyMap(this,joy.mapConfig);
joy.joyStick.startFeed();
this.rememberMac.Remove(addr);
this.rememberMac.Add(addr+":"+ joy.mapConfig.Name);
FileHelper fh = new FileHelper();
fh.SaveFile("remember.txt", string.Join("\r\n", this.rememberMac));
}
}
/// <summary>
/// 循环检测断开的连接自动重连,以及检测手柄的连接状态
/// </summary>
private void ScanJoyConnection()
{
while (true)
{
//BTDeviceInfo joy = timeBoxJoyList.Where(i => i.State == deviceState.CONNECT).FirstOrDefault();
//foreach(var joy in timeBoxJoyList.Where(i => i.State == deviceState.CONNECT))
//{
// if (!joy.joyStick.CheckConnect())
// {
// joy.State = deviceState.LOST;
// joy.joyMap.Dispose();
// OnJoyStateChange?.Invoke(this.timeBoxJoyList);
// }
//}
//foreach(var joy in timeBoxJoyList.Where(i => i.State == deviceState.CONNECTING))
//{
// ConnectJoy(joy);
//}
//foreach(var joy in timeBoxJoyList.Where(i => i.State == deviceState.LOST))
//{
// ConnectJoy(joy);
//}
foreach(var joy in timeBoxJoyList)
{
switch (joy.State)
{
case deviceState.CONNECT:
if (!joy.joyStick.CheckConnect())
{
joy.State = deviceState.LOST;
joy.joyMap.Dispose();
OnJoyStateChange?.Invoke(this.timeBoxJoyList);
OnMessage?.Invoke(joy.bluetoothDeviceInfo.DeviceAddress.ToString() + "连接已丢失!");
}
break;
case deviceState.CONNECTING:
ConnectJoy(joy);
break;
case deviceState.LOST:
ConnectJoy(joy);
break;
}
}
Thread.Sleep(500);
}
}
/// <summary>
/// 连接一个手柄
/// </summary>
/// <param name="index"></param>
public void ConnectJoy(int index)
{
var joy = GetDevice(index);
if (joy != null)
{
if (joy.State == deviceState.DISCONNECT)
{
joy.State = deviceState.CONNECTING;
OnJoyStateChange?.Invoke(this.timeBoxJoyList);
OnMessage?.Invoke(joy.bluetoothDeviceInfo.DeviceAddress.ToString() + "连接中!");
}
}
}
/// <summary>
/// 断连一个手柄
/// </summary>
/// <param name="index"></param>
public void DisconnectJoy(int index)
{
var joy = GetDevice(index);
if (joy != null)
{
//if (joy.State == deviceState.CONNECT || joy.State == deviceState.CONNECTING)
//{
// joy.State = deviceState.DISCONNECT;
// joy.joyStick.Disconnect();
// OnJoyStateChange?.Invoke(this.timeBoxJoyList);
//}
joy.State = deviceState.DISCONNECT;
joy.joyStick.Disconnect();
OnJoyStateChange?.Invoke(this.timeBoxJoyList);
OnMessage?.Invoke(joy.bluetoothDeviceInfo.DeviceAddress.ToString() + "连接已断开!");
var addr = joy.bluetoothDeviceInfo.DeviceAddress.ToString();
this.rememberMac.Remove(addr);
FileHelper fh = new FileHelper();
fh.SaveFile("remember.txt", string.Join("\r\n", this.rememberMac));
}
}
public BTDeviceInfo GetDevice(int index)
{
var joy = this.timeBoxJoyList.Skip(index).FirstOrDefault();
return joy;
}
int _led = 1;
public void ChangeLed(int index)
{
var joy = GetDevice(index);
if (joy != null)
{
int led = _led++ > 3 ? _led = 1: _led;
//int led = _led++;
byte[] secret = new byte[]{
3,
85,
170,
1,
11,
1,
1,
0
};
secret[6] = (byte)led;
joy.SendData(HexHelper.getSignStr(secret));
WriteLog("切换LED灯");
}
}
public void WriteLog(string value)
{
this.OnMessage?.Invoke(value);
}
}
}