wei
2020-12-23 271faaf6664166dc368639b9f2e577f63f901be5
HDL_ON/Common/ApiUtlis.cs
@@ -19,7 +19,7 @@
        {
            get
            {
                if(apiUtlis == null)
                if (apiUtlis == null)
                {
                    apiUtlis = new ApiUtlis();
                }
@@ -33,7 +33,7 @@
        {
            get
            {
                if(httpRequest == null)
                if (httpRequest == null)
                {
                    httpRequest = new HttpServerRequest();
                }
@@ -47,7 +47,7 @@
        {
            bool result = DB_ResidenceData.Instance.HomeGateway.gatewayStatus;
            if(!result)
            if (!result)
            {
            }
@@ -55,7 +55,7 @@
            return result;
        }
        public bool DownloadDataComplete = false;
        public bool DownloadDataComplete = true;
        /// <summary>
        /// 下载数据
        /// </summary>
@@ -64,18 +64,21 @@
            if (MainPage.InternetStatus == 0)
                return;
            //线程开始时间
            var beginTime = DateTime.MinValue;
            var endTime = DateTime.Now.AddSeconds(6);
            MainPage.Log($"开始同步云端数据");
            DownloadDataComplete = false;
            var waitPage = new Loading();
            MainPage.BaseView.AddChidren(waitPage);
            waitPage.Start();
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
            string code = StateCode.SUCCESS;
            var downloadDataThread = new System.Threading.Thread(() =>
            {
                MainPage.Log($"进入读取云端数据线程");
                //===================刷新Token=======================
                code = Ins.HttpRequest.RefreshToken();
                if (code != StateCode.SUCCESS)
@@ -128,9 +131,12 @@
                    {
                        MainPage.Log($"读取房间信息成功");
                        var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(roomResult.Data.ToString());
                        if (revData != null)
                        if (revData == null)
                        {
                            SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list, OptionType.Cover);
                            revData = new SpatialApiPack();
                        }
                        {
                            SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list);
                        }
                    }
                    else
@@ -143,7 +149,10 @@
                    {
                        MainPage.Log($"读取设备信息成功");
                        var deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
                        if (deviceList != null)
                        if (deviceList == null)
                        {
                            deviceList = new DevcieApiPack();
                        }
                        {
                            if (FunctionList.List.GetDeviceFunctionList().Count > 0)
                            {
@@ -166,12 +175,13 @@
                                        i++;
                                        if (localFunction.modifyTime != newFunction.modifyTime)
                                        {
                                            //可优化
                                            localFunction.name = newFunction.name;
                                            localFunction.collect = newFunction.collect;
                                            localFunction.modifyTime = newFunction.modifyTime;
                                            localFunction.roomIds = newFunction.roomIds;
                                            localFunction.bus = newFunction.bus;
                                            localFunction.SaveFunctionData(false);
                                            localFunction.SaveFunctionFile();
                                        }
                                        deviceList.list.Remove(newFunction);//操作完的数据清理掉,剩下的就是新增的功能
                                    }
@@ -180,7 +190,7 @@
                            //处理剩下的新增功能
                            foreach (var newFunction in deviceList.list)
                            {
                                newFunction.SaveFunctionData(false);
                                newFunction.SaveFunctionFile();
                                FunctionList.List.IniFunctionList(newFunction.savePath);
                            }
                        }
@@ -194,9 +204,12 @@
                    var pack = Ins.HttpRequest.GetSceneList();
                    if (pack.Code == StateCode.SUCCESS)
                    {
                        MainPage.Log($"读取场景数据成功");//:\r\n{pack.Data.ToString()}");
                        MainPage.Log($"读取场景数据成功");
                        var sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(pack.Data.ToString());
                        if (sceneList != null)
                        if (sceneList == null)
                        {
                            sceneList = new List<Scene>();
                        }
                        {
                            for (int i = 0; i < FunctionList.List.scenes.Count;)
                            {
@@ -249,7 +262,8 @@
                {
                    Application.RunOnMainThread(() =>
                    {
                        beginTime = DateTime.Now;
                        MainPage.Log($"结束读取云端数据线程");
                        endTime = DateTime.MinValue;
                        DownloadDataComplete = true;
                        waitPage.Hide();
                        waitPage.RemoveFromParent();
@@ -257,34 +271,48 @@
                }
            });
            downloadDataThread.IsBackground = true;
            downloadDataThread.Priority = System.Threading.ThreadPriority.Highest;
            downloadDataThread.Start();
            //网络卡顿,终止下载线程
            new System.Threading.Thread(() => {
                while (beginTime.AddSeconds(6) > DateTime.Now)
            new System.Threading.Thread(() =>
            {
                try
                {
                    if(DownloadDataComplete)
                    while (endTime > DateTime.Now)
                    {
                        break;
                        if (DownloadDataComplete)
                        {
                            break;
                        }
                        System.Threading.Thread.Sleep(100);
                    }
                    System.Threading.Thread.Sleep(100);
                }
                if (DownloadDataComplete)
                {
                    //下载完成初始化数据
                    SpatialInfo.CurrentSpatial.Clear();
                }
                else
                {
                    downloadDataThread.Abort();
                    Application.RunOnMainThread(() => {
                    if (!DownloadDataComplete)
                    {
                        //downloadDataThread.Abort();
                    }
                    DownloadDataComplete = true;
                    Application.RunOnMainThread(() =>
                    {
                        waitPage.Hide();
                        waitPage.RemoveFromParent();
                    });
                    //初始化数据
                    //SpatialInfo.CurrentSpatial.Clear();
                }
            }) { IsBackground = true }.Start();
                catch { }
                finally
                {
                    #region 读取本地数据
                    //读取音乐信息
                    UI.Music.A31MusicModel.ReadMusicStates();
                    //搜索网关
                    DriverLayer.Control.Ins.SearchLoaclGateway();
                    #endregion
                }
            })
            { IsBackground = true }.Start();
        }
    }
}
}