123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Blue;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- 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);
- void GetRefrencePos(int sceneID);
- }
- public class PointService : IPointService
- {
- public ScenePosRotInfo PosRot { get; private set; }
- private string UrlPointCloud;
- private string UrlObjOfReference;
- public void OnInit()
- {
-
- UrlPointCloud = "https://api-fat2.ghz-tech.com/mr-navigator/v1";
- UrlObjOfReference = "https://api-fat2.ghz-tech.com/mr-navigator/v1/project/unityReference";
- }
- 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 = HttpSDKAction.Instance.managerUrl+HttpAction.pointcloud;
-
-
-
-
-
- JsonData data = new JsonData();
- data["projectId"] = int.Parse(projectId);
- HttpTool.Instance.Post(url, data.ToJson(), callback);
-
-
-
-
-
- }
- public void DownloadPointFile(string downloadingUrl, PointFileType fileType)
- {
- CoroutineSystem.Instance.Start_Coroutine(DownloadPointFileRequest(downloadingUrl,fileType));
- }
- public void GetRefrencePos(int sceneID)
- {
-
- CoroutineSystem.Instance.Start_Coroutine(GetObjectOfRefrence(HttpSDKAction.Instance.managerUrl+HttpAction.refrence, sceneID));
- }
- #region 协程
- private IEnumerator GetPointRequest(string methodName, Action<string> callback)
- {
- string url = UrlPointCloud + methodName;
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- webRequest.SetRequestHeader("authorization", UserInfo.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();
- Debug.Log("Hjj Back GetPointFileDownloadUrl : ");
- Debug.Log("Hjj Back GetPointFileDownloadUrl : ");
- 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.SendEvent(new LoadMapFileEvent() { fileName = fileName });
- }
- }
-
- }
- else
- {
- Debug.LogWarning("文件已存在");
- if (fileType == PointFileType.bytes)
- this.SendEvent(new LoadMapFileEvent() { fileName = fileName });
- }
- }
- private IEnumerator GetObjectOfRefrence(string url,int sceneID)
- {
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- ObjofRefrendenceInfo TestGet = new ObjofRefrendenceInfo();
- TestGet.id = sceneID;
- string jsonData = JsonConvert.SerializeObject(TestGet);
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
- webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
- Debug.LogError("DGJ Post GetObjectOfRefrence");
- yield return webRequest.SendWebRequest();
- if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
- else
- {
- Debug.LogError("DGJ Post GetObjectOfRefrenceelse");
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
- {
- string message = webRequest.downloadHandler.text;
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200")
- {
- message = jobject["data"]["position"].ToString();
- if (!string.IsNullOrWhiteSpace(message))
- {
- Debug.LogError("DGJ Post message:"+message);
- ScenePosRotInfo ScenePosRotInfoList = JsonConvert.DeserializeObject<ScenePosRotInfo>(message);
- PosRot = ScenePosRotInfoList;
- PosRot.x = (float)PosRot.x / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x;
- PosRot.z = (GameManager.Instance.WebMapSize.y - (float)PosRot.y) / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y;
- PosRot.y = -1;
-
- this.SendEvent(new GetReferencePosEvent() { PosRot = PosRot });
- }
- }
- }
- }
- }
- }
- #endregion
- }
|