BikeSensorDataLogger.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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() => new KeyValuePair<long, string[]>(timestamp, new[]
  27. {
  28. speed.ToString("F4", CultureInfo.InvariantCulture),
  29. cadence.ToString("F4", CultureInfo.InvariantCulture),
  30. heartRate.ToString(),
  31. torque.ToString("F4", CultureInfo.InvariantCulture),
  32. power.ToString("F4", CultureInfo.InvariantCulture)
  33. });
  34. }
  35. public class BikeSensorDataLogger : SensorDataLogger<BikeSensorDataLog>
  36. {
  37. public override string Key => "bike_sensor_data";
  38. private BikeSensorData bikeSensorData;
  39. public override void Start()
  40. {
  41. base.Start();
  42. bikeSensorData = BikeSensorData.Instance;
  43. }
  44. private void Update()
  45. {
  46. var hr = (int) (bikeSensorData.HrData?.HeartRate ?? -1);
  47. var speed = bikeSensorData.SpeedData?.SpeedKmh ?? -1f;
  48. var cadence = bikeSensorData.PowermeterData?.InstantaneousCadence ?? -1f;
  49. var power = bikeSensorData.PowermeterData?.InstantaneousPower ?? -1f;
  50. var torque = bikeSensorData.PowermeterData?.CrankTorque ?? -1f;
  51. Log(new BikeSensorDataLog(Helpers.RoundToLong(Time.time * 1000), speed, cadence, hr, torque, power));
  52. }
  53. public override IEnumerable<BikeSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  54. {
  55. throw new NotImplementedException(); //TODO
  56. }
  57. }
  58. }