JLChen
2021-08-18 c8c33200e43f05136eca9fc8ff4942f8def31cc6
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
501
502
503
504
505
506
using System;
using System.Collections.Generic;
using System.Threading;
 
using HDL_ON;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
#if __Android__
using Android.Content;
using Android.Graphics;
using Android.Widget;
using Com.Hdl.Hdllinphonesdk;
#else
using Foundation;
#endif
 
namespace Shared
{
    public class HDLLinphone
    {
        public HDLLinphone()
        {
        }
 
        /// <summary>
        /// 
        /// </summary>
        private static HDLLinphone m_Current = null;
        /// <summary>
        /// 
        /// </summary>
        public static HDLLinphone Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HDLLinphone();
                }
                return m_Current;
            }
        }
 
        #region ■  --  初始化SDK_______________________________
        /// <summary>
        /// 初始化SDK
        /// </summary>
        void InitLinphone()
        {
            //防止重复初始化
            if (isInitSdk) return;
 
            isInitSdk = true;
 
#if __IOS__
 
            Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().InitalLinPhone();
#else
            Com.Hdl.Hdllinphonesdk.HDLLinphoneKit.Instance.InitLinphone(Application.Activity);
            setOnHDLLinphoneCallListener();
#endif
 
 
        }
 
        /// <summary>
        /// 当前登录的Sip账号
        /// </summary>
        HDLSipInfo currentHDLSipInfo;
        /// <summary>
        /// 设置sip登录账号
        /// </summary>
        /// <param name="mHDLSipInfo"></param>
        public void SetAccountAndLogin(HDLSipInfo mHDLSipInfo) {
 
            this.currentHDLSipInfo = mHDLSipInfo;
 
            if (mHDLSipInfo == null) return;
          
#if __IOS__
            //Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().Login("3333", "85521566", "116.62.26.215:5060");
            Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().Login(mHDLSipInfo.sipAccount, mHDLSipInfo.sipPasswd, mHDLSipInfo.realm);
#else
            Com.Hdl.Hdllinphonesdk.HDLLinphoneKit.Instance.SetAccountAndLogin(mHDLSipInfo.sipAccount, mHDLSipInfo.sipPasswd, mHDLSipInfo.realm);
            //Com.Hdl.Hdllinphonesdk.Core.EasyLinphone.SetAccountAndLogin("61723164995710", "8ec02ce0", "47.94.42.230:25060");
#endif
 
        }
        #endregion
 
        #region ■  -- 动作回调和原生交互,提交记录到云端_______________________________
        /// <summary>
        /// 可视对讲设备参数
        /// </summary>
        public HDLCallVideoInfo mHDLCallVideoInfo;
 
        /// <summary>
        /// 初始化呼叫参数
        /// </summary>
        /// <param name="mHDLCallVideoInfo"></param>
        /// <param name="mInterphoneType"></param>
        public void InitCallInfo(HDLCallVideoInfo mHDLCallVideoInfo)
        {
            this.mHDLCallVideoInfo = mHDLCallVideoInfo;
        }
 
        /// <summary>
        /// 判断callId是否为空
        /// </summary>
        /// <returns></returns>
        bool CheckmHDLCallVideoInfoIsNullOrEmpty()
        {
            //return false;
            return (mHDLCallVideoInfo == null || string.IsNullOrEmpty(mHDLCallVideoInfo.CallId));
        }
 
        /// <summary>
        /// 截图成功
        /// </summary>
        public void ScreenshotSuccessfulAction(byte[] dataBytes)
        {
            //Utlis.WriteLine("ScreenshotSuccessfulAction");
 
            if (CheckmHDLCallVideoInfoIsNullOrEmpty()) return;
 
            new Thread(() =>
            {
                //var imageName = Guid.NewGuid().ToString();
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("callId", mHDLCallVideoInfo.CallId);
                dic.Add("images", dataBytes);
#if __IOS__
                dic.Add("imagesName", "_IOS.jpg");
#else
                dic.Add("imagesName", "_Android.jpg");
#endif
 
                var requestJson = HttpUtil.GetSignRequestJson(dic);
                var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_FL_Screenshot, requestJson);
                if (revertObj.Code == StateCode.SUCCESS)
                {
                    //Utlis.WriteLine("POST 截图上传成功");
                }
                else
                {
                    Utlis.WriteLine("POST 截图上传失败 code: " + revertObj.Code);
                }
 
            })
            { IsBackground = false }.Start();
 
        }
 
        DateTime UnlockDateTime = DateTime.MinValue;
        #endregion
 
        #region ■  --  新接听和开锁接口_______________________________
        /// <summary>
        /// 开锁
        /// </summary>
        public void HDLUnlockAction()
        {
            //Utlis.WriteLine("UnlockAction");
 
            if (mHDLCallVideoInfo == null) return;
 
            new Thread(() =>
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("deviceId", mHDLCallVideoInfo.DeviceId);//设备id
                dic.Add("callId", mHDLCallVideoInfo.CallId);
                dic.Add("interphoneType", mHDLCallVideoInfo.InterphoneType);
                dic.Add("homeId", mHDLCallVideoInfo.HomeId);
                var requestJson = HttpUtil.GetSignRequestJson(dic);
                var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_VideoDevice_OpenDoorbell, requestJson);
 
                Application.RunOnMainThread(() =>
                {
 
#if __IOS__
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        //和原生监控界面交互、发送开锁成功通知
                        NSNotificationCenter.DefaultCenter.PostNotificationName("lcCallDelegateOpenDoorSuccess", null);
                    }
                    else
                    {
                        Utlis.WriteLine("POST 开锁失败 code: " + revertObj.Code);
                    }
#else
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        HDLLinphoneKit.Instance.OnOpenSuccess();
                    }
                    else
                    {
                        Utlis.WriteLine("POST 开锁失败 code: " + revertObj.Code);
                        HDLLinphoneKit.Instance.OnOpenError(revertObj.message);
                    }
#endif
 
 
                });
 
 
            })
            { IsBackground = false }.Start();
        }
 
        /// <summary>
        /// 更改通话状态
        /// </summary>
        public void HDLUpdateCallStatus(CallStatusType callStatusType, InterphoneType interphoneType, int callDuration = 0)
        {
 
            if (CheckmHDLCallVideoInfoIsNullOrEmpty()) return;
 
            new Thread(() =>
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("callId", mHDLCallVideoInfo.CallId);//呼叫id
                dic.Add("callStatus", callStatusType.ToString());//可用值:MISSED,RECEIVED,REJECT
                dic.Add("interphoneTypeEnum", interphoneType.ToString());//可用值:FLVI,IMOUVISIAL
                if (callStatusType == CallStatusType.RECEIVED && callDuration > 0)
                {
                    dic.Add("callDuration", callDuration);//通话时长(秒)
                }
 
                var requestJson = HttpUtil.GetSignRequestJson(dic);
                var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_VideoDevice_UpdateCallStatus, requestJson);
                if (revertObj.Code == StateCode.SUCCESS)
                {
 
                }
                else
                {
                    Utlis.WriteLine("POST 更新状态失败 code: " + revertObj.Code);
                }
 
            })
            { IsBackground = false }.Start();
 
        }
        #endregion
 
        #region ■  --  获取sip账号_______________________________
        /// <summary>
        /// 是否初始化了SDK
        /// </summary>
        public bool isInitSdk;
        /// <summary>
        /// 获取当前住宅的SIP账号
        /// </summary>
        public HDLSipInfo GetHDLSipInfo(string homeId)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add("homeId", homeId);
 
            var requestJson = HttpUtil.GetSignRequestJson(d);
            var resultObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_VideoDevice_GetSipAccount, requestJson);
 
            if (resultObj.Code == StateCode.SUCCESS)
            {
                if (string.IsNullOrEmpty(resultObj.Data.ToString()))
                {
                    //控的话代表当前住宅不支持
                    return null;
                }
                else
                {
                    var info = Newtonsoft.Json.JsonConvert.DeserializeObject<HDLSipInfo>(resultObj.Data.ToString());
                    return info;
                }
            }
            else
            {
                return null;
            }
 
        }
 
        /// <summary>
        /// 获取当前住宅Sip账号信息并初始化LinphoneSDK和登录
        /// </summary>
        /// <param name="homeId"></param>
        public void getHDLSipInfoAndInitSDK(string homeId)
        {
            try
            {
                //先清空呼叫和监视设备信息
                InitCallInfo(null);
                HDLSipInfo mHDLSipInfo = GetHDLSipInfo(homeId);
                if (mHDLSipInfo != null)
                {
                    InitLinphone();
                    mHDLSipInfo.homeId = homeId;
                    SetAccountAndLogin(mHDLSipInfo);
                    //Application.RunOnMainThread(() =>
                    //{
                    //    InitLinphone();
                    //    SetAccountAndLogin(mHDLSipInfo);
                    //});
                }
            }
            catch
            {
 
            }
        }
        #endregion
 
        #region ■  --  跳转监控页面_______________________________
        /// <summary>
        /// 跳转监控页面
        /// </summary>
        /// <param name="mESVideoInfo"></param>
        public void ShowESVideoMonitor(ESVideoInfo mESVideoInfo)
        {
            HDLCallVideoInfo mHDLCallVideoInfo = new HDLCallVideoInfo();
            mHDLCallVideoInfo.HomeId = mESVideoInfo.HomeId;
            mHDLCallVideoInfo.DeviceId = mESVideoInfo.Lc_DeviceId;
            mHDLCallVideoInfo.DeviceName = mESVideoInfo.DeviceName;
            mHDLCallVideoInfo.InterphoneType = InterphoneType.HDL.ToString();
            mHDLCallVideoInfo.DeviceSipAccount = mESVideoInfo.deviceSipAccount;//监视主动呼叫门口机时用到
            InitCallInfo(mHDLCallVideoInfo);
 
#if __IOS__
               
#else
            Com.Hdl.Hdllinphonesdk.HDLLinphoneKit.Instance.CallTo(mHDLCallVideoInfo.DeviceSipAccount, true);
            var intent = new Intent(Shared.Application.Activity, typeof(Com.Hdl.Hdllinphonesdk.Activity.HDLLinphoneMonitorActivity)); ;
            intent.PutExtra(HDLLinphoneKit.KeyTitleName, mHDLCallVideoInfo.DeviceName);
            Shared.Application.Activity.StartActivity(intent);
#endif
 
        }
        #endregion
 
 
        #region ■  --  跳转呼叫页面_______________________________
        /// <summary>
        /// 跳转呼叫页面
        /// </summary>
        /// <param name="mESVideoInfo"></param>
        public void ShowESVideoIntercom(ESVideoInfo mESVideoInfo)
        {
            HDLCallVideoInfo mHDLCallVideoInfo = new HDLCallVideoInfo();
            mHDLCallVideoInfo.CallId = mESVideoInfo.callId;
            mHDLCallVideoInfo.HomeId = mESVideoInfo.HomeId;
            mHDLCallVideoInfo.DeviceId = mESVideoInfo.Lc_DeviceId;
            mHDLCallVideoInfo.DeviceName = mESVideoInfo.DeviceName;
            mHDLCallVideoInfo.InterphoneType = InterphoneType.HDL.ToString();
            mHDLCallVideoInfo.DeviceSipAccount = mESVideoInfo.deviceSipAccount;
            InitCallInfo(mHDLCallVideoInfo);
 
#if __IOS__
               
#else
            var intent = new Intent(Shared.Application.Activity, typeof(Com.Hdl.Hdllinphonesdk.Activity.HDLLinphoneIntercomActivity)); ;
            intent.PutExtra(HDLLinphoneKit.KeyTitleName, mHDLCallVideoInfo.DeviceName);
            Shared.Application.Activity.StartActivity(intent);
#endif
 
        }
        #endregion
 
 
        #region ■  --  Android相关操作_______________________________
 
#if __Android__
 
        /// <summary>
        /// 接听、挂断、开锁等动作监听处理
        /// </summary>
        private class HDLLinphoneCallListener : Java.Lang.Object, Com.Hdl.Hdllinphonesdk.Callback.IOnHDLLinphoneCallListener
        {
            [Weak] HDLLinphone hdlLinphone;
 
            public HDLLinphoneCallListener(HDLLinphone mHDLLinphone)
            {
                hdlLinphone = mHDLLinphone;
            }
 
 
            //showToast
            void showToast(string text)
            {
                Toast.MakeText(Application.Activity, text, ToastLength.Short).Show();
            }
 
            public void OnAnswerAction()
            {
                showToast("接听");
                hdlLinphone.HDLUpdateCallStatus(CallStatusType.RECEIVED, InterphoneType.HDL);
            }
 
            public void OnHangUpAction(int callDuration)
            {
 
                showToast("挂断 通话时长:" + callDuration);
                hdlLinphone.HDLUpdateCallStatus(CallStatusType.RECEIVED, InterphoneType.HDL, callDuration);
            }
 
            public void OnRejectCallAction()
            {
                showToast("拒接");
                hdlLinphone.HDLUpdateCallStatus(CallStatusType.REJECT, InterphoneType.HDL);
            }
 
            public void OnScreenshotSuccessfulAction(Bitmap p0)
            {
                showToast("截图成功");
            }
 
            public void OnUnlockAction()
            {
                hdlLinphone.HDLUnlockAction();
            }
 
        }
 
        /// <summary>
        /// 设置原生的Listener监听
        /// </summary>
        void setOnHDLLinphoneCallListener()
        {
            HDLLinphoneKit.Instance.OnHDLLinphoneCallListener = new HDLLinphoneCallListener(this);
        }
 
#endif
        #endregion
    }
 
    /// <summary>
    /// SIP可视对讲参数
    /// </summary>
    public class HDLCallVideoInfo
    {
        /// <summary>
        /// 设备序列号,通讯必要字段
        /// </summary>
        public string DeviceId = string.Empty;
        /// <summary>
        /// 设备名称
        /// </summary>
        public string DeviceName = string.Empty;
        /// <summary>
        /// 设备Sip账号
        /// </summary>
        public string DeviceSipAccount = string.Empty;
        /// <summary>
        /// 呼叫记录Id
        /// </summary>
        public string CallId = string.Empty;
        /// <summary>
        /// 住宅Id
        /// </summary>
        public string HomeId = string.Empty;
        /// <summary>
        /// 账号
        /// </summary>
        public string Username = string.Empty;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password = string.Empty;
        /// <summary>
        /// Domain
        /// </summary>
        public string Domain = string.Empty;
        /// <summary>
        /// 类型
        /// </summary>
        public string InterphoneType;
 
    }
 
    /// <summary>
    /// SIP账号相关信息
    /// </summary>
    public class HDLSipInfo
    {
        /// <summary>
        /// Sip服务器
        /// </summary>
        public string proxy { get; set; }
        /// <summary>
        /// 设备域
        /// </summary>
        public string realm { get; set; }
        /// <summary>
        /// sip账号
        /// </summary>
        public string sipAccount { get; set; }
        /// <summary>
        /// sipPasswd    sip密码
        /// </summary>
        public string sipPasswd { get; set; }
        /// <summary>
        /// 住宅ID
        /// </summary>
        public string homeId;
    }
 
 
}