HttpSocket.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using LitJson;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class HttpSocket : MonoSingleton<HttpSocket>
  6. {
  7. public bool isOpen = true;
  8. private float times = 0;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. InvokeRepeating("SendIpToElectric", 0, 60f);
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. //times += Time.deltaTime;
  18. //if(times>1)
  19. //{
  20. // times = 0;
  21. // Debug.Log("AAAAAAA");
  22. // SendIpToInitialize();
  23. //}
  24. }
  25. /// <summary>
  26. /// 初始化 获取初始JSON信息
  27. /// </summary>
  28. public void SendIpToInitialize()
  29. {
  30. SendNetConnectProtocol sendNet = new SendNetConnectProtocol();
  31. // Debug.Log(DeviceSN.Instance.SendSerialBy16());
  32. sendNet.Tagid = DeviceSN.Instance.SendSerialBy16();
  33. sendNet.Port = 9981;
  34. sendNet.ProjectID = 11;
  35. string jsonData = JsonMapper.ToJson(sendNet);
  36. //Debug.Log(jsonData);
  37. //SocketSend(jsonData);
  38. //JFSocket.GetInstance().SendMessage(jsonData);
  39. HttpTool.Instance.Post("regist", jsonData, AnalysisJson);
  40. //HttpTool.Instance.Get("regist", CallBack);
  41. }
  42. //reporter: '606bee3edfda85745b71348d',
  43. //sn: '3ewqeq2e2qewe',
  44. //cmd: 'insertPositionRecord',
  45. //positionTs: '1617690133',
  46. //project: '6067d03db492a38dfe6e2265',
  47. //position: '{"x":1.3,"y":3.3,"z":1.2}',
  48. //positionType: 'PointCloud',
  49. /// <summary>
  50. /// 同步当前位置
  51. /// </summary>
  52. /// <param name="pos"></param>
  53. /// <param name="PointionTs"></param>
  54. public void SendIpToPointPos(Vector3 pos, string PointionTs)
  55. {
  56. if (!isOpen) return;
  57. SendNetConnectPointCloud sendNet = new SendNetConnectPointCloud();
  58. sendNet.reporter = "606bee3edfda85745b71348d";
  59. sendNet.sn = DeviceSN.Instance.SendSerialBy16();
  60. sendNet.cmd = "insertPositionRecord";
  61. sendNet.positionTs = PointionTs;
  62. sendNet.project = "6067d03db492a38dfe6e2265";
  63. sendNet.position.x = pos.x;
  64. sendNet.position.y = pos.y;
  65. sendNet.position.z = pos.z;
  66. sendNet.positionType = "PointCloud";
  67. //sendNet.viewpointId = viewpointId;
  68. //viewpointId = "null";
  69. string jsonData = JsonMapper.ToJson(sendNet);
  70. // Debug.Log(jsonData);
  71. // SocketSendPointCloud(jsonData);
  72. // JFSocket.GetInstance().SendMessage(jsonData);
  73. HttpTool.Instance.Post("regist", jsonData, CallBack);
  74. }
  75. //projectId:"<uuid>",
  76. //sn:"<string>",
  77. //cmdType:"increasePointTrigger",
  78. //viewpointId:"<uuid>",
  79. //triggerTs:<int64>
  80. //
  81. /// <summary>
  82. /// 进入触发区域后向后台发送信息
  83. /// </summary>
  84. /// <param name="pointId"></param>
  85. /// <param name="ts"></param>
  86. public void SendIpToPointTrigger(string pointId, long ts)
  87. {
  88. if (!isOpen) return;
  89. SendNetConnectPointTrigger sendNet = new SendNetConnectPointTrigger();
  90. sendNet.projectId = "606bee3edfda85745b71348d";
  91. sendNet.sn = DeviceSN.Instance.SendSerialBy16();
  92. sendNet.viewpointId = pointId;
  93. sendNet.triggerTs = ts;
  94. string jsonData = JsonMapper.ToJson(sendNet);
  95. Debug.Log(jsonData);
  96. //SocketSendPointCloud(jsonData);
  97. // JFSocket.GetInstance().SendMessage(jsonData);
  98. HttpTool.Instance.Post("regist", jsonData, CallBack);
  99. }
  100. //sn: '3ewqeq2e2qewe',
  101. //cmd: 'answer',
  102. //correct: 2,
  103. //incorrect: 1,
  104. /// <summary>
  105. /// 答题结束后向服务器发送结果信息
  106. /// </summary>
  107. /// <param name="infor"></param>
  108. public void SendIpTOQuestion(SendQuestionInfor infor)
  109. {
  110. if (!isOpen) return;
  111. string jsonData = JsonMapper.ToJson(infor);
  112. //Debug.Log("========================================="+jsonData);
  113. //SocketSendPointCloud(jsonData);
  114. // JFSocket.GetInstance().SendMessage(jsonData);
  115. HttpTool.Instance.Post("regist", jsonData, CallBack);
  116. }
  117. /* 电量数据结构
  118. * sn: '3ewqeq2e2qewe'
  119. * cmd: 'showElectricity'
  120. * electricity: 20
  121. * status:""
  122. */
  123. /// <summary>
  124. /// 向服务器发送眼镜的剩余电量以及电池状态
  125. /// 在start函数中使用InvokeRepeating60秒调用一次
  126. /// </summary>
  127. public void SendIpToElectric()
  128. {
  129. if (!isOpen) return;
  130. int electricity = (int)((SystemInfo.batteryLevel) * 100f);
  131. BatteryStatus batterystate = SystemInfo.batteryStatus;
  132. string state = "";
  133. switch (batterystate)
  134. {
  135. case BatteryStatus.Unknown:
  136. state = "无法确定设备的电池状态";
  137. break;
  138. case BatteryStatus.Charging:
  139. state = "设备已插入并正在充电";
  140. break;
  141. case BatteryStatus.Discharging:
  142. state = "设备已拔出并放电";
  143. break;
  144. case BatteryStatus.NotCharging:
  145. state = "设备已插入,但无法充电";
  146. break;
  147. case BatteryStatus.Full:
  148. state = "设备已插入并且电池已充满";
  149. break;
  150. default:
  151. state = "无法确定设备的电池状态";
  152. break;
  153. }
  154. SendNetConnectElectricity sendNet = new SendNetConnectElectricity();
  155. sendNet.sn = DeviceSN.Instance.SendSerialBy16();
  156. sendNet.electricity = electricity;
  157. sendNet.status = state;
  158. string jsonData = JsonMapper.ToJson(sendNet);
  159. // SocketSendPointCloud(jsonData);
  160. // JFSocket.GetInstance().SendMessage(jsonData);
  161. HttpTool.Instance.Post("regist", jsonData, CallBack);
  162. }
  163. /// <summary>
  164. /// Post 回调
  165. /// </summary>
  166. /// <param name="str"></param>
  167. public void CallBack(string str)
  168. {
  169. Debug.Log(str);
  170. }
  171. /// <summary>
  172. /// 获取初始Json成功后的回调
  173. /// </summary>
  174. /// <param name="str"></param>
  175. public void AnalysisJson(string str)
  176. {
  177. GameManage.Instance.AnalysisJson(str);
  178. }
  179. }