From 6a9ad7ec93218913a2ce3b898bb036f18f8f0da4 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 13 八月 2020 17:06:36 +0800
Subject: [PATCH] 20200813

---
 HDL_ON/DriverLayer/Control_Bus.cs |  322 ++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 232 insertions(+), 90 deletions(-)

diff --git a/HDL_ON/DriverLayer/Control_Bus.cs b/HDL_ON/DriverLayer/Control_Bus.cs
index 7ba3779..241684e 100644
--- a/HDL_ON/DriverLayer/Control_Bus.cs
+++ b/HDL_ON/DriverLayer/Control_Bus.cs
@@ -1,14 +1,24 @@
 锘縰sing System;
 using System.Collections.Generic;
+using System.Net.Sockets;
+using System.Text;
 using HDL_ON.DAL;
 using HDL_ON.DAL.Net;
 using HDL_ON.Entity;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using Shared;
+using Shared.Net;
 
 namespace HDL_ON
 {
     public partial class Control
     {
         public DateTime LatestDateTime = DateTime.Now;
+        /// <summary>
+        /// 鎺у埗澶辫触娆℃暟
+        /// </summary>
+        public static int controlLostCount = 0;
 
         /// <summary>
         /// 鎵�鏈夊井淇″涓�绔彛鐨勬帶鍒堕兘浼氭斁鍒拌繖涓泦鍚堥噷
@@ -16,28 +26,9 @@
         private static List<Control> controlList = new List<Control>(50);
 
         /// <summary>
-        /// 鍙戦�佹暟鎹紝绛夊緟鏈夊弽棣�
-        /// </summary>
-        /// <returns>The bytes send has return.</returns>
-        static byte[] ControlBytesSendHasReturn(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3)
-        {
-            Control control = new Control();
-            control.Send(new Target()
-            {
-                IPEndPoint = CommonPage.EndPoint,
-                Command = command,
-                SubnetID = subnetID,
-                DeviceID = deviceID,
-                AddData = gatewayBytes,
-            }, sendCount, true);
-
-            return control.UsefulBytes;
-        }
-
-        /// <summary>
         /// 鍙戦�佹暟鎹紝涓嶉渶瑕佺瓑寰呭洖澶�
         /// </summary>
-        static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null)
+       public static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null)
         {
             Control control = new Control();
             control.Send(new Target()
@@ -49,9 +40,97 @@
                 AddData = gatewayBytes,
             }, sendCount, false);
             MainPage.Log(command.ToString() + "::" + CommonPage.EndPoint.ToString());
-
         }
 
+        public static byte[] ReadGatewayIPAddress()
+        {
+            Control control = new Control();
+            control.ReadGatewayIP();
+            return control.UsefulBytes;
+        }
+
+        static DAL.Net.TcpListener tcpListener;
+
+        /// <summary>
+        /// 000E 鎼滅储鍥炲
+        /// </summary>
+        /// <param name="usefullBytes"></param>
+        static void ReceiveReadRemark(byte[] usefullBytes)
+        {
+            try
+            {
+                //璐﹀彿娌$櫥褰曚笉鍥炲
+                if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin)
+                {
+                    return;
+                }
+
+                if (tcpListener == null)
+                {
+                    tcpListener = new DAL.Net.TcpListener();
+                    tcpListener.OpenServer(8586);
+                }
+
+                if (!CommonPage.isHttpListenerStart)
+                {
+                    //鏁版嵁鎺ユ敹绔彛娌℃墦寮�锛屼笉鍥炲
+                    CommonPage.InitHttpListener();
+                }
+                var sendStr = MainPage.LoginUser.accountString;
+                if (usefullBytes.Length == 0)
+                {
+                    SenRemark(sendStr);
+                }
+                else
+                {
+                    bool isExit = false;
+
+                    for (int i = 0, len = usefullBytes.Length; i < len; i++)
+                    {
+                        if (i % 2 == 0)
+                        {
+                            if ((usefullBytes[i] & 0xFF) == 252
+                                    && (usefullBytes[i + 1] & 0xFF) == 252)
+                            {
+                                isExit = true;
+                                break;
+                            }
+                        }
+                    }
+
+                    if (!isExit)
+                    {
+                        //涓嶅瓨鍦紝浠h〃娌℃敹鍒版湰鏈虹殑鍙戦�侊紝缁х画鍥炲
+                        SenRemark(sendStr);
+                    }
+
+                }
+
+            }
+            catch { }
+        }
+
+        /// <summary>
+        /// 000F鍥炲澶囨敞
+        /// </summary>
+        /// <param name="sendStr"></param>
+        static void SenRemark(string sendStr)
+        {
+            byte[] sendBytes = new byte[20];
+            byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr);
+            //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
+            Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
+
+            var control = new Control();
+            control.Send(new Target()
+            {
+                IPEndPoint = CommonPage.EndPoint,
+                Command = Command.ReadRemarkACK,
+                SubnetID = 0xFF,
+                DeviceID = 0xFF,
+                AddData = sendBytes,
+            }, 0, false); //璁剧疆褰撳墠鍙戦�佹寚浠ゆ柟寮忎负锛氫换浣曟儏鍐典笅鏈湴鍙戦�併�佷笉闄愬埗銆佷笉鍔犲瘑鏄庢枃鍙戦��
+        }
         /// <summary>
         /// 澶勭悊鎺ユ敹鍥炴潵鐨勬暟鎹�
         /// </summary>
@@ -62,6 +141,25 @@
         /// <param name="remoteEndPoint">婧愮綉缁滃鎺ュ瓧</param>
         public static void ManagerReceive(byte subnetID, byte deviceID, Command command, byte targetSubnetID, byte targetDeviceID, byte[] usefulBytes, System.Net.EndPoint remoteEndPoint)
         {
+            if (((System.Net.IPEndPoint)remoteEndPoint).Port == 6688)
+            {
+        
+                for (int i = 0; i < controlList.Count; i++)
+                {
+                    try
+                    {
+                        var control = controlList[i];
+                        control.LatestDateTime = DateTime.Now;
+                        control.UsefulBytes = usefulBytes;//
+                        control.run();
+                    }
+                    catch (Exception ex)
+                    {
+                        Console.WriteLine($"control error : {ex.Message}");
+                    }
+                }
+            }
+
             try
             {
                 string receiveFlag = string.Format("{0},{1},{2},", subnetID, deviceID, (int)command);
@@ -74,7 +172,21 @@
                     case Command.SetLogicLoopColorACK:
                         receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
                         break;
-
+                    case Command.ReadLogicLoopColorACK:
+                    case Command.ReadACModeACK:
+                    case Command.SetACModeACK:
+                    case Command.ReadFloorHeatACK:
+                    case Command.SetFloorHeatACK:
+                        receiveFlag += string.Format("{0}", usefulBytes[0]);
+                        break;
+                    case Command.ReadRemark:
+                        //buspro 璇诲娉ㄥ洖澶�
+                        Console.WriteLine("buspro 璇诲娉ㄥ洖澶�");
+                        ReceiveReadRemark(usefulBytes);
+                        break;
+                    case Command.ReadLightAllLoopBrightnessACK:
+                        receiveFlag += "";
+                        break;
                     //case Command.YIPanelDeviceInofACK:
                     //    for (int i = 0; i < 4; i++)
                     //    {
@@ -125,15 +237,11 @@
                     //case Command.ReadSensorPushValuesACK:
                     //    receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                     //    break;
-                    //case Command.ReadLightEquipmentAllLoopBrightnessACK:
-                    //    break;
                     //case Command.UpLoadInfraredACK:
                     //    receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                     //    break;
                     //case Command.SetCommonACK:
                     //case Command.InfraredChannelControlACK:
-                    //case Command.ReadACModeACK:
-                    //case Command.SetACModeACK:
                     //case Command.Serverx_FH_CMD_ACK:
                     //case Command.ReadSensorHistoryACK:
                     //case Command.SetSensorAutomationTargetLevelEnableACK:
@@ -223,14 +331,14 @@
                     //case Command.Remote3thACK:
                     //    receiveFlag = string.Format("{0},", (int)command);
                     //    break;
-                    //case Command.ReadDeviceLoopInfoACK:
-                    //    if (usefulBytes[0] == 1)
-                    //    {
-                    //        receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ;
-                    //    }
-                    //    else
-                    //        receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
-                    //    break;
+                    case Command.ReadDeviceLoopInfoACK:
+                        if (usefulBytes[0] == 1)
+                        {
+                            receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ;
+                        }
+                        else
+                            receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
+                        break;
                     //case Command.SetDeviceLoopInfoACK:
                     //case Command.InfraredControlACK:
                     //case Command.SetSecurityByPassACK:
@@ -243,18 +351,17 @@
                     //case Command.ReadWirelessPanelButtonKeyACK:
                     //case Command.WriteWirelessPanelButtonKeyACK:
                     //case Command.ReadDryContactStatusACK:
-                    //case Command.InstructionPanelKeyACK:
-                    //case Command.ReadInstructionPanelKeyACK:
-                    //case Command.ReadAnalogValueACK:
-                    //case Command.ReadSensorTargetRemarkACK:
-                    //case Command.SetHornTargetStateACK:
-                    //case Command.ReadHornTargetStateACK:
-                    //    receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
-                    //    break;
+                    case Command.InstructionPanelKeyACK:
+                    case Command.ReadInstructionPanelKeyACK:
+                        //case Command.ReadAnalogValueACK:
+                        //case Command.ReadSensorTargetRemarkACK:
+                        //case Command.SetHornTargetStateACK:
+                        //case Command.ReadHornTargetStateACK:
+                        receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
+                        break;
                     //case Command.AssignedAddressACK:
                     //case Command.UpdataCurtainModelRunTimeACK:
                     //case Command.ReadCurtainStutasACK:
-                    //case Command.ReadLogicLoopColorACK:
                     //case Command.ReadPanleTempACK:
                     //case Command.FreshAirReadACK:
                     //case Command.FreshAirControlACK:
@@ -297,9 +404,9 @@
                             control.run();
                         }
                     }
-                    catch
+                    catch (Exception ex)
                     {
-
+                        Console.WriteLine($"control error : {ex.Message}");
                     }
                 }
             }
@@ -404,22 +511,22 @@
             add();
             if (CommonPage.IsRemote)
             {
-                MqttCommon.MqttRemoteSend(Packet.Bytes);
-                Packet.FlagDateTime = DateTime.Now;
-                Packet.HaveSendCount--;
-                //杩欓噷鏄噸鍙戜袱娆�
-                while (Packet.HaveSendCount < 3)
-                {
-                    if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks)
-                    {
-                        MqttCommon.MqttRemoteSend(Packet.Bytes);
-                        Packet.FlagDateTime = DateTime.Now;
-                        Packet.HaveSendCount++;
-                    }
-                    System.Threading.Thread.Sleep(100);
-                }鈥�                allDone.Set();鈥�            }
+             鈥�            }
             else
             {
+                //Bus socket鏃犳硶鎺у埗锛岄噸鍚満鍒�
+                if (controlLostCount > 10)
+                {
+                    BusSocket.Stop();
+                    new System.Threading.Thread(() =>
+                    {
+                        System.Threading.Thread.Sleep(1000);
+                        BusSocket.Start(6000);
+                        controlLostCount = 0;
+                    })
+                    { IsBackground = true }.Start();
+                }
+
                 try
                 {
                     MainPage.Log("鍙戦�佹暟鎹�:" + SendFlag);
@@ -429,10 +536,11 @@
                     //杩欓噷鏄噸鍙戜袱娆�
                     while (Packet.HaveSendCount < 3)
                     {
-                        if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks)
+                        if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
                         {
                             MainPage.Log("閲嶅彂鏁版嵁:" + SendFlag);
                             BusSocket.AsyncBeginSend(Packet);
+                            controlLostCount++;
                         }
                         System.Threading.Thread.Sleep(100);
                     }
@@ -453,17 +561,25 @@
         /// </summary>
         Packet Packet;
 
-        private void ini(Target target, bool send = true)
+        private void ini(Target target)
         {
             this.SendFlag = string.Format("{0},{1},{2},", target.SubnetID, target.DeviceID, (int)target.Command + 1);
 
             switch (target.Command)
             {
                 case Command.SetSingleLight:
+                case Command.ReadLogicLoopColor:
+                case Command.ReadACMode:
+                case Command.SetACMode:
+                case Command.ReadFloorHeat:
+                case Command.SetFloorHeat:
                     this.sendFlag += string.Format("{0}", target.AddData[0]);
                     break;
                 case Command.SetLogicLoopColor:
                     this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
+                    break;
+                case Command.ReadLightAllLoopBrightness:
+                    this.SendFlag += "";
                     break;
                 //case Command.YIPanelDeviceInof:
                 //    for (int i = 0; i < 4; i++)
@@ -496,8 +612,6 @@
                 //    break;
                 //case Command.SetCommonSwitch:
                 //case Command.InfraredChannelControl:
-                //case Command.ReadACMode:
-                //case Command.SetACMode:
                 //case Command.ReadFoolHeat:
                 //case Command.SetFoolHeat:
                 //case Command.Serverx_FH_CMD:
@@ -592,12 +706,12 @@
                 //case Command.Remote3th:
                 //    this.SendFlag = string.Format("{0},", (int)target.Command + 1);
                 //    break;
-                //case Command.ReadDeviceLoopInfo:
-                //    if (target.AddData[0] == 1)//鐗规畩澶勭悊鐏厜绫伙紝DMX妯″潡姣忎竴涓洖璺笉鍥哄畾灏忕被锛屾牴鎹叿浣撹缃潵鍥炲
-                //        this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]);
-                //    else
-                //        this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
-                //    break;
+                case Command.ReadDeviceLoopInfo:
+                    if (target.AddData[0] == 1)//鐗规畩澶勭悊鐏厜绫伙紝DMX妯″潡姣忎竴涓洖璺笉鍥哄畾灏忕被锛屾牴鎹叿浣撹缃潵鍥炲
+                        this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]);
+                    else
+                        this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
+                    break;
                 //case Command.SetDeviceLoopInfo:
                 //case Command.InfraredControl:
                 //case Command.SetSecurityByPass:
@@ -611,16 +725,15 @@
                 //case Command.SetCurtainModelStutas:
                 //case Command.DownloadInfrared:
                 //case Command.UpLoadInfrared:
-                //case Command.InstructionPanelKey:
-                //case Command.ReadInstructionPanelKey:
-                //case Command.ReadSensorTargetRemark:
-                //case Command.SetHornTargetState:
-                //    this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[1]);
-                //    break;
+                case Command.InstructionPanelKey:
+                case Command.ReadInstructionPanelKey:
+                    //case Command.ReadSensorTargetRemark:
+                    //case Command.SetHornTargetState:
+                    this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[1]);
+                    break;
                 //case Command.AssignedAddress:
                 //case Command.UpdataCurtainModelRunTime:
                 //case Command.ReadCurtainStatus:
-                //case Command.ReadLogicLoopColor:
                 //case Command.ReadPanleTemp:
                 //case Command.FreshAirRead:
                 //case Command.FreshAirControl:
@@ -650,12 +763,10 @@
                     //return;
                     break;
             }
-            if (send)
-            {
-                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
-                thread.IsBackground = true;
-                thread.Start(Packet);
-            }
+
+            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
+            thread.IsBackground = true;
+            thread.Start(Packet);
         }
 
         /// <summary>
@@ -663,18 +774,49 @@
         /// </summary>
         /// <param name="target">鍙戦�佸璞�</param>
         /// <param name="sendCount">閲嶅彂娆℃暟</param>
-        public void Send(Target target, int sendCount, bool isWait)
+        void Send(Target target, int sendCount, bool isWait)
         {
-            Packet = new Packet(target.SendBytes, target.IPEndPoint);
-            Packet.HaveSendCount = 3 - sendCount;
-
-            ini(target);
-
-            if (isWait)
+            try
             {
-                this.wait();
+                Packet = new Packet(target.SendBytes, target.IPEndPoint);
+                Packet.HaveSendCount = 3 - sendCount;
+
+                ini(target);
+
+                if (isWait)
+                {
+                    this.wait();
+                }
+            }
+            catch(Exception ex)
+            {
+                MainPage.Log($"Send bus data error {ex.Message}");
             }
         }
 
+        /// <summary>
+        /// 璇诲彇缃戝叧IP
+        /// </summary>
+        void ReadGatewayIP()
+        {
+            try
+            {
+                var sendJob = new JObject { { "vendor_code", "HDL" }, { "command", "search" } };
+                var SearchGateway = JsonConvert.SerializeObject(sendJob);
+                var SearchGatewayPayload = Encoding.ASCII.GetBytes(SearchGateway);
+                Packet = new Packet(SearchGatewayPayload, CommonPage.GetGatewayIP_EndPoint);
+                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
+                thread.IsBackground = true;
+                thread.Start(Packet);
+
+                wait();
+            }
+            catch (Exception ex)
+            {
+                MainPage.Log($"Send bus data error {ex.Message}");
+            }
+        }
+
+
     }
 }

--
Gitblit v1.8.0