From 56c4924b268da500483bd2de9522db5b34965120 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 09 四月 2020 13:35:10 +0800
Subject: [PATCH] 20200409

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

diff --git a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
index 34e3fad..4ab1f9b 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
@@ -1,3 +1,154 @@
+//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.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+//        }
+
+//        static bool isConnectiong = false;
+
+//        /// <summary>
+//        /// 妫�娴�/杩炴帴杩滅▼
+//        /// </summary>
+//        public static void CheckLinkRemote (int status)
+//        {
+//            //if (isConnectiong) {
+//            //    return;
+//            //}
+//            //isConnectiong = true;
+//            UserConfig.Instance.internetStatus = status;
+//#if wallon
+//                return;
+//#endif
+//            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
+//                return;
+//            }
+
+//            new System.Threading.Thread (async () => {
+//                try {
+//                    if (status == 0) {
+//                        Application.RunOnMainThread (() => {
+//                            MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
+//                            MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
+//                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
+//                        });
+//                    } else {
+//                        if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
+//                            return;
+//                        }
+//                        //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 ());
+//                        });
+//                        bool canRemote = false;
+//                        if (CommonPage.IsRemote) {
+//                            //await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+//                            //await SmartHome.MqttCommon.StartCloudMqtt ();
+
+//                            SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
+//                        }
+//                        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 (".", "")) {
+//                                    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
+//                                        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) {
+//                                                //var gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, common.SubnetID, common.DeviceID, new byte [] { });
+//                                                #region
+//                                                control.Send (new Target () {
+//                                                    IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000),
+//                                                    Command = Command.ReadGateWayModelInfo,
+//                                                    SubnetID = common.SubnetID,
+//                                                    DeviceID = common.DeviceID,
+//                                                    AddData = new byte [] { },
+//                                                }, SendCount.Three, true,true);
+//                                                var gatewayBytes = control.UsefulBytes;
+//                                                #endregion
+//                                                if (gatewayBytes != null) {
+//                                                    common.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 1, 20).Trim ('\0');
+//                                                    common.Remote_ProjectName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 21, 20).Trim ('\0');
+//                                                    common.Remote_UserName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 41, 8).Trim ('\0');
+//                                                    common.Remote_Password = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 49, 8).Trim ('\0');
+//                                                    var requestJson2 = @"{'Token':'" + MainPage.LoginUser.LoginTokenString + "','MAC':'" + common.MAC + "','Password':'" + common.Remote_Password + "'}";
+//                                                    var revertObj2 = MainPage.RequestHttps ("UpdateRemoteSwitchPassword", requestJson2, true, false);
+//                                                    return;
+//                                                }
+//                                                Application.RunOnMainThread (() => {
+//                                                    MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
+//                                                    UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
+//                                                    MainPage.Loading.Hide ();
+//                                                });
+//                                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
+//                                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+//                                                return;
+//                                            }
+//                                        }
+//                                    } else {
+//                                        break;
+//                                    }
+//                                }
+//                            }
+//                        }
+//                        if (canRemote && !CommonPage.IsRemote) {
+//                            if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.MAC)) {
+//                                Shared.SimpleControl.CommonPage.IsRemote = true;
+//                                await SmartHome.MqttCommon.StartCloudMqtt ();
+//                            }
+//                        }
+//                    }
+//                } catch (Exception ex) {
+//                    Console.WriteLine ("CheckLinkRemote : " + ex.ToString ());
+//                } finally {
+//                    isConnectiong = false;
+//                    Application.RunOnMainThread (() => {
+//                        MainPage.Loading.Hide ();
+//                    });
+//                }
+//            }) { IsBackground = true }.Start ();
+//        }
+//    }
+//}
+
+
 using System;
 using Shared.SimpleControl.Phone;
 using System.Collections.Generic;
@@ -8,145 +159,117 @@
 {
     public class EquipmentPublicClass
     {
-        /*
-                          
-         */
+
         public void AlertNotOnline ()
         {
-            new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+            new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
         }
 
-        static bool isConnectiong = false;
+        static object lockObj = new object ();
 
         /// <summary>
         /// 妫�娴�/杩炴帴杩滅▼
         /// </summary>
+        /// <returns>The link remote.</returns>
+        /// <param name="status">Status.</param>
         public static void CheckLinkRemote (int status)
         {
-            //if (isConnectiong) {
-            //    return;
-            //}
-            //isConnectiong = true;
-            UserConfig.Instance.internetStatus = status;
-#if wallon
-                return;
-#endif
-            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
-                return;
-            }
+            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;
+                            }
 
-            new System.Threading.Thread (async () => {
-                try {
-                    if (status == 0) {
-                        Application.RunOnMainThread (() => {
-                            MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
-                            MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
-                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
-                        });
-                    } else {
-                        if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
-                            return;
-                        }
-                        //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 ());
-                        });
-                        bool canRemote = false;
-                        if (CommonPage.IsRemote) {
-                            //await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
-                            //await SmartHome.MqttCommon.StartCloudMqtt ();
+                            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 ());
+                            });
+                            List<string> linkList = new List<string> ();
+                            GatewayBase common = null;
+                            string gateWayString = "";
+                            if (gateWayList.Count > 0) {
+                                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);
 
-                            SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
-                        }
-                        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 (".", "")) {
-                                    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
-                                        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) {
-                                                //var gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, common.SubnetID, common.DeviceID, new byte [] { });
-                                                #region
-                                                control.Send (new Target () {
-                                                    IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000),
-                                                    Command = Command.ReadGateWayModelInfo,
-                                                    SubnetID = common.SubnetID,
-                                                    DeviceID = common.DeviceID,
-                                                    AddData = new byte [] { },
-                                                }, SendCount.Three, true,true);
-                                                var gatewayBytes = control.UsefulBytes;
-                                                #endregion
-                                                if (gatewayBytes != null) {
-                                                    common.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 1, 20).Trim ('\0');
-                                                    common.Remote_ProjectName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 21, 20).Trim ('\0');
-                                                    common.Remote_UserName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 41, 8).Trim ('\0');
-                                                    common.Remote_Password = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 49, 8).Trim ('\0');
-                                                    var requestJson2 = @"{'Token':'" + MainPage.LoginUser.LoginTokenString + "','MAC':'" + common.MAC + "','Password':'" + common.Remote_Password + "'}";
-                                                    var revertObj2 = MainPage.RequestHttps ("UpdateRemoteSwitchPassword", requestJson2, true, false);
-                                                    return;
-                                                }
-                                                Application.RunOnMainThread (() => {
-                                                    MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
-                                                    UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
-                                                    MainPage.Loading.Hide ();
-                                                });
-                                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
-                                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
-                                                return;
+                                        if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) {
+                                            if (!string.IsNullOrEmpty (common.Remote_UserName) && !string.IsNullOrEmpty (common.Remote_Password) &&
+                                                !string.IsNullOrEmpty (common.Remote_GroupName) && !string.IsNullOrEmpty (common.Remote_ProjectName)) {
+                                                break;
                                             }
                                         }
-                                    } else {
-                                        break;
+                                        common = null;
                                     }
                                 }
+                            } 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);
+                                }
+                            }
+#endif
+                            if (common == null || common.Type == DeviceType.UnKown) {
+                                return;
+                            }
+                            Application.RunOnMainThread (() => {
+                                MainPage.Loading.Start ("Please wait...");
+                            });
+                            if (status == 1) {
+                                var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
+                                SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true);
+                            } else {
+                                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.MAC == mac0) {
+                                        Application.RunOnMainThread (() => {
+                                            MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
+                                        });
+                                        return;
+                                    }
+                                }
+                                var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
+                                SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true);
                             }
                         }
-                        if (canRemote && !CommonPage.IsRemote) {
-                            if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.MAC)) {
-                                Shared.SimpleControl.CommonPage.IsRemote = true;
-                                await SmartHome.MqttCommon.StartCloudMqtt ();
+                    } catch (Exception ex) {
+                        Console.WriteLine (ex.ToString ());
+                    } finally {
+                        Application.RunOnMainThread (() => {
+                            if (status != 0) {
+                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                             }
-                        }
+                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
+                            MainPage.Loading.Hide ();
+                        });
                     }
-                } catch (Exception ex) {
-                    Console.WriteLine ("CheckLinkRemote : " + ex.ToString ());
-                } finally {
-                    isConnectiong = false;
-                    Application.RunOnMainThread (() => {
-                        MainPage.Loading.Hide ();
-                    });
-                }
-            }) { IsBackground = true }.Start ();
+                });
+            }
         }
     }
+
 }
-
-
 

--
Gitblit v1.8.0