using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEngine.SceneManagement; using UnityEngine.UI; using exprivia; using sam; public class OrbitSelectSubsession : ProductSelectorSession { Canvas canvas; public Text productNameText; public Text startTimeText; public Text endTimeText; public SatelliteDataTypeConfig cfg; DateTime startTime; DateTime endTime; string previousSceneName; public List products = new List(); public List<(DateTime,DateTime)> productDates = new List<(DateTime, DateTime)>(); public int selectedProductIndex = -1; bool flagInitialized = false; public override void Initialize(SatelliteDataTypeConfig _cfg, DateTime _startTime, DateTime _endTime, string psn) { flagInitialized = false; canvas = GetComponentInChildren(); cfg = _cfg; startTime = _startTime; endTime = _endTime; previousSceneName = psn; StartCoroutine("FetchProductListCoroutine"); } IEnumerator FetchProductListCoroutine() { products.Clear(); productDates.Clear(); startTimeText.text = startTime.ToString("MMMM dd yyyy, h:mm:ss tt"); endTimeText.text = endTime.ToString("MMMM dd yyyy, h:mm:ss tt"); int numDays =(int)((endTime - startTime).TotalDays + 1); for (int i = 0; i < numDays; i++) { DateTime d = startTime.AddDays(i); string daystring = d.ToString("yyyyMMdd"); string url = cfg.url + "/Index/" + daystring + ".idx"; UnityWebRequest webRequest = UnityWebRequestManager.GetUnityWebRequest(url); using (webRequest) { Debug.Log($"downloading idx file {url} ..."); // Avoid reading product list from cache, as this could change on server. // See EUAT-213 webRequest.SetRequestHeader("Cache-Control", "max-age=0, no-cache, no-store"); webRequest.SetRequestHeader("Pragma", "no-cache"); // Request and wait for the desired resource. yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ConnectionError) { Debug.LogWarning("UnityWebRequest error: " + webRequest.error + " \"" + url + "\" "); //onError(this); } else { Debug.Log($"DownloadDataFile: downloaded file: {url}"); if (webRequest.downloadHandler.text[0] == '<') { Debug.LogWarning($"IdxFile {url} not found on server"); } else { ProcessIdxFile(webRequest.downloadHandler.text); } } } } if (products.Count == 0) { Debug.LogWarning("FetchProductListCoroutine: no products found"); Sprite warningIcon = Resources.Load("Icons/Warning"); PopUp.NewPopUp(canvas, warningIcon, "No products found in specified time interval", "Ok", (i) => { Debug.Log($"FetchProductListCoroutine: Modal dialog closed with value {i}"); if (!string.IsNullOrEmpty(previousSceneName)) { Debug.Log($"SWITCHING BACK TO SCENE {previousSceneName}"); exprivia.ui.Utils.LoadScene(previousSceneName); } else { Debug.Log("SHOULD GO BACK TO PREVIOUS SCENE"); } }); yield break; } selectedProductIndex = 0; BroadcastMessage("OnProductListUpdated", products, SendMessageOptions.DontRequireReceiver); flagInitialized = true; GameObject go = GetComponentInChildren()?.gameObject; if (go != null) Destroy(go); SelectProduct(0); } public void ProcessIdxFile(string text) { Debug.Log($"ProcessIdxFile called with text: \n{text}"); using (var reader = new StringReader(text)) { for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) { // extract start and end time (DateTime start, DateTime end) dates = cfg.ExtractDatesFromFilename(line); if ((dates.end < startTime) || (dates.start > endTime)) { continue; } if (!products.Contains(line)) { products.Add(line); productDates.Add(dates); } } } } public void SelectNextProduct() { if (flagInitialized) SelectProduct((selectedProductIndex + 1) % products.Count); } public void SelectPreviousProduct() { if (flagInitialized) SelectProduct((selectedProductIndex + products.Count - 1) % products.Count); } public void SelectProduct(int index) { selectedProductIndex = Mathf.Clamp(index, 0, products.Count-1); BroadcastMessage("OnOrbitSelected", (products[selectedProductIndex], productDates[selectedProductIndex]), SendMessageOptions.DontRequireReceiver); } public void OnOrbitSelected((string name, (DateTime start,DateTime end) dates) product) { productNameText.text = (cfg.productNameExtractionFunc != null) ? cfg.productNameExtractionFunc.ExtractProductName(product.name) : product.name; startTimeText.text = product.dates.start.ToString("MMMM dd yyyy, h:mm:ss tt"); endTimeText.text = product.dates.end.ToString("MMMM dd yyyy, h:mm:ss tt"); } public void VisualizeSelectedProduct() { // write to global dictionary: GlobalDictionary.SetValue("ProductName", products[selectedProductIndex]); GlobalDictionary.SetValue("SatelliteDataTypeConfig", cfg); GlobalDictionary.SetValue("DataVariableParentSceneName", SceneManager.GetActiveScene().name); string sceneName = cfg.dataVariableSceneName; Debug.Log($"SWITCHING TO SCENE {sceneName}"); exprivia.ui.Utils.LoadScene(sceneName); } public void OnBackButtonPressed() { if (!string.IsNullOrEmpty(previousSceneName)) { Debug.Log($"SWITCHING BACK TO SCENE {previousSceneName}"); exprivia.ui.Utils.LoadScene(previousSceneName); } else { Debug.Log("SHOULD GO BACK TO PREVIOUS SCENE"); } } }