IPointService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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-fat2.ghz-tech.com/mr-navigator/v1";
  44. UrlObjOfReference = "https://api-fat2.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 = "https://api-fat2.ghz-tech.com/mr-navigator/v1/project/pointcloudInfo";// UrlPointCloud + methodName;
  53. // 创建UnityWebRequest
  54. // UnityWebRequest request = new UnityWebRequest(url, "POST");
  55. // 设置请求头,指定数据类型为 JSON
  56. // request.SetRequestHeader("Content-Type", "application/json");
  57. // request.SetRequestHeader("Authorization", UserInfo.Instance.Token);
  58. JsonData data = new JsonData();
  59. data["projectId"] = int.Parse(projectId);
  60. Debug.Log("Hjj url " + url);
  61. Debug.Log("Hjj Authorization " + UserInfo.Instance.Token);
  62. Debug.Log("Hjj ToJson " + data.ToJson());
  63. HttpTool.Instance.Post(url, data.ToJson(), callback);
  64. // 设置请求体,即包含 JSON 数据的字符串
  65. // byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(data.ToJson());
  66. // request.uploadHandler = new UploadHandlerRaw(bodyRaw);
  67. // 发送请求
  68. // CoroutineSystem.Instance.Start_Coroutine(GetPointGetPointFileRequest(request, callback));
  69. }
  70. public void DownloadPointFile(string downloadingUrl, PointFileType fileType)
  71. {
  72. CoroutineSystem.Instance.Start_Coroutine(DownloadPointFileRequest(downloadingUrl,fileType));
  73. }
  74. public void GetRefrencePos(int sceneID)
  75. {
  76. CoroutineSystem.Instance.Start_Coroutine(GetObjectOfRefrence(UrlObjOfReference,sceneID));
  77. }
  78. #region 协程
  79. private IEnumerator GetPointRequest(string methodName, Action<string> callback)
  80. {
  81. string url = UrlPointCloud + methodName;
  82. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  83. {
  84. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  85. //设置header
  86. foreach (var v in HttpTool.Instance.RequestHeader)
  87. {
  88. webRequest.SetRequestHeader(v.Key, v.Value);
  89. }
  90. yield return webRequest.SendWebRequest();
  91. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  92. Debug.LogError("GetPoint failed: " + webRequest.error + "\n" + webRequest.downloadHandler.text);
  93. else
  94. callback?.Invoke(webRequest.downloadHandler.text);
  95. }
  96. }
  97. private IEnumerator GetPointGetPointFileRequest(UnityWebRequest webRequest, Action<string> callback)
  98. {
  99. yield return webRequest.SendWebRequest();
  100. Debug.Log("Hjj Back GetPointFileDownloadUrl : ");
  101. Debug.Log("Hjj Back GetPointFileDownloadUrl : ");
  102. if ( webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  103. Debug.LogError("GetPointFileDownloadUrl failed: " + webRequest.error);
  104. else
  105. callback?.Invoke(webRequest.downloadHandler.text);
  106. }
  107. /// <summary>
  108. /// 下载点云文件 UnityWebRequest
  109. /// </summary>
  110. /// <param name="downloadingUrl">下载的Url</param>
  111. /// <param name="fileType">文件类型</param>
  112. /// <returns></returns>
  113. private IEnumerator DownloadPointFileRequest(string downloadingUrl, PointFileType fileType)
  114. {
  115. string fileName = this.GetUtility<GetFileNameUtility>().GetFileName(downloadingUrl, fileType.ToString());
  116. if (!File.Exists(Application.persistentDataPath + "/Map Data/" + fileName))
  117. {
  118. Debug.LogWarning("文件不存在,下载文件");
  119. using (UnityWebRequest webRequestPoint = UnityWebRequest.Get(downloadingUrl))
  120. {
  121. webRequestPoint.timeout = 30;//设置超时,若webRequest.SendWebRequest()连接超时会返回,且isNetworkError为true
  122. yield return webRequestPoint.SendWebRequest();
  123. if (webRequestPoint.result == UnityWebRequest.Result.ProtocolError || webRequestPoint.result == UnityWebRequest.Result.ConnectionError)
  124. Debug.Log("Download Point Cloud Error:" + webRequestPoint.error);
  125. else
  126. {
  127. if (Directory.Exists(Application.persistentDataPath + "/Map Data") == false) // Map Data文件夹不存在
  128. Directory.CreateDirectory(Application.persistentDataPath + "/Map Data");
  129. if (System.IO.File.Exists(Application.persistentDataPath + "/Map Data/" + fileName)) // Map Data/fileName 文件存在则删除
  130. System.IO.File.Delete(Application.persistentDataPath + "/Map Data/" + fileName);
  131. var File = webRequestPoint.downloadHandler.data; //获取二进制数据
  132. FileStream nFile = new FileStream(Application.persistentDataPath + "/Map Data/" + fileName, FileMode.Create); //创建文件写入对象
  133. nFile.Write(File, 0, File.Length); //写入数据
  134. nFile.Close(); nFile.Dispose(); nFile = null;//关闭当前流并释放与之相关联的所有系统资源
  135. if (fileType == PointFileType.bytes)
  136. this.SendEvent(new LoadMapFileEvent() { fileName = fileName });
  137. }
  138. }
  139. }
  140. else
  141. {
  142. Debug.LogWarning("文件已存在");
  143. if (fileType == PointFileType.bytes)
  144. this.SendEvent(new LoadMapFileEvent() { fileName = fileName });
  145. }
  146. }
  147. private IEnumerator GetObjectOfRefrence(string url,int sceneID)
  148. {
  149. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  150. {
  151. ObjofRefrendenceInfo TestGet = new ObjofRefrendenceInfo();
  152. TestGet.id = sceneID;
  153. string jsonData = JsonConvert.SerializeObject(TestGet);
  154. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  155. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  156. webRequest.downloadHandler = new DownloadHandlerBuffer();
  157. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  158. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  159. Debug.LogError("DGJ Post GetObjectOfRefrence");
  160. yield return webRequest.SendWebRequest();
  161. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  162. Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  163. else
  164. {
  165. Debug.LogError("DGJ Post GetObjectOfRefrenceelse");
  166. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  167. {
  168. string message = webRequest.downloadHandler.text;
  169. JObject jobject = JObject.Parse(message);
  170. if (jobject["code"].ToString() == "200")
  171. {
  172. message = jobject["data"]["position"].ToString();
  173. if (!string.IsNullOrWhiteSpace(message))
  174. {
  175. Debug.LogError("DGJ Post message:"+message);
  176. ScenePosRotInfo ScenePosRotInfoList = JsonConvert.DeserializeObject<ScenePosRotInfo>(message);
  177. PosRot = ScenePosRotInfoList;
  178. PosRot.x = (float)PosRot.x / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x;
  179. PosRot.z = (GameManager.Instance.WebMapSize.y - (float)PosRot.y) / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y;
  180. PosRot.y = -1;
  181. // Vector3 PosRot2 = new Vector3((float)PosRot.x / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, -1f, );
  182. this.SendEvent(new GetReferencePosEvent() { PosRot = PosRot });
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. #endregion
  190. }