2021-04-01 09:33:19 +08:00
|
|
|
|
using Assets.Scripts.Devices.Ant;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using Assets.Scripts.Devices.Ant.Interfaces;
|
2021-04-14 15:02:33 +08:00
|
|
|
|
using Assets.Scripts.UI.Control;
|
2021-04-27 18:26:30 +08:00
|
|
|
|
using Assets.Scripts;
|
2021-04-30 18:03:34 +08:00
|
|
|
|
using Assets.Scripts.UI.Prefab.Device;
|
2021-05-19 14:38:48 +08:00
|
|
|
|
using Assets.Scripts.Devices;
|
2021-04-01 09:33:19 +08:00
|
|
|
|
|
|
|
|
|
|
public class ConnectDeviceModal : PFUIPanel
|
|
|
|
|
|
{
|
|
|
|
|
|
private float timer = 1.0f;
|
|
|
|
|
|
private SensorType mSensorType = SensorType.None;
|
|
|
|
|
|
public SensorType SensorType {
|
|
|
|
|
|
get {
|
|
|
|
|
|
return mSensorType;
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2021-04-29 18:10:09 +08:00
|
|
|
|
//Debug.Log(value);
|
2021-04-01 09:33:19 +08:00
|
|
|
|
mSensorType = value;
|
2021-04-27 18:26:30 +08:00
|
|
|
|
var deviceInfoStr = Utils.deviceDict[value.ToString()];
|
|
|
|
|
|
if (deviceInfoStr != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var deviceInfo = deviceInfoStr.Split('|');
|
|
|
|
|
|
if (deviceInfo.Length == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
Title.text = deviceInfo[0];
|
|
|
|
|
|
Logo.sprite = Resources.Load<Sprite>(deviceInfo[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private Image Logo;
|
|
|
|
|
|
private UnityEngine.Object deviceItem;
|
|
|
|
|
|
private VerticalLayoutGroup content;
|
2021-04-15 17:09:35 +08:00
|
|
|
|
private PfUIButton connectBtn;
|
2021-09-07 16:11:15 +08:00
|
|
|
|
private Text connectBtnText;
|
2021-06-04 18:28:11 +08:00
|
|
|
|
private Dictionary<string, DeviceItem> deviceList;
|
2021-04-27 18:26:30 +08:00
|
|
|
|
private Text noDevice,Title;
|
2021-04-23 09:22:12 +08:00
|
|
|
|
private RectTransform searchIconRect;
|
2021-04-01 09:33:19 +08:00
|
|
|
|
|
|
|
|
|
|
protected override void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Awake();
|
|
|
|
|
|
|
2021-06-04 18:28:11 +08:00
|
|
|
|
deviceList = new Dictionary<string, DeviceItem>();
|
2021-04-09 09:44:06 +08:00
|
|
|
|
|
|
|
|
|
|
var container = this.transform.Find("GameObject");
|
|
|
|
|
|
var closeBtn = container.Find("CloseBtn");
|
|
|
|
|
|
Logo = container.Find("Logo").GetComponent<Image>();
|
2021-04-27 18:26:30 +08:00
|
|
|
|
Title = container.Find("Title").GetComponent<Text>();
|
2021-04-14 15:02:33 +08:00
|
|
|
|
var panel = container.Find("Panel");
|
|
|
|
|
|
content = panel.Find("Scroll View").Find("Viewport").Find("Content").GetComponent<VerticalLayoutGroup>();
|
|
|
|
|
|
|
2021-04-23 09:22:12 +08:00
|
|
|
|
noDevice = container.Find("NoDevice").GetComponent<Text>();
|
|
|
|
|
|
|
|
|
|
|
|
searchIconRect = container.Find("SearchIcon").GetComponent<RectTransform>();
|
|
|
|
|
|
_x = searchIconRect.localPosition.x;
|
|
|
|
|
|
_y = searchIconRect.localPosition.y;
|
|
|
|
|
|
|
2021-06-04 18:28:11 +08:00
|
|
|
|
base.SetRounded(panel.GetComponent<Image>().transform, 20f);
|
2021-04-14 15:02:33 +08:00
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
UIManager.AddEvent(closeBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Close();
|
2021-04-29 18:10:09 +08:00
|
|
|
|
//DestroyImmediate(this.gameObject);
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
deviceItem = Resources.Load("UI/Prefab/Device/DeviceItem");
|
|
|
|
|
|
|
2021-04-15 17:09:35 +08:00
|
|
|
|
connectBtn = container.Find("ConnectBtn").GetComponent<PfUIButton>();
|
2021-09-07 16:11:15 +08:00
|
|
|
|
connectBtnText = connectBtn.transform.Find("Text").GetComponent<Text>();
|
2021-04-14 15:02:33 +08:00
|
|
|
|
|
|
|
|
|
|
UIManager.AddEvent(connectBtn.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
|
|
|
|
{
|
2021-09-07 16:11:15 +08:00
|
|
|
|
if (!connectBtnText.text.Equals("DISCONNECT"))
|
2021-04-14 15:02:33 +08:00
|
|
|
|
{
|
2021-09-07 16:11:15 +08:00
|
|
|
|
foreach (var item in deviceList)
|
2021-04-14 15:02:33 +08:00
|
|
|
|
{
|
2021-09-07 16:11:15 +08:00
|
|
|
|
if (item.Value.GetStatus() == false)
|
2021-06-04 18:28:11 +08:00
|
|
|
|
{
|
2021-09-07 16:11:15 +08:00
|
|
|
|
if (SensorType == SensorType.SpeedCadence)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Value.DeviceInfo.Sensor == SensorType.SpeedCadence || item.Value.DeviceInfo.Sensor == SensorType.Cadence)
|
|
|
|
|
|
{
|
2021-09-15 14:17:11 +08:00
|
|
|
|
Debug.Log("断开设备" + item.Value.DeviceInfo.Name);
|
2021-09-07 16:11:15 +08:00
|
|
|
|
item.Value.DeviceInfo.Disconnect();
|
|
|
|
|
|
DeviceCache.Remove(item.Value.DeviceInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (item.Value.DeviceInfo.Sensor == SensorType)
|
2021-06-04 18:28:11 +08:00
|
|
|
|
{
|
|
|
|
|
|
item.Value.DeviceInfo.Disconnect();
|
|
|
|
|
|
DeviceCache.Remove(item.Value.DeviceInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-07 16:11:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
var dd = deviceList.Select(d => d.Value).Where(d => d.GetStatus()).FirstOrDefault();
|
|
|
|
|
|
if (dd != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
dd.DeviceInfo.Connect();
|
|
|
|
|
|
DeviceCache.Add(dd.DeviceInfo);
|
2021-04-14 15:02:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-07 16:11:15 +08:00
|
|
|
|
else
|
2021-04-14 15:02:33 +08:00
|
|
|
|
{
|
2021-09-07 16:11:15 +08:00
|
|
|
|
foreach (var item in deviceList)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("116" + item.Value.DeviceInfo.Name+ item.Value.GetStatus().ToString());
|
|
|
|
|
|
if (item.Value.GetStatus())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Value.DeviceInfo.Sensor == SensorType)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.Value.DeviceInfo.Disconnect();
|
|
|
|
|
|
Debug.Log("121" + item.Value.DeviceInfo.Name + "断开");
|
|
|
|
|
|
DeviceCache.Remove(item.Value.DeviceInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-14 15:02:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
this.Close();
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2021-06-04 18:28:11 +08:00
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
2021-04-14 15:02:33 +08:00
|
|
|
|
protected override void Start()
|
2021-04-01 09:33:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
SearchDevice();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 15:02:33 +08:00
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
// Update is called once per frame
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
timer -= Time.deltaTime;
|
|
|
|
|
|
if(timer <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
SearchDevice();
|
|
|
|
|
|
timer = 1.0f;
|
|
|
|
|
|
}
|
2021-04-23 09:22:12 +08:00
|
|
|
|
|
|
|
|
|
|
Move();
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SearchDevice()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SensorType != SensorType.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Debug.Log(AntConnector.Instance().discoveredDevices.Count(d => d.Sensor == SensorType));
|
|
|
|
|
|
|
2021-05-19 14:38:48 +08:00
|
|
|
|
var devices = new List<AbstractDevice>();
|
|
|
|
|
|
//var a = AntConnector.Instance().discoveredDevices;
|
2021-04-01 09:33:19 +08:00
|
|
|
|
switch (SensorType)
|
|
|
|
|
|
{
|
2021-06-04 18:28:11 +08:00
|
|
|
|
case SensorType.SpeedCadence:
|
2021-04-01 09:33:19 +08:00
|
|
|
|
case SensorType.Cadence:
|
2021-05-19 14:38:48 +08:00
|
|
|
|
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is ICadenceDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList();
|
2021-04-01 09:33:19 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case SensorType.HeartRate:
|
2021-05-19 14:38:48 +08:00
|
|
|
|
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is IHeartRateDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList();
|
2021-04-01 09:33:19 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case SensorType.Power:
|
2021-05-19 14:38:48 +08:00
|
|
|
|
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is IPowerDevice && d.State == DeviceState.Connected)).OrderBy(d=>d.Priority).ToList();
|
2021-04-01 09:33:19 +08:00
|
|
|
|
break;
|
2021-06-04 18:28:11 +08:00
|
|
|
|
case SensorType.Speed:
|
2021-05-19 14:38:48 +08:00
|
|
|
|
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType || (d is ISpeedDevice && d.State == DeviceState.Connected)).OrderBy(d => d.Priority).ToList();
|
2021-04-01 09:33:19 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case SensorType.Trainer:
|
2021-05-19 14:38:48 +08:00
|
|
|
|
devices = App.MainDeviceAdapter.GetDevices().Where(d => d.Sensor == SensorType).ToList();
|
2021-04-01 09:33:19 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case SensorType.VirtualPower:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var device in devices)
|
|
|
|
|
|
{
|
2021-06-04 18:28:11 +08:00
|
|
|
|
if (deviceList.ContainsKey(device.Id))
|
2021-04-01 09:33:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
2021-04-14 15:02:33 +08:00
|
|
|
|
}
|
2021-04-01 09:33:19 +08:00
|
|
|
|
|
|
|
|
|
|
var gameObject = (GameObject)Instantiate(deviceItem);
|
|
|
|
|
|
gameObject.transform.SetParent(content.transform);
|
2021-04-29 18:10:09 +08:00
|
|
|
|
gameObject.transform.localScale = new Vector3(1, 1, 1);
|
2021-04-01 09:33:19 +08:00
|
|
|
|
var deviceItemObj = gameObject.GetComponent<DeviceItem>();
|
|
|
|
|
|
deviceItemObj.DeviceInfo = device;
|
2021-09-07 16:11:15 +08:00
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
UIManager.AddEvent(deviceItemObj.gameObject, EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceItemObj.Set(true);
|
2021-09-09 16:29:22 +08:00
|
|
|
|
|
|
|
|
|
|
var trainer = SensorType == SensorType.Trainer && deviceItemObj.DeviceInfo.State == DeviceState.Connected;
|
|
|
|
|
|
var nontrainer = deviceItemObj.DeviceInfo.Sensor != SensorType.Trainer && SensorType != SensorType.Trainer && deviceItemObj.DeviceInfo.State == DeviceState.Connected;
|
|
|
|
|
|
if (trainer || nontrainer)
|
2021-09-07 16:11:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
connectBtnText.text = "DISCONNECT";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
connectBtnText.text = "CONNECT";
|
|
|
|
|
|
}
|
2021-09-09 16:29:22 +08:00
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
foreach (var item in deviceList)
|
|
|
|
|
|
{
|
2021-06-04 18:28:11 +08:00
|
|
|
|
if (item.Key != deviceItemObj.DeviceInfo.Id)
|
2021-04-01 09:33:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
item.Value.Set(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
2021-04-14 15:02:33 +08:00
|
|
|
|
|
2021-04-01 09:33:19 +08:00
|
|
|
|
//UIManager.AddEvent(deviceItemObj.gameObject, EventTriggerType.Deselect, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
|
|
|
|
//{
|
|
|
|
|
|
// //Debug.Log("de select");
|
|
|
|
|
|
// if(deviceItemObj.DeviceInfo.Sensor == SensorType)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// deviceItemObj.DeviceInfo.Disconnect();
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}));
|
|
|
|
|
|
|
2021-06-04 18:28:11 +08:00
|
|
|
|
deviceList.Add(device.Id, deviceItemObj);
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-07 16:11:15 +08:00
|
|
|
|
if(deviceList.All(d=>d.Value.GetStatus() == false))
|
2021-04-01 09:33:19 +08:00
|
|
|
|
{
|
2021-04-14 15:02:33 +08:00
|
|
|
|
var firstDevice = deviceList.Select(d => d.Value).Where(d => d.DeviceInfo.State == DeviceState.Connected).OrderByDescending(d => d.DeviceInfo.Priority).FirstOrDefault();
|
|
|
|
|
|
if (firstDevice != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
firstDevice.Set(true);
|
2021-09-09 16:29:22 +08:00
|
|
|
|
|
|
|
|
|
|
var trainer = SensorType == SensorType.Trainer && firstDevice.DeviceInfo.State == DeviceState.Connected;
|
|
|
|
|
|
var nontrainer = firstDevice.DeviceInfo.Sensor != SensorType.Trainer && SensorType != SensorType.Trainer && firstDevice.DeviceInfo.State == DeviceState.Connected;
|
|
|
|
|
|
if(trainer || nontrainer)
|
2021-09-07 16:11:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
connectBtnText.text = "DISCONNECT";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
connectBtnText.text = "CONNECT";
|
2021-09-09 16:29:22 +08:00
|
|
|
|
}
|
2021-04-14 15:02:33 +08:00
|
|
|
|
//connectBtn.text.text = "DISCOUNECT";
|
2021-04-23 09:22:12 +08:00
|
|
|
|
noDevice.text = firstDevice.DeviceInfo.Name;
|
2021-04-14 15:02:33 +08:00
|
|
|
|
}
|
2021-06-04 18:28:11 +08:00
|
|
|
|
//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)
|
|
|
|
|
|
//}
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-14 15:02:33 +08:00
|
|
|
|
|
2021-06-09 18:16:35 +08:00
|
|
|
|
public override void Show()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Show();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 15:02:33 +08:00
|
|
|
|
public override void Close()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Close();
|
2021-04-29 18:10:09 +08:00
|
|
|
|
//DestroyImmediate(this.gameObject);
|
|
|
|
|
|
Destroy(this.gameObject);
|
2021-04-14 15:02:33 +08:00
|
|
|
|
}
|
2021-04-23 09:22:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float time = 0;
|
|
|
|
|
|
float _x = 0;
|
|
|
|
|
|
float _y = 0;
|
2021-04-29 18:10:09 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 做search动画
|
|
|
|
|
|
/// </summary>
|
2021-04-23 09:22:12 +08:00
|
|
|
|
void Move()
|
|
|
|
|
|
{
|
|
|
|
|
|
var hudu = (2 * Mathf.PI / 360) * 1 * time;
|
|
|
|
|
|
|
|
|
|
|
|
var x = _x + Mathf.Sin(hudu) * 8f;
|
|
|
|
|
|
var y = _y + Mathf.Cos(hudu) * 8f;
|
|
|
|
|
|
|
|
|
|
|
|
var rect = searchIconRect.localPosition;
|
|
|
|
|
|
//rect.x = x;
|
|
|
|
|
|
//rect.y = y;
|
|
|
|
|
|
rect.x = x;
|
|
|
|
|
|
rect.y = y;
|
|
|
|
|
|
searchIconRect.localPosition = rect;
|
|
|
|
|
|
|
|
|
|
|
|
time += 1.8f;
|
|
|
|
|
|
|
|
|
|
|
|
//Debug.Log(x + ", " + y);
|
|
|
|
|
|
|
|
|
|
|
|
//yield return null;
|
|
|
|
|
|
}
|
2021-04-01 09:33:19 +08:00
|
|
|
|
}
|