黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.MainPage.ControlForm
{
    /// <summary>
    /// 主页深度卡片的底层共通
    /// </summary>
    public class DeviceDetailCardCommonForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 界面关闭事件
        /// </summary>
        public Action<CommonDevice> FormCloseEvent = null;
        /// <summary>
        /// 设备对象
        /// </summary>
        public CommonDevice device = null;
        /// <summary>
        /// 当前选择的房间对象(再次刷新界面时,这个东西有可能是null)
        /// </summary>
        public Common.Room nowSelectRoom = null;
        /// <summary>
        /// 标记它是由哪个控件调起的
        /// </summary>
        public ViewGroup RowOrCardControl = null;
        /// <summary>
        /// 状态控件
        /// </summary>
        private NormalViewControl btnStatu = null;
        /// <summary>
        /// 设备名称控件
        /// </summary>
        private NormalViewControl btnDeviceName = null;
        /// <summary>
        /// 房间名称
        /// </summary>
        private NormalViewControl btnRoomName = null;
        /// <summary>
        /// 是否获取网关反馈的结果  0:没有获取得到  1:已经获取得到
        /// </summary>
        public int ResponeResult = 0;
        /// <summary>
        /// 当前端点的功能类型
        /// </summary>
        private DeviceFunctionType nowDeviceFuncType = DeviceFunctionType.A未定义;
        /// <summary>
        /// 白色背景的宽度
        /// </summary>
        private int backFrameWidth = 0;
        /// <summary>
        /// 白色背景的高度
        /// </summary>
        private int backFrameHeight = 0;
        /// <summary>
        /// 当前的文本
        /// </summary>
        private string CurrentText = string.Empty;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_device">设备对象</param>
        /// <param name="i_nowSelectRoom">房间</param>
        /// <param name="i_backWidth">白色背景的宽度(非真实值)</param>
        /// <param name="i_backHeight">白色背景的高度(非真实值)</param>
        public void ShowForm(CommonDevice i_device, Common.Room i_nowSelectRoom, int i_backWidth, int i_backHeight)
        {
            this.CurrentText = Language.StringByID(R.MyInternationalizationString.Current) + "  ";
            this.nowSelectRoom = i_nowSelectRoom;
            this.backFrameWidth = i_backWidth;
            this.backFrameHeight = i_backHeight;
 
            //更多
            var btnMore = new MostRightIconControl(69, 69);
            btnMore.UnSelectedImagePath = "Item/More.png";
            topFrameLayout.AddChidren(btnMore);
            btnMore.InitControl();
            btnMore.ButtonClickEvent += (sender, e) =>
            {
                var detailInfo = new DeviceDetailInfoForm();
                detailInfo.AddForm(device, nowSelectRoom);
            };
 
            //初始化中部信息
            this.InitMiddleFrame(i_device);
            //添加设备上报事件
            this.AddDeviceReportEvent();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        /// <param name="i_device">设备对象</param>
        /// <param name="backWidth">白色背景的宽度(非真实值)</param>
        /// <param name="backHeight">白色背景的高度(非真实值)</param>
        private void InitMiddleFrame(CommonDevice i_device)
        {
            this.device = i_device;
            this.nowDeviceFuncType = device.DfunctionType;
 
            //清空bodyFrame
            this.ClearBodyFrame();
 
            //白色背景控件
            var frameWhiteBack = new FrameLayout();
            frameWhiteBack.Y = Application.GetRealHeight(115);
            frameWhiteBack.Width = Application.GetRealWidth(this.backFrameWidth);
            frameWhiteBack.Height = Application.GetRealHeight(this.backFrameHeight);
            frameWhiteBack.BackgroundColor = UserCenterColor.Current.White;
            frameWhiteBack.Radius = (uint)Application.GetRealHeight(17);
            frameWhiteBack.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(frameWhiteBack);
 
            //设备名称
            this.btnDeviceName = new NormalViewControl(100, 60, true);
            btnDeviceName.Y = Application.GetRealHeight(46);
            btnDeviceName.TextSize = 15;
            btnDeviceName.IsBold = true;
            btnDeviceName.Text = Common.LocalDevice.Current.GetDeviceEpointName(i_device);
            btnDeviceName.Width = btnDeviceName.GetRealWidthByText();
            btnDeviceName.TextAlignment = TextAlignment.Center;
            btnDeviceName.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(btnDeviceName);
 
            //状态
            this.btnStatu = new NormalViewControl(700, 42, true);
            btnStatu.Y = btnDeviceName.Bottom + Application.GetRealHeight(12);
            btnStatu.TextSize = 10;
            btnStatu.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnStatu.TextAlignment = TextAlignment.Center;
            btnStatu.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(btnStatu);
 
            //房间的黑色背景
            var frameRoomBlack = new FrameLayout();
            frameRoomBlack.Height = Application.GetRealHeight(138);
            frameRoomBlack.Width = frameWhiteBack.Width;
            frameRoomBlack.BackgroundColor = 0xff232323;
            frameRoomBlack.Radius= (uint)Application.GetRealHeight(17);
            frameRoomBlack.Gravity = Gravity.BottomCenter;
            frameWhiteBack.AddChidren(frameRoomBlack);
 
            //让上部变直角(因为安卓这里,单独指定左下和右下角圆角的话,圆不了)
            var frameTemp = new FrameLayout();
            frameTemp.Height = Application.GetRealHeight(30);
            frameTemp.Width = frameWhiteBack.Width;
            frameTemp.BackgroundColor = frameRoomBlack.BackgroundColor;
            frameRoomBlack.AddChidren(frameTemp);
 
            //房间图标
            var btnRoomIcon = new IconViewControl(81);
            btnRoomIcon.X = ControlCommonResourse.XXLeft;
            btnRoomIcon.UnSelectedImagePath = "Item/Room.png";
            btnRoomIcon.Gravity = Gravity.CenterVertical;
            frameRoomBlack.AddChidren(btnRoomIcon);
 
            //房间名称
            this.btnRoomName = new NormalViewControl(600, 52, true);
            btnRoomName.X = btnRoomIcon.Right + Application.GetRealWidth(12);
            btnRoomName.TextSize = 12;
            btnRoomName.TextColor = UserCenterColor.Current.White;
            btnRoomName.Gravity = Gravity.CenterVertical;
            if (this.nowSelectRoom != null)
            {
                btnRoomName.Text = this.nowSelectRoom.Name;
            }
            else
            {
                //未分配
                btnRoomName.TextID = R.MyInternationalizationString.uDeviceNotAssignedRoom;
            }
            frameRoomBlack.AddChidren(btnRoomName);
 
            //收藏
            var btnCollect = new IconBigViewControl(69, 69);
            btnCollect.UnSelectedImagePath = "Item/Collection.png";
            btnCollect.SelectedImagePath = "Item/CollectionSelected.png";
            btnCollect.IsSelected = HdlRoomLogic.Current.IsCollectInRoom(i_device);
            frameWhiteBack.AddChidren(btnCollect);
            btnCollect.InitControl();
            btnCollect.X = Application.GetRealWidth(850) - btnCollect.XOffset;
            btnCollect.Y = Application.GetRealHeight(35) - btnCollect.YOffset;
            btnCollect.ButtonClickEvent += (sender, e) =>
            {
                //状态取反
                btnCollect.IsSelected = !btnCollect.IsSelected;
                if (btnCollect.IsSelected == false)
                {
                    //取消收藏
                    HdlRoomLogic.Current.DeleteLoveDevice(i_device);
                }
                else
                {
                    //添加收藏
                    HdlRoomLogic.Current.AddLoveDevice(i_device);
                }
            };
 
            //底层初始化中部控件完成之后,调用各自界面的初始化函数
            this.InitMiddleFrameAfter(frameWhiteBack);
        }
 
        /// <summary>
        /// 底层初始化中部控件完成之后
        /// </summary>
        /// <param name="frameWhiteBack">白色背景控件</param>
        public virtual void InitMiddleFrameAfter(FrameLayout frameWhiteBack)
        {
        }
 
        #endregion
 
        #region ■ 是否获取网关反馈的结果_____________
 
        /// <summary>
        /// 检测是否获取网关反馈的结果,如果网关没有回复,则会弹出消息
        /// </summary>
        /// <param name="listControl">需要限制不能点击的控件(超时时,它会还原Select状态)</param>
        /// <param name="lastAction">函数运行完成后的回调函数。参数false:没有等到网关回复 true:接收到了网关回复</param>
        public void StartCheckResponeResult(List<ButtonBase> listControl, Action<bool> lastAction = null)
        {
            this.ResponeResult = 0;
 
            //先让指定控件不能点击
            foreach (var myContr in listControl)
            {
                myContr.CanClick = false;
            }
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                int waitime = 40;
                while (waitime > 0)
                {
                    System.Threading.Thread.Sleep(100);
                    if (this.ResponeResult == 1)
                    {
                        //已经获取得到数据
                        break;
                    }
                    waitime--;
                    //2秒的时候,还是接受不到的话,强制再次刷新设备状态
                    if (waitime == 20)
                    {
                        //从新发送获取设备的状态(强制)
                        this.device.HadReadDeviceStatu = false;
                        if (this.RowOrCardControl != null)
                        {
                            this.RowOrCardControl.GetType().InvokeMember("SendStatuComand", System.Reflection.BindingFlags.InvokeMethod, null, this.RowOrCardControl, null);
                        }
                    }
                }
                if (waitime <= 0 && this.Parent != null)
                {
                    //没有获取得到结果
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        var msgContr = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.FAIL));
                        msgContr.Show();
                    });
                }
                //开关按钮可以再点击
                foreach (var myContr in listControl)
                {
                    myContr.CanClick = true;
                }
                //函数运行完成的回调函数
                lastAction?.Invoke(this.ResponeResult == 1);
                lastAction = null;
            });
        }
 
        /// <summary>
        /// 检测网关的反馈结果(属性上报的对象:device.DeviceStatusReport)
        /// </summary>
        /// <param name="comandDiv">命令区分</param>
        /// <param name="report">上报数据</param>
        public virtual bool CheckResponeResultStatu(ReceiveComandDiv comandDiv, CommonDevice report)
        {
            return false;
        }
 
        /// <summary>
        /// 添加设备上报事件
        /// </summary>
        private void AddDeviceReportEvent()
        {
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(this.device);
            //属性上报
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceDetailCardAttribute" + mainKeys, ReceiveComandDiv.A设备属性上报, (report) =>
            {
                string mainKey2 = Common.LocalDevice.Current.GetDeviceMainKeys(report);
                if (mainKeys != mainKey2)
                {
                    //不是同一个东西
                    return;
                }
                //检测结果
                if (this.CheckResponeResultStatu(ReceiveComandDiv.A设备属性上报, report) == true)
                {
                    //结果已经接收到
                    this.ResponeResult = 1;
                }
            });
        }
 
        #endregion
 
        #region ■ 界面重新激活事件___________________
 
        /// <summary>
        /// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
        /// </summary>
        public override int FormActionAgainEvent()
        {
            //如果不是喜爱房间的话,它的房间有可能被切换了
            if (this.nowSelectRoom == null || this.nowSelectRoom.IsLove == false)
            {
                this.nowSelectRoom = HdlRoomLogic.Current.GetRoomByDevice(this.device);
            }
            //如果功能类型变更了
            if (this.nowDeviceFuncType != this.device.DfunctionType)
            {
                //重新刷新界面,初始化中部信息
                this.InitMiddleFrame(this.device);
            }
            else
            {
                //刷新名字
                this.btnDeviceName.Text = Common.LocalDevice.Current.GetDeviceEpointName(this.device);
                btnDeviceName.Width = btnDeviceName.GetRealWidthByText();
                btnDeviceName.Gravity = Gravity.CenterHorizontal;
                if (this.nowSelectRoom == null)
                {
                    //未分配
                    this.btnRoomName.TextID = R.MyInternationalizationString.uDeviceNotAssignedRoom;
                }
                else
                {
                    this.btnRoomName.Text = this.nowSelectRoom.Name;
                }
            }
 
            return 1;
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(this.device);
            HdlGatewayReceiveLogic.Current.RemoveEvent("DeviceDetailCardAttribute" + mainKeys);
 
            //回调函数
            this.FormCloseEvent?.Invoke(this.device);
            this.FormCloseEvent = null;
 
            base.CloseFormBefore();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 设置状态文本(不需要指定【当前两个字】)
        /// </summary>
        /// <param name="text"></param>
        public void SetStatuText(string text)
        {
            this.btnStatu.Text = this.CurrentText + text;
        }
 
        /// <summary>
        /// 设置PM2.5传感器状态文本(不需要指定【当前两个字】)
        /// </summary>
        /// <param name="text"></param>
        public void SetPmTwoPointFiveStatuText(string text)
        {
            this.btnStatu.Text = text;
        }
 
        /// <summary>
        /// 重新设置设备名字控件和状态控件的Y轴
        /// </summary>
        /// <param name="i_NameY">设备名字控件的Y轴(真实值)</param>
        /// <param name="i_StatuY">状态控件的Y轴(真实值)</param>
        public void ResetDeviceNameAndStatuPoint(int i_NameY, int i_StatuY)
        {
            btnDeviceName.Y = i_NameY;
            btnStatu.Y = i_StatuY;
        }
 
        #endregion
    }
}