146 lines
4.6 KiB
C#
146 lines
4.6 KiB
C#
using Assets.Scripts.Ble;
|
|
using Assets.Scripts.Devices.Ble.Interfaces;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.Devices.Ble.Characteristic
|
|
{
|
|
|
|
[Flags]
|
|
public enum TrainerDataFlag
|
|
{
|
|
MoreData = 1,
|
|
Cadence = 4,
|
|
Power = 64
|
|
}
|
|
public class FtmsIndoorBikeData : ICharacteristic
|
|
{
|
|
|
|
public int? InstantPower { get; set; }
|
|
public int? InstantCadence { get; set; }
|
|
public double? InstantSpeed { get; set; }
|
|
|
|
|
|
public Guid Uuid => ServiceUuids.Characteristics.IndoorBikeData;
|
|
|
|
public Guid ServiceUuid => ServiceUuids.Get(ServiceUuids.Ftms).IdGuid;
|
|
|
|
public bool IsOptional => false;
|
|
|
|
public TrainerDataFlag Flags;
|
|
public void HandleAttributeReceived(byte[] data)
|
|
{
|
|
if(data.Length < 2)
|
|
{
|
|
return;
|
|
}
|
|
this.Flags = (TrainerDataFlag)BitConverter.ToUInt16(data, 0);
|
|
int b = 2;
|
|
if (Flags.HasFlag(TrainerDataFlag.Cadence))
|
|
{
|
|
this.InstantSpeed = BitConverter.ToUInt16(data, b) / 100f;
|
|
this.InstantCadence = (ushort)(BitConverter.ToUInt16(data, b + 2) / 2);
|
|
b += this.SizeOfDataForFlag(TrainerDataFlag.Cadence);
|
|
}
|
|
if (Flags.HasFlag(TrainerDataFlag.Power))
|
|
{
|
|
this.InstantPower = BitConverter.ToUInt16(data, b);
|
|
b += this.SizeOfDataForFlag(TrainerDataFlag.Power);
|
|
}
|
|
//Debug.Log("速度:" + this.InstantSpeed + "踏频:" + this.InstantCadence + "功率:" + this.InstantPower);
|
|
}
|
|
|
|
public void SetUnavailable()
|
|
{
|
|
//throw new NotImplementedException();
|
|
}
|
|
|
|
private static int GetFieldSize(FtmsIndoorBikeData.IndoorBikeDataField field)
|
|
{
|
|
int num = 0;
|
|
switch (field)
|
|
{
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.InstantaneousSpeed:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.AverageSpeed:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.InstantaneousCadence:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.AverageCadence:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.TotalDistance:
|
|
num = 3;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.ResistanceLevel:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.InstantaneousPower:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.AveragePower:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.ExpendedEnergy:
|
|
num = 5;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.HeartRate:
|
|
num = 1;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.MetabolicEquivalent:
|
|
num = 1;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.ElapsedTime:
|
|
num = 2;
|
|
break;
|
|
case FtmsIndoorBikeData.IndoorBikeDataField.RemainingTime:
|
|
num = 2;
|
|
break;
|
|
}
|
|
return num;
|
|
}
|
|
|
|
|
|
private enum IndoorBikeDataField
|
|
{
|
|
InstantaneousSpeed,
|
|
AverageSpeed,
|
|
InstantaneousCadence,
|
|
AverageCadence,
|
|
TotalDistance,
|
|
ResistanceLevel,
|
|
InstantaneousPower,
|
|
AveragePower,
|
|
ExpendedEnergy,
|
|
HeartRate,
|
|
MetabolicEquivalent,
|
|
ElapsedTime,
|
|
RemainingTime,
|
|
}
|
|
|
|
|
|
public int SizeOfDataForFlag(TrainerDataFlag flag)
|
|
{
|
|
int num = 0;
|
|
switch (flag)
|
|
{
|
|
//case RowerDataFlag.MoreData:
|
|
// break;
|
|
case TrainerDataFlag.MoreData: num = 2; break;
|
|
case TrainerDataFlag.Power: num = 2; break;
|
|
case TrainerDataFlag.Cadence: num = 4; break;
|
|
default:
|
|
break;
|
|
}
|
|
return num;
|
|
}
|
|
}
|
|
}
|