2021-04-02 19:17:23 +08:00
|
|
|
|
using Assets.Scripts.Devices.Ant;
|
|
|
|
|
|
using Assets.Scripts.Devices.Ant.Interfaces;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
|
|
|
|
{
|
|
|
|
|
|
public class DeviceServiceMonoBase: MonoBehaviour
|
|
|
|
|
|
{
|
2021-04-16 11:23:18 +08:00
|
|
|
|
public void TrackResistance(double grade)
|
|
|
|
|
|
{
|
2021-04-19 14:38:31 +08:00
|
|
|
|
if (grade > 10)
|
|
|
|
|
|
{
|
|
|
|
|
|
grade = 10;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (grade <-10 )
|
|
|
|
|
|
{
|
|
|
|
|
|
grade = -10;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 11:23:18 +08:00
|
|
|
|
var device = (_device as FitDevice);
|
|
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
device.SetTrackResistance(grade);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-02 19:17:23 +08:00
|
|
|
|
#region 获取设备数据
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新速度(KM/H)
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double UpDateSpeed()
|
|
|
|
|
|
{
|
|
|
|
|
|
return SpeedDevice == null ? 0 : SpeedDevice.Speed;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新心率
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="heart"></param>
|
|
|
|
|
|
public int? UpDateHeart()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (HeartDevice != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HeartDevice.HeartRate;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新功率
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double UpdatePower()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PowerDevice == null ? 0 : PowerDevice.Power;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新踏频
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cadence"></param>
|
|
|
|
|
|
public int UpdateCadence()
|
|
|
|
|
|
{
|
|
|
|
|
|
return CadenceDevice == null ? 0 : CadenceDevice.Cadence;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新距离(KM)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="distance"></param>
|
|
|
|
|
|
public double UpdateDistance(double speed)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Math.Round(speed / 3600, 4);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 设备接口
|
|
|
|
|
|
|
|
|
|
|
|
#region field
|
|
|
|
|
|
|
|
|
|
|
|
protected AbstractAntDevice _device;
|
|
|
|
|
|
protected AntConnector _antConnector;
|
|
|
|
|
|
|
|
|
|
|
|
public int ManufacturerId { get; private set; }
|
|
|
|
|
|
public string DeviceNumber { get; private set; }
|
|
|
|
|
|
public int? AntModelId { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
private readonly static System.Collections.Generic.Dictionary<string, string> _routeDataCache = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 事件
|
|
|
|
|
|
|
|
|
|
|
|
//用户骑行界面非正常关闭(不清楚有哪些非正常),资源没有释放,导致定时器仍在运作。
|
|
|
|
|
|
public static bool baseExit { get; set; } = false;
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
public bool CheckAnt()
|
|
|
|
|
|
{
|
|
|
|
|
|
//TODO
|
|
|
|
|
|
//if (!ConfigHelper.IsNeedContectedToDevice)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
//}
|
|
|
|
|
|
_antConnector = AntConnector.Instance();
|
|
|
|
|
|
var devices = _antConnector.discoveredDevices.Where(d => d.State == DeviceState.Connected && (d.Sensor == SensorType.Trainer || d.Sensor == SensorType.Power));
|
|
|
|
|
|
if (devices.Count() > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
_device = devices.OrderByDescending(d => d.Sensor == SensorType.Trainer).First();
|
|
|
|
|
|
if (_device != null && _device is IRequiresRiderWeight)
|
|
|
|
|
|
{
|
2021-04-12 17:35:56 +08:00
|
|
|
|
(_device as IRequiresRiderWeight).RiderWeight = 65;//TODO:App.CurrentUser.Weight;
|
2021-04-02 19:17:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
ManufacturerId = _device.ManufacturerId;
|
|
|
|
|
|
DeviceNumber = $"{ _device.DeviceNumber },{ _device.DeviceType }";
|
|
|
|
|
|
AntModelId = _device.AntModelId;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2021-04-12 17:35:56 +08:00
|
|
|
|
//if (App.CurrentUser.FTP <= 0)
|
2021-04-02 19:17:23 +08:00
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
//}
|
|
|
|
|
|
if (_device == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
//设备列表
|
|
|
|
|
|
///速率设备
|
|
|
|
|
|
protected ISpeedDevice SpeedDevice
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
//速度和踏频一体的传感器,里面的速度是不可以读的,只允许读 踏频,切记
|
|
|
|
|
|
if (_antConnector == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
var device = _antConnector.discoveredDevices.Where(d => d.State == DeviceState.Connected && d is ISpeedDevice).FirstOrDefault();
|
|
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (ISpeedDevice)device;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 心率设备
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected IHeartRateDevice HeartDevice
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_antConnector == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
var device = _antConnector.discoveredDevices.Where(d => d.State == DeviceState.Connected && d is IHeartRateDevice).FirstOrDefault();
|
|
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (IHeartRateDevice)device;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 功率设备
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected IPowerDevice PowerDevice
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_antConnector == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
var powerDevices = _antConnector.discoveredDevices.Where(d => d.State == DeviceState.Connected && d is IPowerDevice).ToList();
|
|
|
|
|
|
var power = powerDevices.FirstOrDefault(d => d.Sensor == SensorType.Power);
|
|
|
|
|
|
if (power != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (IPowerDevice)power;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (powerDevices.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return (IPowerDevice)powerDevices.FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 踏频设备
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected ICadenceDevice CadenceDevice
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_antConnector == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
var cadences = _antConnector.discoveredDevices.Where(d => d.State == DeviceState.Connected && d is ICadenceDevice).ToList();
|
|
|
|
|
|
var cadence = cadences.FirstOrDefault(d => d.Sensor == SensorType.Cadence);
|
|
|
|
|
|
if (cadence != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (ICadenceDevice)cadence;
|
|
|
|
|
|
}
|
|
|
|
|
|
var spdCad = cadences.FirstOrDefault(d => d.Sensor == SensorType.SpeedCadence);
|
|
|
|
|
|
if (spdCad != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (ICadenceDevice)spdCad;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cadences.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return (ICadenceDevice)cadences.FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 设备交互
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
|
///// 设置骑行模式和骑行数据
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
///// <param name="model">0自由骑,2阻力模式,3轨道模式</param>
|
|
|
|
|
|
///// <param name="data"></param>
|
|
|
|
|
|
//public void SetCyClingModelData(string model, double data)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (recorderData == null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// switch (model)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// case "0":
|
|
|
|
|
|
// recorderData.SendDataMode = SendDataMode.Freedom;
|
|
|
|
|
|
// SeedFecWindResistance();
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case "1":
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case "2":
|
|
|
|
|
|
// recorderData.SendDataMode = SendDataMode.Resistance;
|
|
|
|
|
|
// SeedFecResistance(data);
|
|
|
|
|
|
// //{
|
|
|
|
|
|
// // recorderData.SlopeGrade = data;
|
|
|
|
|
|
// // TrackResistance(data);
|
|
|
|
|
|
// //}
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case "3":
|
|
|
|
|
|
// recorderData.SendDataMode = SendDataMode.Track;
|
|
|
|
|
|
// int index = Route.GetStepIndex(recorderData.EndDistance);
|
|
|
|
|
|
// double grade = Route.GetPatchByIndex(index, false, recorderData.EndDistance);
|
|
|
|
|
|
// recorderData.SlopeGrade = grade;
|
|
|
|
|
|
|
|
|
|
|
|
// var grade1 = this._sensitivity / 100D * grade;
|
|
|
|
|
|
// //Trace.WriteLine("给设备发的坡度:"+grade, nameof(MapboxController));
|
|
|
|
|
|
// TrackResistance(grade1);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// default:
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|