From d41f1d4a478c1afcc43f3a5abb0ba76e376bc7c1 Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期二, 09 三月 2021 15:23:25 +0800
Subject: [PATCH] 2

---
 HDL_ON/Common/ApiUtlis.cs |  347 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 340 insertions(+), 7 deletions(-)

diff --git a/HDL_ON/Common/ApiUtlis.cs b/HDL_ON/Common/ApiUtlis.cs
index d234a0d..3141f5d 100644
--- a/HDL_ON/Common/ApiUtlis.cs
+++ b/HDL_ON/Common/ApiUtlis.cs
@@ -1,7 +1,10 @@
 锘縰sing System;
+using System.Collections.Generic;
 using HDL_ON;
 using HDL_ON.DAL;
 using HDL_ON.DAL.Server;
+using HDL_ON.Entity;
+using Shared;
 
 namespace HDL_ON.Common
 {
@@ -11,13 +14,26 @@
         {
         }
 
-        static HttpServerRequest httpRequest;
-
-        public static HttpServerRequest HttpRequest
+        static ApiUtlis apiUtlis;
+        public static ApiUtlis Ins
         {
             get
             {
-                if(httpRequest == null)
+                if (apiUtlis == null)
+                {
+                    apiUtlis = new ApiUtlis();
+                }
+                return apiUtlis;
+            }
+        }
+
+        HttpServerRequest httpRequest;
+
+        public HttpServerRequest HttpRequest
+        {
+            get
+            {
+                if (httpRequest == null)
                 {
                     httpRequest = new HttpServerRequest();
                 }
@@ -29,9 +45,326 @@
         /// </summary>
         public bool GatewayOnlineVerification()
         {
-            bool result =
+            bool result = DB_ResidenceData.Instance.HomeGateway.gatewayStatus;
+            DriverLayer.Control.Ins.GatewayOnline_Cloud = result;
+            if (!result)
+            {
 
-            return 
+            }
+
+            return result;
+        }
+
+        public bool DownloadDataComplete = true;
+        /// <summary>
+        /// 涓嬭浇鏁版嵁
+        /// </summary>
+        public void DownloadData()
+        {
+            if (MainPage.InternetStatus == 0)
+                return;
+            //绾跨▼寮�濮嬫椂闂�
+            var endTime = DateTime.Now.AddSeconds(5);
+
+            MainPage.Log($"寮�濮嬪悓姝ヤ簯绔暟鎹�");
+
+            DownloadDataComplete = false;
+
+            var waitPage = new Loading();
+            MainPage.BaseView.AddChidren(waitPage);
+            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)
+                {
+                    MainPage.Log($"鍒锋柊token澶辫触");
+                    return;
+                }
+                endTime = DateTime.Now.AddSeconds(5);
+                MainPage.Log($"鍒锋柊token鎴愬姛");
+                //===================鍒锋柊浣忓畢淇℃伅=======================
+                code = Ins.HttpRequest.GetHomePager();
+                if (code != StateCode.SUCCESS)
+                {
+                    MainPage.Log($"鍒锋柊浣忓畢淇℃伅澶辫触");
+                    return;
+                }
+                endTime = DateTime.Now.AddSeconds(5);
+                MainPage.Log($"鍒锋柊浣忓畢淇℃伅鎴愬姛");
+                //===================鍒锋柊涓汉淇℃伅=======================
+                code = Ins.HttpRequest.GetUserInfo();
+                if (code != StateCode.SUCCESS)
+                {
+                    MainPage.Log($"鍒锋柊涓汉淇℃伅澶辫触");
+                    return;
+                }
+                endTime = DateTime.Now.AddSeconds(5);
+                MainPage.Log($"鍒锋柊涓汉淇℃伅鎴愬姛");
+
+                //int count = 0;
+                //while (count < 10)
+                //{
+                //    if (DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
+                //    {
+                //        System.Threading.Thread.Sleep(200);
+                //    }
+                //    else
+                //    {
+                //        break;
+                //    }
+                //    count++;
+                //}
+                //if (DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
+                //{
+                //    return;
+                //}
+
+
+                try
+                {
+                    //===================鎴块棿=======================
+                    var roomResult = Ins.HttpRequest.GetRoomList();
+                    if (roomResult.Code == StateCode.SUCCESS)
+                    {
+                        endTime = DateTime.Now.AddSeconds(100);
+                        MainPage.Log($"璇诲彇鎴块棿淇℃伅鎴愬姛");
+                        var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(roomResult.Data.ToString());
+                        if (revData == null)
+                        {
+                            revData = new SpatialApiPack();
+                        }
+                        {
+                            SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list);
+                        }
+                        endTime = DateTime.Now.AddSeconds(5);
+                    }
+                    else
+                    {
+                        MainPage.Log($"璇诲彇鎴块棿鏁版嵁澶辫触:Code:{roomResult.Code}; msg:{roomResult.message}");
+                    }
+                    //===================璁惧=======================
+                    var deviceResult = Ins.HttpRequest.GetDeviceList();
+                    if (deviceResult.Code == StateCode.SUCCESS)
+                    {
+                        endTime = DateTime.Now.AddSeconds(100);
+                        MainPage.Log($"璇诲彇璁惧淇℃伅鎴愬姛");
+                        var deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
+                        if (deviceList == null)
+                        {
+                            deviceList = new DevcieApiPack();
+                        }
+                        string delFile = "";
+                        if (FunctionList.List.GetDeviceFunctionList().Count > 0)
+                        {
+
+                            for (int i = 0; i < FunctionList.List.GetDeviceFunctionList().Count;)
+                            {
+                                var localFunction = FunctionList.List.GetDeviceFunctionList()[i];
+                                if (localFunction.Spk_Prefix == FunctionCategory.Music || string.IsNullOrEmpty(localFunction.Spk_Prefix))
+                                {
+                                    i++;
+                                    continue;
+                                }
+                                var newFunction = deviceList.list.Find((obj) => obj.deviceId == localFunction.deviceId);
+                                //if (newFunction == null)//濡傛灉浜戠鏈�鏂版暟鎹病鏈夎鏉℃暟鎹紝鍒欐湰鍦伴渶瑕佸垹鎺夎鏁版嵁璁板綍
+                                //{
+                                //    FunctionList.List.DeleteFunction(localFunction);
+                                //}
+                                //else
+                                //{
+                                //    MainPage.Log($"deviceType:{localFunction.spk} local:{localFunction.modifyTime} server:{newFunction.modifyTime}");
+                                //    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.SaveFunctionFile();
+                                //    //}
+                                //    localFunction = newFunction;
+                                //    localFunction.SaveFunctionFile();
+                                //    deviceList.list.Remove(newFunction);//鎿嶄綔瀹岀殑鏁版嵁娓呯悊鎺夛紝鍓╀笅鐨勫氨鏄柊澧炵殑鍔熻兘
+                                //}
+
+                                if (delFile == localFunction.savePath)
+                                {
+                                    i++;
+                                    continue;
+                                }
+                                delFile = localFunction.savePath;
+                                FunctionList.List.DeleteFunction(localFunction);
+
+                            }
+
+
+                        }
+                        //澶勭悊鍓╀笅鐨勬柊澧炲姛鑳�
+                        foreach (var newFunction in deviceList.list) 
+                        {
+                            newFunction.SaveFunctionFile();
+                            FunctionList.List.IniFunctionList(newFunction.savePath);
+                        }
+                        endTime = DateTime.Now.AddSeconds(5);
+                    }
+                    else
+                    {
+                        MainPage.Log($"璇诲彇浜戠璁惧鏁版嵁澶辫触:Code:{deviceResult.Code};  Msg:{deviceResult.message}");
+                    }
+
+                    //===================鍦烘櫙==========================
+                    var pack = Ins.HttpRequest.GetSceneList();
+                    if (pack.Code == StateCode.SUCCESS)
+                    {
+                        endTime = DateTime.Now.AddSeconds(100);
+                        MainPage.Log($"璇诲彇鍦烘櫙鏁版嵁鎴愬姛");
+                        var sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(pack.Data.ToString());
+                        if (sceneList == null)
+                        {
+                            sceneList = new List<Scene>();
+                        }
+                        for (int i = 0; i < FunctionList.List.scenes.Count;)
+                        {
+                            var localScene = FunctionList.List.scenes[i];
+                            if (localScene == null)
+                            {
+                                FunctionList.List.scenes.Remove(localScene);
+                                continue;
+                            }
+                            var newScene = sceneList.Find((obj) => obj.userSceneId == localScene.userSceneId);
+                            if (newScene == null)//濡傛灉浜戠鏈�鏂版暟鎹病鏈夎鏉℃暟鎹紝鍒欐湰鍦伴渶瑕佸垹鎺夎鏁版嵁璁板綍
+                            {
+                                FunctionList.List.DeleteScene(localScene, false);
+                            }
+                            else
+                            {
+                                i++;
+                                if (localScene.modifyTime != newScene.modifyTime)
+                                {
+                                    localScene.name = newScene.name;
+                                    localScene.collect = newScene.collect;
+                                    localScene.modifyTime = newScene.modifyTime;
+                                    localScene.roomIds = newScene.roomIds;
+                                    localScene.image = newScene.image;
+                                    localScene.SaveSceneFile();
+                                }
+                                sceneList.Remove(newScene);//鎿嶄綔瀹岀殑鏁版嵁娓呯悊鎺夛紝鍓╀笅鐨勫氨鏄柊澧炵殑鍔熻兘
+                            }
+                        }
+                        //澶勭悊鍓╀笅鐨勬柊澧炲姛鑳�
+                        foreach (var tempScene in sceneList)
+                        {
+                            tempScene.SaveSceneFile();
+                            FunctionList.List.scenes.Add(tempScene);
+                        }
+                        //濡傛灉鏄竴绔彛闇�瑕佹彁鍓嶄笅杞藉満鏅暟鎹紝鍚﹀垯鎺у埗涓嶄簡涓�绔彛鍦烘櫙
+                        if (DB_ResidenceData.Instance.GatewayType == 0)
+                        {
+                            var sceneIds = new List<string>();
+                            foreach (var tempScene in FunctionList.List.scenes)
+                            {
+                                sceneIds.Add(tempScene.userSceneId);
+                            }
+                            var packInfo = HttpRequest.GetSceneListInfo(sceneIds);
+                            if (packInfo.Code == StateCode.SUCCESS)
+                            {
+                                var serverTempList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(packInfo.Data.ToString());
+                                foreach (var localScene in FunctionList.List.scenes)
+                                {
+                                    var serverTemp = serverTempList.Find((obj) => obj.userSceneId == localScene.userSceneId);
+                                    if (serverTemp != null)
+                                    {
+                                        localScene.functions = serverTemp.functions;
+                                        localScene.SaveSceneFile();
+                                    }
+                                }
+                            }
+                            else
+                            {
+                                IMessageCommon.Current.ShowErrorInfoAlter(packInfo.Code);
+                            }
+                        }
+                        endTime = DateTime.Now.AddSeconds(5);
+                    }
+                    else
+                    {
+                        MainPage.Log($"璇诲彇浜戠鍦烘櫙鏁版嵁澶辫触:Code:{pack.Code}; Msg:{pack.message}");
+                    }
+
+                    //===================璇诲彇閫昏緫鍒楄〃==========================
+                    UI.UI2.Intelligence.Automation.MainView.GetLogicList();
+                }
+                catch (Exception ex)
+                {
+                    MainPage.Log($"鏁版嵁鍒濆鍖栧け璐ワ細{ex.Message}");
+                }
+                finally
+                {
+                    Application.RunOnMainThread(() =>
+                    {
+                        MainPage.Log($"缁撴潫璇诲彇浜戠鏁版嵁绾跨▼");
+                        endTime = DateTime.MinValue;
+                        DownloadDataComplete = true;
+                        waitPage.Hide();
+                        waitPage.RemoveFromParent();
+                    });
+                    //涓嶈�冭檻缃戝叧鎯呭喌锛岀洿鎺ヨ闃�
+                    DAL.Mqtt.MqttClient.InitState();
+
+                    FunctionList.List.Read3tyFunctionStatus();
+                }
+            });
+            downloadDataThread.IsBackground = true;
+            downloadDataThread.Priority = System.Threading.ThreadPriority.Highest;
+            downloadDataThread.Start();
+
+            //缃戠粶鍗¢】锛岀粓姝笅杞界嚎绋�
+            new System.Threading.Thread(() =>
+            {
+                try
+                {
+                    while (endTime > DateTime.Now)
+                    {
+                        if (DownloadDataComplete)
+                        {
+                            break;
+                        }
+                        System.Threading.Thread.Sleep(100);
+                    }
+                    if (!DownloadDataComplete)
+                    {
+                        //downloadDataThread.Abort();
+                    }
+                    DownloadDataComplete = true;
+                    Application.RunOnMainThread(() =>
+                    {
+                        waitPage.Hide();
+                        waitPage.RemoveFromParent();
+                    });
+                    //鍒濆鍖栨暟鎹�
+                    //SpatialInfo.CurrentSpatial.Clear();
+
+                }
+                catch { }
+                finally
+                {
+                    #region 璇诲彇鏈湴鏁版嵁
+                    //璇诲彇闊充箰淇℃伅
+                    UI.Music.A31MusicModel.ReadMusicStates();
+                    //鎼滅储缃戝叧
+                    DriverLayer.Control.Ins.SearchLoaclGateway();
+                    #endregion
+                }
+            })
+            { IsBackground = true }.Start();
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.8.0