123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class HttpSocket : MonoSingleton<HttpSocket>
- {
- public bool isOpen = true;
- private float times = 0;
- // Start is called before the first frame update
- void Start()
- {
- InvokeRepeating("SendIpToElectric", 0, 60f);
- }
- // Update is called once per frame
- void Update()
- {
- //times += Time.deltaTime;
- //if(times>1)
- //{
- // times = 0;
- // Debug.Log("AAAAAAA");
- // SendIpToInitialize();
- //}
- }
- /// <summary>
- /// 初始化 获取初始JSON信息
- /// </summary>
- public void SendIpToInitialize()
- {
- SendNetConnectProtocol sendNet = new SendNetConnectProtocol();
- // Debug.Log(DeviceSN.Instance.SendSerialBy16());
- sendNet.Tagid = DeviceSN.Instance.SendSerialBy16();
- sendNet.Port = 9981;
- sendNet.ProjectID = 11;
- string jsonData = JsonMapper.ToJson(sendNet);
- //Debug.Log(jsonData);
- //SocketSend(jsonData);
- //JFSocket.GetInstance().SendMessage(jsonData);
- HttpTool.Instance.Post("regist", jsonData, AnalysisJson);
- //HttpTool.Instance.Get("regist", CallBack);
- }
- //reporter: '606bee3edfda85745b71348d',
- //sn: '3ewqeq2e2qewe',
- //cmd: 'insertPositionRecord',
- //positionTs: '1617690133',
- //project: '6067d03db492a38dfe6e2265',
- //position: '{"x":1.3,"y":3.3,"z":1.2}',
- //positionType: 'PointCloud',
- /// <summary>
- /// 同步当前位置
- /// </summary>
- /// <param name="pos"></param>
- /// <param name="PointionTs"></param>
- public void SendIpToPointPos(Vector3 pos, string PointionTs)
- {
- if (!isOpen) return;
- SendNetConnectPointCloud sendNet = new SendNetConnectPointCloud();
- sendNet.reporter = "606bee3edfda85745b71348d";
- sendNet.sn = DeviceSN.Instance.SendSerialBy16();
- sendNet.cmd = "insertPositionRecord";
- sendNet.positionTs = PointionTs;
- sendNet.project = "6067d03db492a38dfe6e2265";
- sendNet.position.x = pos.x;
- sendNet.position.y = pos.y;
- sendNet.position.z = pos.z;
- sendNet.positionType = "PointCloud";
- //sendNet.viewpointId = viewpointId;
- //viewpointId = "null";
- string jsonData = JsonMapper.ToJson(sendNet);
- // Debug.Log(jsonData);
- // SocketSendPointCloud(jsonData);
- // JFSocket.GetInstance().SendMessage(jsonData);
- HttpTool.Instance.Post("regist", jsonData, CallBack);
- }
- //projectId:"<uuid>",
- //sn:"<string>",
- //cmdType:"increasePointTrigger",
- //viewpointId:"<uuid>",
- //triggerTs:<int64>
- //
- /// <summary>
- /// 进入触发区域后向后台发送信息
- /// </summary>
- /// <param name="pointId"></param>
- /// <param name="ts"></param>
- public void SendIpToPointTrigger(string pointId, long ts)
- {
- if (!isOpen) return;
- SendNetConnectPointTrigger sendNet = new SendNetConnectPointTrigger();
- sendNet.projectId = "606bee3edfda85745b71348d";
- sendNet.sn = DeviceSN.Instance.SendSerialBy16();
- sendNet.viewpointId = pointId;
- sendNet.triggerTs = ts;
- string jsonData = JsonMapper.ToJson(sendNet);
- Debug.Log(jsonData);
- //SocketSendPointCloud(jsonData);
- // JFSocket.GetInstance().SendMessage(jsonData);
- HttpTool.Instance.Post("regist", jsonData, CallBack);
- }
- //sn: '3ewqeq2e2qewe',
- //cmd: 'answer',
- //correct: 2,
- //incorrect: 1,
- /// <summary>
- /// 答题结束后向服务器发送结果信息
- /// </summary>
- /// <param name="infor"></param>
- public void SendIpTOQuestion(SendQuestionInfor infor)
- {
- if (!isOpen) return;
- string jsonData = JsonMapper.ToJson(infor);
- //Debug.Log("========================================="+jsonData);
- //SocketSendPointCloud(jsonData);
- // JFSocket.GetInstance().SendMessage(jsonData);
- HttpTool.Instance.Post("regist", jsonData, CallBack);
- }
- /* 电量数据结构
- * sn: '3ewqeq2e2qewe'
- * cmd: 'showElectricity'
- * electricity: 20
- * status:""
- */
- /// <summary>
- /// 向服务器发送眼镜的剩余电量以及电池状态
- /// 在start函数中使用InvokeRepeating60秒调用一次
- /// </summary>
- public void SendIpToElectric()
- {
- if (!isOpen) return;
- int electricity = (int)((SystemInfo.batteryLevel) * 100f);
- BatteryStatus batterystate = SystemInfo.batteryStatus;
- string state = "";
- switch (batterystate)
- {
- case BatteryStatus.Unknown:
- state = "无法确定设备的电池状态";
- break;
- case BatteryStatus.Charging:
- state = "设备已插入并正在充电";
- break;
- case BatteryStatus.Discharging:
- state = "设备已拔出并放电";
- break;
- case BatteryStatus.NotCharging:
- state = "设备已插入,但无法充电";
- break;
- case BatteryStatus.Full:
- state = "设备已插入并且电池已充满";
- break;
- default:
- state = "无法确定设备的电池状态";
- break;
- }
- SendNetConnectElectricity sendNet = new SendNetConnectElectricity();
- sendNet.sn = DeviceSN.Instance.SendSerialBy16();
- sendNet.electricity = electricity;
- sendNet.status = state;
- string jsonData = JsonMapper.ToJson(sendNet);
- // SocketSendPointCloud(jsonData);
- // JFSocket.GetInstance().SendMessage(jsonData);
- HttpTool.Instance.Post("regist", jsonData, CallBack);
- }
- /// <summary>
- /// Post 回调
- /// </summary>
- /// <param name="str"></param>
- public void CallBack(string str)
- {
- Debug.Log(str);
- }
- /// <summary>
- /// 获取初始Json成功后的回调
- /// </summary>
- /// <param name="str"></param>
- public void AnalysisJson(string str)
- {
- GameManage.Instance.AnalysisJson(str);
- }
- }
|