HttpSocket.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(6f);
  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 = (int)SystemInfo.batteryLevel*100;
  78. sendNet.electricity = electricity;
  79. sendNet.status = state;
  80. string jsonData = JsonMapper.ToJson(sendNet);
  81. Debug.Log("电量===》"+jsonData);
  82. HttpTool.Instance.Post(HttpAction.sn_electricity, jsonData, CallBack);
  83. }
  84. /// <summary>
  85. /// 进入触发区域后向后台发送信息
  86. /// </summary>
  87. /// <param name="pointId"></param>
  88. public void SendIpToPointTrigger(string pointId, int id, 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.id = id;
  95. string jsonData = JsonMapper.ToJson(sendNet);
  96. Debug.Log(jsonData);
  97. HttpTool.Instance.Post("/sn/viewpoint", jsonData, CallBack);
  98. }
  99. private void CallBack(string message)
  100. {
  101. Debug.Log(message);
  102. }
  103. }
  104. public class SendNetConnectPointCloud
  105. {
  106. public string sn { get; set; }
  107. public int projectId { get; set; }
  108. public string positionType { get; set; }
  109. public Position position { get; set; }
  110. public SendNetConnectPointCloud()
  111. {
  112. position = new Position();
  113. }
  114. }
  115. public class Position
  116. {
  117. /// <summary>
  118. ///
  119. /// </summary>
  120. public float x { get; set; }
  121. /// <summary>
  122. ///
  123. /// </summary>
  124. public float y { get; set; }
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. public float z { get; set; }
  129. }
  130. public class SendNetConnectElectricity
  131. {
  132. public string sn { get; set; }
  133. public int projectId { get; set; }
  134. public int electricity { get; set; }
  135. public string status { get; set; }
  136. }
  137. public class SendNetConnectPointTrigger
  138. {
  139. public int projectId { get; set; }
  140. public string sn { get; set; }
  141. public int id { get; set; }
  142. }
  143. public class SendSnInfo
  144. {
  145. public string sn { get; set; }
  146. public int projectId { get; set; }
  147. }