105 lines
4.2 KiB
C#
Raw Normal View History

2021-06-04 10:35:58 +08:00
using Assets.Scripts.Ble;
using Assets.Scripts.Devices.Ant.Interfaces;
using Assets.Scripts.Devices.Ble.Characteristic;
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 SpeedCadence : BleDevice, ISpeedDevice, ICadenceDevice
{
private CyclingSpeedCadenceMeasurement _cyclingSpeedCadenceMeasurement;
public int Cadence { get => _cyclingSpeedCadenceMeasurement.Cadence; set => throw new NotImplementedException(); }
public double Speed { get => _cyclingSpeedCadenceMeasurement.Speed; set => throw new NotImplementedException(); }
private double _wheelCircumference;
private List<BleServiceInfo> Services;
public SpeedCadence(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence)
{
Debug.Log("创建速度踏频设备");
Priority = 1;
2021-06-04 10:35:58 +08:00
_cyclingSpeedCadenceMeasurement = new CyclingSpeedCadenceMeasurement(this._wheelCircumference);
base.Characteristics.Add(_cyclingSpeedCadenceMeasurement);
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
}
public void SetWheelCircumference(double value)
{
}
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
{
this.Services = discoveredServices;
foreach (var service in this.Services)
{
//Debug.Log($"11111 "+ service.Id.ToString());
hwInterface.DiscoverCharacteristic(service, (hwInterface, service1, response) =>
{
Debug.Log($"设备{ this.Name }的char: { string.Join("\r\n", response.Data.Select(d=>d.Id)) }");
foreach (var character in response.Data)
{
if (character.MatchGuid(_cyclingSpeedCadenceMeasurement.Uuid))
{
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
{
//Debug.Log("1111111111111111111111");
});
continue;
}
}
foreach (var item in Characteristics.Where(c => c.IsOptional))
{
//Debug.Log(item.GetType() + "服务可用"+ item.Uuid.ToString() +", service:" + item.ServiceUuid);
var ccc = response.Data.FirstOrDefault(r => r.MatchGuid(item.Uuid));
if (ccc == null)
{
item.SetUnavailable();
}
else
{
Debug.Log(item.GetType() + "服务可用");
GetBatteryLevel(ccc);
}
}
});
}
}
public void GetBatteryLevel(BleCharacteristicInfo bbbb)
{
this.hwInterface.ReadCharacteristic(bbbb, (hwInterface1, characteristic1, response1) => {
Debug.Log("read收到消息" + string.Join(",", response1));
});
}
private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
//Debug.Log("power main call" + string.Join(",", response.Data));
//Debug.Log(characteristic.MatchGuid(cyclingPowerMeasurement.Uuid));
//Debug.Log(characteristic.Service.MatchGuid(cyclingPowerMeasurement.ServiceUuid));
//if (characteristic.MatchGuid(cyclingPowerMeasurement.Uuid))
//{
// cyclingPowerMeasurement.HandleAttributeReceived(response.Data);
//}
foreach (var item in base.Characteristics)
{
if (characteristic.MatchGuid(item.Uuid))
{
item.HandleAttributeReceived(response.Data);
}
}
}
}
}