SensorStatus.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using Controller;
  3. using Controller.Bicycle;
  4. using Display;
  5. using Sensors.ANT;
  6. using Sensors.Polar;
  7. using SicknessReduction.Haptic;
  8. using TMPro;
  9. using Tracking;
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. using UnityEngine.UI;
  13. using Valve.VR;
  14. namespace Sensors
  15. {
  16. public class SensorStatus : MonoBehaviour
  17. {
  18. public TextMeshProUGUI speedStatusText;
  19. public TextMeshProUGUI hrStatusText;
  20. public TextMeshProUGUI powerStatusText;
  21. public TextMeshProUGUI polarStatusText;
  22. public TextMeshProUGUI mqttStatusText;
  23. public SensorBikeController sensorBikeController;
  24. public FrontWheelTracker fwTracker;
  25. public KineticLegTracker legTracker;
  26. public bool waitForTrackers = true;
  27. public bool waitForSpeed = true;
  28. public bool waitForHr = true;
  29. public bool waitForPolar = true;
  30. public bool waitForPower = true;
  31. public bool waitForMqtt = false;
  32. private SteamVR_TrackedObject fwObject;
  33. private SteamVR_TrackedObject legObject;
  34. private DataBroker dataBroker;
  35. private bool mqttReady;
  36. private string mqttClient;
  37. private string mqttTopic;
  38. private bool PolarReady => BikeSensorData.Instance.PolarData.HasValue &&
  39. BikeSensorData.Instance.PolarData.Value.Acc != Vector3.zero;
  40. private void OnEnable()
  41. {
  42. sensorBikeController.enabled = false;
  43. if (waitForMqtt)
  44. {
  45. dataBroker = DataBroker.Instance;
  46. dataBroker.onClientSubscribed = onClientSubscribedToTopic;
  47. }
  48. else
  49. {
  50. mqttStatusText.enabled = false;
  51. }
  52. }
  53. private void onClientSubscribedToTopic(string clientName, string topic)
  54. {
  55. mqttClient = clientName;
  56. mqttTopic = topic;
  57. mqttReady = true;
  58. }
  59. private void Start()
  60. {
  61. fwObject = fwTracker.GetComponent<SteamVR_TrackedObject>();
  62. legObject = legTracker.GetComponent<SteamVR_TrackedObject>();
  63. }
  64. private void Update()
  65. {
  66. var hrData = BikeSensorData.Instance.HrData;
  67. var speedData = BikeSensorData.Instance.SpeedData;
  68. var powerData = BikeSensorData.Instance.PowermeterData;
  69. var polarData = BikeSensorData.Instance.PolarData;
  70. hrStatusText.text = hrData.HasValue
  71. ? $"Heart Rate: Connected ({hrData?.HeartRate})"
  72. : "Heart Rate: Disconnected";
  73. speedStatusText.text =
  74. speedData.HasValue ? $"Speed: Connected ({speedData?.SpeedKmh}) " : "Speed: Disconnected";
  75. powerStatusText.text = powerData.HasValue
  76. ? $"Power/Cadence: Connected ({powerData?.InstantaneousPower})"
  77. : "Power/Cadence: Disconnected";
  78. polarStatusText.text = PolarReady
  79. ? $"Polar: Connected ({polarData?.Acc}, {polarData?.EcgValue})"
  80. : "Polar: Disconnected";
  81. if (waitForMqtt)
  82. {
  83. mqttStatusText.text = mqttReady
  84. ? $"Mqtt: Connected ({mqttClient}, {mqttTopic})"
  85. : "Mqtt: Disconnected";
  86. }
  87. if (!(waitForTrackers && (!fwObject.isValid || !legObject.isValid)
  88. || waitForSpeed && !speedData.HasValue
  89. || waitForHr && !hrData.HasValue
  90. || waitForPower && !powerData.HasValue
  91. || waitForPolar && !PolarReady
  92. || waitForMqtt && !mqttReady)
  93. )
  94. {
  95. //TODO: show text, that we are ready and start on button press
  96. Destroy(gameObject);
  97. }
  98. }
  99. private void OnDestroy()
  100. {
  101. sensorBikeController.enabled = true;
  102. }
  103. }
  104. }