BikeSensorDataLogger.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Sensors;
  6. using UnityEngine;
  7. using UnityEngine.TestTools;
  8. namespace Logging
  9. {
  10. public class BikeSensorDataLog
  11. {
  12. private float timestamp;
  13. private float speed;
  14. private float cadence;
  15. private int heartRate;
  16. private float torque;
  17. private float power;
  18. public BikeSensorDataLog(float timestamp, float speed, float cadence, int heartRate, float torque, float power)
  19. {
  20. this.timestamp = timestamp;
  21. this.speed = speed;
  22. this.cadence = cadence;
  23. this.heartRate = heartRate;
  24. this.torque = torque;
  25. this.power = power;
  26. }
  27. public string[] Serialize() =>
  28. new[]
  29. {
  30. timestamp.ToString("F4", CultureInfo.InvariantCulture),
  31. speed.ToString("F4", CultureInfo.InvariantCulture),
  32. cadence.ToString("F4", CultureInfo.InvariantCulture),
  33. heartRate.ToString(),
  34. torque.ToString("F4", CultureInfo.InvariantCulture),
  35. power.ToString("F4", CultureInfo.InvariantCulture)
  36. };
  37. }
  38. public class BikeSensorDataLogger : SensorDataLogger<BikeSensorDataLog>
  39. {
  40. public override string Key => "bike_sensor_data";
  41. private BikeSensorData bikeSensorData;
  42. public override void Start()
  43. {
  44. base.Start();
  45. bikeSensorData = BikeSensorData.Instance;
  46. }
  47. private void Update()
  48. {
  49. var hr = (int) (bikeSensorData.HrData?.HeartRate ?? -1);
  50. var speed = bikeSensorData.SpeedData?.SpeedKmh ?? -1f;
  51. var cadence = bikeSensorData.PowermeterData?.InstantaneousCadence ?? -1f;
  52. var power = bikeSensorData.PowermeterData?.InstantaneousPower ?? -1f;
  53. var torque = bikeSensorData.PowermeterData?.CrankTorque ?? -1f;
  54. Log(new BikeSensorDataLog(Time.time, speed, cadence, hr, torque, power));
  55. }
  56. public override void Log(BikeSensorDataLog value)
  57. {
  58. Buffer.Add(value.Serialize());
  59. }
  60. public override IEnumerable<BikeSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  61. {
  62. throw new NotImplementedException(); //TODO
  63. }
  64. }
  65. }