JLChen
2021-01-11 5a5a2b696866f947b6025d26c3302e8ffef46435
Crabtree/SmartHome/UI/SimpleControl/Phone/Schedule/AddSchedule.cs
@@ -7,6 +7,7 @@
    public class AddSchedule : FrameLayout
    {
        VerticalScrolViewLayout bodyView;
        FrameLayout devicesListView;
        Action<Timer> action;
        List<Common> roomDeviceList = new List<Common>();
        List<string> roomNameList = new List<string> ();
@@ -25,7 +26,7 @@
        public void ShowPage (Timer timerTemp, List<Timer> listTimer, bool isEdit = false)
        {
            UserMiddle.SettingPageView.ScrollEnabled = false;
            timerTemp.RegionID = UserConfig.Instance.CurrentRegion.RegionID;
            timerTemp.RegionID = UserConfig.Instance.CurrentRegion.Id;
            #region topview
            var topView = new RowLayout () {
                Y = Application.GetRealHeight (36),
@@ -124,7 +125,7 @@
                X = btnTime.Right + Application.GetRealWidth (190),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetRealWidth (75),
                Height = Application.GetRealHeight (75),
                Height = Application.GetRealWidth (75),
                UnSelectedImagePath = "CrabtreeAdd/Timer.png",
            };
            timeView.AddChidren (btnTimeIcon);
@@ -431,11 +432,16 @@
                timerTemp.RoomName = roomNameList [0];
            }
            btnRoomChoose.Text = timerTemp.RoomName;
            #endregion
            #region Devices
            timerControlDeviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DeviceInfo>> (timerTemp.ControlDeviceData);//devicesControlDataList
            try {
                timerControlDeviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DeviceInfo>> (timerTemp.ControlDeviceData);//devicesControlDataList
            } catch {
                timerControlDeviceList = null;
            }
            if (timerControlDeviceList == null)
                timerControlDeviceList = new List<DeviceInfo> ();
@@ -454,7 +460,7 @@
            };
            devicesView.AddChidren (btnDeviceTitle);
            var devicesListView = new FrameLayout () {
           devicesListView = new FrameLayout () {
                Height = Application.GetRealHeight (130 * roomDeviceList.Count),
            };
            bodyView.AddChidren (devicesListView);
@@ -564,22 +570,59 @@
                if (btnMonday.IsSelected == false && btnFriday.IsSelected == false && btnThursday.IsSelected == false && btnSunday.IsSelected == false && btnTuesday.IsSelected == false && btnSaturday.IsSelected == false && btnWednesday.IsSelected == false) {
                    //new Alert ("", "请选择日期!", "Close").Show ();
                    new Alert ("", "Please setting date!", "Close").Show ();
                    new Alert ("", "Please select the days.", "Close").Show ();
                    return;
                }
                timerTemp.TimerName = etName.Text.Trim ();
                if (string.IsNullOrEmpty (timerTemp.TimerName)) {
                string mTimerName = etName.Text.Trim ();
                if (string.IsNullOrEmpty (mTimerName)) {
                    //new Alert ("", "请填写定时器名称!", "Close").Show ();
                    new Alert ("", "Please set name for the timer!", "Close").Show ();
                    return;
                } else if (listTimer.Find ((obj) => obj.TimerName == timerTemp.TimerName) != null && !isEdit) {
                    new Alert ("", "The timer name already exists. Please change it before saving it.", "Close").Show ();
                    new Alert ("", "Please fill the tab Lable name.", "Close").Show ();
                    return;
                }
                //else if (listTimer.Find ((obj) => obj.TimerName == timerTemp.TimerName) != null && !isEdit) {//2020-01-09 编辑的时候也提示重复名字
                //    //if (isEdit) {
                //    //}
                //    new Alert ("", "The timer name already exists. Please change it before saving it.", "Close").Show ();
                //    return;
                //}
                if (timerControlDeviceList.Count == 0) {
                    new Alert ("", "Please choose at least one device!", "Close").Show ();
                    return;
                }
                //名字重复判断限制
                var mTimer = listTimer.Find ((obj) => obj.TimerName == mTimerName);
                if (isEdit) {
                    if (mTimer != null && mTimer.Id != timerTemp.Id) {
                        new Alert ("", "The timer name already exists. Please change it before saving it.", "Close").Show ();
                        return;
                    }
                } else {
                    if (mTimer != null) {
                        new Alert ("", "The timer name already exists. Please change it before saving it.", "Close").Show ();
                        return;
                    }
                }
                //2020-02-10 时间重复判断限制
                string mExecutionTime = btnTimelbl.Text;
                //timerTemp.ExecutionTime = btnTimelbl.Text;
                var mTimerExecutionTime = listTimer.Find ((obj) => (obj.ExecutionTime == mExecutionTime));
                if (isEdit) {
                    if (mTimerExecutionTime != null && mTimerExecutionTime.Id != timerTemp.Id) {
                        new Alert ("", "The timer time already exists. Please change it before saving it.", "Close").Show ();
                        return;
                    }
                } else {
                    if (mTimerExecutionTime != null) {
                        new Alert ("", "The timer time already exists. Please change it before saving it.", "Close").Show ();
                        return;
                    }
                }
                timerTemp.Periodicity = "";
                if (btnMonday.IsSelected == true && btnFriday.IsSelected == true && btnThursday.IsSelected == true && btnSunday.IsSelected == true && btnTuesday.IsSelected == true && btnSaturday.IsSelected == true && btnWednesday.IsSelected == true) {
                    timerTemp.TimerType = TimerType.EveryDay;
@@ -616,9 +659,12 @@
                if (timerTemp.Periodicity [timerTemp.Periodicity.Length - 1] == ',') {
                    timerTemp.Periodicity = timerTemp.Periodicity.Remove (timerTemp.Periodicity.Length - 1, 1);
                }
                timerTemp.IsStart = true;
                if (string.IsNullOrEmpty (timerTemp.Guid)) {
                    timerTemp.Guid = Guid.NewGuid ().ToString ();
                if (string.IsNullOrEmpty (timerTemp.Id)) {
                    timerTemp.Id = Guid.NewGuid ().ToString ();
                }
                timerTemp.ControlDeviceData = Newtonsoft.Json.JsonConvert.SerializeObject (timerControlDeviceList);
                MainPage.Loading.Start ("Saving...");
@@ -630,21 +676,23 @@
                        var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (timerTemp);
                        ResponsePack revertObj = null;
                        if (isEdit) {
                            revertObj = MainPage.RequestHttps ("EditTimer", requestJson);
                        } else {
                            revertObj = MainPage.RequestHttps ("AddTimer", requestJson);
                            revertObj = MainPage.RequestHttps (API.EditTimer, requestJson);
                        } else {
                            revertObj = MainPage.RequestHttps (API.AddTimer, requestJson);
                        }
                        Application.RunOnMainThread (() => {
                            if (revertObj == null) {
                                new Alert ("", "Unable to save data, please check the network.", "Close").Show ();
                            } else if (revertObj.StateCode == "SUCCESS") {
                            } else if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                                this.RemoveFromParent ();
                                action (timerTemp);
                            } else {
                                ShowErrorInfo (revertObj.StateCode);
                            }
                        });
                    } catch (Exception ex) {
                        Console.WriteLine (ex.Message);
                        Utlis.WriteLine (ex.Message);
                    } finally {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
@@ -655,9 +703,44 @@
        }
        /// <summary>
        //(1)Success 则调用此接口成功
        //(2)NoRecord 住宅未绑定网关!
        //(3)NoExist 住宅不存在!
        //(4)Exist 生成的Guid已存在!
        /// </summary>
        void ShowErrorInfo (string stateCodeStr)
        {
            string mes = "";
            if (stateCodeStr == "NoRecord") {
                // 住宅未绑定网关!
                mes = ErrorCode.HomeNoRecord;
            } else if (stateCodeStr == "NoExist") {
                //住宅不存在!
                mes = ErrorCode.HomeNoExist;
            } else if (stateCodeStr == "Exist") {
                //生成的Guid已存在!
                mes = ErrorCode.GuidExist;
            }  else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
            }
            if (!string.IsNullOrEmpty (mes)) {
                //Application.RunOnMainThread (() => {
                new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                //});
            }
        }
        private static void InitDeviceListView (List<Common> roomDeviceList, List<DeviceInfo> timerControlDeviceList, FrameLayout devicesListView)
        {
            devicesListView.RemoveAll ();
            System.Threading.Tasks.Task.Run (() => {
                try {
                    int iniIndex = 0;
@@ -668,6 +751,15 @@
                            i++;
                        }
                    }
#if __IOS__
#else
                  //刷新devicesListView 高度
                    devicesListView.Height = Application.GetRealHeight (130 * roomDeviceList.Count);
#endif
                    foreach (var deviceCommon2 in roomDeviceList) {
                        if (deviceCommon2 == null)
                            continue;
@@ -731,7 +823,7 @@
                                        Tag = deviceCommon.CommonLoopID,
                                    };
                                    DeviceRowView.AddChidren (tempSwitch);
                                    if (deviceCommon.Type == DeviceType.FanModule) {
                                        #region 风扇
@@ -782,7 +874,7 @@
                                            ProgressColor = SkinStyle.Current.SelectedColor,
                                            ThumbRadius = Application.GetRealWidth (3),
                                            Max = 8,
                                            Progress =2,// (int)(deviceCommon.SendBytes[1]),
                                            Progress = 2,// (int)(deviceCommon.SendBytes[1]),
                                        };
                                        DeviceRowView.AddChidren (fanLevel);
@@ -803,7 +895,7 @@
                                                    btnFanLevel.Text = "S" + e.ToString ();
                                                }
                                            } catch (Exception ex) {
                                                Console.WriteLine ("ss1: " + ex.Message);
                                                Utlis.WriteLine ("ss1: " + ex.Message);
                                            }
                                        };
@@ -991,12 +1083,14 @@
                                        #endregion
                                    } else if (deviceCommon.Type == DeviceType.CurtainModel || deviceCommon.Type == DeviceType.CurtainTrietex || deviceCommon.Type == DeviceType.CurtainRoller) {
                                        #region 窗帘
                                        //2020-07-09 刷新控制操作码
                                        deviceCommon.Command = (int)Command.UpdataCurtainModelStutas;
                                        tempSwitch.RemoveFromParent ();
                                       var tempSwitch1 = new Button () {
                                           Width = Application.GetMinRealAverage (90),
                                           Height = Application.GetMinRealAverage (53),
                                           X = Application.GetRealWidth (640 - 90 - 20),
                                           Radius = 5,
                                        var tempSwitch1 = new Button () {
                                            Width = Application.GetMinRealAverage (90),
                                            Height = Application.GetMinRealAverage (53),
                                            X = Application.GetRealWidth (640 - 90 - 20),
                                            Radius = 5,
                                            BorderColor = SkinStyle.Current.Transparent,
                                            BorderWidth = 1,
                                            Gravity = Gravity.CenterVertical,
@@ -1026,10 +1120,13 @@
                                                btnChoose.IsSelected = true;
                                            }
                                            deviceCommon.SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                            if (timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath) == null) {
                                            //2020-07-09
                                            DeviceInfo mDeviceInfo = timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath);
                                            if (mDeviceInfo == null) {
                                                timerControlDeviceList.Add (deviceCommon);
                                            } else {
                                                timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath).SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                                mDeviceInfo.Command = (int)Command.UpdataCurtainModelStutas;
                                                mDeviceInfo.SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                            }
                                        };
@@ -1058,10 +1155,19 @@
                                                }
                                                deviceCommon.SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                                if (timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath) == null) {
                                                //if (timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath) == null) {
                                                //    timerControlDeviceList.Add (deviceCommon);
                                                //} else {
                                                //    timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath).SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                                //}
                                                //2020-07-09
                                                DeviceInfo mDeviceInfo = timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath);
                                                if (mDeviceInfo == null) {
                                                    timerControlDeviceList.Add (deviceCommon);
                                                } else {
                                                    timerControlDeviceList.Find ((obj) => obj.DevicePath == deviceCommon.SavePath).SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                                    mDeviceInfo.Command = (int)Command.UpdataCurtainModelStutas;
                                                    mDeviceInfo.SendBytes = new byte [] { deviceCommon.LoopID, (byte)CurrentBrightness };
                                                }
                                            } else {
                                                timerControlDeviceList.Remove (timerControlDeviceList.Find ((obj) => obj.DeviceType == (int)deviceCommon.Type &&
@@ -1071,13 +1177,13 @@
                                        #endregion
                                    }
                                }catch(Exception ex) {
                                    Console.WriteLine ("AddSchedule error : "+ ex.Message);
                                    Utlis.WriteLine ("AddSchedule error : "+ ex.Message);
                                }
                            });
                        }
                    }
                } catch (Exception ex) {
                    Console.WriteLine (ex.Message);
                    Utlis.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();