JLChen
2020-02-20 965290476c027deb96891cde1095d9a100581a6b
Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
@@ -43,7 +43,11 @@
                            MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
                            MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
                        });
                        await DisConnectRemoteAsync ();
                    } else {
                       
                        //Application.RunOnMainThread (() => {
@@ -65,12 +69,19 @@
                            //await SmartHome.MqttCommon.StartCloudMqtt ();
                            SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
                        }
                        if (gateWayList == null || gateWayList.Count == 0) {
                            Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                            await DisConnectRemoteAsync ();
                        }
                        //本地是否存在与当前住宅绑定网关的数据
                        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 (".", "")) {
                                    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) });
@@ -91,26 +102,6 @@
                                            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) {
                                                CommonPage.IsRemote = false;
                                                //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;
@@ -127,14 +118,51 @@
                                }
                            }
                        }
                        if (canRemote && !CommonPage.IsRemote) {
                        //能远程
                        if (canRemote) {
                            if (!CommonPage.IsRemote) {
                            if (UserConfig.Instance.CheckHomeGateways ()) {
                                if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways[0].GatewayUniqueId)) {
                                    Shared.SimpleControl.CommonPage.IsRemote = true;
                                        Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                                        CommonPage.IsRemote = true;
                                        SmartHome.MqttCommon.IsGatewayOnline = false;
                                    //await SmartHome.MqttCommon.StartCloudMqtt ();
                                }
                            }
                        }
                        } else {
                            //不能远程,如果之前远程模式则断开MQTT连接,显示为未连接
                            if (CommonPage.IsRemote) {
                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                                CommonPage.IsRemote = false;
                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
                            }
                            if (!HomeGatewaysExistsLocally) {
                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                            }
                        }
                        //if (canRemote && !CommonPage.IsRemote) {
                        //    if (UserConfig.Instance.CheckHomeGateways ()) {
                        //        if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
                        //            Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                        //            Shared.SimpleControl.CommonPage.IsRemote = true;
                        //            //await SmartHome.MqttCommon.StartCloudMqtt ();
                        //        }
                        //    }
                        //}
                    }
                } catch (Exception ex) {
                    Console.WriteLine ("CheckLinkRemote : " + ex.ToString ());
@@ -146,6 +174,20 @@
                }
            }) { IsBackground = true }.Start ();
        }
        /// <summary>
        /// 关闭远程连接模式,并断开MQTT
        /// </summary>
        /// <returns></returns>
        static async System.Threading.Tasks.Task DisConnectRemoteAsync (){
            if (CommonPage.IsRemote) {
                CommonPage.IsRemote = false;
                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
            }
        }
    }
}