xm
2020-04-16 6fa9d69da922c8049f5acfcbb9ce9fd26811024c
ZigbeeApp/Shared/Phone/Device/Logic/TemplatePage.cs
New file
@@ -0,0 +1,604 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Shared;
using Shared.Common;
using Shared.Phone;
using Shared.R;
using ZigBee.Device;
namespace Shared.Phone.Device.Logic
{
    public class TemplatePage : FrameLayout
    {
        public TemplatePage()
        {
            UserView.HomePage.Instance.RemoveViewByTag("Logic");
            Tag = "Logic";
        }
        EditText logicTextBox;
        /// <summary>
        /// 图标的索引
        /// </summary>
        public static string s;
        public void Show()
        {
            #region  最上面的布局代码
            UserView.HomePage.Instance.ScrollEnabled = false;//锁住左滑
            TopView view = new TopView();
            this.AddChidren(view.TopRowView());
            if (Common.Logic.CurrentLogic.LogicId != 0)
            {
                view.toptitleNameBtn.Text = Language.StringByID(MyInternationalizationString.editautomation);
            }
            else
            {
                view.toptitleNameBtn.Text = Language.StringByID(MyInternationalizationString.newautomation);
            }
            view.clickBtn.MouseDownEventHandler += (sender, e) =>
            {
                //UserView.HomePage.Instance.ScrollEnabled = true;//恢复左滑
                RemoveFromParent();
            };
            #endregion
            var middle = new VerticalScrolViewLayout
            {
                Y = view.topRowLayout.Bottom,
                Height = Application.GetRealHeight(Method.H - 184 - 180),
                BackgroundColor = ZigbeeColor.Current.LogicMiddleBackgroundColor,
            };
            this.AddChidren(middle);
            var logiciconfl = new FrameLayout
            {
                Height = Application.GetRealHeight(436 + 30),
                BackgroundColor = ZigbeeColor.Current.LogicBackgroundColor,
            };
            middle.AddChidren(logiciconfl);
            var iconBtn = new Button
            {
                Width = Application.GetRealWidth(936),
                Height = Application.GetRealHeight(436),
                X = Application.GetRealWidth(72),
                Y = Application.GetRealHeight(30),
                UnSelectedImagePath = $"ZigeeLogic/logicbj{s}.png",
            };
            logiciconfl.AddChidren(iconBtn);
            #region -----自动化名称 设置名称-----
            var logicnamefl = new FrameLayout
            {
                Height = Application.GetRealHeight(130),
                BackgroundColor = ZigbeeColor.Current.LogicBackgroundColor,
            };
            middle.AddChidren(logicnamefl);
            var text = new Button
            {
                Width = Application.GetRealWidth(300),
                Height = Application.GetRealHeight(60),
                X = Application.GetRealWidth(58),
                TextAlignment = TextAlignment.CenterLeft,
                //Text = "自动化名称",
                TextID = MyInternationalizationString.automationname,
                TextColor = ZigbeeColor.Current.LogicTextBlackColor,
                Y = Application.GetRealHeight(35),
                TextSize = 15,
            };
            logicnamefl.AddChidren(text);
            logicTextBox = new EditText
            {
                Y = Application.GetRealHeight(35),
                Width = Application.GetRealWidth(1080 - 58 - 300),
                Height = Application.GetRealHeight(60),
                X = text.Right,
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = ZigbeeColor.Current.LogicBtnCancelColor,
                TextSize = 14,
                Text = Common.Logic.CurrentLogic.LogicName,
                //TextID=MyInternationalizationString.automation1,
            };
            logicnamefl.AddChidren(logicTextBox);
            var fraline = new FrameLayout
            {
                Height = Application.GetRealHeight(30),
            };
            middle.AddChidren(fraline);
            #endregion
            #region -----显示逻辑条件-----
            LogicView.Addview addconditionview = new LogicView.Addview();
            addconditionview.iconBtn.Visible = true;
            addconditionview.titleBtn.TextID = MyInternationalizationString.ifcondition;
            middle.AddChidren(addconditionview.AddDeviceView());
            ///添加条件的点击事件
            addconditionview.clickBtn.MouseUpEventHandler += (sender, e) =>
            {
                if (Common.Logic.CurrentLogic.Conditions.Count == 1)
                {
                    ConditionView(false);
                }
                else
                {
                    var roomAndDeviceView = new RoomAndDeviceView();
                    UserView.HomePage.Instance.AddChidren(roomAndDeviceView);
                    UserView.HomePage.Instance.PageIndex += 1;
                    roomAndDeviceView.IfType = "condition_mould";
                    roomAndDeviceView.Show(Language.StringByID(MyInternationalizationString.addsensor));
                }
            };
            List<Dictionary<string, string>> ListConditions = new List<Dictionary<string, string>>();
            ListConditions.Clear();
            ListConditions.AddRange(Common.Logic.CurrentLogic.Conditions);
            ListConditions.Add(new Dictionary<string, string>());
            if (Common.Logic.CurrentLogic.Conditions.Count > 1)
            {
                var row = new FrameLayout
                {
                    Height = Application.GetRealHeight(60),
                    BackgroundColor = ZigbeeColor.Current.LogicBlankBackgroundColor,
                };
                middle.AddChidren(row);
                var relationship = new Button
                {
                    Width = Application.GetRealWidth(600),
                    Height = Application.GetRealHeight(60),
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth(58),
                    TextColor = ZigbeeColor.Current.LogicBtnCompleteColor,
                };
                row.AddChidren(relationship);
                if (Common.Logic.CurrentLogic.Relationship == 0)
                {
                    relationship.Text = Language.StringByID(MyInternationalizationString.followingconditions) + Language.StringByID(MyInternationalizationString.Allconditions);
                }
                else
                {
                    relationship.Text = Language.StringByID(MyInternationalizationString.followingconditions) + Language.StringByID(MyInternationalizationString.anycondition);
                }
                EventHandler<MouseEventArgs> editclick = (sender, e) =>
                {
                    ConditionView(true);
                };
                row.MouseUpEventHandler += editclick;
                relationship.MouseUpEventHandler += editclick;
            }
            for (int i = 0; i < ListConditions.Count; i++)
            {
                if (i == (ListConditions.Count - 1))
                {
                    LogicView.AddDeviceView addflview = new LogicView.AddDeviceView();
                    addflview.titleBtn.TextID = MyInternationalizationString.addsensor;
                    middle.AddChidren(addflview.AddFl());
                    addflview.clickBtn.MouseUpEventHandler += (sender, e) =>
                    {
                        if (Common.Logic.CurrentLogic.Conditions.Count == 1)
                        {
                            ConditionView(false);
                        }
                        else
                        {
                            var roomAndDeviceView = new RoomAndDeviceView();
                            UserView.HomePage.Instance.AddChidren(roomAndDeviceView);
                            UserView.HomePage.Instance.PageIndex += 1;
                            roomAndDeviceView.IfType = "condition_mould";
                            roomAndDeviceView.Show(Language.StringByID(MyInternationalizationString.addsensor));
                        }
                    };
                }
                else
                {
                    SelectedDeviceView selecteddevice = new SelectedDeviceView();
                    selecteddevice.Show(middle);
                    selecteddevice.stateRow.AddRightView(selecteddevice.edit);
                    selecteddevice.stateRow.AddRightView(selecteddevice.del);
                    var Type = int.Parse(ListConditions[i]["Type"]);
                    var conditions = ListConditions[i];
                    switch (Type)
                    {
                        /// 1:设备状态变化条件;
                        case 1:
                            {
                                var deviceinof = Method.GetCommonDevice(conditions["MacAddr"], conditions["Epoint"]);
                                selecteddevice.deviceNameBtn.Visible = true;
                                selecteddevice.deviceNameBtn.Text = LocalDevice.Current.GetDeviceEpointName(deviceinof);
                                selecteddevice.regionNameBtn.Visible = true;
                                Method.RoomNmae(selecteddevice.regionNameBtn, deviceinof);
                                selecteddevice.iconBtn.UnSelectedImagePath ="ZigeeLogic/sensor13.png";
                                switch (deviceinof.Type)
                                {
                                    case DeviceType.IASZone:
                                        {
                                            var iASZonedevice = deviceinof as IASZone;
                                            var intvalue = conditions["AttriButeData1"];
                                            switch (iASZonedevice.IasDeviceType)
                                            {
                                                case 13:
                                                    {
                                                        if (intvalue == "1")
                                                        {
                                                            selecteddevice.selecetddevicestateBtn.TextID = MyInternationalizationString.someone;
                                                        }
                                                        else
                                                        {
                                                            int minute = int.Parse(conditions["IgnoreTime"]) / 60;
                                                            int second = int.Parse(conditions["IgnoreTime"]) % 60;
                                                            if (minute != 0 && second != 0)
                                                            {
                                                                selecteddevice.selecetddevicestateBtn.Text = minute.ToString() + Language.StringByID(MyInternationalizationString.minute) + second.ToString() + Language.StringByID(MyInternationalizationString.second) + Language.StringByID(MyInternationalizationString.unattendedtime1);
                                                            }
                                                            else
                                                            {
                                                                if (minute == 0 && second != 0)
                                                                {
                                                                    selecteddevice.selecetddevicestateBtn.Text = second.ToString() + Language.StringByID(MyInternationalizationString.second) + Language.StringByID(MyInternationalizationString.unattendedtime1);
                                                                }
                                                                if (minute != 0 && second == 0)
                                                                {
                                                                    selecteddevice.selecetddevicestateBtn.Text = minute.ToString() + Language.StringByID(MyInternationalizationString.Minute) + Language.StringByID(MyInternationalizationString.unattendedtime1);
                                                                }
                                                            }
                                                        }
                                                    }
                                                    break;
                                            }
                                        }
                                        break;
                                }
                            }
                            break;
                    }
                    ///编辑
                    selecteddevice.edit.MouseUpEventHandler += (sender, e) =>
                    {
                        var deviceinof = Method.GetCommonDevice(conditions["MacAddr"], conditions["Epoint"]);
                        var flMain = new FrameLayout { BackgroundColor = ZigbeeColor.Current.LogicViewBackgroundColor };
                        this.AddChidren(flMain);
                        CurrentDeviceState.CurrentDeviceView(flMain, deviceinof, true, "condition_mould");
                    };
                    ///删除控件
                    selecteddevice.del.MouseUpEventHandler += (sender, e) =>
                    {
                        var alert = new UserCenter.ShowMsgControl(UserCenter.ShowMsgType.Confirm,
                   Language.StringByID(MyInternationalizationString.doyouwanttodelete),
                   Language.StringByID(MyInternationalizationString.confrim));
                        alert.Show();
                        alert.ConfirmClickEvent += () =>
                        {
                            Common.Logic.CurrentLogic.Conditions.Remove(conditions);
                            selecteddevice.selecetdFrameLayout.RemoveFromParent();
                            //var templatePage = new TemplatePage();
                            //UserView.HomePage.Instance.AddChidren(templatePage);
                            //UserView.HomePage.Instance.PageIndex += 1;
                            //templatePage.Show();
                        };
                    };
                }
            }
            #endregion
            #region  ----显示执行目标----
            LogicView.Addview addactionview = new LogicView.Addview();
            addactionview.iconBtn.Visible = true;
            addactionview.titleBtn.TextID = MyInternationalizationString.execute;
            middle.AddChidren(addactionview.AddDeviceView());
            addactionview.clickBtn.MouseUpEventHandler = (sender, e) =>
            {
                Common.Logic.CurrentLogic.LogicName = logicTextBox.Text.Trim();
                var roomAndDeviceView = new RoomAndDeviceView();
                UserView.HomePage.Instance.AddChidren(roomAndDeviceView);
                UserView.HomePage.Instance.PageIndex += 1;
                roomAndDeviceView.IfType = "action_mould";
                roomAndDeviceView.Show(Language.StringByID(MyInternationalizationString.addlighting));
            };
            List<Dictionary<string, object>> ListActions = new List<Dictionary<string, object>>();
            ListActions.Clear();
            ListActions.AddRange(Common.Logic.CurrentLogic.Actions);
            ListActions.Add(new Dictionary<string, object>());
            for (int i = 0; i < ListActions.Count; i++)
            {
                if (i == (ListActions.Count - 1))
                {
                    LogicView.AddDeviceView addflview = new LogicView.AddDeviceView();
                    addflview.titleBtn.TextID = MyInternationalizationString.addlighting;
                    middle.AddChidren(addflview.AddFl());
                    addflview.clickBtn.MouseUpEventHandler += (sender, e) =>
                    {
                        var roomAndDeviceView = new RoomAndDeviceView();
                        UserView.HomePage.Instance.AddChidren(roomAndDeviceView);
                        UserView.HomePage.Instance.PageIndex += 1;
                        roomAndDeviceView.IfType = "action_mould";
                        roomAndDeviceView.Show(Language.StringByID(MyInternationalizationString.addlighting));
                    };
                }
                else
                {
                    SelectedDeviceView actiondevice = new SelectedDeviceView();
                    actiondevice.Show(middle);
                    actiondevice.stateRow.AddRightView(actiondevice.edit);
                    actiondevice.stateRow.AddRightView(actiondevice.del);
                    var linkType = int.Parse(ListActions[i]["LinkType"].ToString());
                    var actions = ListActions[i];
                    switch (linkType)
                    {
                        case 0:
                            {
                                var deviceinof = Method.GetCommonDevice(actions["DeviceAddr"].ToString(), actions["Epoint"].ToString());
                                actiondevice.deviceNameBtn.Visible = true;
                                actiondevice.deviceNameBtn.Text = LocalDevice.Current.GetDeviceEpointName(deviceinof);
                                actiondevice.regionNameBtn.Visible = true;
                                Method.RoomNmae(actiondevice.regionNameBtn, deviceinof);
                                switch (deviceinof.Type)
                                {
                                    case DeviceType.OnOffOutput:
                                        {
                                            actiondevice.iconBtn.UnSelectedImagePath = "ZigeeLogic/light.png";
                                            var TaskList = actions["TaskList"] as List<Dictionary<string, string>>;
                                            if (TaskList == null)
                                            {
                                                continue;
                                            }
                                            foreach (var status in TaskList)
                                            {
                                                if (status["TaskType"].ToString() == "1")
                                                {
                                                    if (status["Data1"].ToString() == "0")
                                                    {
                                                        actiondevice.selecetddevicestateBtn.Text = Language.StringByID(MyInternationalizationString.close);
                                                    }
                                                    else if (status["Data1"].ToString() == "1")
                                                    {
                                                        actiondevice.selecetddevicestateBtn.Text = Language.StringByID(MyInternationalizationString.open);
                                                    }
                                                    else if (status["Data1"].ToString() == "2")
                                                    {
                                                        actiondevice.selecetddevicestateBtn.Text = Language.StringByID(MyInternationalizationString.onoff);
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                }
                            }
                            break;
                    }
                    ///编辑
                    actiondevice.edit.MouseUpEventHandler += (sender, e) => {
                        var flMain = new FrameLayout { BackgroundColor = ZigbeeColor.Current.LogicViewBackgroundColor };
                        this.AddChidren(flMain);
                        var deviceinof = Method.GetCommonDevice(actions["DeviceAddr"].ToString(), actions["Epoint"].ToString());
                        CurrentDeviceState.CurrentDeviceView(flMain, deviceinof, true, "action_mould");
                    };
                    ///删除控件
                    actiondevice.del.MouseUpEventHandler += (sender, e) =>
                    {
                        var alert = new UserCenter.ShowMsgControl(UserCenter.ShowMsgType.Confirm,
                     Language.StringByID(MyInternationalizationString.doyouwanttodelete),
                     Language.StringByID(MyInternationalizationString.confrim));
                        alert.Show();
                        alert.ConfirmClickEvent += () =>
                        {
                            Common.Logic.CurrentLogic.Actions.Remove(actions);
                            actiondevice.selecetdFrameLayout.RemoveFromParent();
                            //var templatePage = new TemplatePage();
                            //UserView.HomePage.Instance.AddChidren(templatePage);
                            //UserView.HomePage.Instance.PageIndex += 1;
                            //templatePage.Show();
                        };
                    };
                }
            }
            #endregion
            #region -----每天 设置周期-----
            var fraHeight = new FrameLayout
            {
                Height = Application.GetRealHeight(30),
            };
            middle.AddChidren(fraHeight);
            LogicView.Addview weekview = new LogicView.Addview();
            weekview.iconBtn.Visible = true;
            weekview.iconBtn.UnSelectedImagePath = "ZigeeLogic/next.png";
            weekview.titleBtn.TextID = MyInternationalizationString.setupcycle;
            middle.AddChidren(weekview.AddDeviceView());
            var weekBtn = new Button
            {
                Width = Application.GetRealWidth(595),
                Height = Application.GetRealHeight(60),
                TextAlignment = TextAlignment.CenterRight,
                //Text = "每天",
                TextColor = ZigbeeColor.Current.LogicBtnCancelColor,
                TextID = MyInternationalizationString.everyday,
                Y = Application.GetRealHeight(35 + 30),
                X = weekview.titleBtn.Right,
                TextSize = 14,
            };
            weekview.frameLayout.AddChidren(weekBtn);
            Method.UpdateWeek(weekBtn, Common.Logic.CurrentLogic);
            EventHandler<MouseEventArgs> cycleclick = (sender, e) =>
            {
                /*------保留每月,每年类型*/
                var cycle = new Cycle(() => { Method.UpdateWeek(weekBtn, Common.Logic.CurrentLogic); });
                UserView.HomePage.Instance.AddChidren(cycle);
                UserView.HomePage.Instance.PageIndex += 1;
                cycle.Show();
                //var cyclicCycle = new CyclicCycle(() => { UpdateWeek(btnweektext); });
                //UserView.HomePage.Instance.AddChidren(cyclicCycle);
                //UserView.HomePage.Instance.PageIndex += 1;
                //cyclicCycle.Show();
            };
            weekBtn.MouseUpEventHandler += cycleclick;
            weekview.clickBtn.MouseUpEventHandler += cycleclick;
            #endregion
            #region  ----推送设置----
            Method.Push(middle);
            #endregion
            var saveBtn = new LogicView.SaveView();
            saveBtn.frameLayout.Y = middle.Bottom;
            saveBtn.frameLayout.Height = Application.GetRealHeight(180);
            this.AddChidren(saveBtn.Show());
            saveBtn.clickviewBtn.MouseUpEventHandler +=(sender, e) =>
            {
                var name = logicTextBox.Text.Trim();
                Method.SaveLogic(LogicView.IfString._Logic, name, LogicView.IfString.Tag, Common.Logic.CurrentLogic);
            };
        }
        void ConditionView(bool edit)
        {
            Common.Logic.CurrentLogic.LogicName = logicTextBox.Text.Trim();
            #region  ------组合条件界面布局部分
            var flMain = new FrameLayout { BackgroundColor = ZigbeeColor.Current.LogicTranslucentColor };
            this.AddChidren(flMain);
            CompleteView completeView = new CompleteView();
            flMain.AddChidren(completeView.Show(2));
            completeView.Btntitle.TextID = MyInternationalizationString.condition;
            EventHandler<MouseEventArgs> clickcancel = (sender, e) =>
            {
                UserView.HomePage.Instance.ScrollEnabled = true;
                flMain.RemoveFromParent();
            };
            flMain.MouseUpEventHandler += clickcancel;
            completeView.Btncancel.MouseUpEventHandler += clickcancel;
            //满足所有条件
            mFunView allFunView = new mFunView();
            allFunView.frameLayout.Y = Application.GetRealHeight(140 + 20);
            completeView.Show(2).AddChidren(allFunView.Show());
            allFunView.titleBtn.TextID = MyInternationalizationString.Allconditions;
            //满足其中一个条件
            mFunView ormFunview = new mFunView();
            ormFunview.frameLayout.Y = allFunView.frameLayout.Bottom;
            completeView.Show(2).AddChidren(ormFunview.Show());
            ormFunview.titleBtn.TextID = MyInternationalizationString.anycondition;
            ormFunview.lineBtn.BackgroundColor = ZigbeeColor.Current.LogicBackgroundColor;
            #endregion
            ///满足所有条件点击事件
            allFunView.clickviewBtn.MouseUpEventHandler += (sedner, e) =>
            {
                allFunView.selectedIconBtn.Visible = true;
                ormFunview.selectedIconBtn.Visible = false;
                allFunView.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnSelectedColor;
                ormFunview.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnNotSelectedColor;
            };
            ///满足其中一个条件点击事件
            ormFunview.clickviewBtn.MouseUpEventHandler += (sedner, e) =>
            {
                allFunView.selectedIconBtn.Visible = false;
                ormFunview.selectedIconBtn.Visible = true;
                allFunView.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnNotSelectedColor;
                ormFunview.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnSelectedColor;
            };
            if (edit)
            {
                if (Common.Logic.CurrentLogic.Relationship == 0)
                {
                    allFunView.selectedIconBtn.Visible = true;
                    ormFunview.selectedIconBtn.Visible = false;
                    allFunView.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnSelectedColor;
                    ormFunview.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnNotSelectedColor;
                }
                else
                {
                    allFunView.selectedIconBtn.Visible = false;
                    ormFunview.selectedIconBtn.Visible = true;
                    allFunView.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnNotSelectedColor;
                    ormFunview.titleBtn.TextColor = ZigbeeColor.Current.LogicBtnSelectedColor;
                }
            }
            completeView.Btncomplete.MouseUpEventHandler += (sender, e) =>
            {
                if (!allFunView.selectedIconBtn.Visible && !ormFunview.selectedIconBtn.Visible)
                {
                    ///可以提示未选中状态;
                    return;
                }
                flMain.RemoveFromParent();
                if (allFunView.selectedIconBtn.Visible)
                {
                    Common.Logic.CurrentLogic.Relationship = 0;
                }
                if (ormFunview.selectedIconBtn.Visible)
                {
                    Common.Logic.CurrentLogic.Relationship = 1;
                }
                if (edit)
                {
                    if (Common.Logic.CurrentLogic.LogicId != 0)
                    {
                        Send.LogicControlSwitch(Common.Logic.CurrentLogic);
                    }
                    var templatePage = new TemplatePage();
                    UserView.HomePage.Instance.AddChidren(templatePage);
                    UserView.HomePage.Instance.PageIndex += 1;
                    templatePage.Show();
                }
                else
                {
                    var roomAndDeviceView = new RoomAndDeviceView();
                    UserView.HomePage.Instance.AddChidren(roomAndDeviceView);
                    UserView.HomePage.Instance.PageIndex += 1;
                    roomAndDeviceView.IfType = "condition_mould";
                    roomAndDeviceView.Show(Language.StringByID(MyInternationalizationString.addsensor));
                }
            };
        }
    }
}