|
@@ -0,0 +1,140 @@
|
|
|
+using System;
|
|
|
+using System.Collections;
|
|
|
+using System.IO;
|
|
|
+using Blue;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.Networking;
|
|
|
+
|
|
|
+public interface IPointService:IService
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ void GetPoint(string methodName, Action<string> callback);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ void GetPointFileDownloadUrl(string methodName, string projectId, PointFileType fileType, Action<string> callback);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ void DownloadPointFile(string downloadingUrl, PointFileType fileType);
|
|
|
+}
|
|
|
+
|
|
|
+public class PointService : IPointService
|
|
|
+{
|
|
|
+ private string UrlPointCloud;
|
|
|
+ public void OnInit()
|
|
|
+ {
|
|
|
+ UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1";
|
|
|
+ }
|
|
|
+
|
|
|
+ public void GetPoint(string methodName, Action<string> callback)
|
|
|
+ {
|
|
|
+ CoroutineSystem.Instance.Start_Coroutine(GetPointRequest(methodName, callback));
|
|
|
+ }
|
|
|
+ public void GetPointFileDownloadUrl(string methodName, string projectId, PointFileType fileType, Action<string> callback)
|
|
|
+ {
|
|
|
+ string url = UrlPointCloud + methodName;
|
|
|
+
|
|
|
+ UriBuilder uriBuilder = new UriBuilder(url);
|
|
|
+
|
|
|
+ uriBuilder.Query = "projectId=" + projectId + "&fileType=" + fileType.ToString();
|
|
|
+
|
|
|
+ UnityWebRequest webRequest = UnityWebRequest.Get(uriBuilder.Uri);
|
|
|
+ webRequest.SetRequestHeader("Authorization", HttpTool.Instance.Token);
|
|
|
+
|
|
|
+ CoroutineSystem.Instance.Start_Coroutine(GetPointGetPointFileRequest(webRequest, callback));
|
|
|
+ }
|
|
|
+ public void DownloadPointFile(string downloadingUrl, PointFileType fileType)
|
|
|
+ {
|
|
|
+ CoroutineSystem.Instance.Start_Coroutine(DownloadPointFileRequest(downloadingUrl,fileType));
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 协程
|
|
|
+
|
|
|
+ private IEnumerator GetPointRequest(string methodName, Action<string> callback)
|
|
|
+ {
|
|
|
+ string url = UrlPointCloud + methodName;
|
|
|
+ using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
|
+ {
|
|
|
+ webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token);
|
|
|
+
|
|
|
+ foreach (var v in HttpTool.Instance.RequestHeader)
|
|
|
+ {
|
|
|
+ webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
+ }
|
|
|
+ yield return webRequest.SendWebRequest();
|
|
|
+
|
|
|
+ if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
|
+ Debug.LogError("GetPoint failed: " + webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
+ else
|
|
|
+ callback?.Invoke(webRequest.downloadHandler.text);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private IEnumerator GetPointGetPointFileRequest(UnityWebRequest webRequest, Action<string> callback)
|
|
|
+ {
|
|
|
+ yield return webRequest.SendWebRequest();
|
|
|
+
|
|
|
+ if( webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
|
+ Debug.LogError("GetPointFileDownloadUrl failed: " + webRequest.error);
|
|
|
+ else
|
|
|
+ callback?.Invoke(webRequest.downloadHandler.text);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private IEnumerator DownloadPointFileRequest(string downloadingUrl, PointFileType fileType)
|
|
|
+ {
|
|
|
+ string fileName = this.GetUtility<GetFileNameUtility>().GetFileName(downloadingUrl, fileType.ToString());
|
|
|
+
|
|
|
+ if (!File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
|
|
|
+ {
|
|
|
+ Debug.LogWarning("文件不存在,下载文件");
|
|
|
+ using (UnityWebRequest webRequestPoint = UnityWebRequest.Get(downloadingUrl))
|
|
|
+ {
|
|
|
+ webRequestPoint.timeout = 30;
|
|
|
+
|
|
|
+ yield return webRequestPoint.SendWebRequest();
|
|
|
+
|
|
|
+ if (webRequestPoint.result == UnityWebRequest.Result.ProtocolError || webRequestPoint.result == UnityWebRequest.Result.ConnectionError)
|
|
|
+ Debug.Log("Download Point Cloud Error:" + webRequestPoint.error);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (Directory.Exists(Application.persistentDataPath + "/Map Data") == false)
|
|
|
+ Directory.CreateDirectory(Application.persistentDataPath + "/Map Data");
|
|
|
+ if (System.IO.File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
|
|
|
+ System.IO.File.Delete(Application.persistentDataPath + "/Map Data/" + fileName);
|
|
|
+
|
|
|
+ var File = webRequestPoint.downloadHandler.data;
|
|
|
+ FileStream nFile = new FileStream(Application.persistentDataPath + "/Map Data/" + fileName, FileMode.Create);
|
|
|
+ nFile.Write(File, 0, File.Length);
|
|
|
+ nFile.Close(); nFile.Dispose(); nFile = null;
|
|
|
+ if (fileType == PointFileType.bytes)
|
|
|
+ this.TriggerEvent(new LoadMapFileEvent() { fileName = fileName });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogWarning("文件已存在");
|
|
|
+ if (fileType == PointFileType.bytes)
|
|
|
+ this.TriggerEvent(new LoadMapFileEvent() { fileName = fileName });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+}
|