123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using LitJson;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using UnityEngine;
- public class UdpClient : MonoBehaviour
- {
- public delegate void logMessagaeBarScanResultDelegat(string message);
- public static logMessagaeBarScanResultDelegat logMBSRD;
- public bool isOpen = true;
- public string viewpointId = "null";
- private static UdpClient instance;
-
- string AddressIP;
- Socket socket;
- EndPoint serverEnd;
- IPEndPoint ipEnd;
- IPEndPoint ipSocketEnd;
- IPEndPoint ipSocketPC;
- IPEndPoint ipSocketPCTest;
- string recvStr;
- string sendStr;
- byte[] recvData = new byte[4096];
- byte[] sendData = new byte[4096];
- int recvLen;
- Thread connectThread;
- private UdpClient()
- {
- }
- public static UdpClient Instance()
- {
- if (instance == null)
- instance = new UdpClient();
- return instance;
- }
-
- void InitSocket()
- {
-
-
- ipSocketPC = new IPEndPoint(IPAddress.Parse("192.168.3.66"), 9981);
- ipSocketEnd = new IPEndPoint(IPAddress.Parse("192.168.3.66"), 9981);
- ipSocketPCTest = new IPEndPoint(IPAddress.Parse("192.168.178.128"), 9981);
- ipEnd = new IPEndPoint(IPAddress.Parse(AddressIP), 9981);
-
-
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- socket.Bind(ipEnd);
-
- IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
- serverEnd = (EndPoint)sender;
-
- SocketSend("hello");
-
- connectThread = new Thread(new ThreadStart(SocketReceive));
- connectThread.Start();
- }
- void SocketSend(string sendStr)
- {
-
- sendData = new byte[4096];
-
- sendData = Encoding.ASCII.GetBytes(sendStr);
-
- socket.SendTo(sendData, sendData.Length, SocketFlags.None, ipSocketEnd);
-
- }
- void SocketSendPointCloud(string sendStr)
- {
-
- sendData = new byte[4096];
-
- sendData = Encoding.ASCII.GetBytes(sendStr);
-
-
- socket.SendTo(sendData, ipSocketPC);
- socket.SendTo(sendData, ipSocketPCTest);
- }
-
- void SocketReceive()
- {
-
- while (true)
- {
-
- recvData = new byte[4096];
-
- recvLen = socket.ReceiveFrom(recvData, ref serverEnd);
-
-
- recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen);
- TestJsonManage.result = recvStr.Trim();
-
- Debug.Log(TestJsonManage.state + " " + " " + logMBSRD);
-
- }
- }
-
- void SocketQuit()
- {
-
- if (connectThread != null)
- {
- connectThread.Interrupt();
- connectThread.Abort();
- }
-
- if (socket != null)
- socket.Close();
- }
-
- void Start()
- {
- if (!isOpen) return;
-
- for (int i = 0; i < Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length; i++)
- {
- if (Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].AddressFamily.ToString() == "InterNetwork")
- {
- AddressIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].ToString();
- }
- }
- InitSocket();
- SendIpToBluetooth();
- InvokeRepeating("SendIpToElectric", 0, 60f);
- }
-
- void Update()
- {
- }
- public void SendIpToBluetooth()
- {
- SendNetConnectProtocol sendNet = new SendNetConnectProtocol();
- sendNet.Tagid = DeviceSN.Instance.SendSerialBy16();
- sendNet.Port = 9981;
- string jsonData = JsonMapper.ToJson(sendNet);
-
- SocketSend(jsonData);
- }
-
-
-
-
-
-
-
- public void SendIpToPointCloud(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";
-
-
- string jsonData = JsonMapper.ToJson(sendNet);
-
- SocketSendPointCloud(jsonData);
- }
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
-
- public void SendIpTOQuestion(SendQuestionInfor infor)
- {
- if (!isOpen) return;
- string jsonData = JsonMapper.ToJson(infor);
-
- SocketSendPointCloud(jsonData);
- }
-
-
-
-
-
- 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);
-
-
- }
- void OnApplicationQuit()
- {
- SocketQuit();
- }
- }
|