BikeSensorData.cs 4.2 KB

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