From 5a5a2b696866f947b6025d26c3302e8ffef46435 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 11 一月 2021 16:11:20 +0800
Subject: [PATCH] 2021-01-11 1.更新MQTT连接和本地搜索网关方法。2.子账号相关接口完善

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Alexa/AlexaDeviceListPage.cs |  327 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 299 insertions(+), 28 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Alexa/AlexaDeviceListPage.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Alexa/AlexaDeviceListPage.cs
index 400f3c9..3d5a60a 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Alexa/AlexaDeviceListPage.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Alexa/AlexaDeviceListPage.cs
@@ -119,17 +119,66 @@
                                 //    DeviceName = common.Name, DeviceID = common.DeviceID, Id = 1, DeviceType = common.Type.ToString (), LoopID = common.LoopID,
                                 //    MAC = UserConfig.Instance.GatewayMAC, RegionID = UserConfig.Instance.CurrentRegion.RegionID, SubnetID = common.SubnetID
                                 //});
+                              
+
                                 if (common != null) {
-                                    var oldDevice = alexaDeviceLista.Find ((obj) => obj.SubnetID == common.SubnetID && obj.DeviceID == common.DeviceID && obj.LoopID == common.LoopID && obj.DeviceType == common.Type.ToString ());
+                                    var commonType = common.Type.ToString ();
+                                    if (commonType == "CurtainModel") {
+                                        commonType = "CurtainModule";
+                                    } else if (commonType == "LightEnergySwitch" || commonType == "LightSwitchSocket" || commonType == "LightEnergySocket") {
+                                        commonType = "LightSwitch";
+                                    } else if (commonType == "ACInfrared") {
+                                        commonType = "HVAC";
+                                    }
+                                    var oldDevice = alexaDeviceLista.Find ((obj) => obj.SubnetID == common.SubnetID && obj.DeviceID == common.DeviceID && obj.LoopID == common.LoopID && obj.DeviceType == commonType);
                                     if (oldDevice == null) {
-                                        AddDevice (common);
+                                        var isSuccess = AddDevice (common);
+                                        if (!isSuccess) {
+                                            //璺冲嚭寰幆
+                                            break;
+                                        }
+                                    } else {
+                                        //2020-07-21 濡傛灉宸茬粡瀛樺湪 鍖归厤璁惧澶囨敞鏄惁闇�瑕佹洿鏂�
+                                        if (oldDevice.DeviceName != common.Name) {
+                                            //Utlis.WriteLine ("鍚嶅瓧涓嶅尮閰嶏紝鎻愪氦鏇存柊锛�" + common.Name);
+                                            var oldName = oldDevice.DeviceName;
+                                            oldDevice.DeviceName = common.Name;
+                                            var isSuccess = UpdateDevice (oldDevice);
+                                            if (!isSuccess) {
+                                                //杩樺師
+                                                oldDevice.DeviceName = oldName;
+                                                //Utlis.WriteLine ("鏇存柊澶辫触锛侊紒锛侊紒");
+                                            } else {
+                                                //Utlis.WriteLine ("鏇存柊鎴愬姛锛�");
+                                            }
+                                        } else {
+                                            //Utlis.WriteLine ("鍚嶅瓧涓�鏍蜂笉鐢ㄦ洿鏂帮細"+ common.Name);
+                                        }
+
                                     }
                                 }
                             }
-                            foreach (var oldDevice in alexaDeviceLista) {
-                                var oldDevicePath = "Equipment_" + oldDevice.DeviceType + "_" + oldDevice.SubnetID + "_" + oldDevice.DeviceID + "_" + (oldDevice.LoopID.ToString ().Length < 2 ? "0" + oldDevice.LoopID.ToString () : oldDevice.LoopID.ToString ());
 
-                                if (!roomDeviceFilePathList.Contains (oldDevicePath)) {
+                            var roomDeviceFilePathListNew = new List<string> ();
+                            foreach (var path in roomDeviceFilePathList) {
+                                var deviceFilePath = path;
+                                if (deviceFilePath.Contains ("LightEnergySocket")) {
+                                    deviceFilePath = deviceFilePath.Replace ("LightEnergySocket", "LightSwitch");
+                                } else if (deviceFilePath.Contains ("LightEnergySwitch")) {
+                                    deviceFilePath = deviceFilePath.Replace ("LightEnergySwitch", "LightSwitch");
+                                } else if (deviceFilePath.Contains ("LightSwitchSocket")) {
+                                    deviceFilePath = deviceFilePath.Replace ("LightSwitchSocket", "LightSwitch");
+                                } else if (deviceFilePath.Contains ("ACInfrared")) {
+                                    deviceFilePath = deviceFilePath.Replace ("ACInfrared", "HVAC");
+                                } else if (deviceFilePath.Contains ("CurtainModel")) {
+                                    deviceFilePath = deviceFilePath.Replace ("CurtainModel", "CurtainModule");
+                                }
+                                roomDeviceFilePathListNew.Add (deviceFilePath);
+                            }
+
+                           foreach (var oldDevice in alexaDeviceLista) {
+                                var oldDevicePath = "Equipment_" + oldDevice.DeviceType + "_" + oldDevice.SubnetID + "_" + oldDevice.DeviceID + "_" + (oldDevice.LoopID.ToString ().Length < 2 ? "0" + oldDevice.LoopID.ToString () : oldDevice.LoopID.ToString ());
+                                if (!roomDeviceFilePathListNew.Contains (oldDevicePath)) {
                                     DelDevice (oldDevice.Id);
                                     Utlis.WriteLine ("delDevice " + oldDevice.DeviceName);
                                 }
@@ -216,7 +265,7 @@
                                                         uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                             Command = (int)Command.UpdataCurtainModelStutas, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                             DeviceName = device.Name, DeviceType = "CurtainModule", LoopId = device.LoopID,
-                                                            DataString = device.Status.ToString ()
+                                                            DataString = ((byte)device.Status).ToString ()
                                                         });
                                                     }
                                                 } else if (common.Type == DeviceType.CurtainRoller) {
@@ -225,7 +274,7 @@
                                                         uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                             Command = (int)Command.UpdataCurtainModelStutas, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                             DeviceName = device.Name, DeviceType = "CurtainRoller", LoopId = device.LoopID,
-                                                            DataString = (device.Status + 3).ToString ()
+                                                            DataString = ((byte)device.CurtainProress + 3).ToString ()
                                                         });
                                                     }
                                                 } else if (common.Type == DeviceType.CurtainTrietex) {
@@ -234,7 +283,7 @@
                                                         uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                             Command = (int)Command.UpdataCurtainModelStutas, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                             DeviceName = device.Name, DeviceType = "CurtainRoller", LoopId = device.LoopID,
-                                                            DataString = (device.Status + 3).ToString ()
+                                                            DataString = ((byte)device.CurtainProress + 3).ToString ()
                                                         });
                                                     }
                                                 } else if (common.Type == DeviceType.HVAC || common.Type == DeviceType.ACInfrared) {
@@ -268,6 +317,15 @@
                                                             Command = (int)Command.SetScene, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                             DeviceName = device.Name, DeviceType = "SceneControl", LoopId = device.AreaID,
                                                             DataString = device.AreaSceneID.ToString ()
+                                                        });
+                                                    }
+                                                } else if (common.Type == DeviceType.UniversalDevice) {//2020-09-02 鏂板閫氱敤寮�鍏�
+                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<UniversalDevice> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
+                                                    if (device != null) {
+                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
+                                                            Command = (int)Command.SetCommonSwitch, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
+                                                            DeviceName = device.Name, DeviceType = "UniversalDevice", LoopId = device.LoopID,
+                                                            DataString = device.SendBytes[1].ToString(),
                                                         });
                                                     }
                                                 }
@@ -356,11 +414,16 @@
                                             }
                                         }
 
-                                        AddScene (uploadScene);
+                                        var isSuccess = AddScene (uploadScene);
+                                        if (!isSuccess) {
+                                            //璺冲嚭寰幆
+                                            break;
+                                        }
                                     }
                                 }
 
                             }
+
 
                             alexaDeviceLista = ReadDeviceList ();
                             Application.RunOnMainThread (() => {
@@ -389,8 +452,8 @@
                         var tempRoom = new Room ();
                         foreach (var device in alexaDeviceLista) {//鐗规畩澶勭悊鏈嶅姟鍣ㄤ笌鏈湴璁惧绫诲瀷涓嶄竴鏍风殑璁惧
                             var deviceTypeSaveString = device.DeviceType;
-                            if (deviceTypeSaveString == "CurtainModule") {
-                                deviceTypeSaveString = "CurtainModel";
+                            if (deviceTypeSaveString == "CurtainModel") {
+                                deviceTypeSaveString = "CurtainModule";
                             } else if (deviceTypeSaveString == "LightEnergySwitch") {
                                 deviceTypeSaveString = "LightSwitch";
                             } else if (deviceTypeSaveString == "LightEnergySocket") {
@@ -435,8 +498,12 @@
             var requestObj = new GetDeviceListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
             var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
             var revertObj = MainPage.RequestHttps (API.GetDeviceList, requestJson);
-            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
-                deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UpdateDeviceObj>> (revertObj.ResponseData.ToString());
+            if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
+                deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UpdateDeviceObj>> (revertObj.ResponseData.ToString ());
+            } else {
+                //2020-06-29 寮圭獥鎻愮ず閿欒
+                ShowErrorInfo (revertObj.StateCode);
+
             }
             return deviceList;
         }
@@ -460,8 +527,105 @@
             //var requestObj = new UploadDevicesObj () { Device = uploadDevice };
             var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (uploadDevice);
             var revertObj = MainPage.RequestHttps (API.UploadDevices, requestJson);
-            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
+            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                 return true;
+            }else if (revertObj.StateCode == "Exist") {
+                Application.RunOnMainThread (() => {
+                    MainPage.AddTip (ErrorCode.AddFailed + " " + ErrorCode.NameExist + " ( " + device.Name + " )");
+                });
+                return true;
+            } else {
+                //2020-06-29 寮瑰嚭鎻愮ず閿欒
+                ShowUploadDevicesInfo (revertObj.StateCode);
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// 涓婁紶璁惧 閿欒鎻愮ず
+        /// </summary>
+        /// <param name="stateCodeStr"></param>
+        void ShowUploadDevicesInfo (string stateCodeStr,bool bUpload = true)
+        {
+            string mes = "";
+            if (stateCodeStr == "NoRecord") {
+                //浣忓畢鏈粦瀹氱綉鍏筹紒
+                mes = ErrorCode.HomeNoRecord;
+            } else if (stateCodeStr == "NoExist") {
+                //浣忓畢涓嶅瓨鍦紒
+                mes = ErrorCode.HomeNoExist;
+            } else if (stateCodeStr == "Exist") {
+                //璁惧鍚嶇О宸插瓨鍦�
+                mes = ErrorCode.NameExist;
+            } else if (stateCodeStr == ErrorCode.NetworkError) {
+                mes = ErrorCode.NetworkError;
+            } else {
+                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
+
+            }
+            if (!string.IsNullOrEmpty (mes)) {
+                Application.RunOnMainThread (() => {
+                    if (bUpload) {
+                        MainPage.AddTip (ErrorCode.AddFailed + " " + mes);
+                    } else {
+                        MainPage.AddTip ("Update failed! " + mes);
+                    }
+
+                    //new Alert ("", ErrorCode.AddFailed + " " + mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                });
+            }
+
+        }
+
+        /// <summary>
+        /// 閿欒鎻愰啋
+        /// </summary>
+        /// <param name="stateCodeStr"></param>
+        void ShowErrorInfo (string stateCodeStr)
+        {
+            string mes = "";
+            if (stateCodeStr == ErrorCode.NetworkError) {
+                mes = ErrorCode.NetworkError;
+            } else {
+                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
+
+            }
+            if (!string.IsNullOrEmpty (mes)) {
+                Application.RunOnMainThread (() => {
+                    MainPage.AddTip (ErrorCode.OperationFailed + " " + mes);
+                });
+            }
+
+        }
+
+        /// <summary>
+        /// 鍒锋柊璁惧鍚嶅瓧
+        /// </summary>
+        public bool UpdateDevice (UpdateDeviceObj mUpdateDeviceObj)
+        {
+            var mUpdateAlexaDeviceObj = new UpdateAlexaDeviceObj () {
+                Id = mUpdateDeviceObj.Id,
+                DeviceType = mUpdateDeviceObj.DeviceType,
+                SubnetID = mUpdateDeviceObj.SubnetID,
+                DeviceID = mUpdateDeviceObj.DeviceID,
+                LoopID = mUpdateDeviceObj.LoopID,
+                DeviceName = mUpdateDeviceObj.DeviceName,
+                MAC = mUpdateDeviceObj.MAC,
+                RegionID = UserConfig.Instance.CurrentRegion.Id,
+            };
+
+            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mUpdateAlexaDeviceObj);
+            var revertObj = MainPage.RequestHttps (API.UpdateDevice, requestJson);
+            if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
+                return true;
+            } else if (revertObj.StateCode == "Exist") {
+                Application.RunOnMainThread (() => {
+                    MainPage.AddTip ("Update failed!" + " " + ErrorCode.NameExist + " ( " + mUpdateDeviceObj.DeviceName + " )");
+                });
+                return true;
+            } else {
+                //2020-06-29 寮瑰嚭鎻愮ず閿欒
+                ShowUploadDevicesInfo (revertObj.StateCode, false);
             }
             return false;
         }
@@ -474,8 +638,11 @@
             var requestObj = new DelDeviceObj () { Id = commonId };
             var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
             var revertObj = MainPage.RequestHttps (API.DeleteDevice, requestJson);
-            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
+            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                 return true;
+            } else {
+                //2020-06-29 寮圭獥鎻愮ず閿欒
+                ShowErrorInfo (revertObj.StateCode);
             }
             return false;
         }
@@ -491,8 +658,11 @@
             var requestObj = new SceneByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
             var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
             var revertObj = MainPage.RequestHttps (API.GetSceneList, requestJson);
-            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
+            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                 sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SceneByRegion>> (revertObj.ResponseData.ToString ());
+            } else {
+                //2020-06-29 寮圭獥鎻愮ず閿欒
+                ShowErrorInfo (revertObj.StateCode);
             }
             return sceneList;
         }
@@ -504,14 +674,23 @@
         {
             var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (sceneMark);
             var revertObj = MainPage.RequestHttps (API.AddSceneAndDeviceList, requestJson);
-            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
+            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                 //var requestAddDeviceObj = new BindDeviceToSceneItemObj () { SceneId = sceneId, Command = command, DeviceID = 0, DataString = dataString };
                 //var requestAddDeviceJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestAddDeviceObj);
                 //var revertAddDeviceObj = MainPage.RequestHttps ("BindDeviceToSceneItem", requestAddDeviceJson);
-                //if (revertAddDeviceObj.StateCode.ToUpper() == "SUCCESS") {
+                //if (revertAddDeviceObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                     return true;
                 //}
+            } else if (revertObj.StateCode == "Exist") {
+                Application.RunOnMainThread (() => {
+                    MainPage.AddTip (ErrorCode.AddFailed + " " + ErrorCode.NameExist + " ( " + sceneMark.SceneName + " )");
+                });
+                return true;
+            } else {
+                //2020-06-29 寮瑰嚭鎻愮ず閿欒
+                ShowUploadDevicesInfo (revertObj.StateCode);
             }
+
             return false;
         }
 
@@ -523,7 +702,7 @@
         //    var requestAddDeviceObj = new BindDeviceToSceneItemObj () { SceneId = sceneId, Command = command, DeviceID = 0, DataString = dataString };
         //    var requestAddDeviceJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestAddDeviceObj);
         //    var revertAddDeviceObj = MainPage.RequestHttps ("BindDeviceToSceneItem", requestAddDeviceJson);
-        //    if (revertAddDeviceObj.StateCode.ToUpper() == "SUCCESS") {
+        //    if (revertAddDeviceObj.StateCode.ToUpper() == StateCode.SUCCESS) {
         //        return true;
         //    }
         //    return false;
@@ -537,12 +716,51 @@
             var requestObj = new DeleteSceneObj () { Id = sceneId };
             var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
             var revertObj = MainPage.RequestHttps (API.DeleteScene, requestJson);
-            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
+            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                 return true;
+            } else {
+                //2020-06-29 寮圭獥鎻愮ず閿欒
+                ShowErrorInfo (revertObj.StateCode);
             }
             return false;
         }
         #endregion
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="bodyView"></param>
+        /// <param name="isScene"></param>
+        void AddTopTitleView (VerticalScrolViewLayout bodyView, bool isScene = false) {
+            var chooseTypeView = new RowLayout () {
+                Height = Application.GetRealHeight (80),
+                BackgroundColor = SkinStyle.Current.TitileView,
+            };
+            bodyView.AddChidren (chooseTypeView);
+            Button btnEquipmentIcon = new Button () {
+                Width = Application.GetRealHeight (65),
+                Height = Application.GetRealHeight (65),
+                UnSelectedImagePath = "Item/LogicModule.png",
+                X = Application.GetRealWidth (30),
+                Gravity = Gravity.CenterVertical,
+            };
+            chooseTypeView.AddChidren (btnEquipmentIcon);
+         
+            Button btnEquipmentType = new Button () {
+                Width = Application.GetRealHeight (460),
+                X = btnEquipmentIcon.Right,
+                TextAlignment = TextAlignment.CenterLeft,
+                Text = Language.StringByID (R.MyInternationalizationString.Device),
+                TextColor = SkinStyle.Current.TextColor1,
+                SelectedTextColor = SkinStyle.Current.DialogTextColor
+            };
+            chooseTypeView.AddChidren (btnEquipmentType);
+
+            if (isScene) {
+                btnEquipmentIcon.UnSelectedImagePath = "Scene/Scene.png";
+                btnEquipmentType.Text = Language.StringByID (R.MyInternationalizationString.Scenes);
+            }
+        }
 
         /// <summary>
         /// 娣诲姞璁惧鐣岄潰
@@ -550,6 +768,12 @@
         public void InitDevcieRow (List<UpdateDeviceObj> list, VerticalScrolViewLayout bodyView)
         {
             bodyView.RemoveAll ();
+
+            if (list.Count > 0) {
+                //娣诲姞鏍囬
+                AddTopTitleView (bodyView);
+            }
+
             foreach (var device in list) {
                 var rowView = new RowLayout () {
                     Height = Application.GetRealHeight (110),
@@ -575,10 +799,31 @@
                     alert.Show ();
                     alert.ResultEventHandler += (s, dd) => {
                         if (dd) {
-                            if (DelDevice (device.Id)) {
-                                rowView.RemoveFromParent ();
-                                list.Remove (device);
-                            }
+                            //if (DelDevice (device.Id)) {
+                            //    rowView.RemoveFromParent ();
+                            //    list.Remove (device);
+                            //}
+
+                            System.Threading.Tasks.Task.Run (() => {
+                                Application.RunOnMainThread (() => {
+                                    MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load));
+                                });
+                                try {
+                                    var result = DelDevice (device.Id);
+                                    Application.RunOnMainThread (() => {
+                                        if (result) {
+                                            rowView.RemoveFromParent ();
+                                            list.Remove (device);
+                                        }
+                                    });
+
+                                } catch { } finally {
+
+                                    Application.RunOnMainThread (() => {
+                                        MainPage.Loading.Hide ();
+                                    });
+                                }
+                            });
                         }
                     };
                 };
@@ -590,6 +835,11 @@
         /// </summary>
         public void InitSceneRow (List<SceneByRegion> list, VerticalScrolViewLayout bodyView)
         {
+            if (list.Count > 0) {
+                //娣诲姞鏍囬
+                AddTopTitleView (bodyView, true);
+            }
+
             foreach (var scene in list) {
                 var rowView = new RowLayout () {
                     Height = Application.GetRealHeight (110),
@@ -621,10 +871,31 @@
                     alert.Show ();
                     alert.ResultEventHandler += (s, dd) => {
                         if (dd) {
-                            if (DelScene (scene.SceneId)) {
-                                rowView.RemoveFromParent ();
-                                list.Remove (scene);
-                            }
+                            //if (DelScene (scene.SceneId)) {
+                            //    rowView.RemoveFromParent ();
+                            //    list.Remove (scene);
+                            //}
+
+                            System.Threading.Tasks.Task.Run (() => {
+                                Application.RunOnMainThread (() => {
+                                    MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load));
+                                });
+                                try {
+                                    var result = DelScene (scene.SceneId);
+                                    Application.RunOnMainThread (() => {
+                                        if (result) {
+                                            rowView.RemoveFromParent ();
+                                            list.Remove (scene);
+                                        }
+                                    });
+
+                                } catch { } finally {
+
+                                    Application.RunOnMainThread (() => {
+                                        MainPage.Loading.Hide ();
+                                    });
+                                }
+                            });
                         }
                     };
                 };

--
Gitblit v1.8.0