From e0b8a7e79691d6187e4ea01a5cdd1f8802f8b00c Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期三, 23 十二月 2020 09:34:43 +0800
Subject: [PATCH] 20201223-1

---
 HDL_ON/Common/ApiUtlis.cs |  227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 223 insertions(+), 4 deletions(-)

diff --git a/HDL_ON/Common/ApiUtlis.cs b/HDL_ON/Common/ApiUtlis.cs
index d234a0d..c564f63 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,9 +14,22 @@
         {
         }
 
-        static HttpServerRequest httpRequest;
+        static ApiUtlis apiUtlis;
+        public static ApiUtlis Ins
+        {
+            get
+            {
+                if(apiUtlis == null)
+                {
+                    apiUtlis = new ApiUtlis();
+                }
+                return apiUtlis;
+            }
+        }
 
-        public static HttpServerRequest HttpRequest
+        HttpServerRequest httpRequest;
+
+        public HttpServerRequest HttpRequest
         {
             get
             {
@@ -29,9 +45,212 @@
         /// </summary>
         public bool GatewayOnlineVerification()
         {
-            bool result =
+            bool result = DB_ResidenceData.Instance.HomeGateway.gatewayStatus;
 
-            return 
+            if(!result)
+            {
+
+            }
+
+            return result;
+        }
+
+        public bool DownloadDataComplete = false;
+        /// <summary>
+        /// 涓嬭浇鏁版嵁
+        /// </summary>
+        public void DownloadData()
+        {
+            if (MainPage.InternetStatus == 0)
+                return;
+
+            DownloadDataComplete = false;
+
+            var waitPage = new Loading();
+            MainPage.BaseView.AddChidren(waitPage);
+            waitPage.Start();
+
+            string code = StateCode.SUCCESS;
+
+            new System.Threading.Thread(() => {
+                //===================鍒锋柊Token=======================
+                code = Ins.HttpRequest.RefreshToken();
+                if(code != StateCode.SUCCESS)
+                {
+                    MainPage.Log($"鍒锋柊token澶辫触");
+                    return;
+                }
+                MainPage.Log($"鍒锋柊token鎴愬姛");
+                //===================鍒锋柊浣忓畢淇℃伅=======================
+                code = Ins.HttpRequest.GetHomePager();
+                if (code != StateCode.SUCCESS)
+                {
+                    MainPage.Log($"鍒锋柊浣忓畢淇℃伅澶辫触");
+                    return;
+                }
+                MainPage.Log($"鍒锋柊浣忓畢淇℃伅鎴愬姛");
+                //===================鍒锋柊涓汉淇℃伅=======================
+                code = Ins.HttpRequest.GetUserInfo();
+                if (code != StateCode.SUCCESS)
+                {
+                    MainPage.Log($"鍒锋柊涓汉淇℃伅澶辫触");
+                    return;
+                }
+                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)
+                    {
+                        MainPage.Log($"璇诲彇鎴块棿淇℃伅鎴愬姛");
+                        var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(roomResult.Data.ToString());
+                        if (revData != null)
+                        {
+                            SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list, OptionType.Cover);
+                        }
+                    }
+                    else
+                    {
+                        MainPage.Log($"璇诲彇鎴块棿鏁版嵁澶辫触:Code:{roomResult.Code}; msg:{roomResult.message}");
+                    }
+                    //===================璁惧=======================
+                    var deviceResult = Ins.HttpRequest.GetDeviceList();
+                    if (deviceResult.Code == StateCode.SUCCESS)
+                    {
+                        MainPage.Log($"璇诲彇璁惧淇℃伅鎴愬姛");
+                        var deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
+                        if (deviceList != null)
+                        {
+                            if (FunctionList.List.GetDeviceFunctionList().Count > 0)
+                            {
+                                for (int i = 0; i < FunctionList.List.GetDeviceFunctionList().Count;)
+                                {
+                                    var localFunction = FunctionList.List.GetDeviceFunctionList()[i];
+                                    if (localFunction.functionCategory == FunctionCategory.Music)
+                                    {
+                                        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.SaveFunctionData(false);
+                                        }
+                                        deviceList.list.Remove(newFunction);//鎿嶄綔瀹岀殑鏁版嵁娓呯悊鎺夛紝鍓╀笅鐨勫氨鏄柊澧炵殑鍔熻兘
+                                    }
+                                }
+                            }
+                            //澶勭悊鍓╀笅鐨勬柊澧炲姛鑳�
+                            foreach (var newFunction in deviceList.list)
+                            {
+                                newFunction.SaveFunctionData(false);
+                                FunctionList.List.IniFunctionList(newFunction.savePath);
+                            }
+                        }
+                    }
+                    else
+                    {
+                        MainPage.Log($"璇诲彇浜戠璁惧鏁版嵁澶辫触:Code:{deviceResult.Code};  Msg:{deviceResult.message}");
+                    }
+
+                    //===================鍦烘櫙==========================
+                    var pack = Ins.HttpRequest.GetSceneList();
+                    if (pack.Code == StateCode.SUCCESS)
+                    {
+                        MainPage.Log($"璇诲彇鍦烘櫙鏁版嵁鎴愬姛");//:\r\n{pack.Data.ToString()}");
+                        var sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(pack.Data.ToString());
+                        if (sceneList != null)
+                        {
+                            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.SaveSceneData(false);
+                                    }
+                                    sceneList.Remove(newScene);//鎿嶄綔瀹岀殑鏁版嵁娓呯悊鎺夛紝鍓╀笅鐨勫氨鏄柊澧炵殑鍔熻兘
+                                }
+                            }
+                            //澶勭悊鍓╀笅鐨勬柊澧炲姛鑳�
+                            foreach (var newScene in sceneList)
+                            {
+                                newScene.SaveSceneData(false);
+                                FunctionList.List.scenes.Add(newScene);
+                            }
+                        }
+                    }
+                    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(() => {
+                        DownloadDataComplete = true;
+                        waitPage.Hide();
+                        waitPage.RemoveFromParent();
+                    });
+                }
+            })
+            { IsBackground = true }.Start();
         }
     }
 }

--
Gitblit v1.8.0