SensorStatus.cs 3.6 KB

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