From 22477f0eca904ec628ab59cd749f959af35c1439 Mon Sep 17 00:00:00 2001
From: Moli <linmolilin@gmail.com>
Date: 星期五, 15 四月 2022 10:15:52 +0800
Subject: [PATCH] 加入按配置使用的功能

---
 Helpers/Tool.cs                      |   17 ++++-
 WOLAwaken/Helper/SendUdpPack.cs      |   33 ++++++++++
 WOLAwaken/Program.cs                 |   62 +++++++++++++++-----
 WOLAwaken/Helper/ReceiveUDPPack.cs   |   29 ++++++++-
 WOLAwaken/appsettings.json           |    7 +
 WOLAwaken/Helper/SendWOLPack.cs      |    9 ++
 PublicCode/SystemManage/SysConfig.cs |   15 ++++
 7 files changed, 140 insertions(+), 32 deletions(-)

diff --git a/Helpers/Tool.cs b/Helpers/Tool.cs
index 717af96..c42ffcc 100644
--- a/Helpers/Tool.cs
+++ b/Helpers/Tool.cs
@@ -677,11 +677,18 @@
         /// <param name="ConfigFileName"></param>
         /// <returns></returns>
         public static SysConfig GetSystemConfig(string ConfigFileName= "appsettings.json") {
-            string value = string.Empty;
-            string Root = AppContext.BaseDirectory;
-            string json = GetTxt(Root + "\\" + ConfigFileName);
-            SysConfig config = json.ToModel<SysConfig>();
-            return config;
+            try
+            {
+                string value = string.Empty;
+                string Root = AppContext.BaseDirectory;
+                string json = GetTxt(Root + "\\" + ConfigFileName);
+                SysConfig config = json.ToModel<SysConfig>();
+                return config;
+            }
+            catch 
+            {
+                throw new Exception("閰嶇疆璇诲彇閿欒锛岃妫�娴嬮」鐩洰褰曚笅鐨刟ppsettings.json鏂囦欢");
+            }
         }
 
         /// <summary>
diff --git a/PublicCode/SystemManage/SysConfig.cs b/PublicCode/SystemManage/SysConfig.cs
index c5c4f38..9b1a97d 100644
--- a/PublicCode/SystemManage/SysConfig.cs
+++ b/PublicCode/SystemManage/SysConfig.cs
@@ -115,6 +115,18 @@
         /// </summary>
         public string toIp { get; set; }
         /// <summary>
+        /// 鏈満缃戠粶ip
+        /// </summary>
+        public string receiveIp { get; set; }
+        /// <summary>
+        /// 鏈満鎺ユ敹绔彛
+        /// </summary>
+        public string receivePort { get; set; }
+        /// <summary>
+        /// 杩愯鐨勫姛鑳斤紝1鐩戝惉锛�2鍙戦�侊紝3鐩戝惉+鍙戦��
+        /// </summary>
+        public string startProject { get; set; }
+        /// <summary>
         /// 杞崲鍚庣殑mac鍦板潃
         /// </summary>
         public int[] mac
@@ -126,7 +138,8 @@
                 int index = 0;
                 foreach (var a in strs)
                 {
-                    macArr[index++] = Convert.ToInt32("0x" + a);
+
+                    macArr[index++] = Convert.ToInt32(a.ToString(), 16);
                 }
                 return macArr;
             }
diff --git a/WOLAwaken/Helper/ReceiveUDPPack.cs b/WOLAwaken/Helper/ReceiveUDPPack.cs
index 956a3dd..1d3f301 100644
--- a/WOLAwaken/Helper/ReceiveUDPPack.cs
+++ b/WOLAwaken/Helper/ReceiveUDPPack.cs
@@ -25,15 +25,29 @@
         /// <summary>
         /// 绾跨▼锛氫笉鏂洃鍚琔DP鎶ユ枃
         /// </summary>
-        static Thread thrRecv;
+        static Task thrRecv;
 
         public static void StartReceive()
         {
+            var receiveIp = "127.0.0.1";
+            var receivePort = 53490;
+            var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
+            if (config != null) 
+            {
+                if (!string.IsNullOrWhiteSpace(config.receiveIp))
+                {
+                    receiveIp = config.receiveIp;
+                }
+                if (!string.IsNullOrWhiteSpace(config.receivePort))
+                {
+                    int.TryParse(config.toPort, out receivePort);
+                }
+            }
             if (!IsUdpcRecvStart) // 鏈洃鍚殑鎯呭喌锛屽紑濮嬬洃鍚�
             {
-                localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53490); // 鏈満IP鍜岀洃鍚鍙e彿
+                localIpep = new IPEndPoint(IPAddress.Parse(receiveIp), receivePort); // 鏈満IP鍜岀洃鍚鍙e彿
                 udpcRecv = new UdpClient(localIpep);
-                thrRecv = new Thread(ReceiveMessage);
+                thrRecv = new Task(() => { ReceiveMessage(); });
                 thrRecv.Start();
                 IsUdpcRecvStart = true;
                 Console.WriteLine("UDP鐩戝惉鍣ㄥ凡鎴愬姛鍚姩");
@@ -44,7 +58,7 @@
         {
             if (IsUdpcRecvStart)
             {
-                thrRecv.Abort(); // 蹇呴』鍏堝叧闂繖涓嚎绋嬶紝鍚﹀垯浼氬紓甯�
+                thrRecv.Dispose(); // 蹇呴』鍏堝叧闂繖涓嚎绋嬶紝鍚﹀垯浼氬紓甯�
                 udpcRecv.Close();
                 IsUdpcRecvStart = false;
                 Console.WriteLine("UDP鐩戝惉鍣ㄥ凡鎴愬姛鍏抽棴");
@@ -55,13 +69,18 @@
         /// 鎺ユ敹鏁版嵁
         /// </summary>
         /// <param name="obj"></param>
-        private static void ReceiveMessage(object obj)
+        private static void ReceiveMessage()
         {
             while (IsUdpcRecvStart)
             {
                 try
                 {
                     byte[] bytRecv = udpcRecv.Receive(ref localIpep);
+                    foreach (var a in bytRecv)
+                    {
+                        Console.Write(a);
+                        Console.Write("-");
+                    }
                     string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length);
                     Console.WriteLine(string.Format("{0}[{1}]", localIpep, message));
 
diff --git a/WOLAwaken/Helper/SendUdpPack.cs b/WOLAwaken/Helper/SendUdpPack.cs
index 6398868..61e3d85 100644
--- a/WOLAwaken/Helper/SendUdpPack.cs
+++ b/WOLAwaken/Helper/SendUdpPack.cs
@@ -45,11 +45,40 @@
         {
             try
             {
-                localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53470); // 鏈満IP鍜岀洃鍚鍙e彿
+                var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
+                var ipConfig = "127.0.0.1";
+                var portConfig = 53470;
+                var toIp = "255.255.255.255";
+                var toPort = 53490;
+                if (config != null)
+                {
+                    if (!string.IsNullOrWhiteSpace(config.sendIp))
+                    {
+                        ipConfig = config.sendIp;
+                    }
+                    if (!string.IsNullOrWhiteSpace(config.sendPort))
+                    {
+                        int.TryParse(config.sendPort, out portConfig);
+                    }
+                    if (!string.IsNullOrWhiteSpace(config.toIp))
+                    {
+                        toIp = config.toIp;
+                    }
+                    if (!string.IsNullOrWhiteSpace(config.toPort))
+                    {
+                        int.TryParse(config.toPort, out toPort);
+                    }
+                }
+                else 
+                {
+                    ipConfig = "127.0.0.1";
+                }
+                localIpep = new IPEndPoint(IPAddress.Parse(ipConfig), portConfig); // 鏈満IP鍜岀洃鍚鍙e彿
+                
                 udpcSend = new UdpClient(localIpep);
 
                 byte[] sendbytes = data;
-                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 53490); // 鍙戦�佸埌鐨処P鍦板潃鍜岀鍙e彿
+                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(toIp), toPort); // 鍙戦�佸埌鐨処P鍦板潃鍜岀鍙e彿
                 string str = "";
                 foreach (var a in sendbytes)
                 {
diff --git a/WOLAwaken/Helper/SendWOLPack.cs b/WOLAwaken/Helper/SendWOLPack.cs
index f3b56f3..c66e86b 100644
--- a/WOLAwaken/Helper/SendWOLPack.cs
+++ b/WOLAwaken/Helper/SendWOLPack.cs
@@ -9,10 +9,17 @@
     public class SendWOLPack
     {
         public byte[] SendData = new byte[102];
-        public int[] macAddress = new int[] { 0x04, 0x92, 0x26, 0xBE, 0x92, 0x98 };
+        public int[] macAddress = new int[] { 0x50, 0xEB, 0xF6, 0x43, 0x50, 0x08 }; //50:EB:F6:43:50:08
 
         public SendWOLPack()
         {
+            var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
+            if (config != null) {
+                if (config.mac != null && config.mac.Length > 0)
+                {
+                    macAddress = config.mac;
+                }
+            }
             int index = 0;
             for (int i = 0; i < 6; i++)
             {
diff --git a/WOLAwaken/Program.cs b/WOLAwaken/Program.cs
index 7017608..6e1007f 100644
--- a/WOLAwaken/Program.cs
+++ b/WOLAwaken/Program.cs
@@ -1,21 +1,51 @@
 锘�// See https://aka.ms/new-console-template for more information
+using Helpers;
 using WOLAwaken.Helper;
 
 Console.WriteLine("Hello, World!");
-var config = Helpers.Tool.GetSystemConfig();
-Console.WriteLine(config.baseSysConfg);
+#region 鍙戦�佹柟娉�
+var SendFun = () => {
+    while (true)
+    {
+        try
+        {
+            new SendWOLPack();
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
+        }
+        Console.WriteLine("END;");
+        Console.WriteLine("鎸変笅鍥炶溅鍙互杩涜涓嬩竴娆″彂閫�;");
+        Console.ReadLine();
+    }
+};
+#endregion
+
+var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
+if (config != null && (!string.IsNullOrWhiteSpace(config.startProject))) 
+{
+    if (config.startProject == "1")
+    {
+        //鍙洃鍚�
+        ReceiveUDPPack.StartReceive();
+    }
+    else if (config.startProject == "2") 
+    {
+        //鍙彂閫�
+        SendFun();
+    }
+    else if (config.startProject == "3") 
+    {
+        //鐩戝惉鍔犲彂閫�
+        ReceiveUDPPack.StartReceive();
+        Task.Delay(1500).Wait();
+        SendFun();
+    } 
+    else 
+    {
+        Console.WriteLine("鏃犻�夐」");
+    }
+}
+
 Console.ReadLine();
-//ReceiveUDPPack.StartReceive();
-//while (true)
-//{
-//    try
-//    {
-//        new SendWOLPack();
-//    }
-//    catch (Exception ex)
-//    {
-//        Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
-//    }
-//    Console.WriteLine("END;");
-//    Console.ReadLine();
-//}
diff --git a/WOLAwaken/appsettings.json b/WOLAwaken/appsettings.json
index 1d9a971..8c4bd71 100644
--- a/WOLAwaken/appsettings.json
+++ b/WOLAwaken/appsettings.json
@@ -1,9 +1,12 @@
 {
   "baseSysConfg": {
-    "macStr": "04-92-26-BE-92-98",
+    "macStr": "50-EB-F6-BE-50-08",
     "sendPort": "53470",
     "toPort": "53490",
     "sendIp": "192.168.2.111",
-    "toIp": "255.255.255.255"
+    "toIp": "255.255.255.255",
+    "receiveIp": "192.168.2.101",
+    "receivePort": "53490",
+    "startProject": "1"
   }
 }

--
Gitblit v1.9.1