1
0

BikeSensorData.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Data;
  3. using System.Threading.Tasks;
  4. using ANT_Managed_Library;
  5. using JetBrains.Annotations;
  6. using Sensors.ANT;
  7. using Sensors.Polar;
  8. using UniRx;
  9. using UnityEngine;
  10. namespace Sensors
  11. {
  12. public sealed class BikeSensorData
  13. {
  14. #region singleton
  15. private static readonly Lazy<BikeSensorData>
  16. lazy =
  17. new Lazy<BikeSensorData>
  18. (() => new BikeSensorData());
  19. public static BikeSensorData Instance => lazy.Value;
  20. #endregion
  21. [CanBeNull] private PowerMeterReceiver powerMeterReceiver;
  22. [CanBeNull] private SpeedSensorReceiver speedSensorReceiver;
  23. [CanBeNull] private HrReceiver hrReceiver;
  24. [CanBeNull] private PolarReceiver polarReceiver;
  25. public SpeedSensorData? SpeedData => speedSensorReceiver?.Connected ?? false ? speedSensorReceiver?.SensorData : null;
  26. public PowermeterSensorData? PowermeterData => powerMeterReceiver?.Connected ?? false ? powerMeterReceiver?.SensorData : null;
  27. public HrSensorData? HrData => hrReceiver?.Connected ?? false ? hrReceiver?.SensorData : null;
  28. public PolarSensorData? PolarData => polarReceiver?.SensorData;
  29. [CanBeNull] public IObservable<EcgData> RawEcgData => polarReceiver?.RawEcgData;
  30. [CanBeNull] public IObservable<AccData> RawAccData => polarReceiver?.RawAccData;
  31. public PolarSensorConfig? PolarConfig => polarReceiver?.SensorConfig;
  32. private readonly Subject<object> polarReceiverAvailable = new Subject<object>();
  33. public Task PolarReceiverAvailable { get; }
  34. private BikeSensorData()
  35. {
  36. PolarReceiverAvailable = polarReceiverAvailable.ToTask();
  37. }
  38. public void StartListening(SpeedSensorConfig? speedSensorConfig = null,
  39. PolarSensorConfig? polarSensorConfig = null, int? powermeterId = null, int? hrAntId = null)
  40. {
  41. if (speedSensorConfig != null)
  42. {
  43. if(speedSensorReceiver != null)
  44. {
  45. throw new InvalidConstraintException(
  46. "BikeSensorData: Already listening to Speed Sensor");
  47. }
  48. speedSensorReceiver = new SpeedSensorReceiver(speedSensorConfig.Value);
  49. try
  50. {
  51. speedSensorReceiver.StartScan();
  52. }
  53. catch (ANT_Exception e)
  54. {
  55. Debug.Log($"Could not initialize SpeedSensorReceiver: {e}");
  56. speedSensorReceiver = null;
  57. }
  58. }
  59. if (hrAntId != null)
  60. {
  61. if(hrReceiver != null)
  62. {
  63. throw new InvalidConstraintException(
  64. "BikeSensorData: Already listening to HR Sensor");
  65. }
  66. hrReceiver = new HrReceiver(hrAntId.Value);
  67. try
  68. {
  69. hrReceiver.StartScan();
  70. }
  71. catch (ANT_Exception e)
  72. {
  73. Debug.Log($"Could not initialize HR Receiver: {e}");
  74. hrReceiver = null;
  75. }
  76. }
  77. if (powermeterId != null)
  78. {
  79. if (powerMeterReceiver != null)
  80. {
  81. throw new InvalidConstraintException(
  82. "BikeSensorData: Already listening to Power Sensor");
  83. }
  84. powerMeterReceiver = new PowerMeterReceiver(powermeterId.Value);
  85. try
  86. {
  87. powerMeterReceiver.StartScan();
  88. }
  89. catch (ANT_Exception e)
  90. {
  91. Debug.Log($"Could not initialize PowerMeter Receiver: {e}");
  92. powerMeterReceiver = null;
  93. }
  94. }
  95. if (polarSensorConfig != null)
  96. {
  97. if (polarReceiver != null)
  98. {
  99. throw new InvalidConstraintException(
  100. "BikeSensorData: Already listening to Polar Sensor");
  101. }
  102. polarReceiver = new PolarReceiver(polarSensorConfig.Value);
  103. polarReceiver.StartListening();
  104. polarReceiverAvailable.OnNext(null);
  105. polarReceiverAvailable.OnCompleted();
  106. }
  107. }
  108. public void Dispose()
  109. {
  110. polarReceiver?.Dispose();
  111. //TODO: also dispose ANT sensors?
  112. polarReceiverAvailable.Dispose();
  113. polarReceiver = null;
  114. speedSensorReceiver = null;
  115. powerMeterReceiver = null;
  116. hrReceiver = null;
  117. }
  118. }
  119. }