黄学彪
2021-01-28 1fcf2302f79f9cbea5ef17c3688311ed65cfabb4
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
using Shared;
using HDL_ON.Stan;
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.UI.CSS;
using HDL_ON.Entity;
using HDL_ON.DriverLayer;
 
namespace HDL_ON.UI
{
    /// <summary>
    /// 涂鸦风扇的控制界面
    /// </summary>
    public class TuyaFanPage : DeviceFunctionCardCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 当前档位控件
        /// </summary>
        private NormalViewControl btnNowGear = null;
        /// <summary>
        /// 滑动条控件
        /// </summary>
        private SeekBarImageControl seekBarContr = null;
        /// <summary>
        /// 开关控件
        /// </summary>
        private IconViewControl btnSwitch = null;
        /// <summary>
        /// 风扇数据
        /// </summary>
        private FanData fanData = new FanData();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 初始化白色区域的内容
        /// </summary>
        public override void InitFrameWhiteContent()
        {
            base.SetTitleText(Language.StringByID(StringId.Fan));
 
            //刷新当前设备的状态缓存
            this.RefreshNowDeviceStatuMemory(this.device);
            //初始化第一个索引页的内容
            this.InitFrameWhiteContent1();
        }
 
        /// <summary>
        /// 初始化第一个索引页的内容
        /// </summary>
        private void InitFrameWhiteContent1()
        {
            //当前风速
            var btnNowSpeed = new NormalViewControl(FrameWhiteCentet1.Width, Application.GetRealHeight(21), false);
            btnNowSpeed.Y = Application.GetRealHeight(164);
            btnNowSpeed.TextAlignment = TextAlignment.Center;
            btnNowSpeed.TextID = StringId.CurrentWindSpeed;
            FrameWhiteCentet1.AddChidren(btnNowSpeed);
 
            //当前档位值
            this.btnNowGear = new NormalViewControl(200, 38, true);
            btnNowGear.TextAlignment = TextAlignment.Center;
            btnNowGear.TextSize = CSS_FontSize.EmphasisFontSize_ExLevel;
            btnNowGear.Gravity = Gravity.CenterHorizontal;
            btnNowGear.Y = btnNowSpeed.Bottom + Application.GetRealHeight(8);
            FrameWhiteCentet1.AddChidren(btnNowGear);
 
            //滑动条
            this.seekBarContr = new SeekBarImageControl(215);
            seekBarContr.Enable = false;
            seekBarContr.Gravity = Gravity.CenterHorizontal;
            seekBarContr.Y = btnNowGear.Bottom + Application.GetRealHeight(30);
            seekBarContr.MinValue = 1;
            seekBarContr.MaxValue = 15;
            FrameWhiteCentet1.AddChidren(seekBarContr);
 
            //开关图标
            this.btnSwitch = new IconViewControl(40);
            btnSwitch.Gravity = Gravity.CenterHorizontal;
            btnSwitch.Y = Application.GetRealHeight(468);
            btnSwitch.UnSelectedImagePath = "Public/PowerClose.png";
            btnSwitch.SelectedImagePath = "Public/PowerOpen.png";
            FrameWhiteCentet1.AddChidren(btnSwitch);
            btnSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SendSwitchComand();
            };
 
            //刷新界面状态
            this.RefreshFormStatu(false);
 
            int oldProgressValue = fanData.SpeedLevel;
            int nowProgressValue = fanData.SpeedLevel;
            //档
            var strView = Language.StringByID(StringId.Gear);
            seekBarContr.ProgressChangedEvent += (div, value) =>
            {
                nowProgressValue = value;
                this.btnNowGear.Text = value + strView;
                //滑动中
                if (div == 0) { this.fanData.IsProgressing = true; }
                //滑动结束
                else
                {
                    this.fanData.IsProgressing = false;
                    this.fanData.ProgressEndTime = DateTime.Now;
                }
            };
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (this.Parent != null)
                {
                    if (nowProgressValue != oldProgressValue)
                    {
                        //发送风速命令
                        this.SendSpeedComand(nowProgressValue);
                        oldProgressValue = nowProgressValue;
                    }
                    System.Threading.Thread.Sleep(1000);
                }
                //界面关闭时
                if (nowProgressValue != oldProgressValue)
                {
                    //发送风速命令
                    this.SendSpeedComand(nowProgressValue);
                }
            });
        }
 
        #endregion
 
        #region ■ 设备状态反馈_______________________
 
        /// <summary>
        /// 设备状态反馈
        /// </summary>
        /// <param name="i_LocalDevice"></param>
        public override void DeviceStatuPush(Function i_LocalDevice)
        {
            //不是同一个东西
            if (this.device.sid != i_LocalDevice.sid) { return; }
 
            //刷新当前设备的状态缓存
            this.RefreshNowDeviceStatuMemory(i_LocalDevice);
            //刷新界面状态
            this.RefreshFormStatu(true);
        }
 
        #endregion
 
        #region ■ 刷新界面状态_______________________
 
        /// <summary>
        /// 刷新界面状态
        /// </summary>
        private void RefreshFormStatu(bool checkTime)
        {
            //开关
            if (this.btnSwitch.IsSelected != this.fanData.Open)
            {
                //相同不变更
                this.btnSwitch.IsSelected = this.fanData.Open;
                this.seekBarContr.Enable = this.fanData.Open;
            }
            //如果是停止滑动的情况
            if (this.fanData.IsProgressing == false)
            {
                if (checkTime == true && (DateTime.Now - this.fanData.ProgressEndTime).TotalSeconds <= 2)
                {
                    //2秒之内不变更滑动条的值(为了不让它来回跳动)
                    return;
                }
                if (this.seekBarContr.Progress != this.fanData.SpeedLevel)
                {
                    //变更进度条的值
                    this.seekBarContr.Progress = this.fanData.SpeedLevel;
                    this.btnNowGear.Text = this.fanData.SpeedLevel + Language.StringByID(StringId.Gear);
                }
            }
        }
 
        #endregion
 
        #region ■ 发送各种命令_______________________
 
        /// <summary>
        /// 发送开关命令
        /// </summary>
        private void SendSwitchComand()
        {
            this.btnSwitch.CanClick = false;
 
            string statu = this.btnSwitch.IsSelected == true ? "off" : "on";
            HdlThreadLogic.Current.RunThread(() =>
            {
                var dic = new Dictionary<string, string>();
                dic.Add(FunctionAttributeKey.OnOff, statu);
                Control.Ins.SendWriteCommand(this.device, dic, true);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    this.btnSwitch.CanClick = true;
                });
            });
        }
 
        /// <summary>
        /// 发送风速命令
        /// </summary>
        private void SendSpeedComand(int value)
        {
            var dic = new Dictionary<string, string>();
            dic.Add("fan_speed_percent", value.ToString());
            Control.Ins.SendWriteCommand(this.device, dic, true);
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 刷新当前设备的状态缓存
        /// </summary>
        private void RefreshNowDeviceStatuMemory(Function i_LocalDevice)
        {
            foreach (var data in i_LocalDevice.attributes)
            {
                //开关
                if (data.key == "on_off") { this.fanData.Open = data.realValue == "on"; }
                //风速档位
                else if (data.key == "fan_speed_percent")
                {
                    var value = data.realValue;
                    if (value != string.Empty)
                    {
                        this.fanData.SpeedLevel = Convert.ToInt32(value);
                    }
                }
            }
        }
 
        #endregion
 
        #region ■ 结构体_____________________________
 
        /// <summary>
        /// 风扇的数据
        /// </summary>
        private class FanData
        {
            /// <summary>
            /// 是否打开
            /// </summary>
            public bool Open = true;
            /// <summary>
            /// 风速档位(1-15)
            /// </summary>
            public int SpeedLevel = 1;
            /// <summary>
            /// 是否正在滑动中
            /// </summary>
            public bool IsProgressing = false;
            /// <summary>
            /// 滑动结束的时间
            /// </summary>
            public DateTime ProgressEndTime = DateTime.Now;
        }
 
        #endregion
    }
}