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
using System;
using System.Collections.Generic;
 
namespace Shared.SimpleControl.Phone
{
    public class SystemACPanel : FrameLayout
    {
        /// <summary>
        /// 显示空调面板界面
        /// </summary>
        /// <returns>The system ACP anel.</returns>
        /// <param name="acPanel">Ac panel.</param>
        /// <param name="bp">Bp.</param>
        public void ShowSystemACPanel (ACPanel acPanel,ButtonPanel bp)
        {
            //SystemMiddle.hideBottom ();
            RemoveAll ();
 
            #region  top
            RowLayout frameLayout = new RowLayout () {
                Height = Application.GetRealHeight (100),
                Width = LayoutParams.MatchParent,
                BackgroundColor = SkinStyle.Current.MainColor
            };
            AddChidren (frameLayout);
 
            Button backButton = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
                Gravity = Gravity.CenterVertical,
            };
            backButton.MouseUpEventHandler+= (sender, e) => {
                (Parent as PageLayout).PageIndex -= 1;
                //SystemEquipmentBase.RefreshView (bp);
            };
            frameLayout.AddChidren (backButton);
 
            EditText textButton = new EditText () {
                X = Application.GetRealWidth (30) + backButton.Right,
                Height = Application.GetRealHeight (50),
                Width = Application.GetRealWidth (400),
                Text = acPanel.Name,
                Gravity = Gravity.CenterVertical,
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = SkinStyle.Current.TextColor1,
                SelectedBackgroundColor = SkinStyle.Current.SysEditBox,
                Enable = false,
                TextSize = 15,
                IsSelected = false,
 
            };
            frameLayout.AddChidren (textButton);
 
            Button editor = new Button () {
                X = Application.GetRealWidth (520),
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (70),
                UnSelectedImagePath = "Item/Editor.png",
                SelectedImagePath="Item/EditorSelected.png",
                Gravity = Gravity.CenterVertical,
            };
            frameLayout.AddChidren (editor);
            editor.MouseUpEventHandler += (sender, e) => {
                if (editor.IsSelected) {
                    editor.IsSelected = textButton.IsSelected = textButton.Enable = false;
                    MainPage.Loading.Start ();
                    byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (textButton.Text.Trim ());
                    System.Threading.Tasks.Task.Run (() => {
                        byte [] updateBytes = Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, acPanel.SubnetID, acPanel.DeviceID, new byte [] {
                            acPanel.BigClass,acPanel.MinClass,acPanel.LoopID});
                        if (updateBytes == null) {
                            Application.RunOnMainThread (() => {
                                new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                MainPage.Loading.Hide ();
                            });
                            return;
                        }
                        byte [] uBytes = new byte [20];
                        Array.Copy (remakeBytes, 0, uBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
                        Array.Copy (uBytes, 0, updateBytes, 3, 20 < uBytes.Length ? 20 : uBytes.Length);
                        var reBytes = Control.ControlBytesSendHasReturn (Command.SetDeviceLoopInfo, acPanel.SubnetID, acPanel.DeviceID, updateBytes);
                        if (reBytes != null) {
                            Application.RunOnMainThread (() => {
                                acPanel.Name = textButton.Text.Trim ();
                                IO.FileUtils.SaveEquipmentMessage (acPanel, acPanel.LoopID.ToString ());
                                MainPage.Loading.Hide ();
                            });
                        } else {
                            Application.RunOnMainThread (() => {
                                new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                MainPage.Loading.Hide ();
                            });
                        }
                    });
                } else {
                    textButton.Enable = textButton.IsSelected = editor.IsSelected = true;
                }
            };
            #endregion
            byte [] acBytes = null;
          
            Button btnOutput =new Button(){
                Width = Application.GetRealWidth (303),
                Height = Application.GetRealHeight (50),
                X = Application.GetRealWidth(60),
                Y = Application.GetRealHeight(100),
                TextID = R.MyInternationalizationString.ChooseAC,
                TextAlignment = TextAlignment.CenterLeft,
            };
            AddChidren(btnOutput);
            VerticalScrolViewLayout verticalScrolView = new VerticalScrolViewLayout { 
                Width = Application.GetRealWidth (508),
                Height = Application.GetRealHeight(700),
                Y = btnOutput.Bottom+ Application.GetRealHeight(5),
                Gravity = Gravity.CenterHorizontal,
                BackgroundColor = SkinStyle.Current.SysSubtitleView
            };
            AddChidren (verticalScrolView);
          
            List<string> willBeShowList = IO.FileUtils.ReadFiles().FindAll((fileName)=>{
                return fileName.Contains("Equipment_")&&(fileName.Contains(DeviceType.HVAC .ToString ())||fileName.Contains(DeviceType.ACInfrared.ToString () ) || fileName.Contains (DeviceType.ACInfrared.ToString ())  )&&(fileName.Split('_').Length == 5);
            });
 
            byte [] sendData = new byte [8];
            int num = 0;
            foreach (string equipment in willBeShowList)//显示在本地找到的所有设备
            {
                var v = IO.FileUtils.ReadFile (equipment);
                string data = CommonPage.MyEncodingUTF8.GetString (v);
                HVAC ac = Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (data);
                Button btnEquipment = new Button () {
                    Width = LayoutParams.MatchParent,
                    Height = Application.GetRealHeight (90),
                    TextAlignment = TextAlignment.Center,
                    Text = ac.Name,
                    Tag = ac,
                    BackgroundColor = SkinStyle.Current.ButtonColor,
                    SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
                    Gravity = Gravity.CenterHorizontal
                };
                verticalScrolView.AddChidren (btnEquipment);
 
                btnEquipment.Y = Application.GetRealHeight (10) + num * (btnEquipment.Height + Application.GetRealHeight(30));
                num++;
 
                btnEquipment.MouseUpEventHandler += (sender, e) => {
                    //if (btnEquipment.IsSelected) {
                    //    Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.CancelBinding), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                    //    alert.Show ();
                    //    alert.ResultEventHandler += (sender2, e2) => {
                    //        if (e2) {
                    //            sendData [0] = 1;
                    //            sendData [1] = 255;
                    //            sendData [2] = 255;
                    //            sendData [3] = 10;
                    //            sendData [4] = 1;
                    //            sendData [5] = 1;
                    //            sendData [6] = 1;
                    //            sendData [7] = 1;
                    //            System.Threading.Tasks.Task.Run (() => {
                    //                byte [] reFalg = Control.ControlBytesSendHasReturn (Command.SetACPanel, acPanel.SubnetID, acPanel.DeviceID, sendData);
                    //                Application.RunOnMainThread (() => {
                    //                    if (reFalg == null) {
                    //                        new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    //                    } else if (reFalg [0] == 0xF8) {
                    //                        new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.ThePerationWasSuccessful), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    //                    }
                    //                    this.ShowSystemACPanel (acPanel, bp);
                    //                });
                    //            });
                    //        }
                    //    };
                    //} else
                    {
                        Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureBindDevice), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                        alert.Show ();
                        alert.ResultEventHandler += (sender2, e2) => {
                            if (e2) {
                                HVAC h = (HVAC)btnEquipment.Tag;
                                if (acBytes.Length == 42) {
                                    acBytes [2] = h.SubnetID;
                                    acBytes [3] = h.DeviceID;
                                    acBytes [4] = h.LoopID;
                                    byte [] reFalg = Control.ControlBytesSendHasReturn (Command.SetACPanel, acPanel.SubnetID, acPanel.DeviceID, acBytes);
                                    if (reFalg == null) {
                                        new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                    }
                                    this.ShowSystemACPanel (acPanel, bp);
                                } else {
                                    sendData [0] = 1;
                                    sendData [1] = h.SubnetID;
                                    sendData [2] = h.DeviceID;
                                    sendData [3] = 10;
                                    sendData [4] = h.LoopID;
                                    sendData [5] = 1;
                                    sendData [6] = 1;
                                    sendData [7] = 1;
                                    System.Threading.Tasks.Task.Run (() => {
                                        byte [] reFalg = Control.ControlBytesSendHasReturn (Command.SetACPanel, acPanel.SubnetID, acPanel.DeviceID, sendData);
                                        Application.RunOnMainThread (() => {
                                            if (reFalg == null) {
                                                new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                            } else if (reFalg [0] == 0xF8) {
                                                new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.ThePerationWasSuccessful), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                            }
                                            this.ShowSystemACPanel (acPanel, bp);
                                        });
                                    });
                                }
                            }
                        };
                    }
                };
 
                Button btnLine = new Button () {
                    Width = LayoutParams.MatchParent,
                    Height = 1,
                    BackgroundColor = SkinStyle.Current.Black50Transparent
                };
                verticalScrolView.AddChidren (btnLine);
            }
 
            //--------------bottom--------------
            System.Threading.Tasks.Task.Run (() => {
                acBytes = Control.ControlBytesSendHasReturn (Command.ReadACPanel, acPanel.SubnetID, acPanel.DeviceID, new byte [] { });
                Application.RunOnMainThread (() => {
                    for (int i = 0; i < verticalScrolView.ChildrenCount; i++) {
                        if (verticalScrolView.GetChildren (i).GetType () == typeof (Button)) {
                            Button btn = (Button)verticalScrolView.GetChildren (i);
                            if (btn.Tag != null) {
                                var ac = (HVAC)btn.Tag;
                                if (acBytes != null) {
                                    if (acBytes [2] == ac.SubnetID && acBytes [3] == ac.DeviceID && acBytes [5] == ac.LoopID) {
                                        btn.IsSelected = true;
                                    }
                                } else { 
                                    acBytes = Control.ControlBytesSendHasReturn (Command.READ_AIR_ALL_Parameter, acPanel.SubnetID, acPanel.DeviceID, new byte [] { acPanel.LoopID});
                                    if (acBytes != null) {
                                        if (acBytes [2] == ac.SubnetID && acBytes [3] == ac.DeviceID && acBytes [4] == ac.LoopID) {
                                            btn.IsSelected = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            });
        }
    }
}