1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Configuration;
- using System.Data;
- using JetBrains.Annotations;
- public sealed class BikeSensorData
- {
- #region signleton
- private static readonly Lazy<BikeSensorData>
- lazy =
- new Lazy<BikeSensorData>
- (() => new BikeSensorData());
- public static BikeSensorData Instance => lazy.Value;
- #endregion
- [CanBeNull] private PowerMeterReceiver powerMeterReceiver;
- [CanBeNull] private SpeedSensorReceiver speedSensorReceiver;
- [CanBeNull] private HrReceiver hrReceiver;
- [CanBeNull] private PolarReceiver polarReceiver;
- public SpeedSensorData? SpeedData => speedSensorReceiver?.SensorData;
- public PowermeterSensorData? PowermeterData => powerMeterReceiver?.SensorData;
- public HRSensorData? HrData => hrReceiver?.SensorData;
- public PolarSensorData? PolarData => polarReceiver?.SensorData;
- private BikeSensorData()
- {
- }
- public void StartListening(SpeedSensorConfig? speedSensorConfig = null,
- PolarSensorConfig? polarSensorConfig = null, int? powermeterId = null, int? hrAntId = null)
- {
- if(speedSensorReceiver != null)
- {
- throw new InvalidConstraintException(
- "BikeSensorData: Already listening to Speed Sensor");
- }
- if(hrReceiver != null)
- {
- throw new InvalidConstraintException(
- "BikeSensorData: Already listening to HR Sensor");
- }
- if (polarReceiver != null)
- {
- throw new InvalidConstraintException(
- "BikeSensorData: Already listening to Polar Sensor");
- }
- if (powerMeterReceiver != null)
- {
- throw new InvalidConstraintException(
- "BikeSensorData: Already listening to Power Sensor");
- }
-
- if (speedSensorConfig != null)
- {
- speedSensorReceiver = new SpeedSensorReceiver(speedSensorConfig.Value);
- speedSensorReceiver.StartScan();
- }
- if (hrAntId != null)
- {
- hrReceiver = new HrReceiver(hrAntId.Value);
- hrReceiver.StartScan();
- }
- if (powermeterId != null)
- {
- powerMeterReceiver = new PowerMeterReceiver(powermeterId.Value);
- powerMeterReceiver.StartScan();
- }
- if (polarSensorConfig != null)
- {
- polarReceiver = new PolarReceiver(polarSensorConfig.Value); //automatically starts listening
- }
-
- }
- public void Dispose()
- {
- polarReceiver?.Dispose();
- //TODO: also dispose ANT sensors?
- polarReceiver = null;
- speedSensorReceiver = null;
- powerMeterReceiver = null;
- hrReceiver = null;
- }
- }
|