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);
                                }
@@ -356,7 +405,11 @@
                                            }
                                        }
                                        AddScene (uploadScene);
                                        var isSuccess = AddScene (uploadScene);
                                        if (!isSuccess) {
                                            //跳出循环
                                            break;
                                        }
                                    }
                                }
@@ -389,8 +442,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 +488,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 () == "SUCCESS") {
                deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UpdateDeviceObj>> (revertObj.ResponseData.ToString ());
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return deviceList;
        }
@@ -462,6 +519,103 @@
            var revertObj = MainPage.RequestHttps (API.UploadDevices, requestJson);
            if (revertObj.StateCode.ToUpper() == "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 () == "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;
        }
@@ -476,6 +630,9 @@
            var revertObj = MainPage.RequestHttps (API.DeleteDevice, requestJson);
            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
                return true;
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return false;
        }
@@ -493,6 +650,9 @@
            var revertObj = MainPage.RequestHttps (API.GetSceneList, requestJson);
            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
                sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SceneByRegion>> (revertObj.ResponseData.ToString ());
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return sceneList;
        }
@@ -511,7 +671,16 @@
                //if (revertAddDeviceObj.StateCode.ToUpper() == "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;
        }
@@ -539,6 +708,9 @@
            var revertObj = MainPage.RequestHttps (API.DeleteScene, requestJson);
            if (revertObj.StateCode.ToUpper() == "SUCCESS") {
                return true;
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return false;
        }