黄学彪
2020-07-13 f3e65daca7978b21b5888f49b1bf35e1a6e5d4fd
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlSceneLogic.cs
@@ -79,10 +79,10 @@
        /// <summary>
        /// 刷新本地的全部场景
        /// </summary>
        public async Task<bool> RefreshSceneUIList()
        public bool RefreshSceneUIList()
        {
            //获取网关存在的场景
            var sceneList = await ZigBee.Device.Scene.GetSceneListAsync();
            var sceneList = this.RefreshSceneListFromGateway();
            if (sceneList == null)
            {
                return false;
@@ -95,6 +95,8 @@
                    continue;
                }
                listEsxit.Add(scene.ScenesId);
                //刷新scene的信息
                this.RefreshScene(scene);
            }
            var listDelete = new List<SceneUI>();
@@ -113,6 +115,108 @@
            }
            return true;
        }
        /// <summary>
        /// 刷新scene的信息
        /// </summary>
        /// <param name="sceneInfo"></param>
        private void RefreshScene(Scene.GetSceneAllInfo sceneInfo)
        {
            if (this.dicScenes.ContainsKey(sceneInfo.ScenesId) == true)
            {
                var localScene = this.dicScenes[sceneInfo.ScenesId];
                //只刷新名字和绑定表
                localScene.Name = sceneInfo.ScenesName;
                localScene.AdjustTargetList.Clear();
                localScene.AdjustTargetList.AddRange(sceneInfo.DeviceList);
                localScene.Save();
            }
            else
            {
                var newScene = new SceneUI();
                newScene.Name = sceneInfo.ScenesName;
                newScene.IconPath = "SceneIcon/0.png";
                newScene.Id = sceneInfo.ScenesId;
                newScene.AdjustTargetList.AddRange(sceneInfo.DeviceList);
                //添加缓存
                newScene.Save();
                //加入缓存
                this.dicScenes[newScene.Id] = newScene;
                //备份
                HdlAutoBackupLogic.AddOrEditorFile(newScene.FileName);
            }
        }
        /// <summary>
        /// 从网关重新刷新场景列表
        /// </summary>
        /// <returns></returns>
        private List<Scene.GetSceneAllInfo> RefreshSceneListFromGateway()
        {
            var mainGateway = ZbGateway.MainGateWay;
            if (mainGateway == null)
            {
                //获取网关对象失败
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uGetGatewayTagartFail));
                return null;
            }
            //超时时间
            int TimeOut = 0;
            bool receiptAll = false;
            bool canReceve = false;
            var listScene = new List<Scene.GetSceneAllInfo>();
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                if (topic == gatewayID + "/" + "Scene/GetAllInfo_Respon")
                {
                    var sceneGetAllInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<Scene.GetSceneAllInfo>(jobject["Data"].ToString());
                    listScene.Add(sceneGetAllInfo);
                    //更够接收得到场景
                    canReceve = true;
                    if (sceneGetAllInfo.ScenesNum == sceneGetAllInfo.ScenesSum)
                    {
                        //接收完成
                        receiptAll = true;
                        TimeOut = 0;
                    }
                }
            };
            mainGateway.Actions += action;
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 807 } };
            mainGateway.Send(("Scene/GetAllInfo"), System.Text.Encoding.UTF8.GetBytes(jObject.ToString()));
            int waitTime = 20 * 6;
            while (receiptAll == false && TimeOut < waitTime)
            {
                //全部接收才退出
                System.Threading.Thread.Sleep(50);
                TimeOut++;
            }
            mainGateway.Actions -= action;
            action = null;
            if (canReceve == false)
            {
                //获取场景列表失败,网关回复超时
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uGetSceneListFailAndTimeOut));
                return null;
            }
            else if (receiptAll == false)
            {
                //网络不稳定,场景列表信息缺损
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uNetworkUnStableAndSceneInfoIsNotFull));
                return null;
            }
            return listScene;
        }
        #endregion
@@ -299,6 +403,8 @@
            {
                nowRoom.ListSceneId.Add(scene.Id);
                nowRoom.Save();
                //添加收藏场景时,需要刷新主页
                UserView.UserPage.Instance.RefreshAllForm = true;
            }
        }
@@ -512,6 +618,24 @@
                    HdlAutoBackupLogic.DeleteFile(sceneUI.IconPath);
                }
            }
            //删除场景时,需要刷新主页
            UserView.UserPage.Instance.RefreshAllForm = true;
        }
        /// <summary>
        /// 清空本地全部的场景数据
        /// </summary>
        public void DeleteAllLocalScene()
        {
            var listScene = new List<SceneUI>();
            foreach (var scene in this.dicScenes.Values)
            {
                listScene.Add(scene);
            }
            foreach (var scene in listScene)
            {
                this.DeleteLocalScene(scene);
            }
        }
        /// <summary>
@@ -540,12 +664,28 @@
            {
                nowRoom.ListSceneId.Remove(scene.Id);
                nowRoom.Save();
                //取消收藏场景时,需要刷新主页
                UserView.UserPage.Instance.RefreshAllForm = true;
            }
        }
        #endregion
        #region ■ 获取场景___________________________
        /// <summary>
        /// 获取本地全部的场景(包含未分配)
        /// </summary>
        /// <returns></returns>
        public List<SceneUI> GetAllLocalScene()
        {
            //房间的场景放在前面
            var listScene = this.GetAllRoomSceneList();
            //未分配的场景
            var listUnalloctScene = this.GetUnalloctedScenes();
            listScene.AddRange(listUnalloctScene);
            return listScene;
        }
        /// <summary>
        /// 通过场景id获取场景
@@ -589,13 +729,14 @@
        /// <returns></returns>
        public string GetZoneById(int sceneId)
        {
            var room =HdlRoomLogic.Current.GetRoomBySceneId(sceneId);
            var room = HdlRoomLogic.Current.GetRoomBySceneId(sceneId);
            if (room == null)
            {
                return null;
            }
            var floorName = Shared.Common.Config.Instance.Home.GetFloorNameById(room.FloorId);
            if (floorName == null)
            var floorName = HdlResidenceLogic.Current.GetFloorNameById(room.FloorId);
            //更改代码:如果floorName=“”也要判断,否则有“,”
            if (string.IsNullOrEmpty(floorName))
            {
                return room.Name;
            }
@@ -634,11 +775,6 @@
                    sceneUIs.Add(myScene);
                }
            }
            if (sceneUIs.Count == 0)
            {
                return null;
            }
            return sceneUIs;
        }
@@ -652,6 +788,11 @@
            var listAllRoom = HdlRoomLogic.Current.GetAllListRooms();
            foreach (var room in listAllRoom)
            {
                if (room.IsLove == true)
                {
                    //不包含收藏房间
                    continue;
                }
                foreach (int sceneId in room.ListSceneId)
                {
                    if (this.dicScenes.ContainsKey(sceneId) == true)
@@ -825,7 +966,7 @@
        public List<string> GetAllSceneFile()
        {
            List<string> listSceneFile = new List<string>();
            List<string> listAllFile = Global.FileListByHomeId();
            List<string> listAllFile = HdlFileLogic.Current.GetRootPathListFile();
            foreach (string file in listAllFile)
            {