HttpSocket.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using LitJson;
  2. using QFramework;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class HttpSocket : MonoSingleton<HttpSocket>
  7. {
  8. public bool isOpen = false;
  9. public int projectid;
  10. private float times = 0;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. InvokeRepeating("SendIpToElectric", 0, 60f);
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. times += Time.deltaTime;
  20. if (times > 1)
  21. {
  22. times = 0;
  23. SendIpToPointPos(Vector3.zero);
  24. // SendIpToPointTrigger(projectid, "博物馆");
  25. }
  26. }
  27. /// <summary>
  28. /// 同步当前位置
  29. /// </summary>
  30. /// <param name="pos"></param>
  31. /// <param name="PointionTs"></param>
  32. public void SendIpToPointPos(Vector3 pos)
  33. {
  34. if (!isOpen) return;
  35. SendNetConnectPointCloud sendNet = new SendNetConnectPointCloud();
  36. sendNet.sn = DeviceSN.Instance.SendSerialBy16();
  37. sendNet.projectId = projectid;
  38. sendNet.position.x = pos.x;
  39. sendNet.position.y = pos.y;
  40. sendNet.position.z = pos.z;
  41. sendNet.positionType = "PointCloud";
  42. string jsonData = JsonMapper.ToJson(sendNet);
  43. // HttpTool.Instance.PostTest("/sn/position", jsonData, CallBack);
  44. }
  45. /// <summary>
  46. /// 进入触发区域后向后台发送信息
  47. /// </summary>
  48. /// <param name="pointId"></param>
  49. /// <param name="ts"></param>
  50. public void SendIpToPointTrigger(string Name)
  51. {
  52. if (!isOpen) return;
  53. SendNetConnectPointTrigger sendNet = new SendNetConnectPointTrigger();
  54. sendNet.projectId = projectid;
  55. sendNet.sn = DeviceSN.Instance.SendSerialBy16();
  56. sendNet.name = Name;
  57. string jsonData = JsonMapper.ToJson(sendNet);
  58. // Debug.Log(jsonData);
  59. //SocketSendPointCloud(jsonData);
  60. // JFSocket.GetInstance().SendMessage(jsonData);
  61. HttpTool.Instance.PostTest("/sn/viewpoint", jsonData, CallBack);
  62. }
  63. /// <summary>
  64. /// 向服务器发送眼镜的剩余电量以及电池状态
  65. /// 在start函数中使用InvokeRepeating60秒调用一次
  66. /// </summary>
  67. public void SendIpToElectric()
  68. {
  69. if (!isOpen) return;
  70. int electricity = (int)((SystemInfo.batteryLevel) * 100f);
  71. BatteryStatus batterystate = SystemInfo.batteryStatus;
  72. string state = "";
  73. switch (batterystate)
  74. {
  75. case BatteryStatus.Unknown:
  76. state = "无法确定设备的电池状态";
  77. break;
  78. case BatteryStatus.Charging:
  79. state = "设备已插入并正在充电";
  80. break;
  81. case BatteryStatus.Discharging:
  82. state = "设备已拔出并放电";
  83. break;
  84. case BatteryStatus.NotCharging:
  85. state = "设备已插入,但无法充电";
  86. break;
  87. case BatteryStatus.Full:
  88. state = "设备已插入并且电池已充满";
  89. break;
  90. default:
  91. state = "无法确定设备的电池状态";
  92. break;
  93. }
  94. SendNetConnectElectricity sendNet = new SendNetConnectElectricity();
  95. sendNet.sn = DeviceSN.Instance.SendSerialBy16();
  96. sendNet.electricity = electricity;
  97. sendNet.status = state;
  98. sendNet.projectId = projectid;
  99. string jsonData = JsonMapper.ToJson(sendNet);
  100. // SocketSendPointCloud(jsonData);
  101. // JFSocket.GetInstance().SendMessage(jsonData);
  102. HttpTool.Instance.PostTest("/sn/electricity", jsonData, CallBack);
  103. if(electricity<20)
  104. {
  105. SendNetConnectAlert sendAlert = new SendNetConnectAlert();
  106. sendAlert.sn = DeviceSN.Instance.SendSerialBy16();
  107. sendAlert.projectId = projectid;
  108. sendAlert.message = "电量过低";
  109. string json = JsonMapper.ToJson(sendAlert);
  110. HttpTool.Instance.PostTest("/alert/create", json, CallBack);
  111. }
  112. }
  113. /// <summary>
  114. /// Post 回调
  115. /// </summary>
  116. /// <param name="str"></param>
  117. public void CallBack(string str)
  118. {
  119. // Debug.Log(str);
  120. }
  121. ///// <summary>
  122. ///// 获取初始Json成功后的回调
  123. ///// </summary>
  124. ///// <param name="str"></param>
  125. //public void AnalysisJson(string str)
  126. //{
  127. // GameManage.Instance.AnalysisJson(str);
  128. //}
  129. }