Network.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using Newtonsoft.Json.Linq;
  8. using TouchSocket.Core;
  9. using TouchSocket.Sockets;
  10. using Unity.VisualScripting;
  11. using UnityEngine;
  12. public class Network : MonoBehaviour
  13. {
  14. public int Port = 5555;
  15. private UdpSession _udpClient;
  16. private Dictionary<string, string> _device1ReadMessages;
  17. private Dictionary<string, string> _device2ReadMessages;
  18. private StreamWriter writer;
  19. private string _txtPath;
  20. void Start()
  21. {
  22. _txtPath = Application.dataPath + "stream.data";
  23. _device1ReadMessages = new Dictionary<string, string>();
  24. _device2ReadMessages = new Dictionary<string, string>();
  25. _udpClient = new UdpSession();
  26. _udpClient.Received += ReceiveMsg;
  27. _udpClient.Setup(new TouchSocketConfig()
  28. .SetBindIPHost(new IPHost(Port))
  29. .SetUdpDataHandlingAdapter(() => new NormalUdpDataHandlingAdapter()))
  30. .Start();
  31. Debug.Log("UDP Client Start!!");
  32. }
  33. private void WriteIntoTxt(string message)
  34. {
  35. FileInfo file = new FileInfo(_txtPath);
  36. if (!file.Exists)
  37. {
  38. writer = file.CreateText();
  39. }
  40. else
  41. {
  42. writer = file.AppendText();
  43. }
  44. writer.WriteLine(message);
  45. writer.Flush();
  46. writer.Dispose();
  47. writer.Close();
  48. }
  49. private void ReceiveMsg(EndPoint endpoint, ByteBlock byteblock, IRequestInfo requestinfo)
  50. {
  51. string msg = Encoding.UTF8.GetString(byteblock.Buffer, 0, byteblock.Len);
  52. //Debug.Log(msg);
  53. //WriteIntoTxt(msg);
  54. JObject obj = JObject.Parse(msg);
  55. JToken token = obj.GetValue("Device1_"+Port);
  56. JArray array = token["Parameter"] as JArray;
  57. for (int i = 0; i < array.Count; i++)
  58. {
  59. JObject obj1 = array[i] as JObject;
  60. string key = obj1.GetValue("Name").ToString();
  61. string value = obj1.GetValue("Value").ToString();
  62. _device1ReadMessages.AddOrUpdate(key, value);
  63. }
  64. JToken token2 = obj.GetValue("Device2_"+Port);
  65. JArray array2 = token2["Parameter"] as JArray;
  66. for (int i = 0; i < array.Count; i++)
  67. {
  68. JObject obj1 = array2[i] as JObject;
  69. string key = obj1.GetValue("Name").ToString();
  70. string value = obj1.GetValue("Value").ToString();
  71. _device2ReadMessages.AddOrUpdate(key, value);
  72. }
  73. }
  74. // Update is called once per frame
  75. void Update()
  76. {
  77. }
  78. public float Convert2Angle(int index,string key)
  79. {
  80. float angle = 0;
  81. if (index ==1)
  82. {
  83. if (!_device1ReadMessages.ContainsKey(key))
  84. {
  85. return 0;
  86. }
  87. string str = _device1ReadMessages[key];
  88. if (!string.IsNullOrEmpty(str))
  89. {
  90. angle = Single.Parse(str);
  91. }
  92. }
  93. if (index == 2)
  94. {
  95. if (!_device2ReadMessages.ContainsKey(key))
  96. {
  97. return 0;
  98. }
  99. string str = _device2ReadMessages[key];
  100. if (!string.IsNullOrEmpty(str))
  101. {
  102. angle = Single.Parse(str);
  103. }
  104. }
  105. return angle;
  106. }
  107. private void OnDestroy()
  108. {
  109. _udpClient.Received -= ReceiveMsg;
  110. _udpClient.Stop();
  111. _udpClient.Dispose();
  112. }
  113. }