Esempio n. 1
0
    //Repeatedly read telemetry data from server using an HTTP GET request and update notification data
    IEnumerator GetTelemetryData()
    {
        while (true)
        {
            string numericalStr = "blank:blank", switchStr = "blank:blank", jsonStr = "blank:blank";

            //Get numerical data

            using (UnityWebRequest www1 = UnityWebRequest.Get(numericalDataURL))
            {
                yield return(www1.Send());

                if (www1.isError)
                {
                    numericalServerConnErr = true;
                    Debug.Log(www1.error);
                }
                else
                {
                    numericalServerConnErr = false;
                    numericalStr           = www1.downloadHandler.text;
                    numericalStr           = numericalStr.Trim();
                }
            }

            //Get switch data
            using (UnityWebRequest www2 = UnityWebRequest.Get(switchDataURL))
            {
                yield return(www2.Send());

                if (www2.isError)
                {
                    switchServerConnErr = true;
                    Debug.Log(www2.error);
                }
                else
                {
                    switchServerConnErr = false;
                    switchStr           = www2.downloadHandler.text;
                    switchStr           = switchStr.Trim();
                }
            }

            //Parse and update notifications if valid connections to servers exist
            //Note that BOTH connections must be working for any updates to occur
            //TODO: if one server goes down, figure out how to only update notifications from the other server
            if (!switchServerConnErr && !numericalServerConnErr)
            {
                jsonStr = numericalStr.Substring(1, numericalStr.Length - 3) + "," + switchStr.Substring(2, switchStr.Length - 3);
                JSONData jsonData = JSONData.CreateFromJSON(jsonStr);
                UpdateNotificationsArray(jsonData);
            }

            //Clear notifications
            foreach (Transform child in notificationsPanel.transform)
            {
                Destroy(child.gameObject);
            }

            //Clear right panel text
            foreach (Transform child in textPanel.transform)
            {
                Destroy(child.gameObject);
            }

            //Create new notifications
            //Special notifications for server communication failure appear first (if any)
            SwitchData switchConnNotification, numConnNotification;
            int        index = 0;
            if (switchServerConnErr)
            {
                switchConnNotification = new SwitchData("Switch server connection", "false", false);
                CreateTelemetryNotification(switchConnNotification, index);
                ++index;
            }
            if (numericalServerConnErr)
            {
                numConnNotification = new SwitchData("Telemetry server connection", "false", false);
                CreateTelemetryNotification(numConnNotification, index);
                ++index;
            }
            for (; index < MAX_NOTIFICATIONS; ++index)
            {
                if (notificationsList[index] == null)
                {
                    break;                                   //hit end of list early
                }
                if (notificationsList[index].severity == Severity.NOMINAL)
                {
                    break;                                                        //only show errors and warnings
                }
                CreateTelemetryNotification(notificationsList[index], index);
            }

            //Update notification icon
            Severity notifySeverity = notificationsList[0].severity; //there should always be something in index 0, even if server is down
            if (switchServerConnErr || numericalServerConnErr)
            {
                notifySeverity = Severity.CRITICAL;
            }
            String notifyIconPath = String.Format("Icons/notify-{0}", notifySeverity.ToString());
            Sprite notifyIcon     = Resources.Load <Sprite>(notifyIconPath);
            notifyImage.GetComponent <Image>().sprite = notifyIcon;

            //Update notification count
            int numNotifications = (NUM_NUMERICAL + NUM_SWITCH + NUM_TIMER) - notificationsList.FindAll(x => x.severity == Severity.NOMINAL).Count;
            if (numNotifications == 0)
            {
                notificationCountText.SetActive(false);
            }
            else
            {
                notificationCountText.SetActive(true);
                notificationCountText.GetComponentInChildren <Text>().text = "" + numNotifications;
            }

            /*
             * if (notifySeverity == Severity.CRITICAL)
             * {
             *  notifyImage.GetComponent<AudioSource>().Play();
             * }
             * else
             * {
             *  notifyImage.GetComponent<AudioSource>().Stop();
             * }
             */
            //Create telemetry text for right panel
            CreateTelemetryTextHeaders();
            for (int j = 0; j < numericalTextList.Count; ++j)
            {
                CreateTelemetryText(numericalTextList[j], j, TelemetryType.NUMERICAL);
            }
            for (int k = 0; k < switchTextList.Count; ++k)
            {
                CreateTelemetryText(switchTextList[k], k, TelemetryType.SWITCH);
            }
            for (int m = 0; m < timerTextList.Count; ++m)
            {
                CreateTelemetryText(timerTextList[m], m, TelemetryType.TIMER);
            }

            //Get pressure, oxygen, temperature, and battery data
            NumericalData suit_pressure   = numericalTextList[SUIT_PRESSURE_INDEX];
            NumericalData oxygen_pressure = numericalTextList[OXYGEN_INDEX];
            NumericalData temperature     = numericalTextList[TEMPERATURE_INDEX];
            TimerData     battery         = timerTextList[BATTERY_INDEX];

            //Update the pressure, oxygen, temperature, and battery icons and text
            if (suit_pressure != null)
            {
                String pressureIconPath = String.Format("Icons/suit-{0}", suit_pressure.severity.ToString());
                Sprite pressureIcon     = Resources.Load <Sprite>(pressureIconPath);
                pressureImage.sprite = pressureIcon;
                pressureText.GetComponentInChildren <Text>().text = suit_pressure.value + " " + suit_pressure.units;
            }
            if (oxygen_pressure != null)
            {
                String oxygenIconPath = String.Format("Icons/oxygen-{0}", oxygen_pressure.severity.ToString());
                Sprite oxygenIcon     = Resources.Load <Sprite>(oxygenIconPath);
                oxygenImage.sprite = oxygenIcon;
                oxygenText.GetComponentInChildren <Text>().text = oxygen_pressure.value + " " + oxygen_pressure.units;
            }
            if (temperature != null)
            {
                String temperatureIconPath = String.Format("Icons/temperature-{0}", temperature.severity.ToString());
                Sprite temperatureIcon     = Resources.Load <Sprite>(temperatureIconPath);
                temperatureImage.sprite = temperatureIcon;
                temperatureText.GetComponentInChildren <Text>().text = temperature.value + " " + temperature.units;
            }
            if (battery != null)
            {
                String batteryIconPath = String.Format("Icons/battery-{0}", battery.severity.ToString());
                Sprite batteryIcon     = Resources.Load <Sprite>(batteryIconPath);
                batteryImage.sprite = batteryIcon;
            }

            //Update pressure and oxygen arrows if they have values
            //If null, the arrows won't change their rotation
            if (suit_pressure != null)
            {
                double pressureAngle = ValueToDegrees(suit_pressure);
                pressureArrow.GetComponent <RectTransform>().rotation = Quaternion.Euler(0, 0, (float)pressureAngle);
            }
            if (oxygen_pressure != null)
            {
                double oxygenAngle = ValueToDegrees(oxygen_pressure);
                oxygenArrow.GetComponent <RectTransform>().rotation = Quaternion.Euler(0, 0, (float)oxygenAngle);
            }

            //Wait before pulling data again
            yield return(new WaitForSecondsRealtime((float)REFRESH_RATE));
        }
    }