wei
2021-08-27 1f5fdd892e8efc9f1babe1ace9d810803edef396
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
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.DriverLayer;
using HDL_ON.Entity;
using HDL_ON.Stan;
using HDL_ON.UI.CSS;
using Shared;
 
namespace HDL_ON.UI
{
    public class AirFreshRelayControlPage : DeviceFunctionCardCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 风速图标控件
        /// </summary>
        private IconViewControl btnFan = null;
        /// <summary>
        /// 风速文本控件
        /// </summary>
        private NormalViewControl btnFanView = null;
        /// <summary>
        /// 开关图标
        /// </summary>
        private IconViewControl btnSwitch = null;
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 初始化白色区域的内容
        /// </summary>
        public override void InitFrameWhiteContent()
        {
            base.SetTitleText(Language.StringByID(StringId.AirFresh));
 
            //刷新当前设备的状态缓存
            this.RefreshNowDeviceStatuMemory(this.device);
            //初始化第一个索引页的内容
            this.InitFrameWhiteContent1();
 
            Control.Ins.SendReadCommand(device);
        }
 
        /// <summary>
        /// 初始化第一个索引页的内容
        /// </summary>
        private void InitFrameWhiteContent1()
        {
            //一个背景图片
            var framePic = new FrameLayout();
            framePic.Y = Application.GetRealHeight(87);
            framePic.Width = this.GetPictrueRealSize(247);
            framePic.Height = this.GetPictrueRealSize(247);
            framePic.Gravity = Gravity.CenterHorizontal;
            framePic.BackgroundImagePath = "FunctionIcon/AirFresh/AirFreshBgIcon.png";
            this.FrameWhiteCentet1.AddChidren(framePic);
 
            //风速图标
            this.btnFan = new IconViewControl(28);
            btnFan.Gravity = Gravity.CenterHorizontal;
            btnFan.Y = Application.GetRealHeight(367);
            this.FrameWhiteCentet1.AddChidren(btnFan);
            List<string> fanSpeedList = new List<string>();
            fanSpeedList = device.GetAttribute(FunctionAttributeKey.FanSpeed).value;
            btnFan.MouseUpEventHandler += (sender, e) =>
            {
                ShowFanSelectView();
                //return;
                //if ( !btnSwitch.IsSelected){return;}
                //var curFanSpeedIndex = fanSpeedList.IndexOf(device.GetAttrState(FunctionAttributeKey.FanSpeed));
                //var sendFanSpeedIndex = curFanSpeedIndex == fanSpeedList.Count - 1 ? 0 : 1+curFanSpeedIndex;
 
                //device.SetAttrState(FunctionAttributeKey.FanSpeed, fanSpeedList[sendFanSpeedIndex]);
                //new System.Threading.Thread(() =>
                //{
                //    Dictionary<string, string> d = new Dictionary<string, string>();
                //    d.Add(FunctionAttributeKey.FanSpeed, fanSpeedList[sendFanSpeedIndex]);
                //    Control.Ins.SendWriteCommand(device, d);
                //})
                //{ IsBackground = true }.Start();
                //RefreshFanSpeed();
            };
            //风速文本
            this.btnFanView = new NormalViewControl(this.FrameWhiteCentet1.Width / 3, Application.GetRealHeight(18), false);
            btnFanView.Gravity = Gravity.CenterHorizontal;
            btnFanView.Y = btnFan.Bottom + Application.GetRealHeight(6);
            btnFanView.TextAlignment = TextAlignment.Center;
            btnFanView.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
            btnFanView.TextColor = CSS_Color.TextualColor;
            this.FrameWhiteCentet1.AddChidren(btnFanView);
 
            //开关图标
            this.btnSwitch = new IconViewControl(32);
            btnSwitch.Gravity = Gravity.CenterHorizontal;
            btnSwitch.Y = Application.GetRealHeight(468);
            btnSwitch.UnSelectedImagePath = "Public/PowerClose.png";
            btnSwitch.SelectedImagePath = "Public/PowerOpen.png";
            FrameWhiteCentet1.AddChidren(btnSwitch);
            btnSwitch.MouseUpEventHandler = (sender, e) =>
            {
                btnSwitch.IsSelected = !btnSwitch.IsSelected;
                device.trait_on_off.curValue = btnSwitch.IsSelected ? "on" : "off";
                new System.Threading.Thread(() =>
                {
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.OnOff, device.trait_on_off.curValue.ToString());
                    Control.Ins.SendWriteCommand(device, d);
                })
                { IsBackground = true }.Start();
            };
 
            //刷新界面状态
            this.RefreshFormStatu();
 
            if (fanSpeedList.Count > 0)
            {
                this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan1.png";
                this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan1Select.png";
                this.btnFanView.Text = Language.StringByID(StringId.LowWindSpeed);
            }
        }
 
        #endregion
 
        #region ■ 显示风速选择界面___________________
 
        /// <summary>
        /// 显示风速选择界面
        /// </summary>
        private void ShowFanSelectView()
        {
            //整个灰色界面
            var frameBack = new Dialog();
 
            var dialogBody = new NormalFrameLayout();
            frameBack.AddChidren(dialogBody);
            dialogBody.ButtonClickEvent = (sender, e) =>
            {
                frameBack.Close();
            };
            frameBack.Show();
 
            var fanAttr = device.GetAttribute(FunctionAttributeKey.FanSpeed);
            if(fanAttr == null)
            {
                return;
            }
            var valueCount = fanAttr.value.Count;
 
            //菜单控件(风速)
            var menuContr = new DialogTitleMenuControl(valueCount, Language.StringByID(StringId.FanSpeed));
            //menuContr.X = Application.GetRealWidth(209);
            menuContr.Gravity = Gravity.CenterHorizontal;
            menuContr.Y = Application.GetRealHeight(231);
            menuContr.Width = Application.GetRealWidth(160);
            if (valueCount > 2)
            {
                menuContr.Height = Application.GetRealHeight(199);
            }
            else
            {
                menuContr.Height = Application.GetRealHeight(154);
            }
            dialogBody.AddChidren(menuContr);
 
            foreach(var fanAttrValue in fanAttr.value)
            {
                bool isSelecte = false;
                var iconPath = "FunctionIcon/AirFresh/Fan1.png";
                string text = Language.StringByID(StringId.FanOneGear);
                if (fanAttrValue == "low" || fanAttrValue == "level_1")
                {
                    text = Language.StringByID(StringId.LowWindSpeed);
                    if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low"|| this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "level_1")
                    {
                        iconPath = "FunctionIcon/AirFresh/Fan1Select.png";
                        isSelecte = true;
                    }
                }
                else if (fanAttrValue == "medium" || fanAttrValue == "level_2")
                {
                    text = Language.StringByID(StringId.MiddleWindSpeed);
                    iconPath = "FunctionIcon/AirFresh/Fan2.png";
                    if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium" || this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "level_2")
                    {
                        iconPath = "FunctionIcon/AirFresh/Fan2Select.png";
                        isSelecte = true;
                    }
                }
                else if (fanAttrValue == "high" || fanAttrValue == "level_3")
                {
                    text = Language.StringByID(StringId.HighWindSpeed);
                    iconPath = "FunctionIcon/AirFresh/Fan3.png";
                    if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high" || this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "level_3")
                    {
                        iconPath = "FunctionIcon/AirFresh/Fan3Select.png";
                        isSelecte = true;
                    }
                }
 
                menuContr.AddRowMenu(text, iconPath, isSelecte, () =>
                {
                    frameBack.Close();
                    //发送档位命令
                    this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, fanAttrValue);
                });
            }
 
            //if (fanAttr.value.Contains("low"))
            //{
            //    //1档
            //    var iconPath = this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low" ? "FunctionIcon/AirFresh/Fan1Select.png" : "FunctionIcon/AirFresh/Fan1.png";
            //    menuContr.AddRowMenu(Language.StringByID(StringId.FanOneGear), iconPath, this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low", () =>
            //    {
            //        frameBack.Close();
            //        //发送档位命令
            //        this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, "low");
            //    });
            //}
 
            //if (fanAttr.value.Contains("medium"))
            //{
            //    //2档
            //    var iconPath = this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium" ? "FunctionIcon/AirFresh/Fan2Select.png" : "FunctionIcon/AirFresh/Fan2.png";
            //    menuContr.AddRowMenu(Language.StringByID(StringId.FanTwoGear), iconPath, this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium", () =>
            //    {
            //        frameBack.Close();
            //        //发送档位命令
            //        this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, "medium");
            //    });
            //}
 
            //if (fanAttr.value.Contains("high"))
            //{
            //    //3档
            //    var iconPath = this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high" ? "FunctionIcon/AirFresh/Fan3Select.png" : "FunctionIcon/AirFresh/Fan3.png";
            //    menuContr.AddRowMenu(Language.StringByID(StringId.FanThreeGear), iconPath, this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high", () =>
            //    {
            //        frameBack.Close();
            //        //发送档位命令
            //        this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, "high");
            //    });
            //}
        }
 
        #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();
        }
 
        #endregion
 
        #region ■ 刷新界面状态_______________________
 
        /// <summary>
        /// 刷新界面状态
        /// </summary>
        private void RefreshFormStatu()
        {
            //开关
            this.btnSwitch.IsSelected = this.device.GetAttrState(FunctionAttributeKey.OnOff) == "on";
 
            this.RefreshFanSpeed();
        }
 
        /// <summary>
        /// 刷新风速状态
        /// </summary>
        private void RefreshFanSpeed()
        {
            //风速
            if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low")
            {
                this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan1.png";
                this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan1Select.png";
                this.btnFanView.Text = Language.StringByID(StringId.LowWindSpeed);
            }
            else if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium")
            {
                this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan2.png";
                this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan2Select.png";
                this.btnFanView.Text = Language.StringByID(StringId.MiddleWindSpeed);
            }
            else if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high")
            {
                this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan3.png";
                this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan3Select.png";
                this.btnFanView.Text = Language.StringByID(StringId.HighWindSpeed);
            }
            this.btnFan.CanClick = this.btnFan.IsSelected = this.device.GetAttrState(FunctionAttributeKey.OnOff) == "on";
        }
 
        #endregion
 
        #region ■ 发送各种命令_______________________
 
        /// <summary>
        /// 发送其他命令
        /// </summary>
        /// <param name="btnIcon">按钮</param>
        /// <param name="comadKey">命令主键</param>
        /// <param name="comadValue">命令</param>
        private void SendOtherComand(IconViewControl btnIcon, string comadKey, string comadValue)
        {
            btnIcon.CanClick = false;
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //获取发送命令的样板(bus协议是需要一次性把全部命令一起发送的)
                var dic = this.GetSendComandSample();
                dic[comadKey] = comadValue;
                Control.Ins.SendWriteCommand(this.device, dic);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    btnIcon.CanClick = true;
                });
            });
        }
 
        /// <summary>
        /// 获取发送命令的样板(bus协议是需要一次性把全部命令一起发送的)
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, string> GetSendComandSample()
        {
            var dic = new Dictionary<string, string>();
            //开关
            dic[FunctionAttributeKey.OnOff] = this.device.GetAttrState(FunctionAttributeKey.OnOff);
            //风速
            dic[FunctionAttributeKey.FanSpeed] = this.device.GetAttrState(FunctionAttributeKey.FanSpeed);
 
            return dic;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 刷新当前设备的状态缓存
        /// </summary>
        private void RefreshNowDeviceStatuMemory(Function i_LocalDevice)
        {
            for (int i = 0; i < i_LocalDevice.attributes.Count; i++)
            {
                var data = i_LocalDevice.attributes[i];
                //开关
                if (data.key == FunctionAttributeKey.OnOff) { this.device.SetAttrState(FunctionAttributeKey.OnOff, data.state); }
                //风速
                else if (data.key == FunctionAttributeKey.FanSpeed) { this.device.SetAttrState(FunctionAttributeKey.FanSpeed, data.state); }
            }
        }
 
        #endregion
 
 
 
 
    }
}