IPointService.cs 10 KB

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