PolarSensorDataLogger.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Sensors;
  6. using UnityEngine;
  7. namespace Logging
  8. {
  9. public class PolarSensorDataLog
  10. {
  11. private float timestamp;
  12. private float accX;
  13. private float accY;
  14. private float accZ;
  15. private float ecg;
  16. public PolarSensorDataLog(float timestamp, float accX, float accY, float accZ, float ecg)
  17. {
  18. this.timestamp = timestamp;
  19. this.accX = accX;
  20. this.accY = accY;
  21. this.accZ = accZ;
  22. this.ecg = ecg;
  23. }
  24. public string[] Serialize() =>
  25. new[]
  26. {
  27. timestamp.ToString("F4", CultureInfo.InvariantCulture),
  28. accX.ToString("F4", CultureInfo.InvariantCulture),
  29. accY.ToString("F4", CultureInfo.InvariantCulture),
  30. accZ.ToString("F4", CultureInfo.InvariantCulture),
  31. ecg.ToString("F4", CultureInfo.InvariantCulture)
  32. };
  33. }
  34. public class PolarSensorDataLogger : SensorDataLogger<PolarSensorDataLog>
  35. {
  36. public override string Key => "polar_sensor_data";
  37. private BikeSensorData bikeSensorData;
  38. public override void Start()
  39. {
  40. base.Start();
  41. bikeSensorData = BikeSensorData.Instance;
  42. }
  43. private void Update()
  44. {
  45. var ecg = bikeSensorData.PolarData?.EcgValue ?? -9999f;
  46. var acc = bikeSensorData.PolarData?.Acc ?? new Vector3(-9999, -9999, -9999);
  47. Log(new PolarSensorDataLog(Time.time, acc.x, acc.y, acc.z, ecg));
  48. }
  49. public override void Log(PolarSensorDataLog value)
  50. {
  51. Buffer.Add(value.Serialize());
  52. }
  53. public override IEnumerable<PolarSensorDataLog> ReadLog(IEnumerable<IEnumerable<string>> lines)
  54. {
  55. throw new System.NotImplementedException(); //TODO
  56. }
  57. }
  58. }