123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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<SteamVR_TrackedObject>();
- legObject = legTracker.GetComponent<SteamVR_TrackedObject>();
- }
- 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;
- }
- }
- }
|