wxr
2021-03-08 8e5ba5665861b29a4b8da0335c81d620aa891862
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
using System;
using HDL_ON.DAL.Server;
using Shared;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace HDL_ON.UI.UI2.PersonalCenter.PirDevice
{
    public class Method
    {
        /// <summary>
        /// 添加遥控器的方法
        /// </summary>
        public void AddControl(FrameLayout frameLayout, Action<Control> action)
        {
            View.TipView tipView = new View.TipView();
            tipView.InputBox(frameLayout, "", (name, frame) =>
             {
                 ///清除之前列表数据
                 Pir.BuottonList.Clear();
                 if (!string.IsNullOrEmpty(name))
                 {
                     Control control = new Control();
                     control.name = name;
                     control.type = "learn";
                     control.spk = "ir.learn";
                     control.deviceId = Pir.currPir.deviceId;
                     ThreadAddControl(control, frame, action);
 
 
 
                 }
             }, false);
 
        }
        /// <summary>
        /// 发送遥控器命令方法
        /// </summary>
        /// <param name="control"></param>
        /// <param name="frame"></param>
        public void ThreadAddControl(Control control, FrameLayout frame, Action<Control> action)
        {
 
            DAL.Server.ResponsePackNew responsePackNew = null;
            Loading loading = new Loading();
            frame.AddChidren(loading);
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    responsePackNew = PirSend.Add(control);
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                        {
                            bool bool_if = false;
                            string sid = responsePackNew.Data.ToString();
                            var mqttdate = MqttDate();
                            for (int i = 0; i < mqttdate.objects.Count; i++) {
                                var objects = mqttdate.objects[i];
                                if (sid == objects.sid) {
                                    bool_if = true;
                                    break;
                                }
                            }
                            if (bool_if)
                            {
                                control.deviceId = mqttdate.id;
                                ///这里:监听MTTP推送下来主题,才知道是否添加成功
                                frame.RemoveFromParent();//添加成功关闭弹窗
                                action(control);
                            }
                            else
                            {
                                //监听Mqtt推送下来状态码做提示
                                View.FailView failView = new View.FailView();
                                failView.ShouError((view) =>
                                {
                                    view.Close();
                                    ThreadAddControl(control, frame, action);
                                });
                            }
 
 
                        }
                        else
                        {
                            ErrorShow(responsePackNew);
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
 
        }
 
        /// <summary>
        /// 获取红外设备列表
        /// </summary>
        public static void GetPirDeviceList(FrameLayout frame, Action action)
        {
            //清除之前列表;
            Pir.pirDeviceList.Clear();
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    responsePackNew = PirSend.GetDeviceList("ir.module");
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        try
                        {
                            if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                            {
                                var jobject = Newtonsoft.Json.Linq.JObject.Parse(responsePackNew.Data.ToString());
                                string list = jobject["list"].ToString();
 
                                var jArray = Newtonsoft.Json.Linq.JArray.Parse(list);
                                for (int a = 0; a < jArray.Count; a++)
                                {
                                    var jay = jArray[a];
                                    string spk = jay["spk"].ToString();
                                    if (spk == "ir.module")
                                    {
                                        //数据返序列化为Logic对象
                                        var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                        var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Pir>(str);
                                        if (pirJosn != null)
                                        {
                                            if (null == Pir.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
                                            {
 
                                                Pir.pirDeviceList.Add(pirJosn);
 
                                            }
                                        }
                                    }
 
                                }
                                GetControlList(frame, action);
 
                            }
                            else
                            {
                                Method method = new Method();
                                method.ErrorShow(responsePackNew);
                            }
                        }
                        catch { }
 
                    });
                }
 
            })
            { IsBackground = true }.Start();
 
        }
        /// <summary>
        /// 获取遥控器列表
        /// </summary>
        public static void GetControlList(FrameLayout frame, Action action)
        {
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    for (int i = 0; i < Pir.pirDeviceList.Count; i++)
                    {
                        var pirDevice = Pir.pirDeviceList[i];
                        try
                        {
                            responsePackNew = PirSend.ControlList(pirDevice);
 
                        }
                        catch { }
                        finally
                        {
                            Application.RunOnMainThread(() =>
                            {
                                try
                                {
                                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                                    {
                                        var jArray = Newtonsoft.Json.Linq.JArray.Parse(responsePackNew.Data.ToString());
                                        for (int a = 0; a < jArray.Count; a++)
                                        {
                                            var jay = jArray[a];
                                            //数据返序列化为Logic对象
                                            var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                            var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                                            if (pirJosn != null)
                                            {
                                                if (null == pirDevice.FunctioList.Find((c) => c.sid == pirJosn.sid))
                                                {
                                                    pirDevice.FunctioList.Add(pirJosn);
                                                }
                                            }
 
                                        }
                                    }
                                    else
                                    {
                                        //Method method = new Method();
                                        //method.ErrorShow(responsePackNew);
                                    }
                                }
                                catch { }
 
                            });
                        }
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        action();
                    });
                }
            })
            { IsBackground = true }.Start();
 
        }
 
        /// <summary>
        /// MQTT主题推送下来的数据(遥控器添加)
        /// </summary>
        public static string addcontronsid = "";
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Cloud MqttDate()
        {
            var dateTime = DateTime.Now;
            while ((DateTime.Now - dateTime).TotalMilliseconds < 5 * 1000)
            {
                if (!string.IsNullOrEmpty(addcontronsid))
                {
                    break;
                }
 
            }
            return PushData();
        }
 
        public Cloud PushData()
        {
            Cloud cloud = new Cloud();
            if (addcontronsid != "")
            {
                //var date = Newtonsoft.Json.JsonConvert.SerializeObject(addcontronsid);
                cloud = Newtonsoft.Json.JsonConvert.DeserializeObject<Cloud>(addcontronsid);
 
            }
            return cloud;
        }
 
 
 
        /// <summary>
        /// 错误码提示
        /// </summary>
        /// <param name="responsePackNew"></param>
        public void ErrorShow(ResponsePackNew responsePackNew)
        {
 
            if (responsePackNew != null && responsePackNew.Code == "14005")
            {
                new Intelligence.Automation.LogicView.TipPopView().FlashingBox(Language.StringByID(StringId.gatewayNotOnline));
 
            }
            else
            {
                new Intelligence.Automation.LogicView.TipPopView().FlashingBox(Language.StringByID(StringId.saveFail));
 
            }
        }
 
    }
    [Serializable]
    public class Cloud
    {
        /// <summary>
        /// 设备id
        /// </summary>
        public string id = string.Empty;
        public List<Objects> objects = new List<Objects>();
        public string time_stamp = string.Empty;
 
 
    }
    [Serializable]
    public class Objects
    {
        public string sid = string.Empty;
        public string spk = string.Empty;
    }
 
}