wxr
2024-12-02 ea0b1e8e5f43c5fd0a7d479e25ede3b8cbea464a
HDL_ON/DAL/DriverLayer/Control_Tcp.cs
@@ -52,154 +52,154 @@
        #region ■ 搜索Link网关_______________________
//        /// <summary>
//        /// 搜索Link网关
//        /// </summary>
//        /// <param name="resultEvent">搜索到网关的回调函数,-1:异常了 1:收到一个A网关</param>
//        /// <param name="searchTime">搜索时间(秒)</param>
//        /// <param name="connectLocalOnly">当搜索到网关时,是否只连接本地的(不能瞎连其他的网关,但是网关搜索界面特殊)</param>
//        public void SearchLinkGateway(Action<int, AGatewayInfo> resultEvent, int searchTime, bool connectLocalOnly)
//        {
//            this.isSearchGateway = true;
//            //算了,在这里启动远程连接吧,反正也就执行一次
//            HdlLinkCloudConnectLogic.Current.StartCloudMqttConnection();
        //        /// <summary>
        //        /// 搜索Link网关
        //        /// </summary>
        //        /// <param name="resultEvent">搜索到网关的回调函数,-1:异常了 1:收到一个A网关</param>
        //        /// <param name="searchTime">搜索时间(秒)</param>
        //        /// <param name="connectLocalOnly">当搜索到网关时,是否只连接本地的(不能瞎连其他的网关,但是网关搜索界面特殊)</param>
        //        public void SearchLinkGateway(Action<int, AGatewayInfo> resultEvent, int searchTime, bool connectLocalOnly)
        //        {
        //            this.isSearchGateway = true;
        //            //算了,在这里启动远程连接吧,反正也就执行一次
        //            HdlLinkCloudConnectLogic.Current.StartCloudMqttConnection();
//            var listGateway = HdlLinkGatewayLogic.Current.GetAllLocalGateway();
//            var dicGateway = new Dictionary<string, ZigBee.Device.LinkGateway>();
//            foreach (var gw in listGateway)
//            {
//                //获取本地全部的A网关的mac
//                dicGateway[gw.GwMac] = gw;
//            }
//            //重复检测
//            var listCheck = new HashSet<string>();
        //            var listGateway = HdlLinkGatewayLogic.Current.GetAllLocalGateway();
        //            var dicGateway = new Dictionary<string, ZigBee.Device.LinkGateway>();
        //            foreach (var gw in listGateway)
        //            {
        //                //获取本地全部的A网关的mac
        //                dicGateway[gw.GwMac] = gw;
        //            }
        //            //重复检测
        //            var listCheck = new HashSet<string>();
//            var sendData = "Topic:/user/all/custom/gateway/search\r\n";
        //            var sendData = "Topic:/user/all/custom/gateway/search\r\n";
//            var dic = new Dictionary<string, string>();
//            dic["id"] = HdlGatewayResourse.AGatewayLinkFlage;
//            dic["time_stamp"] = HdlHttpLogic.Current.GetTimestamp().PadRight(13, '0');
//            var bodyData = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
//            sendData += "Length:" + bodyData.Length + "\r\n\r\n";
//            sendData += bodyData;
        //            var dic = new Dictionary<string, string>();
        //            dic["id"] = HdlGatewayResourse.AGatewayLinkFlage;
        //            dic["time_stamp"] = HdlHttpLogic.Current.GetTimestamp().PadRight(13, '0');
        //            var bodyData = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
        //            sendData += "Length:" + bodyData.Length + "\r\n\r\n";
        //            sendData += bodyData;
//            //初始化udp
//            var udpLogic = new HdlUdpLogic();
//            if (udpLogic.InitUdp("239.0.168.188", 8585) == false)
//            {
//                //关闭udp
//                udpLogic.CloseUdp();
        //            //初始化udp
        //            var udpLogic = new HdlUdpLogic();
        //            if (udpLogic.InitUdp("239.0.168.188", 8585) == false)
        //            {
        //                //关闭udp
        //                udpLogic.CloseUdp();
//                resultEvent?.Invoke(-1, null);
//                return;
//            }
        //                resultEvent?.Invoke(-1, null);
        //                return;
        //            }
//            HDL_ON.DriverLayer.Control.Ins.OpenUdp(8585);
        //            HDL_ON.DriverLayer.Control.Ins.OpenUdp(8585);
//            //接收事件
//            Action<System.Net.IPEndPoint, string> actionEvent = (ipPoint, result) =>
//            {
//                //处理搜索A网关时,网关回复的数据
//                try
//                {
//                    /////
//                    System.Net.IPAddress s = ipPoint.Address;
//                    MainPage.Log("TcpClient->SearchLinkGateway", $"{s}==网关搜索本地回复,数据内容{result}");
        //            //接收事件
        //            Action<System.Net.IPEndPoint, string> actionEvent = (ipPoint, result) =>
        //            {
        //                //处理搜索A网关时,网关回复的数据
        //                try
        //                {
        //                    /////
        //                    System.Net.IPAddress s = ipPoint.Address;
        //                    MainPage.Log("TcpClient->SearchLinkGateway", $"{s}==网关搜索本地回复,数据内容{result}");
//                    //检验A协议网关主题(如果检验是指定主题,则返回负载数据,否则返回null)
//                    var resultData = HdlLinkGatewaySendLogic.Current.CheckLinkGatewayTopic("/user/all/custom/gateway/search_reply", result);
//                    if (resultData == null) { return; }
//                    //调用回调函数
//                    var info = Newtonsoft.Json.JsonConvert.DeserializeObject<AGatewayInfo>(resultData);
//                    if (info == null)
//                    {
//                        //数据异常,退出
//                        return;
//                    }
//                    if (!(string.IsNullOrEmpty(info.HomeId) == true || info.HomeId == Common.Config.Instance.HomeId))
//                    {
//                        //网关已经绑定其他住宅,退出
//                        return;
//                    }
//                    ///tcp连接目标设备的ip
//                    info.Ip_address = ipPoint.Address.ToString();
//                    //搜索到的这个网关,是否是存在在本地了的
//                    if (dicGateway.ContainsKey(info.Device_mac) == true)
//                    {
//                        var gateWay = dicGateway[info.Device_mac];
//                        //如果搜索得到本地存在的网关,则使用本地通信
//                        gateWay.OnlineStatu = 1;
//                        gateWay.isLocalEncrypt = info.isLocalEncrypt;
//                        gateWay.Master = info.Master;
//                        //gateWay.Slaver_list1 = info.Slaver_list1;
//                        gateWay.Oid = info.Oid;
//                        gateWay.InGatewayHomeId = info.HomeId;
//                        info.IsBindGateway = true;
        //                    //检验A协议网关主题(如果检验是指定主题,则返回负载数据,否则返回null)
        //                    var resultData = HdlLinkGatewaySendLogic.Current.CheckLinkGatewayTopic("/user/all/custom/gateway/search_reply", result);
        //                    if (resultData == null) { return; }
        //                    //调用回调函数
        //                    var info = Newtonsoft.Json.JsonConvert.DeserializeObject<AGatewayInfo>(resultData);
        //                    if (info == null)
        //                    {
        //                        //数据异常,退出
        //                        return;
        //                    }
        //                    if (!(string.IsNullOrEmpty(info.HomeId) == true || info.HomeId == Common.Config.Instance.HomeId))
        //                    {
        //                        //网关已经绑定其他住宅,退出
        //                        return;
        //                    }
        //                    ///tcp连接目标设备的ip
        //                    info.Ip_address = ipPoint.Address.ToString();
        //                    //搜索到的这个网关,是否是存在在本地了的
        //                    if (dicGateway.ContainsKey(info.Device_mac) == true)
        //                    {
        //                        var gateWay = dicGateway[info.Device_mac];
        //                        //如果搜索得到本地存在的网关,则使用本地通信
        //                        gateWay.OnlineStatu = 1;
        //                        gateWay.isLocalEncrypt = info.isLocalEncrypt;
        //                        gateWay.Master = info.Master;
        //                        //gateWay.Slaver_list1 = info.Slaver_list1;
        //                        gateWay.Oid = info.Oid;
        //                        gateWay.InGatewayHomeId = info.HomeId;
        //                        info.IsBindGateway = true;
//                        Common.Config.Instance.Home.NowHomeOnlineStatu = "1";
//                    }
//                    //搜索到的网关,建立链接
//                    if (connectLocalOnly == false || info.IsBindGateway == true)
//                    {
//                        this.ConnectLocalLinkGateway(info.Device_mac, info.Ip_address, info.GatewayId, info.isLocalEncrypt);
//                    }
//                    else
//                    {
//                        //获取连接对象
//                        var connectObj = this.GetConnectObject(info.Device_mac, false);
//                        if (connectObj != null)
//                        {
//                            //如果有这个对象, 则替换一下它的网关id
//                            connectObj.GatewayId = info.GatewayId;
//                            connectObj.localEncrypt = info.isLocalEncrypt;
//                            return;
//                        }
//                    }
//                    if (listCheck.Contains(info.Device_mac) == true)
//                    {
//                        //已经处理过了
//                        return;
//                    }
//                    listCheck.Add(info.Device_mac);
        //                        Common.Config.Instance.Home.NowHomeOnlineStatu = "1";
        //                    }
        //                    //搜索到的网关,建立链接
        //                    if (connectLocalOnly == false || info.IsBindGateway == true)
        //                    {
        //                        this.ConnectLocalLinkGateway(info.Device_mac, info.Ip_address, info.GatewayId, info.isLocalEncrypt);
        //                    }
        //                    else
        //                    {
        //                        //获取连接对象
        //                        var connectObj = this.GetConnectObject(info.Device_mac, false);
        //                        if (connectObj != null)
        //                        {
        //                            //如果有这个对象, 则替换一下它的网关id
        //                            connectObj.GatewayId = info.GatewayId;
        //                            connectObj.localEncrypt = info.isLocalEncrypt;
        //                            return;
        //                        }
        //                    }
        //                    if (listCheck.Contains(info.Device_mac) == true)
        //                    {
        //                        //已经处理过了
        //                        return;
        //                    }
        //                    listCheck.Add(info.Device_mac);
//#if DEBUG
//                    Console.WriteLine($"搜索网关捕抓到网关:{info.Device_mac}");
//#endif
//                    //info
        //#if DEBUG
        //                    Console.WriteLine($"搜索网关捕抓到网关:{info.Device_mac}");
        //#endif
        //                    //info
//                    //回调函数
//                    resultEvent?.Invoke(1, info);
//                }
//                catch (Exception e)
//                {
//                    string str = e.Message;
//                }
//            };
//            udpLogic.ReceviceEvent += actionEvent;
//            var dateTime = System.DateTime.Now.AddSeconds(searchTime);
//            while (this.isSearchGateway && System.DateTime.Now < dateTime)
//            {
//                udpLogic.SendData(sendData, "239.0.168.188", 8585);
//                udpLogic.SendData(sendData, "255.255.255.255", 8585);
//                System.Threading.Thread.Sleep(1000);
//            }
//            udpLogic.ReceviceEvent -= actionEvent;
//            //别关那么快
//            System.Threading.Thread.Sleep(300);
//            this.isSearchGateway = false;
//            //关闭udp
//            udpLogic.CloseUdp();
        //                    //回调函数
        //                    resultEvent?.Invoke(1, info);
        //                }
        //                catch (Exception e)
        //                {
        //                    string str = e.Message;
        //                }
        //            };
        //            udpLogic.ReceviceEvent += actionEvent;
        //            var dateTime = System.DateTime.Now.AddSeconds(searchTime);
        //            while (this.isSearchGateway && System.DateTime.Now < dateTime)
        //            {
        //                udpLogic.SendData(sendData, "239.0.168.188", 8585);
        //                udpLogic.SendData(sendData, "255.255.255.255", 8585);
        //                System.Threading.Thread.Sleep(1000);
        //            }
        //            udpLogic.ReceviceEvent -= actionEvent;
        //            //别关那么快
        //            System.Threading.Thread.Sleep(300);
        //            this.isSearchGateway = false;
        //            //关闭udp
        //            udpLogic.CloseUdp();
//        }
        //        }
//        /// <summary>
//        /// 停止搜索Link网关
//        /// </summary>
//        public void StopSearchLinkGateway()
//        {
//            this.isSearchGateway = false;
//        }
        //        /// <summary>
        //        /// 停止搜索Link网关
        //        /// </summary>
        //        public void StopSearchLinkGateway()
        //        {
        //            this.isSearchGateway = false;
        //        }
        #endregion
@@ -359,6 +359,8 @@
            connectData.tcpClient?.CommSend(byteData, byteData.Length);
        }
        #endregion
        #region ■ 处理Link网关的数据_________________
@@ -377,7 +379,7 @@
                    //非正规主题
                    return;
                }
                var connectData = this.GetConnectObjectBySocket(socket);
                if (connectData == null)
@@ -401,7 +403,7 @@
                var bodyData = System.Text.Encoding.UTF8.GetString(bytes);
                MainPage.Log(DateTime.Now + ":Control_Tcp->本地接收数据", $"Topic:{topic}\r\nData:{bodyData}");
                if (topic.Contains("thing/property/up") == true)
                {
                    //这个是Link设备的状态上报,别整,只要zigbee的