BikeSensorDataLogger.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Logging.Base;
  5. using Sensors;
  6. using UnityEngine;
  7. namespace Logging.Data
  8. {
  9. public readonly struct BikeSensorDataLog : ISerializable
  10. {
  11. private readonly long timestamp;
  12. private readonly float speed;
  13. private readonly float cadence;
  14. private readonly int heartRate;
  15. private readonly float torque;
  16. private readonly float power;
  17. public BikeSensorDataLog(long timestamp, float speed, float cadence, int heartRate, float torque, float power)
  18. {
  19. this.timestamp = timestamp;
  20. this.speed = speed;
  21. this.cadence = cadence;
  22. this.heartRate = heartRate;
  23. this.torque = torque;
  24. this.power = power;
  25. }
  26. public KeyValuePair<long, string[]> Serialize()
  27. {
  28. return new KeyValuePair<long, string[]>(timestamp, new[]
  29. {
  30. speed.ToString("F4", CultureInfo.InvariantCulture),
  31. cadence.ToString("F4", CultureInfo.InvariantCulture),
  32. heartRate.ToString(),
  33. torque.ToString("F4", CultureInfo.InvariantCulture),
  34. power.ToString("F4", CultureInfo.InvariantCulture)
  35. });
  36. }
  37. }
  38. public class BikeSensorDataLogger : SensorDataLogger<BikeSensorDataLog>
  39. {
  40. [Tooltip("Uses Heart Rate transmitted by bluetooth instead of ANT+")]
  41. public bool usePolarHeartRate = true;
  42. public bool useBlePowerData = true;
  43. private BikeSensorData bikeSensorData;
  44. public override string Key => "bike_sensor_data";
  45. public override void Start()
  46. {
  47. base.Start();
  48. bikeSensorData = BikeSensorData.Instance;
  49. }
  50. private void Update()
  51. {
  52. var hr = usePolarHeartRate
  53. ? bikeSensorData.BleData?.Hr ?? -1
  54. : (int) (bikeSensorData.HrData?.HeartRate ?? -1);
  55. var speed = bikeSensorData.SpeedData?.SpeedKmh ?? -1f;
  56. var power = useBlePowerData
  57. ? bikeSensorData.BleData?.PowermeterData.power ?? -1f
  58. : bikeSensorData.PowermeterData?.InstantaneousPower ?? -1f;
  59. var cadence = useBlePowerData
  60. ? bikeSensorData.BleData?.PowermeterData.cadence ?? -1f
  61. : bikeSensorData.PowermeterData?.InstantaneousCadence ?? -1f;
  62. var torque = useBlePowerData
  63. ? bikeSensorData.BleData?.PowermeterData.torque ?? -1f
  64. : bikeSensorData.PowermeterData?.CrankTorque ?? -1f;
  65. Log(new BikeSensorDataLog(Helpers.RoundToLong(Time.time * 1000), speed, cadence, hr, torque, power));
  66. }
  67. public override IEnumerable<BikeSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  68. {
  69. throw new NotImplementedException(); //TODO
  70. }
  71. }
  72. }