225 lines
7.9 KiB
C#
Raw Permalink Normal View History

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
{
2022-05-11 17:11:48 +08:00
public Guid Uuid => ServiceUuids.Characteristics.C2RowerData35;
public Guid ServiceUuid => ServiceUuids.Characteristics.C2Service;
public bool IsOptional => false;
public static event EventHandler EnableChanged;
2022-06-30 14:39:13 +08:00
public static event EventHandler RowerTypeChanged;
2022-03-31 18:30:07 +08:00
private static bool _isEnabled;
public static bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
if (EnableChanged != null)
{
2022-06-13 18:59:02 +08:00
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)
{
2022-06-13 18:59:02 +08:00
this.PullChanged(this, null);
}
}
}
2022-03-30 18:25:51 +08:00
public static bool isReadyStatus { get; private set; }
2022-06-30 14:39:13 +08:00
public static RowerTaskPanel.RowerType rowerType = new RowerTaskPanel.RowerType() { type = 0 ,value = 0};
public void HandleAttributeReceived(byte[] data)
{
2022-06-30 14:39:13 +08:00
2022-05-11 17:11:48 +08:00
if (data[0] == 0x35)
{
2022-05-11 17:11:48 +08:00
PeakDriveForce = LbsToNewton(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 13)) / 10, true);
AverageDriveForce = LbsToNewton(Convert.ToDouble(BitConvertHelper.ToUInt16(data, 15)) / 10, true);
2022-05-11 17:11:48 +08:00
StrokeCount = BitConvertHelper.ToUInt16(data, 19);
}
2022-05-11 17:11:48 +08:00
else if (data[0] == 0x3d)
{
2022-06-30 14:39:13 +08:00
if (data[2] == 0)
{
PullValue = 0;
}
2022-05-11 17:11:48 +08:00
//老款表头不支持
2022-06-13 18:59:02 +08:00
//List<ushort> r = new List<ushort>();
for (int i = 3; i < data.Length; i += 2)
{
2022-06-30 14:39:13 +08:00
ushort pull = Convert.ToUInt16(Math.Round(LbsToNewton(data[i], true)));
2022-06-13 18:59:02 +08:00
//r.Add(pull);
PullValue = pull;
}
2022-03-30 18:25:51 +08:00
2022-06-30 14:39:13 +08:00
// Debug.Log("拉力曲线:" + string.Join(",", r));
2022-03-30 18:25:51 +08:00
}
2022-05-11 17:11:48 +08:00
else if (data[0] == 0x31)
2022-03-30 18:25:51 +08:00
{
2022-06-30 14:39:13 +08:00
ElapsedTime = (int)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 1)) / 100);
TotalDistance = (uint)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 4)) / 10);
int status = (int)data[7];
2022-03-30 18:25:51 +08:00
int value = (data[17] << 16) + (data[16] << 8) + data[15];
2022-06-30 14:39:13 +08:00
int type = 0;
//3 里程 5 时间 1直接划船
if (status == 3)
2022-03-30 18:25:51 +08:00
{
2022-06-30 14:39:13 +08:00
type = 1;
}
if (status == 5)
2022-03-30 18:25:51 +08:00
{
2022-06-30 14:39:13 +08:00
type = 2;
value /= 100;
2022-03-30 18:25:51 +08:00
}
2022-06-30 14:39:13 +08:00
//如果课程发生变化
if (status != 0 && type > 0 && ( rowerType.type != type || rowerType.value != value))
2022-03-30 18:25:51 +08:00
{
2022-06-30 14:39:13 +08:00
Debug.Log($"status{status}-type{type}-value{value}");
rowerType.type = type;
rowerType.value = value;
RowerTypeChanged?.Invoke(this,null);
2022-03-30 18:25:51 +08:00
}
2022-06-30 14:39:13 +08:00
2022-03-31 18:30:07 +08:00
var time = ((data[3] << 16) + (data[2] << 8) + data[1]) / 100;
2022-05-11 17:11:48 +08:00
Debug.Log("划船时间" + time);
2022-03-30 18:25:51 +08:00
isReadyStatus = data[9] == 0;
2022-06-13 18:59:02 +08:00
var hasChanged = data[19] != ResistanceLevel;
ResistanceLevel = data[19];
if (RowerResChanged != null && hasChanged)
2022-05-11 17:11:48 +08:00
{
2022-06-13 18:59:02 +08:00
RowerResChanged.Invoke(this, null);
2022-05-11 17:11:48 +08:00
}
2022-03-30 18:25:51 +08:00
//isReadyStatus = data[2] == 1 || data[2] == 129;
}
2022-05-11 17:11:48 +08:00
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);
}
2022-06-23 18:55:08 +08:00
else if (data[0] == 0x39)
{
2022-06-30 14:39:13 +08:00
TrainingTime = (Convert.ToDouble(BitConvertHelper.ToUInt24(data, 5)) / 100);
ElapsedTime = (int)TrainingTime;
2022-06-24 18:03:36 +08:00
TotalDistance = (uint)(Convert.ToDouble(BitConvertHelper.ToUInt24(data, 8)) / 10);
2022-06-23 18:55:08 +08:00
CompleteEvent?.Invoke(this, null);
}
2022-06-24 18:03:36 +08:00
Debug.Log("数据" + string.Join(",", data) + $" | {ElapsedTime}-{TotalDistance}");
2022-03-30 18:25:51 +08:00
//else if (data[0] == 34)
//{
// isReadyStatus = data[2] == 1 || data[2] == 129;
//}
}
2022-05-11 17:11:48 +08:00
public event EventHandler StartEvent;
2022-06-23 18:55:08 +08:00
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();
}
2022-05-11 17:11:48 +08:00
/// <summary>
/// 划桨频率
/// </summary>
public uint StrokeRate { get; set; } = 0;
/// <summary>
/// 划桨次数
/// </summary>
public UInt16 StrokeCount { get; set; } = 0;
/// <summary>
/// 平均划桨频率
/// </summary>
public int AverageStrokeRate { get; set; } = 0;
2022-06-07 11:32:23 +08:00
private UInt32 _totalDistance = 1;
2022-05-31 16:51:07 +08:00
public UInt32 TotalDistance
{
get => _totalDistance;
set
2022-05-11 17:11:48 +08:00
{
2022-05-31 16:51:07 +08:00
if (_totalDistance == 0 && value != 0 && StartEvent != null)
2022-05-11 17:11:48 +08:00
{
2022-06-13 18:59:02 +08:00
StartEvent.Invoke(this, null);
2022-05-11 17:11:48 +08:00
}
2022-05-31 16:51:07 +08:00
_totalDistance = value;
}
2022-05-11 17:11:48 +08:00
}
2022-05-31 16:51:07 +08:00
//private UInt16 _instantaneousPace = 0;
/// <summary>
/// 即时配速
/// </summary>
public UInt16 InstantaneousPace { get; set; } = 0;
2022-05-11 17:11:48 +08:00
/// <summary>
/// 平均配速
/// </summary>
public UInt16 AveragePace { get; set; } = 0;
public int InstantaneousPower { get; set; } = 0;
public int AveragePower { get; set; } = 0;
public int ElapsedTime { get; set; } = 0;
2022-06-30 14:39:13 +08:00
public double TrainingTime { get; set; } = 0;
2022-05-11 17:11:48 +08:00
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;
2022-06-30 14:39:13 +08:00
this.TrainingTime = 0;
2022-05-11 17:11:48 +08:00
}
}
}