修复设备选择界面多设备选择bug

This commit is contained in:
suntao 2021-06-04 18:28:11 +08:00
parent 7729332f41
commit 5671b35dce
21 changed files with 111 additions and 56 deletions

View File

@ -1718,7 +1718,7 @@ PrefabInstance:
- target: {fileID: 2947521786256289149, guid: 95d5b69c6ebb5f34bb864b2c0756d442,
type: 3}
propertyPath: mType
value: 1
value: 5
objectReference: {fileID: 0}
- target: {fileID: 4634260803276928508, guid: 95d5b69c6ebb5f34bb864b2c0756d442,
type: 3}
@ -2274,7 +2274,7 @@ PrefabInstance:
- target: {fileID: 2947521786256289149, guid: 95d5b69c6ebb5f34bb864b2c0756d442,
type: 3}
propertyPath: mType
value: 5
value: 4
objectReference: {fileID: 0}
- target: {fileID: 4634260803276928508, guid: 95d5b69c6ebb5f34bb864b2c0756d442,
type: 3}

View File

@ -13,6 +13,11 @@ namespace Assets.Scripts.Devices
public SensorType Sensor { get; protected set; }
public string Id
{
get; protected set;
}
public virtual string Name
{
get;
@ -34,8 +39,9 @@ namespace Assets.Scripts.Devices
/// </summary>
public int SignalStrength { get; internal set; }
protected AbstractDevice(NetworkType networkType)
protected AbstractDevice(string id, NetworkType networkType)
{
this.Id = id;
this.Network = networkType;
}

View File

@ -93,8 +93,8 @@ namespace Assets.Scripts.Devices.Ant
//private readonly DeviceDetailService _deviceDetailService;
//private readonly AntManufacturerService antManufacturerService;
protected List<IPageHandler> pageHandlers = new List<IPageHandler>();
public AbstractAntDevice(String defaultSourceName, racerSportType sportType, SensorType sensor)
: base(sportType, true)
public AbstractAntDevice(string id, String defaultSourceName, racerSportType sportType, SensorType sensor)
: base(id, sportType, true)
{
State = DeviceState.Disconnected;
this.Name = defaultSourceName;

View File

@ -81,11 +81,11 @@ namespace Assets.Scripts.Devices.Ant
}
//CheckStatus();
deviceList.Add(new FitDevice());
deviceList.Add(new PowerDevice());
deviceList.Add(new CadenceDevice());
deviceList.Add(new HeartRateDevice());
deviceList.Add(new BikeSpdCadDevice());
deviceList.Add(new FitDevice(""));
deviceList.Add(new PowerDevice(""));
deviceList.Add(new CadenceDevice(""));
deviceList.Add(new HeartRateDevice(""));
deviceList.Add(new BikeSpdCadDevice(""));
var timer = new System.Timers.Timer(1000);
timer.AutoReset = true;
@ -336,27 +336,28 @@ namespace Assets.Scripts.Devices.Ant
var device1 = deviceList.SingleOrDefault(d => d.searchProfile.deviceType == deviceTypeId);
if (device1 != null)
{
var id = $"{ deviceNumber }:{ deviceTypeId}:{ transmissionTypeId }";
AbstractAntDevice device = null;
switch (device1.Sensor)
{
case SensorType.None:
break;
case SensorType.Cadence:
device = new CadenceDevice();
device = new CadenceDevice(id);
break;
case SensorType.HeartRate:
device = new HeartRateDevice();
device = new HeartRateDevice(id);
break;
case SensorType.Power:
device = new PowerDevice();
device = new PowerDevice(id);
break;
case SensorType.Speed:
break;
case SensorType.SpeedCadence:
device = new BikeSpdCadDevice();
device = new BikeSpdCadDevice(id);
break;
case SensorType.Trainer:
device = new FitDevice();
device = new FitDevice(id);
break;
case SensorType.VirtualPower:
break;

View File

@ -18,8 +18,8 @@ namespace Assets.Scripts.Devices.Ant
private RotationData _speedData = new RotationData();
private RotationData _cadenceData = new RotationData();
public BikeSpdCadDevice(double wheelCircumfrence_m = DEFAULT_WHEEL_CIRCUMFERENCE_m)
: base("Ant+ Speed&Cadence", racerSportType.Biking, SensorType.SpeedCadence)
public BikeSpdCadDevice(string id, double wheelCircumfrence_m = DEFAULT_WHEEL_CIRCUMFERENCE_m)
: base(id, "Ant+ Speed&Cadence", racerSportType.Biking, SensorType.SpeedCadence)
{
Priority = 1;
this.wheelCircumfrence_m = wheelCircumfrence_m;

View File

@ -28,8 +28,8 @@ namespace Assets.Scripts.Devices.Ant
private DateTime _now = DateTime.Now;
private RotationData _rotationData = new RotationData();
public CadenceDevice()
: base("Ant+ Cadence", racerSportType.Biking, SensorType.Cadence)
public CadenceDevice(string id)
: base(id, "Ant+ Cadence", racerSportType.Biking, SensorType.Cadence)
{
Priority = 2;
//if (speedSensor.isInUse)

View File

@ -26,7 +26,7 @@ namespace Assets.Scripts.Devices.Ant
public readonly byte uid;
public DataSourceBase(racerSportType sportType, bool isHuman):base(NetworkType.ANT)
public DataSourceBase(string id, racerSportType sportType, bool isHuman):base(id, NetworkType.ANT)
{
this.sportType = sportType;
this.isHuman = isHuman;

View File

@ -25,8 +25,8 @@ namespace Assets.Scripts.Devices.Ant
/// </summary>
private double _grade = 0;
private bool IsCalibrating { get; set; }
public FitDevice()
: base("Ant+ Trainer", racerSportType.Unknown, SensorType.Trainer)
public FitDevice(string id)
: base(id, "Ant+ Trainer", racerSportType.Unknown, SensorType.Trainer)
{
Priority = 0;
this.StateChange = (state) =>

View File

@ -26,8 +26,8 @@ namespace Assets.Scripts.Devices.Ant
//ds_AntPlus_BikeSpd speedSensor;
public HeartRateDevice()
: base("Ant+ HeartRate", racerSportType.Unknown, SensorType.HeartRate)
public HeartRateDevice(string id)
: base(id, "Ant+ HeartRate", racerSportType.Unknown, SensorType.HeartRate)
{
Priority = 2;
}

View File

@ -23,8 +23,8 @@ namespace Assets.Scripts.Devices.Ant
//List<IPageHandler> pageHandlers;
public PowerDevice()
: base("Ant+ Power", racerSportType.Biking, SensorType.Power)
public PowerDevice(string id)
: base(id, "Ant+ Power", racerSportType.Biking, SensorType.Power)
{
Priority = 2;
//pageHandlers = new List<IPageHandler>();

View File

@ -23,10 +23,15 @@ namespace Assets.Scripts.Devices.Ble
private readonly HashSet<BleServiceInfo> pendingServices = new HashSet<BleServiceInfo>();
public override string Name { get => peripheralInfo.Name; protected set => base.Name = value; }
public string Address {
get
{
return peripheralInfo.Address;
}
}
public List<ICharacteristic> Characteristics { get; protected set; } = new List<ICharacteristic>();
public BleDevice(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface, SensorType sensor) : base(NetworkType.BLE)
public BleDevice(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface, SensorType sensor) : base(peripheralInfo.Address, NetworkType.BLE)
{
this.hwInterface = bleWinHwInterface;
this.hwInterface.BluetoothStateChangedEvent += BluetoothStateChangedEvent;
@ -45,7 +50,23 @@ namespace Assets.Scripts.Devices.Ble
public void ConnectToPeripheralIfPossible()
{
this.hwInterface.ConnectPeripheral(this.peripheralInfo, PeripheralConnectedAction);
if(this.peripheralInfo == null)
{
return;
}
if(!this.hwInterface.BleState.Equals(BleState.On) || this.State != DeviceState.Disconnected)
{
return;
}
this.State = DeviceState.Connecting;
try
{
this.hwInterface.ConnectPeripheral(this.peripheralInfo, PeripheralConnectedAction);
}
catch (Exception)
{
this.State = DeviceState.Disconnected;
}
}
private void PeripheralConnectedAction(BleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse response)
@ -201,6 +222,7 @@ namespace Assets.Scripts.Devices.Ble
{
if (this.State != DeviceState.Disconnected)
{
Debug.Log("断开设备" + this.Name);
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, ()=> {
this.State = DeviceState.Disconnected;
});
@ -238,7 +260,8 @@ namespace Assets.Scripts.Devices.Ble
public override void Disconnect(bool save = true)
{
//throw new NotImplementedException();
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, null);
//this.hwInterface.DisconnectPeripheral(this.peripheralInfo, null);
this.Disconnect();
}
}
}

View File

@ -62,6 +62,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
return;
this.Cadence = 0;
}
Debug.Log("踏频值:" + this.Cadence);
}
private void HandleSpeedUpdate()

View File

@ -16,7 +16,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
private CyclingPowerMeasurement cyclingPowerMeasurement;
public CyclingPower(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Power)
{
Priority = 2;
cyclingPowerMeasurement = new CyclingPowerMeasurement(1920);
base.Characteristics.Add(cyclingPowerMeasurement);
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;

View File

@ -18,6 +18,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
private HeartRateMeasurement heartRateMeasurement;
public HeartRate(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.HeartRate)
{
Priority = 2;
heartRateMeasurement = new HeartRateMeasurement();
base.Characteristics.Add(heartRateMeasurement);

View File

@ -21,6 +21,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
public SpeedCadence(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence)
{
Debug.Log("创建速度踏频设备");
Priority = 1;
_cyclingSpeedCadenceMeasurement = new CyclingSpeedCadenceMeasurement(this._wheelCircumference);
base.Characteristics.Add(_cyclingSpeedCadenceMeasurement);

View File

@ -266,9 +266,19 @@ namespace Assets.Scripts.Ble
private void GattConnected(WclBleGattThread gattClient, BleResponse response)
{
Debug.Log($"gatt connected { response.ToString() }");
if (!response.IsSuccess)
{
gattClient.Stop();
return;
}
this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
}
private void CleanUpPeripheral(BlePeripheralInfo peripheralInfo)
{
//this.callbacks.Clear()
}
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
{
Debug.Log("gatt disconnected");
@ -326,7 +336,7 @@ namespace Assets.Scripts.Ble
}
private void GattCharacteristicChanged(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
Debug.Log("characteristic changed");
//Debug.Log("characteristic changed");
if(this.wclBleMainThread.GetGattThread(characteristic.Peripheral) != null)
{
this.characteristicReadEvent.Invoke(this, characteristic, response);
@ -418,7 +428,7 @@ namespace Assets.Scripts.Ble
public void Dispose()
{
this.wclBleMainThread.Dispose();
this.wclBleMainThread.Stop();
this.wclBleMainThread = null;
hwInterface = null;
pCache.Clear();

View File

@ -98,7 +98,7 @@ namespace Assets.Scripts.Devices.Ble.Win
});
}
public void Dispose()
public void Stop()
{
Debug.Log("停止thread");
foreach (var item in this.gattClients.Values)

View File

@ -46,7 +46,7 @@ namespace Assets.Scripts.Devices
var adapter = adapters.FirstOrDefault(a => a.Interface == connectionInterface);
if(adapter != null)
{
Debug.Log("bbbbbb " + (adapter.GetState().ToString()));
//Debug.Log("bbbbbb " + (adapter.GetState().ToString()));
return adapter.GetState();
}
return DeviceAdapterState.Unavailable;

View File

@ -41,7 +41,7 @@ public class ConnectDeviceModal : PFUIPanel
private UnityEngine.Object deviceItem;
private VerticalLayoutGroup content;
private PfUIButton connectBtn;
private Dictionary<ushort, DeviceItem> deviceList;
private Dictionary<string, DeviceItem> deviceList;
private Text noDevice,Title;
private RectTransform searchIconRect;
@ -49,7 +49,7 @@ public class ConnectDeviceModal : PFUIPanel
{
base.Awake();
deviceList = new Dictionary<ushort, DeviceItem>();
deviceList = new Dictionary<string, DeviceItem>();
var container = this.transform.Find("GameObject");
var closeBtn = container.Find("CloseBtn");
@ -64,7 +64,7 @@ public class ConnectDeviceModal : PFUIPanel
_x = searchIconRect.localPosition.x;
_y = searchIconRect.localPosition.y;
base.SetRounded(panel.GetComponent<Image>().transform, 20f);
base.SetRounded(panel.GetComponent<Image>().transform, 20f);
UIManager.AddEvent(closeBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
{
@ -82,7 +82,16 @@ public class ConnectDeviceModal : PFUIPanel
{
if (item.Value.GetStatus() == false)
{
if (item.Value.DeviceInfo.Sensor == SensorType)
if (SensorType == SensorType.SpeedCadence)
{
if (item.Value.DeviceInfo.Sensor == SensorType.SpeedCadence || item.Value.DeviceInfo.Sensor == SensorType.Cadence)
{
//Debug.Log("断开设备" + item.Value.DeviceInfo.Name);
item.Value.DeviceInfo.Disconnect();
DeviceCache.Remove(item.Value.DeviceInfo);
}
}
else if (item.Value.DeviceInfo.Sensor == SensorType)
{
item.Value.DeviceInfo.Disconnect();
DeviceCache.Remove(item.Value.DeviceInfo);
@ -90,7 +99,7 @@ public class ConnectDeviceModal : PFUIPanel
}
}
var dd = deviceList.Select(d => d.Value).Where(d => d.GetStatus()).FirstOrDefault();
if(dd != null)
if (dd != null)
{
dd.DeviceInfo.Connect();
DeviceCache.Add(dd.DeviceInfo);
@ -98,7 +107,7 @@ public class ConnectDeviceModal : PFUIPanel
this.Close();
}));
}
// Start is called before the first frame update
@ -131,6 +140,7 @@ public class ConnectDeviceModal : PFUIPanel
//var a = AntConnector.Instance().discoveredDevices;
switch (SensorType)
{
case SensorType.SpeedCadence:
case SensorType.Cadence:
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is ICadenceDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList();
break;
@ -140,9 +150,7 @@ public class ConnectDeviceModal : PFUIPanel
case SensorType.Power:
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is IPowerDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList();
break;
case SensorType.Speed:
break;
case SensorType.SpeedCadence:
case SensorType.Speed:
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is ISpeedDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList();
break;
case SensorType.Trainer:
@ -154,7 +162,7 @@ public class ConnectDeviceModal : PFUIPanel
foreach (var device in devices)
{
if (deviceList.ContainsKey(device.DeviceNumber))
if (deviceList.ContainsKey(device.Id))
{
continue;
}
@ -170,7 +178,7 @@ public class ConnectDeviceModal : PFUIPanel
deviceItemObj.Set(true);
foreach (var item in deviceList)
{
if (item.Key != deviceItemObj.DeviceInfo.DeviceNumber)
if (item.Key != deviceItemObj.DeviceInfo.Id)
{
item.Value.Set(false);
}
@ -186,7 +194,7 @@ public class ConnectDeviceModal : PFUIPanel
// }
//}));
deviceList.Add(device.DeviceNumber, deviceItemObj);
deviceList.Add(device.Id, deviceItemObj);
}
if(deviceList.All(d=>d.Value.GetStatus() == false))
@ -199,6 +207,12 @@ public class ConnectDeviceModal : PFUIPanel
//connectBtn.text.text = "DISCOUNECT";
noDevice.text = firstDevice.DeviceInfo.Name;
}
//DeviceItem firstDevice;
//var devicesTemp = deviceList.Select(d => d.Value).Where(d => d.DeviceInfo.State == DeviceState.Connected);
//if(SensorType == SensorType.Cadence || SensorType == SensorType.SpeedCadence)
//{
// firstDevice = devicesTemp.FirstOrDefault(d=>d)
//}
}
}
}

View File

@ -73,7 +73,7 @@ public class DeviceItem : Selectable, IEventSystemHandler, IPointerClickHandler
public override void OnDeselect(BaseEventData eventData)
{
//base.OnDeselect(eventData);
Debug.Log("deselect");
//Debug.Log("deselect");
//DeviceInfo.Disconnect();

View File

@ -158,7 +158,7 @@ public class DeviceView : MonoBehaviour
powerUnit.text = "W";
}
else if (SensorType == SensorType.SpeedCadence)
else if (SensorType == SensorType.Speed)
{
sprite0 = Resources.Load<Sprite>("Images/Devices/Speed_0");
sprite1 = Resources.Load<Sprite>("Images/Devices/Speed_1");
@ -166,7 +166,7 @@ public class DeviceView : MonoBehaviour
powerUnit.text = "KPH";
}
else if (SensorType == SensorType.Cadence)
else if (SensorType == SensorType.Cadence || SensorType == SensorType.SpeedCadence)
{
sprite0 = Resources.Load<Sprite>("Images/Devices/Cadence_0");
sprite1 = Resources.Load<Sprite>("Images/Devices/Cadence_1");
@ -289,15 +289,14 @@ public class DeviceView : MonoBehaviour
{
case SensorType.None:
break;
case SensorType.SpeedCadence:
case SensorType.Cadence:
return "Cadence Sensor";
case SensorType.HeartRate:
return "Heart Rate Monitor";
case SensorType.Power:
return "Power Meter";
case SensorType.Speed:
break;
case SensorType.SpeedCadence:
case SensorType.Speed:
return "Speed Meter";
case SensorType.Trainer:
return "Smart Trainer";
@ -316,6 +315,7 @@ public class DeviceView : MonoBehaviour
{
case SensorType.None:
break;
case SensorType.SpeedCadence:
case SensorType.Cadence:
//cadenceValue.text = (connectedDevice as ICadenceDevice).Cadence.ToString();
return (connectedDevice as ICadenceDevice).Cadence.ToString();
@ -324,8 +324,6 @@ public class DeviceView : MonoBehaviour
case SensorType.Power:
return (connectedDevice as IPowerDevice).Power.ToString();
case SensorType.Speed:
break;
case SensorType.SpeedCadence:
return (connectedDevice as ISpeedDevice).Speed.ToString();
case SensorType.Trainer:
//powerValue.text = (connectedDevice as IPowerDevice).Power.ToString();
@ -357,6 +355,7 @@ public class DeviceView : MonoBehaviour
{
case SensorType.None:
break;
case SensorType.SpeedCadence:
case SensorType.Cadence:
return devices.FirstOrDefault(d => d is ICadenceDevice);
case SensorType.HeartRate:
@ -369,7 +368,6 @@ public class DeviceView : MonoBehaviour
//}
return item;
case SensorType.Speed:
case SensorType.SpeedCadence:
return devices.FirstOrDefault(d => d is ISpeedDevice);
case SensorType.Trainer:
var item3 = devices.FirstOrDefault(d => d.Sensor == SensorType.Trainer);