wjc
2023-03-28 cafb1acbfc48f9a4b9e6feaeed5641c7da2f255b
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using System;
using HDL_ON.Entity;
using System.Collections.Generic;
using Shared;
#if __IOS__
#else
using Com.Videogo.Hdl;
#endif
 
namespace HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock
{
    /// <summary>
    /// 萤石视频门锁公共方法类
    /// </summary>
    public class CommonMethod
    {
        private Loading loading;
        /// <summary>
        /// 记录当前界面索引值
        /// </summary>
        private int pageLayoutIndexValue;
 
        private static CommonMethod commonMethod = null;
        /// <summary>
        /// 获取对象
        /// </summary>
        public static CommonMethod Current
        {
            get
            {
                if (commonMethod == null)
                {
                    commonMethod = new CommonMethod();
                }
 
                return commonMethod;
 
            }
 
        }
 
        #region  --------- 跳转到Android那边的方法(萤石视频门锁) --------
        /// <summary>
        /// android,ios源生添加设备到云端成功后回调方法
        /// 再读取云端的上萤石设备列表
        /// <paramref name="spk"/>spk</param>
        /// </summary>
        public void SkipAddDeviceActivity(string spk)
        {
 
            this.InitializationAndroidData((isBools) =>
            {
                if (!isBools)
                {
                    this.MainThread(() =>
                    {
                        this.ShowTip("先获取萤石云子账号token失败。");
 
                    });
                }
#if __IOS__
 
#else
                HDLEzvizSdk.Instance.JumpToEZScanActivity(Application.Activity,spk);
                HDLEzvizSdk.SetAddCallback(new AndroidIHdlInterface((isBool, adnroidActionDevSerial) =>
                {
                    if (isBool)
                    {
 
                        var list = VideDoorLockSend.Current.GetVideoDoorLockDeviceList(SPK.VideoDoorLock);
                        if (list.Count > 0)
                        {
                            var function = list.Find((o) => o.sid == adnroidActionDevSerial);
                            if (function != null)
                            {
                                this.SaveFunctionFile(function);
                            }
                        }
 
 
                    }
 
                }));
#endif
            });
 
 
 
 
 
        }
        /// <summary>
        /// 打开实时视频画面
        /// </summary>
        /// <param name="deviceSerial">设备序列号</param>
        /// <param name="deviceId">设备id(云端上deviceId)</param>
        /// <param name="spk">设备spk(云雀上定义好的)</param>
        /// <param name="isDialog">是否需要弹框</param>
        public void SkipRTVActivity(string deviceSerial, string deviceId, string spk, bool isDialog)
        {
 
            this.InitializationAndroidData((isBools) =>
            {
                if (!isBools)
                {
                    this.MainThread(() =>
                    {
                        this.ShowTip("先获取萤石云子账号token失败。");
 
                    });
                }
 
                if (isDialog)
                {
                    Video.View.ShowDialog.Currnet.ClickBox(Language.StringByID(StringId.laizimenlinghujiao), (dialog) =>
                    {
                        dialog.Close();
#if __IOS__
 
#else
                    HDLEzvizSdk.Instance.JumpToEZRealPlayActivity(Application.Activity,deviceId,deviceSerial,spk);
#endif
                    }, (dialog) =>
                    {
                        dialog.Close();
                    });
                }
                else
                {
#if __IOS__
#else
                    HDLEzvizSdk.Instance.JumpToEZRealPlayActivity(Application.Activity,deviceId,deviceSerial,spk);
#endif
                }
 
 
            });
 
 
 
        }
        /// <summary>
        /// 浏览历史记录
        /// </summary>
        public void SkipRecordActivity(string deviceId)
        {
            this.InitializationAndroidData((s) =>
            {
 
#if __IOS__
#else
                HDLEzvizSdk.Instance.JumpLockHistoryActivity(Application.Activity, deviceId);
#endif
 
            });
            
        }
        /// <summary>
        /// 临时密码开门
        /// <paramref name="deviceId"/>
        /// </summary>
        public void SkipPSWActivity(string deviceId)
        {
            this.InitializationAndroidData((isBools) =>
            {
                if (!isBools)
                {
                    this.MainThread(() =>
                    {
                        this.ShowTip("先获取萤石云子账号token失败。");
 
                    });
                }
#if __IOS__
#else
                HDLEzvizSdk.Instance.JumpTempPasswordActivity(Application.Activity, deviceId);
#endif
 
            });
        }
 
        /// <summary>
        /// 初始化萤石SDK(实际就是传参数,多次调用也没有影响)
        /// </summary>
        public void InitializationAndroidData(Action<bool> action)
        {
 
 
            bool isBool = true;
            this.SunThread(() =>
            {
                try
                {
                    //先获取萤石云子账号token
                    var ezChildAccessToken = VideDoorLockSend.Current.GetEZGetChildToken();
                    this.MainThread(() =>
                    {
                        if (string.IsNullOrEmpty(ezChildAccessToken))
                        {
                            //Token
                            isBool = false;
                            return;
                        }
                        Application.RunOnMainThread(() =>
                        {
#if __IOS__
                            //iOS
                            //初始化萤石云SDK,中文国内key、英文海外key 开发者账号使用应用包名申请的APPKEY,不同包名应用需配置不同的APPKEY
                            EZSDK.IOS.EZSDK.SharedInstance().InitLibWithAppKey("1aa98a90489b4838b966b57018b4b04b", "1aa98a90489b4838b966b57018b4b04b");
                            //1.设置所需河东的AccessToken和RefreshToken、域名地址
                            EZSDK.IOS.EZSDK.SharedInstance().SetHDlAccessToken(UserInfo.Current.LoginTokenString, UserInfo.Current.RefreshToken);
                            EZSDK.IOS.EZSDK.SharedInstance().SetRequestHttpsHostAndPlatform(OnAppConfig.Instance.RequestHttpsHost, 1, DB_ResidenceData.Instance.CurrentRegion.id);
                            //2.设置萤石子账号的AccessToken到SDK
                            EZSDK.IOS.EZSDK.SharedInstance().SetEZAccessToken(ezChildAccessToken);
                            //////3.打开摄像头设备列表页面
                            //EZSDK.IOS.EZSDK.SharedInstance().Go2EZvizMonitor();
                            //EZSDK.IOS.EZDeviceInfo info = new EZSDK.IOS.EZDeviceInfo();
                            //info.de
                            //EZSDK.IOS.EZSDK.Play(info);
#else
                            HDLEzvizSdk.Instance.Init(Application.Activity.Application, ezChildAccessToken, UserInfo.Current.LoginTokenString, UserInfo.Current.RefreshToken, OnAppConfig.Instance.RequestHttpsHost, "1aa98a90489b4838b966b57018b4b04b", 1, DB_ResidenceData.Instance.CurrentRegion.id);
 
#endif
                        });
 
                    });
 
                }
                catch (Exception e)
                {
                }
                finally
                {
                    this.MainThread(() =>
                    {
                        action?.Invoke(isBool);
                    });
                }
            });
        }
 
        /// <summary>
        /// 更换住宅
        /// </summary>
        /// <param name="home">对象</param>
        public void ChangeCurrHome(RegionInfoRes home)
        {
            
            if (DB_ResidenceData.Instance.CurrentRegion.id == home.id || string.IsNullOrEmpty(home.id)) return;
#if __IOS__
#else
                HDLEzvizSdk.Instance.SetHomeId(home.id);
#endif
 
        }
        #endregion
 
        /// <summary>
        /// 萤石视频门锁的图标
        /// <param name="comerom">来自那里</param>
        /// </summary>
        public string GetVideoDoorLockIcon(Comerom comerom)
        {
            if (comerom == Comerom.collect)
            {
                return "FunctionIcon/DoorLock/VideoDoorLockOpen.png";
            }
            else
            {
                return "FunctionIcon/DoorLock/VideoDoorlockClose.png";
            }
 
        }
        #region  --------- 功能文件保存,删除 --------
        /// <summary>
        /// 保存本地文件
        /// </summary>
        /// <param name="function">当前设备</param>
        public void SaveFunctionFile(Function function)
        {
            if (function == null) return;
            function.AssembleStatus();
            function.SaveFunctionFile();
            FunctionList.List.IniFunctionList(function.savePath, true);
            MainPage.Log($"文件保存->{function.name}-->{function.spk}");
        }
 
        /// <summary>
        /// 删除本地文件
        /// </summary>
        /// <param name="function">当前设备</param>
        public void DeleteFunction(Function function)
        {
            if (function == null) return;
            FunctionList.List.DeleteFunction(function);
            MainPage.Log($"文件删除->{function.name}-->{function.spk}");
        }
        #endregion
        /// <summary>
        /// 获取Loading对象
        /// </summary>
        /// <returns></returns>
        public Loading Loading
        {
            get
            {
                this.MainThread(() =>
                {
                    //if (loading != null && this.pageLayoutIndexValue == MainPage.BasePageView.ChildrenCount - 1)
                    //{
                    //    //防止二重添加对象
                    //    return;
                    //}
                    if (loading == null)//|| MainPage.BasePageView.ChildrenCount < 1)
                    {
                        loading = new Loading();
                    }
                    //loading.BringToFront();
                    Application.MainPage.AddChidren(loading);
                    //View view = MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1);
                    //if (view is ViewGroup)
                    //{
                    //    pageLayoutIndexValue = MainPage.BasePageView.ChildrenCount - 1;
                    //    ((ViewGroup)view).AddChidren(loading);
                    //}
                });
                return loading;
            }
 
        }
 
        /// <summary>
        /// 获取萤石视频门锁列表
        /// </summary>
        /// <returns></returns>
        public List<Function> GetVideoDoorLockList()
        {
            var list = FunctionList.List.GetVideoDoorLockList();
#if DEBUG
            //list.Add(new Function { name = "视频门锁一", sid = "23456789", spk = SPK.VideoDoorLock });
            //list.Add(new Function { name = "视频门锁二", sid = "2345678922", spk = SPK.VideoDoorLock });
#endif
            return list;
        }
        #region  ---------自定义线程(子线程,主线程)--------
        /// <summary>
        /// 子线程
        /// </summary>
        /// <paramref name="tipType"/>是否要提示错误信息(默认显示)<paramref>
        /// <param name="action"></param>
        public void SunThread(Action action, TipType tipType = TipType.confirmation)
        {
            new System.Threading.Thread(() =>
            {
                try
                {
                    action?.Invoke();
                }
                catch (Exception e)
                {
                    this.ShowAlert(e, tipType);
                }
            })
            { IsBackground = true }.Start();
 
        }
        /// <summary>
        /// 主线程(UI更新)
        /// </summary>
        /// <paramref name="tipType"/>是否要提示错误信息(默认显示)<paramref>
        /// <param name="action"></param>
        public void MainThread(Action action, TipType tipType = TipType.confirmation)
        {
            Application.RunOnMainThread(() =>
            {
                try
                {
                    action?.Invoke();
                }
                catch (Exception e)
                {
                    this.ShowAlert(e, tipType);
                }
            });
 
        }
        #endregion
        #region  --------- 提示框,确认框 --------
        /// <summary>
        /// 对话框(只要程序报错的时候才用到)
        /// </summary>
        /// <paramref name="e"/>异常对象<paramref>
        /// <paramref name="tipType"/>是否要提示错误信息(默认显示)<paramref>
        private void ShowAlert(Exception e, TipType tipType)
        {
 
            if (tipType != TipType.confirmation)
            {
                return;
            }
            Application.RunOnMainThread(() =>
            {
                string msg = e.Message + "\r\n" + e.StackTrace;
                Alert alert = new Alert("抱歉程序出错了.", "按\"确定\"能获取更详细的错误信息.", "取消", "确定");
                alert.Show();
                alert.ResultEventHandler += (alerts, isBool) =>
                {
                    alert.Dismiss();
                    if (isBool)
                    {
                        StackTraceLog stackTraceLog = new StackTraceLog(msg);
                        stackTraceLog.Show();
                    }
                };
            });
        }
        /// <summary>
        /// 信息提示窗口,自动关闭
        /// </summary>
        /// <param name="msg">提示错误文本</param>
        public void ShowTip(string msg, int time = 2)
        {
            Application.RunOnMainThread(() =>
            {
                new PublicAssmebly().TipMsgAutoClose(msg, false, time * 1000);
            });
        }
        #endregion
        /// <summary>
        /// 弹框类型
        /// </summary>
        public enum TipType
        {
            none,//无提示
            flicker,//闪烁框
            confirmation//确认框
        }
        /// <summary>
        /// 表示来自那个界面
        /// </summary>
        public enum Comerom
        {
            function,//功能
            collect,//收藏
            room,//房间
            push,//推送
        }
 
#if __IOS__
        ///// <summary>
        ///// 专门定义给Ios回调用着,没有特殊含义
        ///// </summary>
        //public class IosIHdlInterface : Shared.IOS.HDLLinphoneSDK.HDLLinphoneCallDelegate
        //{
        //    Action<bool, string> mAction;
        //    public IosIHdlInterface(Action<bool, string> action)
        //    {
        //        mAction = action;
        //    }
        //    /// <summary>
        //    /// 
        //    /// </summary>
        //    /// <param name="p0">true是入网成功(源生回调返回)</param>
        //    /// <param name="p1">设备的序列化(源生回调返回)</param>
        //    public void AddCallback(bool p0, string p1)
        //    {
        //        mAction?.Invoke(p0, p1);
        //    }
        //}
#else
        /// <summary>
        /// 专门定义给安卓回调用着,没有特殊含义
        /// </summary>
        public class AndroidIHdlInterface : Java.Lang.Object, IHdlInterface
        {
 
            Action<bool,string> mAction;
            public AndroidIHdlInterface(Action<bool,string> action)
            {
                mAction = action;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="p0">true是入网成功(源生回调返回)</param>
            /// <param name="p1">设备的序列化(源生回调返回)</param>
            public void AddCallback(bool p0, string p1)
            {
                mAction?.Invoke(p0,p1);
            }
 
           
        }
#endif
 
 
    }
 
 
}