黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using Shared;
using Shared.Common;
using Shared.R;
 
namespace Shared.Phone.Device.Logic.DoorLockLogic
{
    public class LockLogicList : FrameLayout
    {
 
        public LockLogicList()
        {
            Tag = "LockListView";
        }
        VerticalRefreshLayout middle;
        public void Show()
        {
 
            #region  上面的布局代码
            TopView view = new TopView();
            this.AddChidren(view.TopRowView());
            view.toptitleNameBtn.TextID = MyInternationalizationString.linkageevent;
            view.clickBtn.MouseDownEventHandler += (sender, e) => { RemoveFromParent(); };
            //加大点击热键
            var addiocnclick = new Button
            {
                Width = Application.GetRealWidth(108+58),
                Height = Application.GetRealHeight(72+20),
                X = Application.GetRealWidth(1080 - 108 - 58),
                Y = Application.GetRealHeight(184 - 72),
            };
            view.topRowLayout.AddChidren(addiocnclick);
 
            var addiocn = new Button
            {
                Width = Application.GetMinRealAverage(72),
                Height = Application.GetMinRealAverage(72),
                X = Application.GetRealWidth(1080 - 108 - 58),
                UnSelectedImagePath = "ZigeeLogic/lockadd.png",
                Y = Application.GetRealHeight(184 - 72 - 20),
            };
            view.topRowLayout.AddChidren(addiocn);
 
            EventHandler<MouseEventArgs> addclick = (sender, e) =>
            {
                //new一个新逻辑对象;
                Common.Logic.CurrentLogic = new Common.Logic();
                Common.Logic.CurrentLogic.IsEnable = 1;//默认为开
                Common.Logic.CurrentLogic.LogicType = 1;//标记自动化类型
                Common.Logic.CurrentLogic.Relationship = 1;
                Common.Logic.CurrentLogic.LogicName = Language.StringByID(MyInternationalizationString.linkageevent);
                var lockLogicCommunalPage = new LockLogicCommunalPage();
                UserView.HomePage.Instance.AddChidren(lockLogicCommunalPage);
                UserView.HomePage.Instance.PageIndex += 1;
                lockLogicCommunalPage.Show(() => { });
            };
 
            addiocnclick.MouseUpEventHandler += addclick;
            addiocn.MouseUpEventHandler += addclick;
 
            #endregion
            middle = new VerticalRefreshLayout
            {
                Y = view.topRowLayout.Bottom,
                Height = Application.GetRealHeight(Method.H - 184),
                BackgroundColor = ZigbeeColor.Current.LogicMiddleBackgroundColor,
            };
            this.AddChidren(middle);
            middle.BeginHeaderRefreshingAction += () =>
            {
                //重新刷新logic列表
                Common.Logic.LockLogicList.Clear();
                Read();
                //关闭刷新View;
                middle.EndHeaderRefreshing();
            };
            Read();
        }
 
        /// <summary>
        /// 读取自动化数据
        /// </summary>
        public async void Read()
        {
 
            CommonPage.Loading.Start();
            if (!BoolExist())
            {
                var Idlist = await Send.GetLogicId(1);
                if (Idlist.Count != 0)
                {
                    var listlogic = await Send.ReadList(Idlist.Count, 1);
                    //foreach可能集合已被修改,枚举操作可能不会执行,可能出现崩溃(建议for)。
                    for (int j = 0; j < listlogic.Count; j++)
                    {
                        var logic = listlogic[j];
                        if (logic.LogicType != 1)
                        {
                            continue;
                        }
                        Common.Logic.LockLogicList.Add(logic);
                      
                    }
                }
            }
            Automationview();
            CommonPage.Loading.Hide();
        }
        /// <summary>
        /// 加载自动化列表的界面
        /// </summary>
        public void Automationview()
        {
            middle.RemoveAll();
 
            //if (Common.Logic.LockLogicList.Count == 0)
            if (!BoolExist())
            {
                //就是为了显示引导添加图标;
                var noFrameLayout = new FrameLayout
                {
                    Height = Application.GetRealHeight(434 + 200 + 32 + 320),
                    //BackgroundColor = ZigbeeColor.Current.LogicMiddleBackgroundColor,
                };
                middle.AddChidren(noFrameLayout);
 
 
                var noIconBtn = new Button
                {
                    Y = Application.GetRealHeight(320),
                    Width = Application.GetMinRealAverage(756),
                    Height = Application.GetMinRealAverage(434),
                    UnSelectedImagePath = "Item/NoFunction.png",
                    X = Application.GetRealWidth(162),
                };
                noFrameLayout.AddChidren(noIconBtn);
 
                var noTextBtn = new Button()
                {
                    Y = noIconBtn.Bottom,
                    Height = Application.GetRealHeight(200) + Application.GetRealHeight(32),
                    Width = Application.GetRealWidth(700),
                    //Gravity = Gravity.CenterHorizontal,
                    Text = Language.StringByID(MyInternationalizationString.tiplocktextnull).Replace("{\\r\\n}", "\r\n"),
                    TextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor,
                    TextAlignment = TextAlignment.Center,
                    IsMoreLines = true,
                    X = Application.GetRealWidth(190),
                };
                noFrameLayout.AddChidren(noTextBtn);
 
            }
            for (int i = 0; i < Common.Logic.LockLogicList.Count; i++)
            {
                var logic = Common.Logic.LockLogicList[i];
                if (!Exist(logic))
                {
                    continue;
                }
                
                var logicRowlayout = new RowLayout
                {
                    Width = Application.GetRealWidth(1080),
                    Height = Application.GetRealHeight(190),
                    LineColor = ZigbeeColor.Current.LogicMiddleBackgroundColor,
                    BackgroundColor = ZigbeeColor.Current.LogicBackgroundColor,
                    SubViewWidth = Application.GetRealWidth(184),//改变编辑控件宽度多少;
 
                };
                middle.AddChidren(logicRowlayout);
 
                var logicnameBtn = new Button
                {
                    Height = Application.GetRealHeight(190),
                    Width = Application.GetRealWidth(600),
                    Text = logic.LogicName,
                    X = Application.GetRealWidth(58),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = ZigbeeColor.Current.LogicTextBlackColor,
                    Gravity = Gravity.CenterVertical,
                    TextSize = 15,
                };
                logicRowlayout.AddChidren(logicnameBtn);
 
 
                var logicswitchBtn = new Button
                {
                    Width = Application.GetRealWidth(104),
                    Height = Application.GetRealHeight(63),
                    UnSelectedImagePath = "ZigeeLogic/logicclose.png",
                    SelectedImagePath = "ZigeeLogic/logicopen.png",
                    X = logicRowlayout.Width - Application.GetRealWidth(104 + 58),
                    Gravity = Gravity.CenterVertical,
                };
                logicRowlayout.AddChidren(logicswitchBtn);
 
                logicswitchBtn.MouseUpEventHandler += (sender, e) =>
                {
                    logicswitchBtn.IsSelected = !logicswitchBtn.IsSelected;
                    if (logicswitchBtn.IsSelected)
                    {
                        //逻辑开
                        logic.IsEnable = 1;
                    }
                    else
                    {
                        //逻辑关
                        logic.IsEnable = 0;
                    }
                    Send.LogicControlSwitch(logic);
 
                };
                if (logic.IsEnable == 1)
                {
                    logicswitchBtn.IsSelected = true;
                }
                else if (logic.IsEnable == 0)
                {
                    logicswitchBtn.IsSelected = false;
                }
                ///编辑
                var edit = new Button
                {
                    BackgroundColor = ZigbeeColor.Current.LogicEditBlackColor1,
                    Text = Language.StringByID(MyInternationalizationString.edit),
                    TextColor = ZigbeeColor.Current.LogicBtnSaveTextColor,
                };
                logicRowlayout.AddRightView(edit);
                edit.MouseUpEventHandler += (sender, e) =>
                {
                    Common.Logic.CurrentLogic = logic;
                    var lockLogicCommunalPage = new LockLogicCommunalPage();
                    UserView.HomePage.Instance.AddChidren(lockLogicCommunalPage);
                    UserView.HomePage.Instance.PageIndex += 1;
                    lockLogicCommunalPage.Show(() => { logicnameBtn.Text = logic.LogicName; Automationview(); });
 
                };
 
                ///删除
                var del = new Button
                {
                    BackgroundColor = ZigbeeColor.Current.LogicDelBlackColor1,
                    Text = Language.StringByID(MyInternationalizationString.del),
                    TextColor = ZigbeeColor.Current.LogicBtnSaveTextColor,
                };
                logicRowlayout.AddRightView(del);
                del.MouseUpEventHandler += (sender, e) =>
                {
                    var alert = new ShowMsgControl(ShowMsgType.Confirm,
                    Language.StringByID(MyInternationalizationString.doyouwanttodelete),
                    Language.StringByID(MyInternationalizationString.confrim));
                    alert.Show();
                    alert.ConfirmClickEvent += () =>
                    {
                        Common.Logic.LockLogicList.Remove(logic);
                        Automationview();
                        Send.DelLogic(logic.LogicId);
                    };
 
                };
            }
 
        }
 
 
        /// <summary>
        /// 查找该门锁是否存在自动化
        /// </summary>
        /// <returns></returns>
        private bool BoolExist()
        {
            bool yes = false;
            for (int i = 0; i < Common.Logic.LockLogicList.Count; i++)
            {
                var logic = Common.Logic.LockLogicList[i];
                if (logic.LogicType != 1)
                {
                    continue;
                }
                
                if (Exist(logic))
                {
                    yes = true;
                    //存在就退出
                    break;
                }
            }
            return yes;
        }
 
        /// <summary>
        /// 查找这一条自动化是否属于该门锁
        /// </summary>
        /// <returns></returns>
        private bool Exist(Common.Logic logic)
        {
            for (int j = 0; j < logic.Accounts.Count; j++)
            {
                //Option4是设备mac;Option2是设备端口;
                if (logic.Accounts[j]["Option4"].ToString() == Send.CurrentDoorLock.DeviceAddr)
                {
                    //查找是否是那个门锁;
                    //如果不是该门锁联动事件不显示出来;
                    return true;
                }
            }
            return false;
        }
    }
}