lss
2020-06-12 9c16d3614d9b88c637f967518a329f239fcd3aaf
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Shared.Common;
using Shared.Phone.UserCenter.SmartSound.Forms;
using Shared.Phone.UserCenter.SmartSound.Util;
using Shared.Phone.UserCenter.SmartSound.Widget;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.SmartSound
{
 
    public class SmartSoundControlForm : EditorCommonForm
    {
 
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        private FrameLayout contentLayout = null;
        public int CurrentIndex = 0;
        private MostRightIconControl btnAddDeviceIcon = null;
        /// <summary>
        ///  智能音箱数据
        /// </summary>
        private static SmartSoundInfo.SoundInfo mSoundInfo = null;
 
        public SmartSoundControlForm(SmartSoundInfo.SoundInfo soundInfo)
        {
            mSoundInfo = soundInfo;
        }
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {            
            //设置标题信息
            base.SetTitleText("语音控制");
 
            //右上添加按钮
            if (btnAddDeviceIcon == null)
            {
                btnAddDeviceIcon = new MostRightIconControl(69, 69);
                btnAddDeviceIcon.UnSelectedImagePath = "Item/Add.png";
                topFrameLayout.AddChidren(btnAddDeviceIcon);
                btnAddDeviceIcon.InitControl();
                btnAddDeviceIcon.ButtonClickEvent += (sender, e) =>
                {
                    var smartSoundDataAdd = new SmartSoundControlContentForm(mSoundInfo);
                    smartSoundDataAdd.AddForm();
                };
            }
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        private void InitMiddleFrame()
        {
            this.ClearBodyFrame();
 
            HdlThreadLogic.Current.RunThread(async () =>
            {
                this.ShowProgressBar();
 
                string json = await GetData();//获取数据
                SmartSoundDataInfo dataInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<SmartSoundDataInfo>(json);
                if (dataInfo != null)
                {
                    if (dataInfo.StateCode == "SUCCESS")
                    {
                        SmartSound.getInstantiate().LayerList = dataInfo.ResponseData.LayerList;
                        SmartSound.getInstantiate().TokenID = mSoundInfo.Id;
                        SmartSound.getInstantiate().UserID = mSoundInfo.UserID;
                    }
                }
 
                HdlThreadLogic.Current.RunMainInThread(() =>
                {
                    if (SmartSound.getInstantiate().LayerList == null || SmartSound.getInstantiate().LayerList.Count < 1)
                    {
                        this.CloseProgressBar();
                        return;
                    }
 
                    contentLayout = new FrameLayout();
                    this.bodyFrameLayout.AddChidren(contentLayout);
                    contentLayout.RemoveAll();
 
                    var pullLayout = new MyPullControl(this, SmartSound.getInstantiate().LayerList);
                    contentLayout.AddChidren(pullLayout);
                    pullLayout.InitControl();
 
                    LoadAllRoomListView();
 
                    //无数据的时候要做一个提示
                    {
                        bool bol = false;
                        for (int i = 0; i < SmartSound.getInstantiate().LayerList.Count; i++)
                        {
                            if (SmartSound.getInstantiate().LayerList[i].RoomList.Count > 0)
                            {
                                bol = true;
                                break;
                            }
                        }
 
                        if (!bol)
                        {
                            pullLayout.Visible = false;
                            this.ShowNotDataImage(this.bodyFrameLayout, new string[] { "还没有控制内容哦,", "点击\"+\"添加" });
                        }
                    }
 
                    this.CloseProgressBar();
 
                });
            });
        }
 
        /// <summary>
        /// 加载房间列表
        /// </summary>
        private void LoadAllRoomListView()
        {
            try
            {
                if (SmartSound.getInstantiate().LayerList == null || SmartSound.getInstantiate().LayerList.Count < 1)                            
                    return;
                                
                // 加载当前楼层的所有房间 ListView
                if (listView == null)
                {
                    listView = new VerticalListControl();
                    contentLayout.AddChidren(listView);
                    listView.Y = Application.GetRealHeight(173);
                    listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(173);
 
                }
                else if (listView.Parent == null)
                {
                    listView = new VerticalListControl();
                    contentLayout.AddChidren(listView);
                    listView.Y = Application.GetRealHeight(173);
                    listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(173);
                }
 
                listView.RemoveAll();
 
                SmartSound.Layer layer = SmartSound.getInstantiate().LayerList[CurrentIndex];
 
                foreach (SmartSound.Room room in layer.RoomList)
                {
                    var roomRowLayout = new RoomRowLayout(this, room);
                    listView.AddChidren(roomRowLayout);
                    roomRowLayout.InitControl();
 
                    roomRowLayout.frameTable.ButtonClickEvent += (sender, e) =>
                    {
                        // 点击房间,进入选择设备界面
                        var deviceChange = new SmartSoundContentForDeviceChange(room);
                        deviceChange.AddForm();
                    };
 
                    roomRowLayout.btnDelect.ButtonClickEvent += (sender, e) =>
                    {
                        var delBtn = sender as NormalViewControl;
                        DelectRoom(delBtn);
                    };
                }
            }
            catch (Exception e)
            {
                string error = e.Message;
            }
        }
 
        private void DelectRoom(NormalViewControl delBtn)
        {
            this.ShowMassage(ShowMsgType.Confirm, "确认删除该房间的控制内容?", async () =>
            {
 
                HdlThreadLogic.Current.RunThread(async () =>
                {
 
                    this.ShowProgressBar("正在删除数据....");
                    //删除房间
                    var tag = (SmartSound.Room)delBtn.GetTagByKey("obj");
 
                    if (SmartSound.getInstantiate().LayerList == null)
                        return;
 
                    for (int i = 0; i < SmartSound.getInstantiate().LayerList.Count; i++)
                    {
                        SmartSound.Layer _layer = SmartSound.getInstantiate().LayerList[i];
 
                        for (int j = 0; j < _layer.RoomList.Count; j++)
                        {
                            SmartSound.Room _room = _layer.RoomList[j];
 
                            if (_room.RoomName == tag.RoomName)
                            {
                                _layer.RoomList.Remove(_room);                                
                            }
                        }
                    }
                    
                    // 重新上传数据
                    string json = await postBatchEdit();
 
                    HdlThreadLogic.Current.RunMainInThread(() =>
                    {
                        //this.InitMiddleFrame();
                        this.LoadAllRoomListView();
                    });
                    this.CloseProgressBar();
                });
 
            }, Language.StringByID(R.MyInternationalizationString.OkMsg));
        }
 
        private async Task<string> GetData()
        {
            // 从服务器获取数据
            string url = string.Format("https://developer.hdlcontrol.com/zigbeespeakerservice/BatchEdit?Platform&UserID={0}&TokenID={1}", mSoundInfo.UserID, mSoundInfo.Id);
            return await MyHttpWebResponse.GetWebRequest(url);
        }
 
        private async Task<string> postBatchEdit()
        {
            // 提交数据至服务器
            string url = "https://developer.hdlcontrol.com/zigbeespeakerservice/BatchEdit";
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(SmartSound.getInstantiate());
            string response_str = MyHttpWebResponse.PostWebRequest(url, json, Encoding.UTF8);
            return response_str;
        }
 
        public override int FormActionAgainEvent()
        {
            ShowForm();
            return base.FormActionAgainEvent();
        }
 
        #region ■ 自定义楼层选择控件_____________________
 
        /// <summary>
        /// 房间列表行
        /// </summary>
        private class MyPullControl : FrameRowControl
        {
 
            private SmartSoundControlForm smartSoundControlData = null;
            private List<SmartSound.Layer> Layers = null;
            /// <summary>
            /// 房间列表
            /// </summary>
            private NormalViewControl btnName = null;
 
            /// <summary>
            /// 房间列表行
            /// </summary>
            public MyPullControl(SmartSoundControlForm _SmartSoundDataAdd, List<SmartSound.Layer> _layers)
            {
                this.smartSoundControlData = _SmartSoundDataAdd;
                this.Layers = _layers;
                this.UseClickStatu = false;
                this.BackgroundColor = UserCenterColor.Current.White;
                this.Height = Application.GetRealHeight(173);
            }
 
            /// <summary>
            /// 初始化控件
            /// </summary>
            public void InitControl()
            {
                try
                {
                    //显示文本
                    btnName = this.AddLeftCaption(string.Empty, 700);
                    btnName.Height = Application.GetRealHeight(60);
                    btnName.TextSize = 17;
                    btnName.Y = Application.GetRealHeight(57);
                    btnName.Text = "房间列表";
 
                    var right_icon = this.AddRightArrow();
                    right_icon.SelectedImagePath = "SmartSound/PullDown.png";
                    right_icon.UnSelectedImagePath = "SmartSound/PullDown.png";
                    right_icon.ButtonClickEvent += (sender, e) =>
                    {
                        ShowPullList();
                    };
                   
                    var layout = this.AddMostRightView(Layers[smartSoundControlData.CurrentIndex].LayerName, 300, false);
                    layout.Name = "pullLayout";
                    layout.ButtonClickEvent += (sender, e) =>
                    {
                        ShowPullList();
                    };
 
                    if (SmartSound.getInstantiate().LayerList.Count < 2)
                    {
                        right_icon.Visible = false;
                        layout.Visible = false;
                    }
                }
                catch(Exception e)
                {
                    string error = e.Message;
                }
            }
 
            private void ShowPullList()
            {
                //房间列表行{房间列表 textView,楼层下拉窗}
                //显示房间{带选择框的;当选中了房间后需要在底部弹出确认按钮,点击确认跳转到新的界面}
                var pull_frame = new TopRightMenuControl(Layers.Count, 2);
                pull_frame.Y = this.Height;
 
                for (int i = 0; i < Layers.Count; i++)
                {
                    //创建楼层
                    pull_frame.AddRowMenu(Layers[i].LayerName, "Item/CreatFloor.png", "Item/CreatFloorSelected.png", () =>
                    {
                        smartSoundControlData.CurrentIndex = i;
                        smartSoundControlData.LoadAllRoomListView();
                    });
                }
            }
        }
        #endregion
 
        #region ■ 自定义智能音箱控件_____________________
 
        /// <summary>
        /// 语音控制房间行
        /// </summary>
        private class RoomRowLayout : RowLayoutControl
        {
 
            /// <summary>
            /// 房间名字
            /// </summary>
            private NormalViewControl btnName = null;
 
            /// <summary>
            /// 删除按钮
            /// </summary>
            public NormalViewControl btnDelect = null;
 
            private SmartSoundControlForm smartSoundControlForm;
            private SmartSound.Room room;
          
            /// <summary>
            /// 自定音箱关控件
            /// </summary>
            public RoomRowLayout(SmartSoundControlForm _smartSoundControlForm, SmartSound.Room _room)
            {
                this.smartSoundControlForm = _smartSoundControlForm;
                this.room = _room;
                this.BackgroundColor = UserCenterColor.Current.White;
                this.Height = Application.GetRealHeight(173);               
            }
 
            /// <summary>
            /// 初始化内部控件
            /// </summary>
            public void InitControl()
            {
                //设备           
                frameTable.AddLeftCaption(this.room.RoomName, 800);
                frameTable.AddRightArrow();
                //底线
                frameTable.AddBottomLine();
                
                btnDelect = base.AddDeleteControl();
                btnDelect.Text = "删除";
                btnDelect.AddTag("obj", room);
            }
        }
 
        #endregion
    }
}