BikeSensorData.cs 2.7 KB

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