黄学彪
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
311
312
313
314
using Shared.Common;
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 DeviceColorLightDetailCardForm : DeviceDetailCardCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 界面上可以操作的控件
        /// </summary>
        private List<ButtonBase> listControl = new List<ButtonBase>();
        /// <summary>
        /// MaxLevel
        /// </summary>
        private const int MaxLevel = 254;
        /// <summary>
        /// 彩灯控件
        /// </summary>
        private WaveSeekBar waveSeekBar = null;
        /// <summary>
        /// 进度值是否在改变中
        /// </summary>
        private bool isProgressing = false;
        /// <summary>
        /// 是否已经初始化了控件(因为底层有可能会刷新整个界面)
        /// </summary>
        private bool hadInitControl = false;
        /// <summary>
        /// 彩灯是否是打开状态(不能用控件的Select来判断,太坑)
        /// </summary>
        private bool IsLightOpen = false;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 底层初始化中部控件完成之后
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        public override void InitMiddleFrameAfter(FrameLayout frameWhiteBack)
        {
            //左滑不能
            this.ScrollEnabled = false;
            //先清空
            this.listControl = new List<ButtonBase>();
            //设置状态文字
            if (((LightBase)this.device).OnOffStatus == 1)
            {
                //亮度 XX
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceCommonLogic.Current.GetMainPageDeviceStatuText(this.device));
            }
            else
            {
                //关闭
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.Close));
            }
 
            //彩灯控件
            this.waveSeekBar = new WaveSeekBar();
            waveSeekBar.Y = Application.GetRealHeight(377);
            waveSeekBar.Width = this.GetPictrueRealSize(271);
            waveSeekBar.Height = this.GetPictrueRealSize(533);
            waveSeekBar.Gravity = Gravity.CenterHorizontal;
            waveSeekBar.WavePadding = Application.GetRealWidth(8);
            waveSeekBar.MaxValue = 100;
            waveSeekBar.Progress = (int)(((DimmableLight)this.device).Level * 1.0 / MaxLevel * 100);
            waveSeekBar.CornerRadius = Application.GetRealHeight(58);
            waveSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarColor_Start, ZigbeeColor.Current.GXCWaveSeekBarColor_End);
            frameWhiteBack.AddChidren(waveSeekBar);
 
            //开关
            var btnSwitch = new IconViewControl(81);
            btnSwitch.UnSelectedImagePath = "Item/Switch.png";
            btnSwitch.SelectedImagePath = "Item/SwitchSelected.png";
            btnSwitch.Y = waveSeekBar.Bottom + Application.GetRealHeight(84);
            btnSwitch.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(btnSwitch);
            listControl.Add(btnSwitch);
            btnSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetSwitchCommand(!btnSwitch.IsSelected);
            };
 
            //设置初始状态
            this.IsLightOpen = ((LightBase)this.device).OnOffStatus == 1;
            if (IsLightOpen == true)
            {
                btnSwitch.IsSelected = true;
            }
 
            //彩灯控件里面的那个显示百分比的控件
            int progressY = waveSeekBar.Y - Application.GetMinReal(154);
            var btnProgress = new NormalViewControl(Application.GetMinReal(135), Application.GetMinReal(104), false);
            btnProgress.Y = progressY;
            btnProgress.UnSelectedImagePath = "Item/ProgressBubbles.png";
            btnProgress.IsBold = true;
            btnProgress.TextColor = UserCenterColor.Current.White;
            btnProgress.Gravity = Gravity.CenterHorizontal;
            btnProgress.TextAlignment = TextAlignment.Center;
            frameWhiteBack.AddChidren(btnProgress);
            btnProgress.Visible = false;
 
            //开始滑动的事件
            waveSeekBar.OnStartTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始变更
                this.isProgressing = true;
                //变更进度百分比的显示
                btnProgress.Y = progressY + waveSeekBar.NowProgressY;
                btnProgress.Text = waveSeekBar.Progress + "%";
                waveSeekBar.IsProgressTextShow = false;
                if (btnProgress.Visible == false)
                {
                    btnProgress.Visible = true;
                }
            };
 
            //结束滑动的事件
            waveSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始结束
                this.isProgressing = false;
                btnProgress.Visible = false;
                waveSeekBar.IsProgressTextShow = true;
            };
 
            //滑动过程中
            int oldProgressValue = waveSeekBar.Progress;//之前的值
            int nowProgressValue = oldProgressValue;//变更的值
            waveSeekBar.OnProgressChangedEvent += (sender, value) =>
            {
                //变更进度百分比的显示
                btnProgress.Y = progressY + waveSeekBar.NowProgressY;
                btnProgress.Text = value + "%";
                if (Common.Config.Instance.Home.IsVirtually == false)
                {
                    nowProgressValue = value;
                }
                else
                {
                    //如果住宅为虚拟住宅,直接改缓存
                    ((DimmableLight)this.device).Level = value * MaxLevel / 100;
                    //亮度 XX
                    this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceCommonLogic.Current.GetMainPageDeviceStatuText(this.device));
                }
            };
 
            if (this.hadInitControl == true)
            {
                //已经完成初始化
                return;
            }
            this.hadInitControl = true;
 
            if (Config.Instance.Home.IsVirtually == true)
            {
                //虚拟住宅
                return;
            }
 
            //开一个线程,监视是否滑动的滑动条,每秒检测一次
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    if (nowProgressValue == oldProgressValue)
                    {
                        //值一样
                        continue;
                    }
                    oldProgressValue = nowProgressValue;
                    //发送进度值
                    ((DimmableLight)this.device).SetLevel((int)(oldProgressValue * MaxLevel / 100.0));
                }
            });
        }
 
        #endregion
 
        #region ■ 发送开关命令_______________________
 
        /// <summary>
        /// 发送开关命令
        /// </summary>
        /// <param name="isOpen"></param>
        private void SetSwitchCommand(bool isOpen)
        {
            //获取当前亮度
            int level = Convert.ToInt32(this.device.GetType().InvokeMember("Level", System.Reflection.BindingFlags.GetField, null, this.device, null));
            //如果住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                ((LightBase)this.device).OnOffStatus = isOpen == true ? 1 : 0;
                if (((LightBase)this.device).OnOffStatus == 1 && level == 0)
                {
                    //如果当前是打开状态,并且亮度为0的话,则需要变成100%亮度
                    this.device.GetType().InvokeMember("Level", System.Reflection.BindingFlags.SetField, null, this.device, new object[] { 100 });
                }
                //刷新开关状态
                this.RefreshSwitchStatu(isOpen);
                return;
            }
 
            //检测是否获取网关反馈的结果,如果网关没有回复,则会弹出消息
            this.StartCheckResponeResult(this.listControl, (result) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    bool statu = ((LightBase)this.device).OnOffStatus == 1;
                    //接收到网关回复
                    if (result == true)
                    {
                        //刷新开关状态
                        this.RefreshSwitchStatu(statu);
                        //状态取反
                        listControl[0].IsSelected = statu;
                    }
                });
            });
            if (isOpen == true)
            {
                //打开
                if (level == 0)
                {
                    //如果当前是打开状态,并且亮度为0的话,则需要变成100%亮度
                    this.device.GetType().InvokeMember("SetLevel", System.Reflection.BindingFlags.InvokeMethod, null, this.device, new object[] { 254 });
                }
                else
                {
                    this.device.SwitchControl(1);
                }
            }
            else
            {
                //关闭
                this.device.SwitchControl(0);
            }
        }
 
        #endregion
 
        #region ■ 是否获取网关反馈的结果_____________
 
        /// <summary>
        /// 检测网关的反馈结果(属性上报的对象:device.DeviceStatusReport)
        /// </summary>
        /// <param name="comandDiv">命令区分</param>
        /// <param name="report">上报数据</param>
        /// <returns></returns>
        public override bool CheckResponeResultStatu(ReceiveComandDiv comandDiv, CommonDevice report)
        {
            if (comandDiv == ReceiveComandDiv.A设备属性上报)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //刷新开关状态
                    this.RefreshSwitchStatu(((LightBase)this.device).OnOffStatus == 1);
                });
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region ■ 刷新开关状态_______________________
 
        /// <summary>
        /// 刷新开关状态
        /// </summary>
        /// <param name="isOpen">打开状态</param>
        private void RefreshSwitchStatu(bool isOpen)
        {
            if (isOpen == true)
            {
                //亮度是必须要刷新的  亮度 XX
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceCommonLogic.Current.GetMainPageDeviceStatuText(this.device));
            }
            if (isOpen == false && this.IsLightOpen == true)
            {
                //状态不一样,才变更字样:关闭
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.Close));
            }
 
            if (this.isProgressing == false)
            {
                //当进度值在手动变更中时,不接收推送
                waveSeekBar.Progress = (int)(((DimmableLight)this.device).Level * 1.0 / MaxLevel * 100);
            }
 
            if (listControl[0].IsSelected != isOpen)
            {
                //开关状态变更
                listControl[0].IsSelected = isOpen;
            }
 
            this.IsLightOpen = isOpen;
        }
 
        #endregion
    }
}