SensorStatus.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.BleData.HasValue &&
  32. BikeSensorData.Instance.BleData.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.BleData;
  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}, {polarData?.Hr})"
  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. Destroy(gameObject);
  67. }
  68. private void OnEnable()
  69. {
  70. sensorBikeController.enabled = false;
  71. if (waitForMqtt)
  72. {
  73. dataBroker = DataBroker.Instance;
  74. dataBroker.onClientSubscribed = onClientSubscribedToTopic;
  75. }
  76. else
  77. {
  78. mqttStatusText.enabled = false;
  79. }
  80. }
  81. private void OnDestroy()
  82. {
  83. sensorBikeController.enabled = true;
  84. }
  85. private void onClientSubscribedToTopic(string clientName, string topic)
  86. {
  87. mqttClient = clientName;
  88. mqttTopic = topic;
  89. mqttReady = true;
  90. }
  91. }
  92. }