using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using exprivia.ui; using sam; using UnityEngine.Serialization; public class MultiDownloadSubsession : ProductSelectorSession { enum ViewMode { Select, Orbits } [FormerlySerializedAs("camera")]public Transform cam; public Transform earth; public Canvas canvas; public Text viewModeButtonText; public Text latitudeText; public Text longitudeText; ViewMode viewMode = ViewMode.Select; Vector3 capCenter; float capRadius = 15.0f; public SatelliteDataTypeConfig cfg; public DateTime startTime; public DateTime endTime; string previousSceneName; List products = new List(); public List<(DateTime, DateTime)> productDates = new List<(DateTime, DateTime)>(); public override void Initialize(SatelliteDataTypeConfig _cfg, DateTime _startTime, DateTime _endTime, string prevSceneName) { cfg = _cfg; startTime = _startTime; endTime = _endTime; previousSceneName = prevSceneName; StartCoroutine("FetchProductListCoroutine"); } IEnumerator FetchProductListCoroutine() { products.Clear(); productDates.Clear(); 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 index file 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.LogError("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.LogError("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; } Debug.Log($"FetchProductListCoroutine: found {products.Count} products "); BroadcastMessage("OnProductListUpdated", products, SendMessageOptions.DontRequireReceiver); BroadcastMessage("OnCapRadiusChanged", capRadius, SendMessageOptions.DontRequireReceiver); } 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); Debug.Log($"PRODUCT RANGE from: {dates.start} to {dates.end}"); if ((dates.end < startTime) || (dates.start > endTime)) { Debug.Log($"discarding product"); continue; } if (!products.Contains(line)) { products.Add(line); productDates.Add(dates); Debug.Log($"ADDED PRODUCT {products.Count}"); } } } } public void OnViewModeButtonClicked() { if (viewMode == ViewMode.Select) { viewMode = ViewMode.Orbits; viewModeButtonText.text = "Select"; BroadcastMessage("OnEnterOrbitsMode", (capCenter, capRadius), SendMessageOptions.DontRequireReceiver); } else { viewMode = ViewMode.Select; viewModeButtonText.text = "Orbits"; BroadcastMessage("OnEnterSelectMode", SendMessageOptions.DontRequireReceiver); } } public void OnScaleSliderAction(float val) { capRadius = val; BroadcastMessage("OnCapRadiusChanged", capRadius, SendMessageOptions.DontRequireReceiver); } public void OnBackButtonClicked() { if (!string.IsNullOrEmpty(previousSceneName)) { Debug.Log($"SWITCHING BACK TO SCENE {previousSceneName}"); exprivia.ui.Utils.LoadScene(previousSceneName); } } void Update() { float epsilon = 0.00001f; if (viewMode == ViewMode.Select) { Vector3 c = cam.position - earth.position; c.Normalize(); if (Math.Abs((c - capCenter).sqrMagnitude) < epsilon) return; capCenter = c; //Debug.Log($"CameraPos: {camera.position} EarthPos: {earth.position} Delta:{capCenter}"); (float,float) p = XYZToLonLat(capCenter); latitudeText.text = "Latitude: " + sam.Utils.FormatLatitude(p.Item2); longitudeText.text = "Longitude: " + sam.Utils.FormatLongitude(p.Item1); BroadcastMessage("OnCapCenterChanged", capCenter, SendMessageOptions.DontRequireReceiver); //Vector3 reprojected = Utils.DegLonLatHgtToXYZ(new Vector3(p.Item1, p.Item2, 0.0f), true); //Debug.Log($"capCenter: {capCenter} LonLat: {p} reprojected:{reprojected}"); } } (float lon, float lat) XYZToLonLat(Vector3 xyz) { float lat = (float)Math.Asin(xyz.y) * Mathf.Rad2Deg; if ((lat == -90.0f) || (lat == 90.0f)) return (0.0f, lat); float lon = -1f * ((float)Math.Atan2(xyz.x, xyz.z) * Mathf.Rad2Deg - 90.0f); return (lon, lat); } }