1
0

BikeSensorData.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Threading.Tasks;
  5. using JetBrains.Annotations;
  6. public sealed class BikeSensorData
  7. {
  8. #region signleton
  9. private static readonly Lazy<BikeSensorData>
  10. lazy =
  11. new Lazy<BikeSensorData>
  12. (() => new BikeSensorData());
  13. public static BikeSensorData Instance => lazy.Value;
  14. #endregion
  15. [CanBeNull] private PowerMeterReceiver powerMeterReceiver;
  16. [CanBeNull] private SpeedSensorReceiver speedSensorReceiver;
  17. [CanBeNull] private HrReceiver hrReceiver;
  18. [CanBeNull] private PolarReceiver polarReceiver;
  19. public SpeedSensorData? SpeedData => speedSensorReceiver?.SensorData;
  20. public PowermeterSensorData? PowermeterData => powerMeterReceiver?.SensorData;
  21. public HRSensorData? HrData => hrReceiver?.SensorData;
  22. public PolarSensorData? PolarData => polarReceiver?.SensorData;
  23. private BikeSensorData()
  24. {
  25. }
  26. public async Task StartListening(SpeedSensorConfig? speedSensorConfig = null,
  27. PolarSensorConfig? polarSensorConfig = null, int? powermeterId = null, int? hrAntId = null)
  28. {
  29. if (speedSensorConfig != null)
  30. {
  31. if(speedSensorReceiver != null)
  32. {
  33. throw new InvalidConstraintException(
  34. "BikeSensorData: Already listening to Speed Sensor");
  35. }
  36. speedSensorReceiver = new SpeedSensorReceiver(speedSensorConfig.Value);
  37. speedSensorReceiver.StartScan();
  38. }
  39. if (hrAntId != null)
  40. {
  41. if(hrReceiver != null)
  42. {
  43. throw new InvalidConstraintException(
  44. "BikeSensorData: Already listening to HR Sensor");
  45. }
  46. hrReceiver = new HrReceiver(hrAntId.Value);
  47. hrReceiver.StartScan();
  48. }
  49. if (powermeterId != null)
  50. {
  51. if (powerMeterReceiver != null)
  52. {
  53. throw new InvalidConstraintException(
  54. "BikeSensorData: Already listening to Power Sensor");
  55. }
  56. powerMeterReceiver = new PowerMeterReceiver(powermeterId.Value);
  57. powerMeterReceiver.StartScan();
  58. }
  59. if (polarSensorConfig != null)
  60. {
  61. if (polarReceiver != null)
  62. {
  63. throw new InvalidConstraintException(
  64. "BikeSensorData: Already listening to Polar Sensor");
  65. }
  66. polarReceiver = new PolarReceiver(polarSensorConfig.Value); //automatically starts listening
  67. await polarReceiver.StartListening();
  68. }
  69. }
  70. public void Dispose()
  71. {
  72. polarReceiver?.Dispose();
  73. //TODO: also dispose ANT sensors?
  74. polarReceiver = null;
  75. speedSensorReceiver = null;
  76. powerMeterReceiver = null;
  77. hrReceiver = null;
  78. }
  79. }