142 lines
5.7 KiB
C#
142 lines
5.7 KiB
C#
using Assets.Scripts.Ble;
|
|
using Assets.Scripts.Devices.Ant.Interfaces;
|
|
using Assets.Scripts.Devices.Ble.Characteristic;
|
|
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.Devices
|
|
{
|
|
public class Ftms : BleDevice, ISpeedDevice, IPowerDevice, ICadenceDevice, ITrainerDevice
|
|
{
|
|
|
|
public int Power { get => _ftmsIndoorBikeData.InstantPower.GetValueOrDefault(0); set => throw new NotImplementedException(); }
|
|
public double Speed { get => _ftmsIndoorBikeData.InstantSpeed.GetValueOrDefault(0); set => throw new NotImplementedException(); }
|
|
public int Cadence { get => _ftmsIndoorBikeData.InstantCadence.GetValueOrDefault(0); set => throw new NotImplementedException(); }
|
|
private FtmsRowerData _rowerData;
|
|
public FtmsRowerData rowerData { get => _rowerData; }
|
|
|
|
private FtmsIndoorBikeData _ftmsIndoorBikeData;
|
|
|
|
private List<BleServiceInfo> Services;
|
|
private BleCharacteristicInfo controlPointCharacteristic;
|
|
public Ftms(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) :base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Trainer)
|
|
{
|
|
this._ftmsIndoorBikeData = new FtmsIndoorBikeData();
|
|
base.Characteristics.Add(this._ftmsIndoorBikeData);
|
|
base.Characteristics.Add(new FtmsFitnessMachineFeature());
|
|
//划船机
|
|
this._rowerData = new FtmsRowerData();
|
|
base.Characteristics.Add(rowerData);
|
|
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
|
|
}
|
|
|
|
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
|
|
{
|
|
this.Services = discoveredServices;
|
|
|
|
foreach (var service in this.Services)
|
|
{
|
|
hwInterface.DiscoverCharacteristic(service, (hwInterface, service1, response) =>
|
|
{
|
|
foreach (var character in response.Data)
|
|
{
|
|
if (character.MatchGuid(ServiceUuids.Characteristics.IndoorBikeData))
|
|
{
|
|
//Debug.Log("功率功能");
|
|
|
|
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
|
|
{
|
|
//Debug.Log("1111111111111111111111");
|
|
});
|
|
}
|
|
else if (character.MatchGuid(ServiceUuids.Characteristics.FitnessMachineControlPoint))
|
|
{
|
|
this.controlPointCharacteristic = character;
|
|
}
|
|
else if (character.MatchGuid(ServiceUuids.Characteristics.RowerData))
|
|
{
|
|
Debug.Log("划船机");
|
|
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
|
|
{
|
|
//Debug.Log("1111111111111111111111");
|
|
//Debug.WriteLine(res.ToString());
|
|
var res1 = (res as BleResponse<byte[]>);
|
|
Debug.Log("收到消息划船机:" + string.Join(",", res1));
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
|
|
{
|
|
foreach (var item in base.Characteristics)
|
|
{
|
|
if (characteristic.MatchGuid(item.Uuid))
|
|
{
|
|
Debug.Log(string.Join(",", response.Data));
|
|
item.HandleAttributeReceived(response.Data);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetErgMode(int targetPower)
|
|
{
|
|
//throw new NotImplementedException();
|
|
byte[] bytes = BitConverter.GetBytes(targetPower);
|
|
this.hwInterface.WriteCharacteristic(this.controlPointCharacteristic, new byte[3] {
|
|
(byte)5,
|
|
bytes[0],
|
|
bytes[1]
|
|
});
|
|
}
|
|
|
|
public void SetResistanceMode(double value)
|
|
{
|
|
//throw new NotImplementedException();
|
|
var data = new byte[]
|
|
{
|
|
(byte)4,
|
|
(byte)0.1
|
|
};
|
|
}
|
|
|
|
public void SetWindResistance(double? height = null)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 轨道阻力
|
|
/// </summary>
|
|
/// <param name="grade"></param>
|
|
public void SetTrackResistance(double grade)
|
|
{
|
|
if (this.State != Ant.DeviceState.Connected)
|
|
return;
|
|
if (controlPointCharacteristic == null)
|
|
return;
|
|
|
|
short windSpeed = 0;
|
|
short value2 = (short)(grade);
|
|
byte rollingResistanceCoefficient = (byte)(0.004 * 10000);
|
|
byte windResistanceCoefficient = 0;
|
|
var data = new List<byte>();// { 17, (byte)windSpeed, (byte)value2, rollingResistanceCoefficient, windResistanceCoefficient };
|
|
data.Add(17);
|
|
data.AddRange(BitConverter.GetBytes(windSpeed));
|
|
data.AddRange(BitConverter.GetBytes(value2));
|
|
data.AddRange(BitConverter.GetBytes(rollingResistanceCoefficient));
|
|
data.AddRange(BitConverter.GetBytes(windResistanceCoefficient));
|
|
|
|
|
|
this.hwInterface.WriteCharacteristic(this.controlPointCharacteristic, data.ToArray());
|
|
}
|
|
}
|
|
}
|