wxr
2022-06-27 2f17a7041e7fbc57b945ed10afa910900afff3b9
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
using System;
using System.Collections.Generic;
using Shared;
namespace Shared.SimpleControl.Phone
{
    public class ManualUpgradeDialog : FrameLayout
    {
        static ManualUpgradeDialog bodyView;
 
        int result = -99;
        byte subnetId = 0;
        public ManualUpgradeDialog ()
        {
            bodyView = this;
        }
 
 
        public void ShowDialog ()
        {
 
            #region 弹窗
            Dialog dialog = new Dialog ();
 
            FrameLayout dialogBodyView = new FrameLayout () {
                Gravity = Gravity.Center,
                Width = Application.GetRealWidth (500),
                Height = Application.GetRealHeight (500),
                BackgroundColor = SkinStyle.Current.DialogColor,
                Radius = 5,
                BorderColor = SkinStyle.Current.Transparent,
                BorderWidth = 0,
            };
            dialog.AddChidren (dialogBodyView);
 
            Button btnTitle = new Button () {
                Height = Application.GetRealHeight (80),
                BackgroundColor = SkinStyle.Current.DialogTitle,
                TextAlignment = TextAlignment.Center,
                TextID = R.MyInternationalizationString.Tip,
                TextColor = SkinStyle.Current.DialogTextColor
            };
            dialogBodyView.AddChidren (btnTitle);
 
            var contentView = new FrameLayout () {
                Y = Application.GetRealHeight (80),
                Height = Application.GetRealHeight (340),
                BackgroundColor = SkinStyle.Current.DialogColor,
            };
            dialogBodyView.AddChidren (contentView);
 
            var btnTipMsg = new Button () {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight (20),
                Width = Application.GetRealWidth (400),
                Height = Application.GetRealHeight (80),
                Text = "Waiting for gateway upgrade",
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = SkinStyle.Current.TextColor,
            };
            contentView.AddChidren (btnTipMsg);
 
 
            //var btnTipMsg = new Button () {
            //    Gravity = Gravity.CenterHorizontal,
            //    Y = btnTipTitle.Bottom,
            //    Width = Application.GetRealWidth (400),
            //    Height = Application.GetRealHeight (80),
            //    TextAlignment = TextAlignment.CenterLeft,
            //    TextColor = SkinStyle.Current.DelColor,
            //};
            //contentView.AddChidren (btnTipMsg);
 
            FrameLayout bottomView = new FrameLayout () {
                Y = Application.GetRealHeight (420),
                Height = Application.GetRealHeight (85),
                BackgroundColor = SkinStyle.Current.DialogTitle
            };
            dialogBodyView.AddChidren (bottomView);
 
            Button btnClose = new Button () {
                TextID = R.MyInternationalizationString.Close,
                TextAlignment = TextAlignment.Center
            };
            bottomView.AddChidren (btnClose);
            btnClose.MouseUpEventHandler += (send2er, e2) => {
                dialog.Close ();
            };
 
            Button btnBottomLine = new Button () {
                X = btnClose.Right,
                Width = 1,
                BackgroundColor = SkinStyle.Current.Black50Transparent,
            };
            bottomView.AddChidren (btnBottomLine);
 
            
            dialog.Show ();
 
 
            new System.Threading.Thread (() => {
 
                List<List<byte>> upgradeData = ReadUpgradeData ();
                byte [] arrayTemp = new byte [4];
                arrayTemp [0] = Convert.ToByte ((upgradeData.Count & 0xFF000000) >> 24);
                arrayTemp [1] = Convert.ToByte ((upgradeData.Count & 0xFF0000) >> 16);
                arrayTemp [2] = Convert.ToByte ((upgradeData.Count & 0xFF00) >> 8);
                arrayTemp [3] = Convert.ToByte (upgradeData.Count & 0xFF);
 
 
                while (true) {
                    var ub = MainPage.GatewayStatus.Split ("_");
                    if (ub.Length > 1) {
                        result = Convert.ToInt32 (ub [1]);
                        {
                            Application.RunOnMainThread (() => {
                                btnTipMsg.Text = "Upgrading gateway " + result + "/" + upgradeData.Count;
                            });
                        }
                    }
                    if (ub.Length > 3) {
                        byte.TryParse (ub [2], out subnetId);
                    }
                    if (MainPage.GatewayStatus.Contains ("upgrading") && result == 0) {
                        SendUpgradeData (subnetId, 0, arrayTemp);
                        Application.RunOnMainThread (() => {
                            btnTipMsg.Text = "Upgrading gateway " + result + "/" + upgradeData.Count;
                        });
                    } else if (result == -99) {
                        System.Threading.Thread.Sleep (100);
                        continue;
                    } else if (result == 100) {
                        Application.RunOnMainThread (() => {
                            btnTipMsg.Text = "Gateway upgrade succeeded. Initializing gateway.";
                            btnTipMsg.TextColor = SkinStyle.Current.TextColor;
                            MainPage.GatewayStatus = "";
                        });
                        break;
                    } else {
                        if (upgradeData.Count >= result) {
                            var listPack = upgradeData [result - 1];//
                            byte [] packData = new byte [2 + listPack.Count];
                            packData [0] = Convert.ToByte (result / 256);
                            packData [1] = Convert.ToByte (result % 256);
                            Array.Copy (listPack.ToArray (), 0, packData, 2, listPack.Count);
                            Console.WriteLine ("packId" + result);
                            SendUpgradeData (subnetId, 0, packData);
 
                        }
                    }
                }
 
 
            }) { IsBackground = true }.Start ();
 
            #endregion
 
        }
        /// <summary>
        /// 读取固件数据
        /// </summary>
        /// <returns></returns>
        private static List<List<byte>> ReadUpgradeData ()
        {
            byte [] buffer = new byte [1024];
            List<List<byte>> upgradeData = new List<List<byte>> ();
            System.IO.Stream stream = Application.Activity.Assets.Open ("ind_C03.02U_2022-06-22.bin");
            int length = 0;
            try {
                while ((length = stream.Read (buffer, 0, buffer.Length)) != 0) {
                    List<byte> bbb = new List<byte> ();
                    for (int i = 0; i < length; i++) {
                        bbb.Add (buffer [i]);
                    }
                    upgradeData.Add (bbb);
                }
            } catch {
            } finally {
                stream.Close ();
            }
 
            return upgradeData;
        }
 
 
        /// <summary>
        /// 发送网关固件数据
        /// </summary>
        /// <param name="subnetId"></param>
        /// <param name="deviceId"></param>
        /// <param name="sendByets"></param>
        /// <returns></returns>
        public int SendUpgradeData (byte subnetId, byte deviceId, byte [] sendByets)
        {
            var resutl = Control.ControlBytesSendHasReturn (Command.enjoyUpgrade2, subnetId, deviceId, sendByets);
            if (resutl == null) {
                return -2;
            }
            if (resutl.Length == 0) {
                return -1;
            }
            if (resutl [0] == 0xF8) {
                return 999999;
            } else {
                if (resutl.Length > 1) {
                    var packId = resutl [0] * 256 + resutl [1];
                    Console.WriteLine ("packId:" + packId);
                    return packId;
                }
                return 1;
            }
        }
 
 
    }
}