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 { public class C2RowerData : ICharacteristic, IRowerCommonData { public static string JUSTROW_HEX = "F1 76 07 01 01 01 13 02 01 01 61 F2"; public static string TERMINATEWORKOUT_HEX = "F1 76 04 13 02 01 02 62 F2"; public static string FIXEDDISTANCE_HEX = "F1 76 18 01 01 03 03 05 80 00 00 07 D0 05 05 80 00 00 01 90 14 01 01 13 02 01 01 28 F2"; public static string FIXEDTIME_HEX = "F1 76 07 01 01 01 13 02 01 01 61 F2"; public Guid Uuid => ServiceUuids.Characteristics.C2RowerData35; public Guid ServiceUuid => ServiceUuids.Characteristics.C2Service; public bool IsOptional => false; public static event EventHandler EnableChanged; private static bool _isEnabled; public static bool IsEnabled { get => _isEnabled; set { _isEnabled = value; if (EnableChanged != null) { EnableChanged(_isEnabled, null); } } } //0.1 lbs of force, max=6553.5m 单位0.1磅 public double PeakDriveForce { get; set; } public double AverageDriveForce { get; set; } public event EventHandler PullChanged; private ushort _pullValue; public ushort PullValue { get { return _pullValue; } set { _pullValue = value; if (this.PullChanged != null) { this.PullChanged(this, null); } } } public static bool IsUserDefined { get; set; } public static bool isReadyStatus { get; private set; } public static RowerTaskPanel.RowerType rowerType { get; private set; } public void HandleAttributeReceived(byte[] data) { if (data[0] == 0x35) { PeakDriveForce = LbsToNewton(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 13)) / 10, true); AverageDriveForce = LbsToNewton(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 15)) / 10, true); ElapsedTime = (int)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 1)) / 100); TotalDistance = (uint)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 4)) / 10); StrokeCount = BitConvertHelper.ToUInt16(data, 19); } else if (data[0] == 0x3d) { //老款表头不支持 //List r = new List(); for (int i = 3; i < data.Length; i += 2) { ushort pull = data[i] < 10 ? (ushort)0 : Convert.ToUInt16(Math.Round(LbsToNewton(data[i], true))); //r.Add(pull); PullValue = pull; } // Debug.Log("拉力曲线:" + string.Join(",", r)); } else if (data[0] == 0x31) { byte status = data[18]; int value = (data[17] << 16) + (data[16] << 8) + data[15]; if (status == 128 && value != 0) { rowerType = new RowerTaskPanel.RowerType(); rowerType.type = 1; rowerType.value = value; IsUserDefined = true; } else if (status == 0) { rowerType = new RowerTaskPanel.RowerType(); rowerType.type = 2; rowerType.value = value / 100; IsUserDefined = false; } else { rowerType = null; IsUserDefined = false; } Debug.Log($"里程:{TotalDistance}峰力值:{PeakDriveForce} 均力值{AverageDriveForce} ReadyStatus:{isReadyStatus} TIME{DateTime.Now}"); //isReadyStatus = data[9] == 0; var time = ((data[3] << 16) + (data[2] << 8) + data[1]) / 100; Debug.Log("划船时间" + time); isReadyStatus = data[9] == 0; var hasChanged = data[19] != ResistanceLevel; ResistanceLevel = data[19]; if (RowerResChanged != null && hasChanged) { RowerResChanged.Invoke(this, null); } //isReadyStatus = data[2] == 1 || data[2] == 129; } else if (data[0] == 0x32) { InstantaneousPace = (ushort)(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 8)) / 100); AveragePace = (ushort)(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 10)) / 100); //AveragePower = BitConvertHelper.ToUInt16(data, 17); StrokeRate = (uint)data[6]; } else if (data[0] == 0x33) { TotalEnergy = BitConvertHelper.ToUInt16(data, 7); AveragePower = BitConvertHelper.ToUInt16(data, 5); } else if (data[0] == 0x36) { InstantaneousPower = BitConvertHelper.ToUInt16(data, 4); //TotalEnergy = BitConvertHelper.ToUInt16(data, 6); } else if (data[0] == 0x39) { ElapsedTime = (int)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 5)) / 100); TotalDistance = (uint)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 8)) / 10); CompleteEvent?.Invoke(this, null); } Debug.Log("数据" + string.Join(",", data) + $" | {ElapsedTime}-{TotalDistance}"); //else if (data[0] == 34) //{ // isReadyStatus = data[2] == 1 || data[2] == 129; //} } public event EventHandler StartEvent; public event EventHandler CompleteEvent; private double LbsToNewton(double lbs,bool isKg = false) => 4.4482216 * lbs * (isKg ? (1f / 9.8f) : 1f); public void SetUnavailable() { //throw new NotImplementedException(); } /// /// 划桨频率 /// public uint StrokeRate { get; set; } = 0; /// /// 划桨次数 /// public UInt16 StrokeCount { get; set; } = 0; /// /// 平均划桨频率 /// public int AverageStrokeRate { get; set; } = 0; private UInt32 _totalDistance = 1; public UInt32 TotalDistance { get => _totalDistance; set { if (_totalDistance == 0 && value != 0 && StartEvent != null) { StartEvent.Invoke(this, null); } _totalDistance = value; } } //private UInt16 _instantaneousPace = 0; /// /// 即时配速 /// public UInt16 InstantaneousPace { get; set; } = 0; /// /// 平均配速 /// public UInt16 AveragePace { get; set; } = 0; public int InstantaneousPower { get; set; } = 0; public int AveragePower { get; set; } = 0; public int ElapsedTime { get; set; } = 0; public int TotalEnergy { get; set; } = 0; public int EnergyPerHour { get; set; } = 0; public int EnergyPerMinute { get; set; } = 0; public event EventHandler RowerResChanged; public int ResistanceLevel { get; set; } = 0; public void Reset() { this.StrokeRate = 0; this.StrokeCount = 0; this.AverageStrokeRate = 0; this.TotalDistance = 0; this.InstantaneousPace = 0; this.AveragePace = 0; this.InstantaneousPower = 0; this.AveragePower = 0; this.ResistanceLevel = 0; this.TotalEnergy = 0; this.EnergyPerHour = 0; this.EnergyPerMinute = 0; this.ElapsedTime = 0; } } }