BikeSensorDataLogger.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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;
  42. private BikeSensorData bikeSensorData;
  43. public override string Key => "bike_sensor_data";
  44. public override void Start()
  45. {
  46. base.Start();
  47. bikeSensorData = BikeSensorData.Instance;
  48. }
  49. private void Update()
  50. {
  51. var hr = usePolarHeartRate
  52. ? bikeSensorData.PolarData?.Hr ?? -1
  53. : (int) (bikeSensorData.HrData?.HeartRate ?? -1);
  54. var speed = bikeSensorData.SpeedData?.SpeedKmh ?? -1f;
  55. var cadence = bikeSensorData.PowermeterData?.InstantaneousCadence ?? -1f;
  56. var power = bikeSensorData.PowermeterData?.InstantaneousPower ?? -1f;
  57. var torque = bikeSensorData.PowermeterData?.CrankTorque ?? -1f;
  58. Log(new BikeSensorDataLog(Helpers.RoundToLong(Time.time * 1000), speed, cadence, hr, torque, power));
  59. }
  60. public override IEnumerable<BikeSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  61. {
  62. throw new NotImplementedException(); //TODO
  63. }
  64. }
  65. }