BikeSensorData.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. private readonly Subject<object> polarReceiverAvailable = new Subject<object>();
  15. [CanBeNull] private HrReceiver hrReceiver;
  16. [CanBeNull] private PolarReceiver polarReceiver;
  17. [CanBeNull] private PowerMeterReceiver powerMeterReceiver;
  18. [CanBeNull] private SpeedSensorReceiver speedSensorReceiver;
  19. private BikeSensorData()
  20. {
  21. PolarReceiverAvailable = polarReceiverAvailable.ToTask();
  22. }
  23. public SpeedSensorData? SpeedData =>
  24. speedSensorReceiver?.Connected ?? false ? speedSensorReceiver?.SensorData : null;
  25. public PowermeterSensorData? PowermeterData =>
  26. 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. public Task PolarReceiverAvailable { get; }
  33. public void StartListening(SpeedSensorConfig? speedSensorConfig = null,
  34. PolarSensorConfig? polarSensorConfig = null, int? powermeterId = null, int? hrAntId = null)
  35. {
  36. if (speedSensorConfig != null)
  37. {
  38. if (speedSensorReceiver != null)
  39. throw new InvalidConstraintException(
  40. "BikeSensorData: Already listening to Speed Sensor");
  41. speedSensorReceiver = new SpeedSensorReceiver(speedSensorConfig.Value);
  42. try
  43. {
  44. speedSensorReceiver.StartScan();
  45. }
  46. catch (ANT_Exception e)
  47. {
  48. Debug.Log($"Could not initialize SpeedSensorReceiver: {e}");
  49. speedSensorReceiver = null;
  50. }
  51. }
  52. if (hrAntId != null)
  53. {
  54. if (hrReceiver != null)
  55. throw new InvalidConstraintException(
  56. "BikeSensorData: Already listening to HR Sensor");
  57. hrReceiver = new HrReceiver(hrAntId.Value);
  58. try
  59. {
  60. hrReceiver.StartScan();
  61. }
  62. catch (ANT_Exception e)
  63. {
  64. Debug.Log($"Could not initialize HR Receiver: {e}");
  65. hrReceiver = null;
  66. }
  67. }
  68. if (powermeterId != null)
  69. {
  70. if (powerMeterReceiver != null)
  71. throw new InvalidConstraintException(
  72. "BikeSensorData: Already listening to Power Sensor");
  73. powerMeterReceiver = new PowerMeterReceiver(powermeterId.Value);
  74. try
  75. {
  76. powerMeterReceiver.StartScan();
  77. }
  78. catch (ANT_Exception e)
  79. {
  80. Debug.Log($"Could not initialize PowerMeter Receiver: {e}");
  81. powerMeterReceiver = null;
  82. }
  83. }
  84. if (polarSensorConfig != null)
  85. {
  86. if (polarReceiver != null)
  87. throw new InvalidConstraintException(
  88. "BikeSensorData: Already listening to Polar Sensor");
  89. polarReceiver = new PolarReceiver(polarSensorConfig.Value);
  90. polarReceiver.StartListening();
  91. polarReceiverAvailable.OnNext(null);
  92. polarReceiverAvailable.OnCompleted();
  93. }
  94. }
  95. public void Dispose()
  96. {
  97. polarReceiver?.Dispose();
  98. //TODO: also dispose ANT sensors?
  99. polarReceiverAvailable.Dispose();
  100. polarReceiver = null;
  101. speedSensorReceiver = null;
  102. powerMeterReceiver = null;
  103. hrReceiver = null;
  104. }
  105. #region singleton
  106. private static readonly Lazy<BikeSensorData>
  107. lazy =
  108. new Lazy<BikeSensorData>
  109. (() => new BikeSensorData());
  110. public static BikeSensorData Instance => lazy.Value;
  111. #endregion
  112. }
  113. }