JLChen
2020-02-28 9137222ece9b978eb97b5598f2eee6e070bcf9d6
Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
@@ -1,8 +1,5 @@
using System;
using Shared.SimpleControl.Phone;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
namespace Shared.SimpleControl
{
@@ -17,25 +14,37 @@
        }
        static bool isConnectiong = false;
        static DateTime mFlagDateTime;
        /// <summary>
        /// 检测/连接远程
        /// </summary>
        public static void CheckLinkRemote (int status)
        /// <param name="status">当前网络状态</param>
        /// <param name="IsReconnectRemote">是否需要重新连接远程</param>
        public static void CheckLinkRemote (int status, bool IsReconnectRemote = true)
        {
            //if (isConnectiong) {
            //    return;
            //}
            //isConnectiong = true;
            //20S后强制重置isConnectiong状态,防止isConnectiong一直为true状态
            if (mFlagDateTime.AddSeconds (20).Ticks <= System.DateTime.Now.Ticks) {
                mFlagDateTime = DateTime.Now;
                if (isConnectiong) {
                    Console.WriteLine ("20s》isConnectiong true ");
                    isConnectiong = false;
                }
            }
            if (isConnectiong) {
                return;
            }
            isConnectiong = true;
            UserConfig.Instance.internetStatus = status;
#if wallon
                return;
                            return;
#endif
            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin || isConnectiong) {
            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
                isConnectiong = false;
                return;
            }
            isConnectiong = true;
            new System.Threading.Thread (async () => {
                try {
                    if (status == 0) {
@@ -65,15 +74,30 @@
                                obj.Split ('_') [1].ToString () == DeviceType.OnePortMqttFR.ToString ());
                        });
                        bool canRemote = false;
                        if (CommonPage.IsRemote) {
                            //await SmartHome.MqttCommon.StartCloudMqtt ();
                            SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
                        }
                        //1.如果本地没网关数据,提示WiFiOffline,并且判断是否需要断开远程
                        if (gateWayList == null || gateWayList.Count == 0) {
                            Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                            await DisConnectRemoteAsync ();
                        }
                        //本地是否存在与当前住宅绑定网关的数据
                        //2.远程连接情况下,是否需要断开连接重连
                        if (IsReconnectRemote) {
                            if (CommonPage.IsRemote) {
                                CommonPage.IsRemote = false;
                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
                            }
                        }
                        //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 ('_');
@@ -81,6 +105,7 @@
                            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) {
@@ -101,6 +126,7 @@
                                            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 网关在同一局域网,显示WiFi状态,取消远程断开远程连接
                                                CommonPage.IsRemote = false;
                                                Application.RunOnMainThread (() => {
                                                    MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
@@ -121,11 +147,12 @@
                        //能远程
                        //5.能远程
                        if (canRemote) {
                            if (!CommonPage.IsRemote) {
                                if (UserConfig.Instance.CheckHomeGateways ()) {
                                    if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
                                        // 5.1能远程,显示CloudUnlink状态,IsRemote设为true开始远程
                                        Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                                        CommonPage.IsRemote = true;
                                        SmartHome.MqttCommon.IsGatewayOnline = false;
@@ -136,33 +163,19 @@
                        } else {
                            //不能远程,如果之前远程模式则断开MQTT连接,显示为未连接
                            //6.不能远程,如果之前远程模式则断开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 ());
@@ -176,12 +189,13 @@
        }
        /// <summary>
        /// 关闭远程连接模式,并断开MQTT
        /// </summary>
        /// <returns></returns>
        static async System.Threading.Tasks.Task DisConnectRemoteAsync (){
        /// <returns></returns>
        static async System.Threading.Tasks.Task DisConnectRemoteAsync ()
        {
            if (CommonPage.IsRemote) {
                CommonPage.IsRemote = false;
@@ -193,3 +207,6 @@