IPointService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using Blue;
  7. using LitJson;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. public interface IPointService:IService
  13. {
  14. /// <summary>
  15. /// 获取点坐标数据
  16. /// </summary>
  17. /// <param name="methodName">后缀</param>
  18. void GetPoint(string methodName, Action<string> callback);
  19. /// <summary>
  20. /// 获取点云文件的下载链接
  21. /// </summary>
  22. /// <param name="methodName">后缀</param>
  23. /// <param name="projectId">场景ID</param>
  24. /// <param name="fileType">文件类型</param>
  25. void GetPointFileDownloadUrl(string methodName, string projectId, PointFileType fileType, Action<string> callback);
  26. /// <summary>
  27. /// 根据点云文件的下载链接去下载点云文件
  28. /// </summary>
  29. /// <param name="downloadingUrl">点云文件的下载链接</param>
  30. /// <param name="fileType">文件类型</param>
  31. /// <param name="sceneID">场景ID</param>
  32. void DownloadPointFile(string downloadingUrl, PointFileType fileType);
  33. void GetRefrencePos(int sceneID); // 获取参照物的坐标旋转
  34. }
  35. public class PointService : IPointService
  36. {
  37. public ScenePosRotInfo PosRot { get; private set; }
  38. private string UrlPointCloud;
  39. private string UrlObjOfReference;
  40. public void OnInit()
  41. {
  42. //UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1";
  43. UrlPointCloud = "https://api-fat3.ghz-tech.com/mr-navigator/v1";
  44. UrlObjOfReference = "https://api-fat3.ghz-tech.com/mr-navigator/v1/project/unityReference";
  45. }
  46. public void GetPoint(string methodName, Action<string> callback)
  47. {
  48. CoroutineSystem.Instance.Start_Coroutine(GetPointRequest(methodName, callback));
  49. }
  50. public void GetPointFileDownloadUrl(string methodName, string projectId, PointFileType fileType, Action<string> callback)
  51. {
  52. string url = UrlPointCloud + methodName;
  53. // 创建URL
  54. UriBuilder uriBuilder = new UriBuilder(url);
  55. // 添加查询参数
  56. uriBuilder.Query = "projectId=" + projectId + "&fileType=" + fileType.ToString();
  57. // 创建UnityWebRequest
  58. UnityWebRequest webRequest = UnityWebRequest.Get(uriBuilder.Uri);
  59. webRequest.SetRequestHeader("Authorization", UserInfo.Instance.Token);
  60. // 发送请求
  61. CoroutineSystem.Instance.Start_Coroutine(GetPointGetPointFileRequest(webRequest, callback));
  62. }
  63. public void DownloadPointFile(string downloadingUrl, PointFileType fileType)
  64. {
  65. CoroutineSystem.Instance.Start_Coroutine(DownloadPointFileRequest(downloadingUrl,fileType));
  66. }
  67. public void GetRefrencePos(int sceneID)
  68. {
  69. CoroutineSystem.Instance.Start_Coroutine(GetObjectOfRefrence(UrlObjOfReference,sceneID));
  70. }
  71. #region 协程
  72. private IEnumerator GetPointRequest(string methodName, Action<string> callback)
  73. {
  74. string url = UrlPointCloud + methodName;
  75. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  76. {
  77. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  78. //设置header
  79. foreach (var v in HttpTool.Instance.RequestHeader)
  80. {
  81. webRequest.SetRequestHeader(v.Key, v.Value);
  82. }
  83. yield return webRequest.SendWebRequest();
  84. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  85. Debug.LogError("GetPoint failed: " + webRequest.error + "\n" + webRequest.downloadHandler.text);
  86. else
  87. callback?.Invoke(webRequest.downloadHandler.text);
  88. }
  89. }
  90. private IEnumerator GetPointGetPointFileRequest(UnityWebRequest webRequest, Action<string> callback)
  91. {
  92. yield return webRequest.SendWebRequest();
  93. if( webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  94. Debug.LogError("GetPointFileDownloadUrl failed: " + webRequest.error);
  95. else
  96. callback?.Invoke(webRequest.downloadHandler.text);
  97. }
  98. /// <summary>
  99. /// 下载点云文件 UnityWebRequest
  100. /// </summary>
  101. /// <param name="downloadingUrl">下载的Url</param>
  102. /// <param name="fileType">文件类型</param>
  103. /// <returns></returns>
  104. private IEnumerator DownloadPointFileRequest(string downloadingUrl, PointFileType fileType)
  105. {
  106. string fileName = this.GetUtility<GetFileNameUtility>().GetFileName(downloadingUrl, fileType.ToString());
  107. if (!File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
  108. {
  109. Debug.LogWarning("文件不存在,下载文件");
  110. using (UnityWebRequest webRequestPoint = UnityWebRequest.Get(downloadingUrl))
  111. {
  112. webRequestPoint.timeout = 30;//设置超时,若webRequest.SendWebRequest()连接超时会返回,且isNetworkError为true
  113. yield return webRequestPoint.SendWebRequest();
  114. if (webRequestPoint.result == UnityWebRequest.Result.ProtocolError || webRequestPoint.result == UnityWebRequest.Result.ConnectionError)
  115. Debug.Log("Download Point Cloud Error:" + webRequestPoint.error);
  116. else
  117. {
  118. if (Directory.Exists(Application.persistentDataPath + "/Map Data") == false) // Map Data文件夹不存在
  119. Directory.CreateDirectory(Application.persistentDataPath + "/Map Data");
  120. if (System.IO.File.Exists(Application.persistentDataPath + "/Map Data/" + fileName)) // Map Data/fileName 文件存在则删除
  121. System.IO.File.Delete(Application.persistentDataPath + "/Map Data/" + fileName);
  122. var File = webRequestPoint.downloadHandler.data; //获取二进制数据
  123. FileStream nFile = new FileStream(Application.persistentDataPath + "/Map Data/" + fileName, FileMode.Create); //创建文件写入对象
  124. nFile.Write(File, 0, File.Length); //写入数据
  125. nFile.Close(); nFile.Dispose(); nFile = null;//关闭当前流并释放与之相关联的所有系统资源
  126. if (fileType == PointFileType.bytes)
  127. this.SendEvent(new LoadMapFileEvent() { fileName = fileName });
  128. }
  129. }
  130. }
  131. else
  132. {
  133. Debug.LogWarning("文件已存在");
  134. if (fileType == PointFileType.bytes)
  135. this.SendEvent(new LoadMapFileEvent() { fileName = fileName });
  136. }
  137. }
  138. private IEnumerator GetObjectOfRefrence(string url,int sceneID)
  139. {
  140. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  141. {
  142. ObjofRefrendenceInfo TestGet = new ObjofRefrendenceInfo();
  143. TestGet.id = sceneID;
  144. string jsonData = JsonConvert.SerializeObject(TestGet);
  145. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  146. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  147. webRequest.downloadHandler = new DownloadHandlerBuffer();
  148. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  149. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  150. yield return webRequest.SendWebRequest();
  151. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  152. Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  153. else
  154. {
  155. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  156. {
  157. string message = webRequest.downloadHandler.text;
  158. JObject jobject = JObject.Parse(message);
  159. if (jobject["code"].ToString() == "200")
  160. {
  161. message = jobject["data"].ToString();
  162. if (!string.IsNullOrWhiteSpace(message))
  163. {
  164. // Debug.LogError("message:"+message);
  165. ScenePosRotInfo ScenePosRotInfoList = JsonConvert.DeserializeObject<ScenePosRotInfo>(message);
  166. PosRot = ScenePosRotInfoList;
  167. this.SendEvent(new GetReferencePosEvent() { PosRot = PosRot });
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. #endregion
  175. }