gxc
2019-10-29 081ea8d273048fd03756718ac6fb48a3c09218e9
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.GatewayAdd
{
    /// <summary>
    /// 搜索无线网关的界面
    /// </summary>
    public class WirelessGatewaySearchForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 动画线程
        /// </summary>
        private bool animateThread = false;
        /// <summary>
        /// 超时时间(500毫秒每单位)
        /// </summary>
        private int timeOut = 360;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddWirelessGateway));
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            //图标
            var btnGwImage = new PicViewControl(248, 230, true);
            btnGwImage.X = Application.GetRealWidth(107);
            btnGwImage.Y = Application.GetRealHeight(507);
            btnGwImage.UnSelectedImagePath = "Gateway/WirelessGatewayImage.png";
            bodyFrameLayout.AddChidren(btnGwImage);
 
            //图标
            var btnPhoneImage = new PicViewControl(236, 478, true);
            btnPhoneImage.X = Application.GetRealWidth(737);
            btnPhoneImage.Y = Application.GetRealHeight(438);
            btnPhoneImage.UnSelectedImagePath = "Gateway/PhoneImage.png";
            bodyFrameLayout.AddChidren(btnPhoneImage);
 
            //智能网关正在配置网络中
            var btnMsg1 = new NormalViewControl(bodyFrameLayout.Width, false);
            btnMsg1.TextID = R.MyInternationalizationString.uGatewaySearching;
            btnMsg1.Y = Application.GetRealHeight(1036);
            btnMsg1.TextAlignment = TextAlignment.Center;
            bodyFrameLayout.AddChidren(btnMsg1);
            //请稍候…
            var btnMsg2 = new NormalViewControl(bodyFrameLayout.Width, false);
            btnMsg2.TextID = R.MyInternationalizationString.uPleaseWait;
            btnMsg2.TextAlignment = TextAlignment.Center;
            btnMsg2.Y = btnMsg1.Bottom;
            bodyFrameLayout.AddChidren(btnMsg2);
 
            //初始化连接的假想动画效果
            this.InitConcetionAnimete();
        }
 
        #endregion
 
        #region ■ 假想动画___________________________
 
        /// <summary>
        /// 初始化连接的假想动画效果
        /// </summary>
        private void InitConcetionAnimete()
        {
            var frame = new FrameLayout();
            frame.Width = Application.GetRealWidth(294);
            frame.Height = Application.GetRealHeight(30);
            frame.X = Application.GetRealWidth(403);
            frame.Y = Application.GetRealHeight(650 + 30 / 2);
            bodyFrameLayout.AddChidren(frame);
 
            //一格的宽度
            int width = Application.GetRealWidth(22);
            //间距大小
            int space = Application.GetRealWidth(9);
 
            //首格只是半格
            var btnFirstPoint = new NormalViewControl(width / 2, 3, false);
            btnFirstPoint.Gravity = Gravity.CenterVertical;
            btnFirstPoint.BackgroundColor = UserCenterColor.Current.ButtomLine;
            frame.AddChidren(btnFirstPoint);
 
            NormalViewControl btnTemp = btnFirstPoint;
            //生成格子
            var listPoint = new List<int>();
            for (int i = 0; i < 8; i++)
            {
                var btnPoint = new NormalViewControl(width, 3, false);
                btnPoint.BackgroundColor = UserCenterColor.Current.ButtomLine;
                btnPoint.Gravity = Gravity.CenterVertical;
                btnPoint.X = btnTemp.Right + space;
                frame.AddChidren(btnPoint);
 
                listPoint.Add(btnPoint.X);
                btnTemp = btnPoint;
            }
 
            //尾格只是半格
            var btnLastPoint = new NormalViewControl(width / 2, 3, false);
            btnLastPoint.Gravity = Gravity.CenterVertical;
            btnLastPoint.X = btnTemp.Right + space;
            btnLastPoint.BackgroundColor = UserCenterColor.Current.ButtomLine;
            frame.AddChidren(btnLastPoint);
 
            btnTemp = null;
 
            //开启连接的假想动画效果线程
            this.StartConcetionAnimeteThread(frame, listPoint, width);
        }
 
        /// <summary>
        /// 开启连接的假想动画效果线程
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="listPoint"></param>
        /// <param name="roundWidth"></param>
        private void StartConcetionAnimeteThread(FrameLayout frame, List<int> listPoint, int roundWidth)
        {
            var btnRound = new PicViewControl(roundWidth, roundWidth, false);
            btnRound.Radius = (uint)roundWidth / 2;
            btnRound.Gravity = Gravity.CenterVertical;
            btnRound.BackgroundColor = UserCenterColor.Current.ConcetionRoundColor;
            btnRound.X = listPoint[0];
            frame.AddChidren(btnRound);
 
            animateThread = true;
            HdlThreadLogic.Current.RunThread(() =>
            {
                int index = 1;
                int timeCount = 0;
                while (animateThread == true && this.Parent != null)
                {
                    System.Threading.Thread.Sleep(500);
                    timeOut--;
                    if (timeCount == 10)
                    {
                        //5秒后清空网关列表
                        HdlGatewayLogic.Current.ClearAllRealGateway();
                        ZigBee.Common.Application.IsSearchingGateway = true;
                        //检测搜索到的网关
                        this.CheckZbGatewayAndSetRow();
                    }
                    timeCount++;
 
                    Application.RunOnMainThread(() =>
                    {
                        if (btnRound != null)
                        {
                            btnRound.X = listPoint[index];
                            index++;
                            if (index == listPoint.Count)
                            {
                                index = 0;
                            }
                        }
                    });
                }
            });
        }
 
        #endregion
 
        #region ■ 网关检测___________________________
 
        /// <summary>
        /// 检测搜索到的网关
        /// </summary>
        private void CheckZbGatewayAndSetRow()
        {
            HdlThreadLogic.Current.RunThread(async () =>
            {
                while (animateThread == true && this.Parent != null)
                {
                    await Task.Delay(1000);
                    if (timeOut < 0)
                    {
                        this.animateThread = false;
                        //先随便乱写点东西好了
                        string msg = "没有发现网关,建议使用XXX链接方式";
                        this.ShowMassage(ShowMsgType.Tip, msg);
                        break;
                    }
 
                    var listCheck = new HashSet<string>();
                    for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
                    {
                        var realWay = ZbGateway.GateWayList[i];
                        string gwId = HdlGatewayLogic.Current.GetGatewayId(realWay);
 
                        if (listCheck.Contains(gwId) == true
                            || HdlGatewayLogic.Current.IsGatewayExist(realWay) == true
                            || HdlGatewayLogic.Current.GetGatewayBaseInfoAttribute(realWay, "IpAddress").ToString() == string.Empty)
                        {
                            //如果处理成功了,则不再处理
                            //本地如果已经有这个网关了,则不再处理
                            //IP没有的网关,我也不知道它是干嘛的
                            continue;
                        }
                        //网关绑定模式
                        var mode = GatewayBindMode.BindAgain;
                        if (realWay.getGatewayBaseInfo.HomeId == Common.Config.Instance.HomeId)
                        {
                            //已经绑定过了
                            mode = GatewayBindMode.Binded;
                        }
                        else if (HdlGatewayLogic.Current.HomeIdIsEmpty(realWay) == true)
                        {
                            //第一次绑定,也就是网关住宅ID为空
                            mode = GatewayBindMode.First;
                        }
                        if (mode != GatewayBindMode.Binded)
                        {
                            //发现新网关,正在处理中,请稍后
                            string msg = Language.StringByID(R.MyInternationalizationString.uFindNewGatewayPleaseWaitToBinding);
                            this.ShowProgressBar(msg);
                        }
                        //执行绑定网关操作(无条件直接绑定,哪怕是住宅ID不一样)
                        var result = await this.DoSaveGateway(realWay, ShowErrorMode.YES, mode);
                        if (mode != GatewayBindMode.Binded)
                        {
                            //关闭进度条
                            this.CloseProgressBar();
                        }
                        //虽然出错了,但是可以无限处理
                        if (result == -1) { continue; }
                        //记录处理成功的网关
                        listCheck.Add(gwId);
 
                        //网关绑定在当前账号下的其他住宅里\r\n请解除绑定后再试(IP地址)
                        if (result == 0) { continue; }
                        //我觉得这里需要获取一下新网关的设备列表
                        Common.LocalDevice.Current.SetDeviceToMemmoryByGateway(realWay);
 
                        Application.RunOnMainThread(() =>
                        {
                            if (this.Parent != null)
                            {
                                //搜索得到无线网关
                                if (mode != GatewayBindMode.Binded)
                                {
                                    //var form = new GatewayInfoAddForm();
                                    //this.AddFromAndRemoveNowForm(form, realWay);
                                }
                            }
                        });
                        //搜索得到无线网关,则中断线程
                        if (mode != GatewayBindMode.Binded)
                        {
                            this.animateThread = false;
                            return;
                        }
                    }
                }
            });
        }
 
        #endregion
 
        #region ■ 执行网关保存_______________________
 
        /// <summary>
        /// 执行网关保存(1:正常  -1:异常  0:当前的网关绑定在了当前账号下的不同住宅里面)
        /// </summary>
        /// <param name="zbGateway">网关对象</param>
        /// <param name="mode">是否显示错误</param>
        /// <param name="bindMode">绑定方式</param>
        /// <returns></returns>
        private async Task<int> DoSaveGateway(ZbGateway zbGateway, ShowErrorMode mode, GatewayBindMode bindMode)
        {
            //添加搜索到的网关到缓存
            //1:正常  -1:异常  0:当前的网关绑定在了当前账号下的不同住宅里面
            int result = 0;
            if (bindMode == GatewayBindMode.BindAgain)
            {
                result = await HdlGatewayLogic.Current.ReBindNewGateway(zbGateway);
            }
            else
            {
                result = await HdlGatewayLogic.Current.AddNewGateway(zbGateway, mode);
            }
            if (result == -1)
            {
                return result;
            }
 
            //前的网关绑定在了当前账号下的不同住宅里面
            if (result == 0)
            {
                //网关绑定在当前账号下的其他住宅里\r\n请解除绑定后再试(IP地址)
                string msg = Language.StringByID(R.MyInternationalizationString.uTheGatewayInOtherResidenceMsg);
                if (msg.Contains("{0}") == true)
                {
                    msg = string.Format(msg, "\r\n") + "(" + HdlGatewayLogic.Current.GetGatewayBaseInfoAttribute(zbGateway, "IpAddress").ToString() + ")";
                }
                this.ShowMassage(ShowMsgType.Tip, msg);
            }
            return result;
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseForm()
        {
            this.animateThread = false;
            ZigBee.Common.Application.IsSearchingGateway = false;
            //停止Wi-Fi配网
            //Com.Mediatek.Elian.ElianNative.StopSmartConnection();
 
            if (UserCenterResourse.listActionFormId.Contains("GatewayManagementForm") == false)
            {
                //刷新主页
                UserView.UserPage.Instance.Fresh();
            }
            base.CloseForm();
        }
 
        #endregion
    }
}