From ed3bfb7462d44747230437717e8673a5192f833f Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期一, 30 十一月 2020 20:34:51 +0800
Subject: [PATCH] 2020-11-30-2

---
 HDL_ON/DriverLayer/UdpSocket.cs |   59 ++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/UdpSocket.cs b/HDL_ON/DriverLayer/UdpSocket.cs
similarity index 74%
rename from HDL_ON/DAL/DriverLayer/UdpSocket.cs
rename to HDL_ON/DriverLayer/UdpSocket.cs
index f63851c..ac83f9b 100644
--- a/HDL_ON/DAL/DriverLayer/UdpSocket.cs
+++ b/HDL_ON/DriverLayer/UdpSocket.cs
@@ -1,8 +1,9 @@
 锘縰sing System;
 using System.Net.Sockets;
 using System.Net;
+using Newtonsoft.Json.Linq;
 
-namespace HDL_ON.DriverLayer
+namespace HDL_ON
 {
 	public class UdpSocket
 	{
@@ -19,34 +20,39 @@
             }
         }
 
+		/// <summary>
+		/// 鐩戝惉绔彛
+		/// </summary>
+		public int port
+		{
+            get
+            {
+				if (MainPage.LoginUser != null && Entity.DB_ResidenceData.residenceData != null)
+				{
+					return Entity.DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585;
+				}
+				return 6000;
+            }
+		}
+
 		//鏈湴Socket
 		private Socket busSocket;
-		int _port = 0;
+
 		/// <summary>
 		/// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
 		/// </summary>
-		public void Start (int port)
+		public void Start ()
         {
-			if (IsRunning)
-			{
-                if (port == _port)
-                {
-                    return;
-                }
-                else
-                {
-                    busSocket.Close();
-                }
+            if (IsRunning || port == 0) {
+                return;
             }
-            _port = port;
 
             busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
             busSocket.EnableBroadcast = true;
             try {
-                //busSocket.Bind (new IPEndPoint (IPAddress.Any, 6000));
-                busSocket.Bind(new IPEndPoint(IPAddress.Any, port));
+                busSocket.Bind (new IPEndPoint (IPAddress.Any, port));
 
-                busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
+				busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
 			}
 			catch{
                 busSocket = null;
@@ -107,6 +113,10 @@
                 Console.WriteLine($"asyncBeginReceive {e.Message}");
             } 
 		}
+		/// <summary>
+        /// 鎼滅储缃戠粶璁惧瑙﹀彂鐨勫洖璋冧簨浠�
+        /// </summary>
+		public Action<string> SearchNetDeviceAction;
 
 		/// <summary>
 		/// 寮傛鎺ユ敹鏁版嵁缁撴潫
@@ -129,11 +139,22 @@
 				packet.Bytes = new byte[len];
 				Array.Copy(bytes, 0, packet.Bytes, 0, packet.Bytes.Length);
 
-				//MainPage.Log($"鎺ユ敹{packet.RemoteEndPoint}鏁版嵁");
 				//mqtt杩炴帴鏁版嵁璇诲彇  A鍗忚缃戠粶璁惧淇℃伅璇诲彇鍥炲 澶勭悊
 				if (((IPEndPoint)packet.RemoteEndPoint).Port == 8585)
 				{
-					Control.Ins.ConvertReceiveData(bytes);
+					var reString = System.Text.Encoding.UTF8.GetString(bytes);
+					var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(reString);
+					JToken jtc = null;
+					jt.TryGetValue("command", out jtc);
+					if (jtc != null && jtc.ToString() == "search_response")
+					{
+						JToken jto = null;
+						jt.TryGetValue("objects", out jto);
+						if (jto != null)
+						{
+							SearchNetDeviceAction?.Invoke(jto.ToString());
+						}
+					}
 				}
 				else if(((IPEndPoint)packet.RemoteEndPoint).Port == 6000)//澶勭悊bus 6000绔彛鐨勬暟鎹�
 				{

--
Gitblit v1.8.0