JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using System.Collections.Generic;
using Shared.SimpleControl;
 
namespace Shared.SimpleControl.Phone
{
    public class ThermostatExtensionPage : FrameLayout
    {
        public static ThermostatExtensionPage curView;
        static bool Display_Temp = true;
 
        static Thermostat thermostat;
        static System.Threading.Thread updateThread;
        static ArcSeekBar mArcSeekBar;
        static Button btnValues;
        static Button btnText;
        static FrameLayout hideView;
        static Button btnPower;
        static Button btnModeIcon;
        public static void UpdateStatus (Thermostat therm)
        {
            Application.RunOnMainThread (() => {
            if (curView == null)
                return;
                if (therm.SubnetID == thermostat.SubnetID && therm.DeviceID == thermostat.DeviceID && therm.ExtensionID == thermostat.ExtensionID) {
                    try {
                        thermostat = therm;
                        btnPower.IsSelected = thermostat.Power == 1 ? true : false;
                        hideView.Visible = !btnPower.IsSelected;
                        if (!Display_Temp) {
                            btnValues.Text = (int)thermostat.Indoor_Humidity_Values + "%";
                            if (Language.CurrentLanguage == "Chinese")
                                btnText.Text = "实时湿度";
                            else
                                btnText.Text = "Real-time humidity";
                        } else {
                            btnValues.Text = Math.Round (thermostat.Indoor_Temperature_Values, 1) + "°C";
                            if (Language.CurrentLanguage == "Chinese")
                                btnText.Text = "实时温度";
                            else
                                btnText.Text = "Real-time temp.";
                        }
                        if (thermostat.Setting_Temperature_Values > 38) {
                            mArcSeekBar.Progress = 38;
                        } else if (thermostat.Setting_Temperature_Values < 16) {
                            mArcSeekBar.Progress = 16;
                        } else {
                            mArcSeekBar.Progress = (int)thermostat.Setting_Temperature_Values;
                        }
                    } catch { }
                }
            });
        }
        static bool stopChangeFace = false;
 
 
        public static void UptateMode (Thermostat cac)
        {
            Application.RunOnMainThread (() => {
                if (curView == null)
                    return;
                if (cac.SubnetID == thermostat.SubnetID && cac.DeviceID == thermostat.DeviceID && cac.HostID == thermostat.HostID) {
                    try {
                        thermostat.CurrentMode = cac.CurrentMode;
                        UptateMode ();
                    } catch { }
                }
            });
        }
 
 
        /// <summary>
        /// 更新运行模式
        /// </summary>
        static void UptateMode ()
        {
            /// 3   运行模式    1-制冷,2-制热,3-通风,4-除湿,5-制热除湿
            switch (thermostat.CurrentMode) {
            case 1:
                btnModeIcon.UnSelectedImagePath = "CapillaryAC/CoolingOn.png";
                btnModeIcon.SelectedImagePath = "CapillaryAC/CoolingOn.png";
                break;
            case 2:
                btnModeIcon.UnSelectedImagePath = "CapillaryAC/HeatingOn.png";
                btnModeIcon.SelectedImagePath = "CapillaryAC/HeatingOn.png";
                break;
            case 3:
                btnModeIcon.UnSelectedImagePath = "CapillaryAC/AerationOn.png";
                btnModeIcon.SelectedImagePath = "CapillaryAC/AerationOn.png";
                break;
            case 4:
                btnModeIcon.UnSelectedImagePath = "CapillaryAC/DehumidificationOn.png";
                btnModeIcon.SelectedImagePath = "CapillaryAC/DehumidificationOn.png";
                break;
            case 5:
                btnModeIcon.UnSelectedImagePath = "CapillaryAC/HeatingAndDehumidificationOn.png";
                btnModeIcon.SelectedImagePath = "CapillaryAC/HeatingAndDehumidificationOn.png";
                break;
            }
            btnModeIcon.IsSelected = !btnModeIcon.IsSelected;
        }
 
        public override void RemoveFromParent ()
        {
            curView = null;
            if (updateThread != null) {
                updateThread.Abort ();
            }
            base.RemoveFromParent ();
        }
 
        public void ShowPage (Thermostat cac)
        {
            thermostat = cac;
            curView = this;
            UserMiddle.FavoritePageView.ScrollEnabled = false;
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (640),
                BackgroundColor = SkinStyle.Current.MainColor,
            };
            AddChidren (topView);
 
            var btnTitle = new Button () {
                TextAlignment = TextAlignment.Center,
                Text = thermostat.Name,
                TextColor = SkinStyle.Current.TextColor1,
                TextSize = 19,
            };
            topView.AddChidren (btnTitle);
 
            var logo = new Button () {
                Width = Application.GetRealWidth (154),
                Height = Application.GetRealHeight (90),
                X = Application.GetRealWidth (486),
                UnSelectedImagePath = MainPage.LogoString,
            };
            topView.AddChidren (logo);
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                (Parent as PageLayout).PageIndex -= 1;
               
            };
            #endregion
 
            mArcSeekBar = new ArcSeekBar {
                Y = Application.GetRealHeight (150),
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth (580),
                Height = Application.GetRealHeight (580),
                ThumbModeFill = true,
                ProgressBarColor = 0xFFF1AC54,
                ArcWidthWithDefaultStyle = 20,
                MinValue = 16,
                MaxValue = 38,
                Progress = (int)thermostat.Setting_Temperature_Values,
                ProgressBarUnitSring = "°C",
                IsClickable = true
            };
            AddChidren (mArcSeekBar);
 
            mArcSeekBar.SetProgressBarColors (0xFFFFE6BD, 0xFFFFAF4F);
 
            btnModeIcon = new Button () {
                Y = mArcSeekBar.Y + Application.GetRealHeight(120),
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetMinRealAverage(100),
                Height = Application.GetMinRealAverage (100),
                UnSelectedImagePath = "CapillaryAC/HeatingOn.png",
                SelectedImagePath = "CapillaryAC/HeatingOn.png"
            };
            AddChidren (btnModeIcon);
            UptateMode ();
 
            btnValues = new Button () {
                Y = btnModeIcon.Bottom,
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth (200),
                Height = Application.GetRealHeight (150),
                TextSize = 34,
                Text = Math.Round (thermostat.Indoor_Temperature_Values ,1)+ "°C",
            };
            AddChidren (btnValues);
 
            btnText = new Button () { 
                Y = btnValues.Bottom,
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth (200),
                Height = Application.GetRealHeight(50),
                Radius = (uint)Application.GetRealHeight(20),
                BackgroundColor = 0xFFFFAF4F,
                Text = "实时温度"
            };
            AddChidren (btnText);
            if (Language.CurrentLanguage == "Chinese")
                btnText.Text = "实时温度";
            else
                btnText.Text = "Real-time temp.";
 
            hideView = new FrameLayout () {
                Y = topView.Bottom,
                BackgroundColor = 0x4F000000,
                Visible = thermostat.Power == (byte)1 ? false : true
            };
            AddChidren (hideView);
 
            btnPower = new Button () {
                Y = mArcSeekBar.Bottom + Application.GetRealHeight (50),
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetMinRealAverage (95),
                Height = Application.GetMinRealAverage (100),
                UnSelectedImagePath = "CapillaryAC/In_Switch.png",
                SelectedImagePath = "CapillaryAC/In_Switch_On.png",
                IsSelected = thermostat.Power == (byte)1 ? true : false
            };
            AddChidren (btnPower);
 
        
 
            btnPower.MouseUpEventHandler += (sender, e) => {
                btnPower.IsSelected = !btnPower.IsSelected;
                thermostat.Power = btnPower.IsSelected == true ? (byte)1 : (byte)0;
                new System.Threading.Thread (() => {
                    thermostat.SetExtension ();
                }) { IsBackground = true }.Start ();
            };
            DateTime changeTime = DateTime.Now;
            updateThread = new System.Threading.Thread (() => {
                while (true) {
                    if (changeTime.AddSeconds (5) < DateTime.Now) {
                        changeTime = DateTime.Now;
                        Application.RunOnMainThread (() => {
                            if (!stopChangeFace) {
                                Display_Temp = !Display_Temp;
                                if (!Display_Temp) {
                                    btnValues.Text =Math.Round( thermostat.Indoor_Humidity_Values,1) + "%";
                                    if (Language.CurrentLanguage == "Chinese")
                                        btnText.Text = "实时湿度";
                                    else
                                        btnText.Text = "Real-time humidity";
                                } else {
                                    btnValues.Text = Math.Round (thermostat.Indoor_Temperature_Values, 1) + "°C";
                                    if (Language.CurrentLanguage == "Chinese")
                                        btnText.Text = "实时温度";
                                    else
                                        btnText.Text = "Real-time temp.";
                                }
                            }
                        });
                        System.Threading.Thread.Sleep (5000);
                    }
                }
            }) { IsBackground = true };
            updateThread.Start ();
 
            Control.ControlBytesSend (Command.ReadThermostatExtension, thermostat.SubnetID, thermostat.DeviceID, new byte [] { thermostat.ExtensionID });
 
            EventHandler<MouseEventArgs> ChangeDisplay = (button, mouseEventArgs) => {
                if (!Display_Temp) {
                    btnValues.Text = Math.Round (thermostat.Indoor_Humidity_Values, 1) + "%";
                    if (Language.CurrentLanguage == "Chinese")
                        btnText.Text = "实时湿度";
                    else
                        btnText.Text = "Real-time humidity";
                } else {
                    btnValues.Text = Math.Round (thermostat.Indoor_Temperature_Values, 1) + "°C";
                    if (Language.CurrentLanguage == "Chinese")
                        btnText.Text = "实时温度";
                    else
                        btnText.Text = "Real-time temp."; 
                }
                Display_Temp = !Display_Temp; 
            };
            btnText.MouseUpEventHandler += ChangeDisplay;
            btnValues.MouseUpEventHandler += ChangeDisplay;
            btnModeIcon.MouseUpEventHandler += ChangeDisplay;
            mArcSeekBar.OnStartTrackingTouchEvent += (sender, e) => {
                stopChangeFace = true;
                UserMiddle.FavoritePageView.ScrollEnabled = false;
                UserMiddle.DevicePageView.ScrollEnabled = false;
            };
            mArcSeekBar.OnStopTrackingTouchEvent += (sender, e) => {
                UserMiddle.FavoritePageView.ScrollEnabled = true;
                UserMiddle.DevicePageView.ScrollEnabled = true;
                if (stopChangeFace) {
                    var sendBytes = BitConverter.GetBytes (Convert.ToSingle (mArcSeekBar.Progress));
                    //if (Display_Temp) {
                        thermostat.Setting_Temperature_Values1 = sendBytes [3];
                        thermostat.Setting_Temperature_Values2 = sendBytes [2];
                        thermostat.Setting_Temperature_Values3 = sendBytes [1];
                        thermostat.Setting_Temperature_Values4 = sendBytes [0];
                    //} else {
                    //    capillaryAC.Setting_Humidity_Values1 = sendBytes [3];
                    //    capillaryAC.Setting_Humidity_Values2 = sendBytes [2];
                    //    capillaryAC.Setting_Humidity_Values3 = sendBytes [1];
                    //    capillaryAC.Setting_Humidity_Values4 = sendBytes [0];
                    //}
                    new System.Threading.Thread (() => {
                        thermostat.SetExtension ();
                    }) { IsBackground = true }.Start ();
                    stopChangeFace = false;
                }
            };
 
 
        }    
    }
}