From d8545fbd1b36a6766c57c987e5d89862b470fed9 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期四, 03 九月 2020 09:46:29 +0800
Subject: [PATCH] 2020-09-03 1.修改风扇默认为关状态。2.Alexa添加设备时,去掉空调和通用开关选择添加支持。 3.场景增加通用开关添加和发送控制支持,Alexa 的场景同样增加通用开关的支持。

---
 Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs |  333 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 215 insertions(+), 118 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
index c6cefce..c48d449 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
@@ -1,144 +1,241 @@
-锘縰sing System;
+using System;
 using Shared.SimpleControl.Phone;
-using System.Collections.Generic;
-using System.Text;
-using System.Net.NetworkInformation;
 
 namespace Shared.SimpleControl
 {
     public class EquipmentPublicClass
     {
-
+        /*
+                          
+         */
         public void AlertNotOnline ()
         {
-            new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+            new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
         }
 
-        static object lockObj = new object ();
-
+        static bool isConnectiong = false;
+        static DateTime mFlagDateTime;
         /// <summary>
         /// 妫�娴�/杩炴帴杩滅▼
         /// </summary>
-        /// <returns>The link remote.</returns>
-        /// <param name="status">Status.</param>
-        public static void CheckLinkRemote (int status)
+        /// <param name="status">褰撳墠缃戠粶鐘舵��</param>
+        /// <param name="IsReconnectRemote">鏄惁闇�瑕侀噸鏂拌繛鎺ヨ繙绋�</param>
+        public static void CheckLinkRemote (int status, bool IsReconnectRemote = true)
         {
-            lock (lockObj) {
-                Console.WriteLine ("CheckLinkRemote!!!");
-                System.Threading.Tasks.Task.Run (() => {
-                    try {
-                        if (status == 0) {
-                            Application.RunOnMainThread (() => {
-                                MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
-                                MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
-                                //UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
-                            });
-                        } else {
-                            MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
-                            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
-                                return;
-                            }
 
-                            var localFileList = IO.FileUtils.ReadFiles ();
-                            var gateWayList = localFileList.FindAll ((obj) => {
-                                return (obj.StartsWith ("Equipment_")) && (
-                                 obj.Split ('_') [1].ToString () == DeviceType.OnePortBus.ToString () ||
-                                    obj.Split ('_') [1].ToString () == DeviceType.RCU.ToString () ||
-                                    obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString ());
-                            });
-                            string rmf = "";
-                            List<string> linkList = new List<string> ();
-                            GatewayBase common = null;
-                            string gateWayString = "";
-                            if (gateWayList.Count > 1) {
-                                foreach (var gatewayFileName in gateWayList) {
-                                    var tempStrings = gatewayFileName.Split ('_');
-                                    if (tempStrings [1].ToString () == DeviceType.OnePortBus.ToString () || tempStrings [1].ToString () == DeviceType.RCU.ToString () ||
-                                        tempStrings [1].ToString () == DeviceType.OnePortWirelessFR.ToString ()) {
-                                        gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
-                                        common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
+            //20S鍚庡己鍒堕噸缃甶sConnectiong鐘舵�侊紝闃叉isConnectiong涓�鐩翠负true鐘舵��
+            if (mFlagDateTime.AddSeconds (20).Ticks <= System.DateTime.Now.Ticks) {
+                mFlagDateTime = DateTime.Now;
+                if (isConnectiong) {
+                    Utlis.WriteLine ("20s銆媔sConnectiong true ");
+                    isConnectiong = false;
+                }
+            }
 
-                                        if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) {
-                                            rmf = gatewayFileName;
-                                            if (!string.IsNullOrEmpty (common.Remote_UserName) && !string.IsNullOrEmpty (common.Remote_Password) &&
-                                                !string.IsNullOrEmpty (common.Remote_GroupName) && !string.IsNullOrEmpty (common.Remote_ProjectName)) {
-                                                break;
-                                            }
-                                        }
-                                        common = null;
-                                    }
-                                }
-                            } else if (gateWayList.Count == 1) {
-                                rmf = gateWayList [0];
-                                gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (rmf));
-                                common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
-                            } else {
-                                return;
-                            }
-#if DEBUG
-                            if (common == null) {
-                                if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
-                                    gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (UserConfig.Instance.RemoteModeFile));
-                                    common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
-                                }
-                            }
+            if (isConnectiong) {
+                return;
+            }
+            isConnectiong = true;
+            UserConfig.Instance.internetStatus = status;
+#if wallon
+                            return;
 #endif
-                            if (common == null || common.Type == DeviceType.UnKown) {
-                                return;
-                            }
-                            Application.RunOnMainThread (() => {
-                                MainPage.Loading.Start ("Please wait...");
-                            });
-                            if (status == 1) {
-                                //if (common.Type == DeviceType.OnePortWirelessFR) {
-                                //    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortWirelessFR> (gateWayString);
-                                //    var mac = gateWay.MAC.Replace (".", "");
-                                //    SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true, mac);
-                                //} else if (common.Type == DeviceType.OnePortBus) {
-                                //    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortBus> (gateWayString);
-                                //    var mac = gateWay.MAC.Replace (".", "");
-                                //    SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true, mac);
-                                //} else if (common.Type == DeviceType.RCU) {
-                                //    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<RCU> (gateWayString);
-                                //    var mac = gateWay.MAC.Replace (".", "");
-                                //    SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true, mac);
-                                //}
-                                var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
-                                SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true);
-                            } else {
+            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
+                isConnectiong = false;
+                return;
+            }
+
+            new System.Threading.Thread (async () => {
+
+                bool IsRemoteLoadingShow = false;
+
+                try {
+                    if (status == 0) {
+                        Application.RunOnMainThread (() => {
+                            MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
+                            MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
+                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
+
+                        });
+
+                        await DisConnectRemoteAsync ();
+
+                    } else {
+
+                        //Application.RunOnMainThread (() => {
+                        //    if (UserMiddle.LinkStatusTip.BackgroundColor == SkinStyle.Current.DelColor) {
+                        //        UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.MainColor;
+                        //    }
+                        //});
+                        var localFileList = IO.FileUtils.ReadFiles ();
+                        var gateWayList = localFileList.FindAll ((obj) => {
+                            return (obj.StartsWith ("Equipment_")) && (
+                             obj.Split ('_') [1].ToString () == DeviceType.OnePortBus.ToString () ||
+                                obj.Split ('_') [1].ToString () == DeviceType.RCU.ToString () ||
+                                obj.Split ('_') [1].ToString () == DeviceType.SuperWireless.ToString () ||
+                                obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString () ||
+                                obj.Split ('_') [1].ToString () == DeviceType.OnePortMqttFR.ToString ());
+                        });
+                        bool canRemote = false;
+                      
+
+
+                        //1.濡傛灉鏈湴娌$綉鍏虫暟鎹紝鎻愮ずWiFiOffline锛屽苟涓斿垽鏂槸鍚﹂渶瑕佹柇寮�杩滅▼
+                        if (gateWayList == null || gateWayList.Count == 0) {
+                            Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                            await DisConnectRemoteAsync ();
+                        }
+
+                        //2.杩滅▼杩炴帴鎯呭喌涓嬶紝鏄惁闇�瑕佹柇寮�杩炴帴閲嶈繛
+                        if (IsReconnectRemote) {
+                            if (CommonPage.IsRemote) {
                                 CommonPage.IsRemote = false;
-                                CommonPage.FindGateway = true;
-                                var result = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, common.SubnetID, common.DeviceID, new byte [] { }, false);
-                                CommonPage.FindGateway = false;
-                                if (result != null) {
-                                    var mac0 = CommonPage.byteToHex16 (result [0]) + "." + CommonPage.byteToHex16 (result [1]) + "." + CommonPage.byteToHex16 (result [2]) + "." + CommonPage.byteToHex16 (result [3]) + "." + CommonPage.byteToHex16 (result [4]) + "." + CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]);
-                                    //if (common.Type == DeviceType.OnePortBus) {
-                                    if (common.MAC == mac0) {
-                                        Application.RunOnMainThread (() => {
-                                            MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
-                                            //UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
-                                        });
-                                        return;
-                                    }
-                                }
-                                var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
-                                SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true);
+                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
                             }
                         }
-                    } catch (Exception ex) {
-                        Console.WriteLine (ex.ToString ());
-                    } finally {
-                        Application.RunOnMainThread (() => {
-                            if (status != 0) {
-                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
+
+                        //3.鍙戦�佷竴娆� CheckGateway鏁版嵁
+                        if (CommonPage.IsRemote) {
+                            //await SmartHome.MqttCommon.StartCloudMqtt ();
+                            SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
+                        }
+
+                        //4.鏈湴鏄惁瀛樺湪涓庡綋鍓嶄綇瀹呯粦瀹氱綉鍏崇殑鏁版嵁
+                        bool HomeGatewaysExistsLocally = false;
+                        foreach (var gatewayFileName in gateWayList) {
+                            var tempStrings = gatewayFileName.Split ('_');
+                            var gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
+                            var common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
+                            if (common != null) {
+                                if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) {
+                                    //4.1 鏈湴瀛樺湪涓庡綋鍓嶄綇瀹呯粦瀹氱綉鍏崇殑鏁版嵁锛屽鏋滄槸WiFi杩炴帴锛屽氨灞�鍩熺綉鎼滅储缃戝叧鏄惁鍦ㄥ悓涓�灞�鍩熺綉
+                                    HomeGatewaysExistsLocally = true;
+                                    canRemote = true;
+                                    if (status == 2) {
+                                        //var result = Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) });
+                                        #region
+                                        //寮�濮嬫湰鍦版悳绱㈢綉鍏�...
+                                        MainPage.LoadingStart ("Searching local gateway...");
+
+                                        var control = new Control ();
+                                        control.Send (new Target () {
+                                            IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000),
+                                            Command = Command.ReadGateway,
+                                            SubnetID = common.SubnetID,
+                                            DeviceID = common.DeviceID,
+                                            AddData = new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) },
+                                        }, SendCount.Three, true, true);
+                                        var result = control.UsefulBytes;
+
+                                        #endregion
+                                        if (result != null) {
+                                            canRemote = false;
+                                            var mac0 = CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]) + "." + CommonPage.byteToHex16 (result [8]) + "." + CommonPage.byteToHex16 (result [9]) + "." + CommonPage.byteToHex16 (result [10]) + "." + CommonPage.byteToHex16 (result [11]) + "." + CommonPage.byteToHex16 (result [12]);
+                                            if (common.MAC == mac0) {
+                                                //4.2 缃戝叧鍦ㄥ悓涓�灞�鍩熺綉锛屾樉绀篧iFi鐘舵�侊紝鍙栨秷杩滅▼鏂紑杩滅▼杩炴帴
+                                                CommonPage.IsRemote = false;
+                                                Application.RunOnMainThread (() => {
+                                                    MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
+                                                    UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
+                                                    MainPage.Loading.Hide ();
+                                                });
+                                                //鎼滅储鎴愬姛锛屽惎鐢ㄦ湰鍦拌繛鎺�
+                                                MainPage.AddTip ("Gateway searched successfully, enable local connection.");
+
+                                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
+                                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+                                                return;
+                                            } else {
+
+                                            }
+                                        } else {
+                                            //鎼滅储澶辫触锛屽惎鐢ㄨ繙绋嬭繛鎺�
+                                            MainPage.AddTip ("Gateway cannot be searched locally.");
+                                            
+                                        }
+                                    } else {
+                                        break;
+                                    }
+                                }
                             }
-                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
-                            MainPage.Loading.Hide ();
-                        });
+                        }
+
+
+
+                        //5.鑳借繙绋�
+                        if (canRemote) {
+                            if (!CommonPage.IsRemote) {
+                                if (UserConfig.Instance.CheckHomeGateways ()) {
+                                    if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
+                                        // 5.1鑳借繙绋�,鏄剧ずCloudUnlink鐘舵�侊紝IsRemote璁句负true寮�濮嬭繙绋�
+                                        //MainPage.AddTip ("Please wait, starting remote connection...");
+                                        //MainPage.LoadingStart ("Please wait, starting remote connection...");
+
+                                        MainPage.LoadingTipShow ("Please wait, starting remote connection...");
+
+                                        CommonPage.IsRemote = true;
+                                        IsRemoteLoadingShow = true;
+                                        //閲嶇疆鍙傛暟鐘舵��
+                                        SmartHome.MqttCommon.InitState ();
+                                        Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
+
+                                    }
+                                }
+                            }
+
+                        } else {
+
+                            //6.涓嶈兘杩滅▼锛屽鏋滀箣鍓嶈繙绋嬫ā寮忓垯鏂紑MQTT杩炴帴,鏄剧ず涓烘湭杩炴帴
+                            if (CommonPage.IsRemote) {
+                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                                CommonPage.IsRemote = false;
+                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+
+                            }
+                            //鏈湴涓嶅瓨鍦ㄤ笌褰撳墠浣忓畢缁戝畾缃戝叧鐨勬暟鎹�
+                            if (!HomeGatewaysExistsLocally) {
+                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                            }
+
+                        }
                     }
-                });
+                } catch (Exception ex) {
+                    Utlis.WriteLine ("CheckLinkRemote : " + ex.ToString ());
+                } finally {
+                    isConnectiong = false;
+                    //if (!IsRemoteLoadingShow) {
+                    //    Application.RunOnMainThread (() => {
+                    //        MainPage.Loading.Hide ();
+                    //    });
+                    //}
+
+                    Application.RunOnMainThread (() => {
+                        MainPage.Loading.Hide ();
+                    });
+                }
+            }) { IsBackground = true }.Start ();
+        }
+
+        //static bool IsRemoteLoadingShow = false;
+
+        /// <summary>
+        /// 鍏抽棴杩滅▼杩炴帴妯″紡锛屽苟鏂紑MQTT
+        /// </summary>
+        /// <returns></returns>
+        static async System.Threading.Tasks.Task DisConnectRemoteAsync ()
+        {
+
+            if (CommonPage.IsRemote) {
+                CommonPage.IsRemote = false;
+                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
             }
         }
     }
-
 }
+
+
+
+
+
+

--
Gitblit v1.8.0