IUpOrDownloadService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Blue;
  2. using LitJson;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. using XRTool.Util;
  11. public interface IUpOrDownloadService : IService
  12. {
  13. List<ScenePosRotInfo> PosRot { get; }
  14. void GetScenePosRot(int projectID);
  15. void UploadScenePosRot(string Url, string JsonString);
  16. }
  17. public class UpOrDownloadService : IUpOrDownloadService
  18. {
  19. public List<ScenePosRotInfo> PosRot { get; private set; }
  20. public int ProjectID { get; private set; }
  21. private string UrlPointCloudPosRot;
  22. public void OnInit()
  23. {
  24. // UrlPointCloudPosRot = "http://192.168.50.114:8888/mr-navigator/v1/project/position";
  25. UrlPointCloudPosRot = "https://api-fat3.ghz-tech.com/mr-navigator/v1/project/position";
  26. }
  27. void IUpOrDownloadService.GetScenePosRot(int projectID)
  28. {
  29. // TimerMgr.Instance.CreateTimer(()=> {
  30. CoroutineSystem.Instance.StartCoroutine(GetScenePosRotRequest(projectID));
  31. // },3,-1);
  32. }
  33. void IUpOrDownloadService.UploadScenePosRot(string Url, string JsonString)
  34. {
  35. CoroutineSystem.Instance.StartCoroutine(RequestUpload(Url,JsonString));
  36. }
  37. private IEnumerator GetScenePosRotRequest(int projectID)
  38. {
  39. using (UnityWebRequest webRequest = new UnityWebRequest(UrlPointCloudPosRot, "POST"))
  40. {
  41. ProjectID = projectID;
  42. ScenePointBodyInfo TestGet = new ScenePointBodyInfo();
  43. TestGet.id = projectID;
  44. TestGet.unity = false;
  45. string jsonData = JsonConvert.SerializeObject(TestGet);
  46. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  47. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  48. webRequest.downloadHandler = new DownloadHandlerBuffer();
  49. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  50. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  51. yield return webRequest.SendWebRequest();
  52. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  53. Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  54. else
  55. {
  56. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  57. {
  58. string message = webRequest.downloadHandler.text;
  59. JObject jobject = JObject.Parse(message);
  60. if (jobject["code"].ToString() == "200")
  61. {
  62. message = jobject["data"].ToString();
  63. if (!string.IsNullOrWhiteSpace(message))
  64. {
  65. // TimerMgr.Instance.CreateTimer(() => {
  66. List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
  67. // GongShi2.F2Dto3D(ScenePosRotInfoList[0].x, ScenePosRotInfoList[0].y, 643f, 1920f, ScenePosRotInfoList[1].x, ScenePosRotInfoList[1].y, 51f, 17.1f);
  68. GongShi2.F2Dto3D(ScenePosRotInfoList[0].x, ScenePosRotInfoList[0].y, GameManager.Instance.WebMapSize.y, GameManager.Instance.WebMapSize.x, ScenePosRotInfoList[1].x, ScenePosRotInfoList[1].y, DataManager.Instance.CurrentScene.length, DataManager.Instance.CurrentScene.width);
  69. // },0.03f,-1);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. public IEnumerator RequestUpload(string Url, string jsonString)
  77. {
  78. using (UnityWebRequest webRequest = new UnityWebRequest(Url, "POST"))
  79. {
  80. JsonData data = JsonMapper.ToObject(jsonString);
  81. JsonData d2 = new JsonData();
  82. d2["position"] = data;
  83. d2["id"] = ProjectID;
  84. d2["unity"] = false;
  85. byte[] bodyRaw = Encoding.UTF8.GetBytes(d2.ToJson());
  86. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  87. webRequest.downloadHandler = new DownloadHandlerBuffer();
  88. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  89. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  90. foreach (var v in HttpTool.Instance.RequestHeader)
  91. {
  92. webRequest.SetRequestHeader(v.Key, v.Value);
  93. }
  94. yield return webRequest.SendWebRequest();
  95. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  96. {
  97. Debug.LogError(webRequest.downloadHandler.error);
  98. InstantiateCommand Command = new InstantiateCommand(
  99. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  100. InstantiateSystem.Instance.BlueObject.NetErrorText);
  101. CommandSystem.Instance.Send(Command);
  102. }
  103. else
  104. {
  105. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  106. {
  107. string message = webRequest.downloadHandler.text;
  108. JObject jobject = JObject.Parse(message);
  109. if (jobject["code"].ToString() == "200")
  110. {
  111. message = jobject["data"].ToString();
  112. if (!string.IsNullOrWhiteSpace(message))
  113. {
  114. List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
  115. PosRot = ScenePosRotInfoList;
  116. }
  117. }
  118. }
  119. InstantiateCommand Command = new InstantiateCommand(
  120. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  121. InstantiateSystem.Instance.BlueObject.SuccessText);
  122. CommandSystem.Instance.Send(Command);
  123. }
  124. }
  125. }
  126. }