using System; using Controller; using Controller.Bicycle; using Display; using Sensors.ANT; using Sensors.Polar; using SicknessReduction.Haptic; using TMPro; using Tracking; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using Valve.VR; namespace Sensors { public class SensorStatus : MonoBehaviour { public TextMeshProUGUI speedStatusText; public TextMeshProUGUI hrStatusText; public TextMeshProUGUI powerStatusText; public TextMeshProUGUI polarStatusText; public TextMeshProUGUI mqttStatusText; public SensorBikeController sensorBikeController; public FrontWheelTracker fwTracker; public KineticLegTracker legTracker; public bool waitForTrackers = true; public bool waitForSpeed = true; public bool waitForHr = true; public bool waitForPolar = true; public bool waitForPower = true; public bool waitForMqtt = false; private SteamVR_TrackedObject fwObject; private SteamVR_TrackedObject legObject; private DataBroker dataBroker; private bool mqttReady; private string mqttClient; private string mqttTopic; private bool PolarReady => BikeSensorData.Instance.PolarData.HasValue && BikeSensorData.Instance.PolarData.Value.Acc != Vector3.zero; private void OnEnable() { sensorBikeController.enabled = false; if (waitForMqtt) { dataBroker = DataBroker.Instance; dataBroker.onClientSubscribed = onClientSubscribedToTopic; } else { mqttStatusText.enabled = false; } } private void onClientSubscribedToTopic(string clientName, string topic) { mqttClient = clientName; mqttTopic = topic; mqttReady = true; } private void Start() { fwObject = fwTracker.GetComponent(); legObject = legTracker.GetComponent(); } private void Update() { var hrData = BikeSensorData.Instance.HrData; var speedData = BikeSensorData.Instance.SpeedData; var powerData = BikeSensorData.Instance.PowermeterData; var polarData = BikeSensorData.Instance.PolarData; hrStatusText.text = hrData.HasValue ? $"Heart Rate: Connected ({hrData?.HeartRate})" : "Heart Rate: Disconnected"; speedStatusText.text = speedData.HasValue ? $"Speed: Connected ({speedData?.SpeedKmh}) " : "Speed: Disconnected"; powerStatusText.text = powerData.HasValue ? $"Power/Cadence: Connected ({powerData?.InstantaneousPower})" : "Power/Cadence: Disconnected"; polarStatusText.text = PolarReady ? $"Polar: Connected ({polarData?.Acc}, {polarData?.EcgValue})" : "Polar: Disconnected"; if (waitForMqtt) { mqttStatusText.text = mqttReady ? $"Mqtt: Connected ({mqttClient}, {mqttTopic})" : "Mqtt: Disconnected"; } if (!(waitForTrackers && (!fwObject.isValid || !legObject.isValid) || waitForSpeed && !speedData.HasValue || waitForHr && !hrData.HasValue || waitForPower && !powerData.HasValue || waitForPolar && !PolarReady || waitForMqtt && !mqttReady) ) { //TODO: show text, that we are ready and start on button press Destroy(gameObject); } } private void OnDestroy() { sensorBikeController.enabled = true; } } }