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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 搜索设备的界面
    /// </summary>
    public class SearchDeviceForm : UserCenterCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 新上报的设备
        /// </summary>
        private Dictionary<string, List<CommonDevice>> dicNewDevice = new Dictionary<string, List<CommonDevice>>();
        /// <summary>
        /// 线程是否已经开启
        /// </summary>
        private bool isThreadStart = false;
        /// <summary>
        /// 锁
        /// </summary>
        private object objLock = new object();
        /// <summary>
        /// 设备上报的主题标识
        /// </summary>
        private string deviceComingTopic = string.Empty;
        /// <summary>
        /// 等待设备的回馈的超时时间(单位:100毫秒)
        /// </summary>
        private int waitDeviceTimeOut = 30;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice));
 
            this.deviceComingTopic = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway) + "/" + "DeviceInComingRespon";
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            //显示配置中的文本信息
            this.ShowDeviceSettingMsg();
 
            new System.Threading.Thread(() =>
            {
                //蓝才刚说有时候网关会收不到入网的命令,所以发三次
                for (int i = 0; i < 3; i++)
                {
                    lock (objLock)
                    {
                        if (this.Parent == null)
                        {
                            return;
                        }
                        //让网关允许入网
                        GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(255);
                    }
                    System.Threading.Thread.Sleep(1000);
                }
            })
            { IsBackground = true }.Start();
 
            //添加监视设备新上报的事件
            GatewayResourse.NowSelectGateway.GwResDataAction += this.AdjustGatewayResultData;
        }
 
        /// <summary>
        /// 显示配置中的文本信息
        /// </summary>
        private void ShowDeviceSettingMsg()
        {
            //进度条一样的东西,没什么逻辑
            var line = new ProgressLine();
            bodyFrameLayout.AddChidren(line);
            line.SetValue(45);
 
            var frameMsg = new FrameLayout();
            frameMsg.Height = Application.GetRealHeight(200);
            frameMsg.Y = Application.GetRealHeight(500);
            frameMsg.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(frameMsg);
 
            //正在发现设备···
            var button1 = new MsgViewControl(frameMsg.Width, Application.GetRealHeight(100));
            button1.TextID = R.MyInternationalizationString.uDeviceSearching;
            frameMsg.AddChidren(button1);
 
            //请稍候
            var button2 = new MsgViewControl(frameMsg.Width, Application.GetRealHeight(100));
            button2.TextID = R.MyInternationalizationString.uPleaseWait;
            button2.Y = button1.Bottom;
            frameMsg.AddChidren(button2);
 
            //停止配置
            var btnStop = new BottomClickButton();
            btnStop.TextID = R.MyInternationalizationString.uBreakSettion;
            bodyFrameLayout.AddChidren(btnStop);
            btnStop.MouseUpEventHandler += (sender, e) =>
            {
                this.CloseForm();
            };
        }
 
        #endregion
 
        #region ■ 新设备入网接收_____________________
 
        /// <summary>
        /// 处理网关主题上报
        /// </summary>
        /// <param name="topic">主题</param>
        /// <param name="resultData">上报数据</param>
        private void AdjustGatewayResultData(string topic, string resultData)
        {
            if (topic != this.deviceComingTopic)
            {
                //不是设备上报主题
                return;
            }
            lock (objLock)
            {
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                CommonDevice.DeviceInfoData info = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceInfoData>(jobject["Data"].ToString());
                if (info.DriveCode != 0)
                {
                    //不需要虚拟设备
                    return;
                }
                //根据设备Type创建对应的设备对象
                var device = Common.LocalDevice.Current.NewDeviceObjectByDeviceId((DeviceType)jobject.Value<int>("Device_ID"));
                if (device == null)
                {
                    return;
                }
                //给新设备设置主键属性
                Common.LocalDevice.Current.SetNewDeviceMainKeys(device, jobject);
                device.CurrentGateWayId = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
                //将DeviceInfo的属性设置到主属性中
                Common.LocalDevice.Current.SetDeviceInfoToMain(device, device);
 
                //添加设备的缓存(Ota的话,重置镜像类型)
                Common.LocalDevice.Current.AddDeviceToMemory(ref device, true);
                device.IsOnline = 1;
 
                if (this.dicNewDevice.ContainsKey(device.DeviceAddr) == false)
                {
                    this.dicNewDevice[device.DeviceAddr] = new List<CommonDevice>();
                }
 
                //刷新超时时间
                this.waitDeviceTimeOut = 30;
 
                if ((device is OTADevice) == false)
                {
                    //不需要200端点的那个设备
                    this.dicNewDevice[device.DeviceAddr].Add(device);
 
                    //有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
                    this.StartShowDeviceMacInfoAddFormThread();
                }
            }
        }
 
        #endregion
 
        #region ■ 显示设备信息画面___________________
 
        /// <summary>
        /// 开启显示设备信息界面的线程
        /// </summary>
        private void StartShowDeviceMacInfoAddFormThread()
        {
            if (this.isThreadStart == true)
            {
                //线程已经开启
                return;
            }
            this.isThreadStart = true;
 
            new System.Threading.Thread(() =>
            {
                while (this.waitDeviceTimeOut >= 0)
                {
                    System.Threading.Thread.Sleep(100);
                    this.waitDeviceTimeOut--;
                }
                lock (objLock)
                {
                    //停止接收
                    GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData;
                    foreach (var listDevice in this.dicNewDevice.Values)
                    {
                        if (listDevice[0].ModelIdentifier == string.Empty)
                        {
                            //没有模块ID的话,再等等呗
                            System.Threading.Thread.Sleep(1500);
                        }
                        //重新变更UI
                        foreach (var device in listDevice)
                        {
                            var ui = Common.LocalDevice.Current.GetDeviceUI(device);
                            ui.IconPath = string.Empty;
                            ui.ReSave();
                        }
                        //目前就弄一个
                        Application.RunOnMainThread(() =>
                        {
                            //显示设备信息画面
                            this.ShowDeviceMacInfoAddForm(listDevice);
                        });
                        break;
                    }
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 显示设备信息画面
        /// </summary>
        /// <param name="listMacDevice">新设备列表</param>
        private void ShowDeviceMacInfoAddForm(List<CommonDevice> listMacDevice)
        {
            if (this.Parent == null)
            {
                return;
            }
            //关闭自身
            this.CloseForm();
            //再关闭设备入网指导界面
            this.CloseFormByFormName("AddDeviceDirectionForm");
            //添加设备
            var form = new DeviceMacInfoAddForm();
            this.AddForm(form, listMacDevice);
        }
 
        #endregion
 
        #region ■ 画面关闭___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        /// <param name="isCloseForm">是否关闭界面,false的时候,只会调用关闭函数里面的附加功能</param>
        public override void CloseForm(bool isCloseForm = true)
        {
            base.CloseForm(isCloseForm);
 
            lock (objLock)
            {
                //关闭入网模式
                GatewayResourse.NowSelectGateway.AddNewDeviceToGateway(0);
                //停止接收
                GatewayResourse.NowSelectGateway.GwResDataAction -= this.AdjustGatewayResultData;
            }
 
        }
 
        #endregion
    }
}