wei
2021-02-23 0f5d7d18f98d6d961b3d21dc2b1b59905e261fff
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
using System;
using System.Collections.Generic;
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 TuyaWaterValvePage : DeviceFunctionCardCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 图片控件
        /// </summary>
        private PicViewControl btnPictrue = null;
        /// <summary>
        /// 控制器控制状态提示文本按钮
        /// </summary>
        NormalViewControl btnControlTip = null;
        /// <summary>
        /// 控制器控制时间提示文本按钮
        /// </summary>
        NormalViewControl btnControlTimeTip = null;
 
        /// <summary>
        /// 
        /// </summary>
        private IconViewControl btnTimeSet = null;
 
        /// <summary>
        /// 开关控件
        /// </summary>
        private IconViewControl btnSwitch = null;
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 初始化白色区域的内容
        /// </summary>
        public override void InitFrameWhiteContent()
        {
            base.SetTitleText(Language.StringByID(StringId.AirCleaner));
 
            //初始化第一个索引页的内容
            this.InitFrameWhiteContent1();
            //刷新界面状态
            this.RefreshFormStatu();
        }
 
        /// <summary>
        /// 初始化第一个索引页的内容
        /// </summary>
        private void InitFrameWhiteContent1()
        {
            //图片控件
            this.btnPictrue = new PicViewControl(130, 118);
            btnPictrue.Y = Application.GetRealHeight(95);
            btnPictrue.Gravity = Gravity.CenterHorizontal;
            btnPictrue.UnSelectedImagePath = "FunctionIcon/Electrical/WaterValve/WaterValveBg.png";
            btnPictrue.SelectedImagePath = "FunctionIcon/Electrical/WaterValve/WaterValveOnBg.png";
            FrameWhiteCentet1.AddChidren(btnPictrue);
            btnPictrue.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SendSwitchComand();
            };
 
            btnControlTip = new NormalViewControl(300,30, true)
            {
                Y = Application.GetRealHeight(231),
                TextAlignment = TextAlignment.Center,
                Gravity = Gravity.CenterHorizontal,
                TextSize = CSS_FontSize.TextFontSize,
                SelectedTextColor = CSS_Color.MainColor,
                TextColor = CSS_Color.TextualColor,
                TextID = StringId.ControllerNotOn,
            };
            FrameWhiteCentet1.AddChidren(btnControlTip);
 
            btnControlTimeTip = new NormalViewControl(300, 30, true)
            {
                Y = Application.GetRealHeight(252),
                Gravity = Gravity.CenterHorizontal,
                TextAlignment = TextAlignment.Center,
                TextSize = CSS_FontSize.TextFontSize,
                //TextColor = 0x00000000,
                SelectedTextColor = CSS_Color.TextualColor,
                TextColor = CSS_Color.TextualColor,
                Text = "00:00:00"
            };
            FrameWhiteCentet1.AddChidren(btnControlTimeTip);
 
            btnTimeSet = new IconViewControl(50)
            {
                Y = Application.GetRealHeight(365),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "FunctionIcon/Electrical/WaterValve/SetTimeIcon.png",
                SelectedImagePath = "FunctionIcon/Electrical/WaterValve/SetTimeIcon.png",
            };
            FrameWhiteCentet1.AddChidren(btnTimeSet);
            btnTimeSet.ButtonClickEvent = (sender, e) => {
                SetControlTime();
            };
 
            //开关图标
            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();
            };
 
            var pack = new DAL.Server.HttpServerRequest().GetDeviceInfoList(new List<string>() { base.device.deviceId  });
        }
        #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.RefreshFormStatu();
        }
 
        #endregion
 
        #region ■ 发送各种命令_______________________
 
        /// <summary>
        /// 发送开关命令
        /// </summary>
        private void SendSwitchComand()
        {
            this.btnPictrue.CanClick = false;
            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.btnPictrue.CanClick = true;
                    this.btnSwitch.CanClick = true;
                });
            });
        }
 
        #endregion
 
        #region ■ 刷新界面状态_______________________
 
        /// <summary>
        /// 刷新界面状态
        /// </summary>
        private void RefreshFormStatu()
        {
            Application.RunOnMainThread(() =>
            {
                var onoffStatu = device.status.Find((obj) => obj.key == FunctionAttributeKey.OnOff);
                var timeStatu = device.status.Find((obj) => obj.key == FunctionAttributeKey.TuyaWaterTime);
 
                if (onoffStatu != null)
                {
                    if (onoffStatu.value.ToString() == "on")
                    {
                        btnControlTip.TextID = StringId.ControllerOn;
                    }
                    else
                    {
                        btnControlTip.TextID = StringId.ControllerNotOn;
                    }
                    this.btnSwitch.IsSelected = this.btnPictrue.IsSelected = onoffStatu.value.ToString() == "on";
                }
                if (timeStatu != null)
                {
                    int.TryParse(timeStatu.value, out waterRunningTime);
                }
                RefreshTimeButton();
            });
        }
 
        System.Threading.Thread refreshTimeThread;
        int waterRunningTime = 0;
        /// <summary>
        /// 刷新水阀时间文本
        /// </summary>
        private void RefreshTimeButton()
        {
            if (refreshTimeThread == null || refreshTimeThread.ThreadState == System.Threading.ThreadState.Stopped)
            {
                refreshTimeThread = new System.Threading.Thread(() =>
                {
                    while (waterRunningTime >= 0)
                    {
                        if (waterRunningTime > 0)
                        {
                            waterRunningTime--;
                        }
                        var sce = waterRunningTime % 60;
                        var min = 0;
                        if(waterRunningTime > 60)
                        {
                            min = (waterRunningTime - sce) / 60 %60;
                        }
                        var hour = 0;
                        if(waterRunningTime > 360)
                        {
                             hour = (waterRunningTime - sce - (min * 60)) / 3600;
                        }
                        Application.RunOnMainThread(() =>
                        {
                            btnControlTimeTip.Text = $"{hour.ToString().PadLeft(2,'0')}:{min.ToString().PadLeft(2, '0')}:{sce.ToString().PadLeft(2, '0')}";
                        });
                        System.Threading.Thread.Sleep(1000);
                    }
                })
                { IsBackground = true };
            }
            refreshTimeThread.Start();
        }
 
        public override void RemoveFromParent()
        {
            if (refreshTimeThread != null)
            {
                refreshTimeThread.Abort();
            }
            base.RemoveFromParent();
        }
 
        #endregion
 
 
        private void SetControlTime()
        {
            var timeControl = new BottomTimeSelectControl();
            timeControl.InitControl(0,0);
            timeControl.FinishEvent = (type,hours,min) => {
                if (type == 1)//0:取消;1:确定
                {
                    HdlThreadLogic.Current.RunThread(() =>
                    {
                        var time = hours * 60 * 60 + min * 60;
                        var dic = new Dictionary<string, string>();
                        dic.Add(FunctionAttributeKey.TuyaWaterTime, time.ToString());
                        Control.Ins.SendWriteCommand(this.device, dic, true);
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            waterRunningTime = time;
                            this.btnControlTimeTip.Text = $"{hours.ToString().PadLeft(2, '0')}:{min.ToString().PadLeft(2, '0')}:00";
                        });
                    });
                }
            };
        }
    }
}