HttpSocket.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System;
  6. using SC.XR.Unity.Module_Device;
  7. using Newtonsoft.Json;
  8. public class HttpSocket : MonoSingleton<HttpSocket>
  9. {
  10. /// <summary>
  11. /// 同步当前位置
  12. /// </summary>
  13. /// <param name="pos">位置</param>
  14. /// <param name="PointionTs">时间</param>
  15. /// <param name="CallBack"></param>
  16. public void SendIpToPointPos(Vector3 pos, Action<string> CallBack)
  17. {
  18. SendNetConnectPointCloud sendNet = new SendNetConnectPointCloud();
  19. sendNet.sn = SendSN.GetSN();
  20. //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  21. sendNet.projectId = DataManager.Instance.ProjectID;
  22. sendNet.position.x = pos.x;
  23. sendNet.position.y = pos.y;
  24. sendNet.position.z = pos.z;
  25. sendNet.positionType = "PointCloud";
  26. string jsonData = JsonConvert.SerializeObject(sendNet);
  27. //Debug.Log("/sn/position"+jsonData);
  28. HttpTool.Instance.Post("/sn/position", jsonData, CallBack);
  29. }
  30. /// <summary>
  31. /// 向服务器发送眼镜的剩余电量以及电池状态
  32. /// 在start函数中使用InvokeRepeating60秒调用一次
  33. /// </summary>
  34. /// <param name="CallBack"></param>
  35. public void SendIpToElectric()
  36. {
  37. StartCoroutine(WaitSendElectric());
  38. }
  39. IEnumerator WaitSendElectric()
  40. {
  41. while (true)
  42. {
  43. CalElectric();
  44. yield return new WaitForSeconds(60f);
  45. }
  46. }
  47. private void CalElectric()
  48. {
  49. int electricity = (int)((SystemInfo.batteryLevel) * 100f);
  50. BatteryStatus batterystate = SystemInfo.batteryStatus;
  51. string state = "";
  52. switch (batterystate)
  53. {
  54. case BatteryStatus.Unknown:
  55. state = "无法确定设备的电池状态";
  56. break;
  57. case BatteryStatus.Charging:
  58. state = "设备已插入并正在充电";
  59. break;
  60. case BatteryStatus.Discharging:
  61. state = "设备已拔出并放电";
  62. break;
  63. case BatteryStatus.NotCharging:
  64. state = "设备已插入,但无法充电";
  65. break;
  66. case BatteryStatus.Full:
  67. state = "设备已插入并且电池已充满";
  68. break;
  69. default:
  70. state = "无法确定设备的电池状态";
  71. break;
  72. }
  73. SendNetConnectElectricity sendNet = new SendNetConnectElectricity();
  74. sendNet.sn = SendSN.GetSN();
  75. //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  76. sendNet.projectId = DataManager.Instance.ProjectID;
  77. electricity = 100;
  78. sendNet.electricity = electricity;
  79. sendNet.status = state;
  80. string jsonData = JsonMapper.ToJson(sendNet);
  81. Debug.Log(jsonData);
  82. HttpTool.Instance.Post("/sn/electricity", jsonData, CallBack);
  83. }
  84. /// <summary>
  85. /// 进入触发区域后向后台发送信息
  86. /// </summary>
  87. /// <param name="pointId"></param>
  88. public void SendIpToPointTrigger(string pointId, string name, Action<string> CallBack)
  89. {
  90. SendNetConnectPointTrigger sendNet = new SendNetConnectPointTrigger();
  91. sendNet.sn =SendSN.GetSN();
  92. //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  93. sendNet.projectId = DataManager.Instance.ProjectID;
  94. sendNet.name = name;
  95. string jsonData = JsonMapper.ToJson(sendNet);
  96. Debug.Log(jsonData);
  97. HttpTool.Instance.Post("/sn/viewpoint", jsonData, CallBack);
  98. }
  99. public void SendSnInfo(SceneValue scene, int index, Action<int, string> CallBack)
  100. {
  101. SendSnInfo sendNet = new SendSnInfo();
  102. sendNet.sn =SendSN.GetSN();
  103. //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  104. sendNet.projectId = scene.id;
  105. string jsonData = JsonMapper.ToJson(sendNet);
  106. Debug.Log(jsonData);
  107. HttpTool.Instance.PostSnInfo("/client/snInfo", jsonData, index, CallBack);
  108. }
  109. private void CallBack(string message)
  110. {
  111. Debug.Log(message);
  112. }
  113. }
  114. public class SendNetConnectPointCloud
  115. {
  116. public string sn { get; set; }
  117. public int projectId { get; set; }
  118. public string positionType { get; set; }
  119. public Position position { get; set; }
  120. public SendNetConnectPointCloud()
  121. {
  122. position = new Position();
  123. }
  124. }
  125. public class Position
  126. {
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. public double x { get; set; }
  131. /// <summary>
  132. ///
  133. /// </summary>
  134. public double y { get; set; }
  135. /// <summary>
  136. ///
  137. /// </summary>
  138. public double z { get; set; }
  139. }
  140. public class SendNetConnectElectricity
  141. {
  142. public string sn { get; set; }
  143. public int projectId { get; set; }
  144. public int electricity { get; set; }
  145. public string status { get; set; }
  146. }
  147. public class SendNetConnectPointTrigger
  148. {
  149. public int projectId { get; set; }
  150. public string sn { get; set; }
  151. public string name { get; set; }
  152. }
  153. public class SendSnInfo
  154. {
  155. public string sn { get; set; }
  156. public int projectId { get; set; }
  157. }