JLChen
2021-08-18 c8c33200e43f05136eca9fc8ff4942f8def31cc6
HDL_ON/DAL/ThirdPartySdk/HDLLinphone.cs
@@ -1,8 +1,17 @@
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
@@ -32,70 +41,466 @@
            }
        }
        public void InitLinphone()
        #region ■  --  初始化SDK_______________________________
        /// <summary>
        /// 初始化SDK
        /// </summary>
        void InitLinphone()
        {
            //防止重复初始化
            if (isInitSdk) return;
            isInitSdk = true;
#if __IOS__
            Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().InitalLinPhone();
#else
            Com.Hdl.Hdllinphonesdk.Core.EasyLinphone.InitLinphone(Application.Activity);
            Com.Hdl.Hdllinphonesdk.HDLLinphoneKit.Instance.InitLinphone(Application.Activity);
            setOnHDLLinphoneCallListener();
            Com.Hdl.Hdllinphonesdk.Core.EasyLinphone.SetAccountAndLogin("3333", "85521566", "116.62.26.215:5060");
#endif
        }
#if __Android__
        /// <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
                class HDLLinphoneCallListener : Java.Lang.Object, Com.Hdl.Hdllinphonesdk.Callback.IOnHDLLinphoneCallListener
        }
        #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)
                {
                    //showToast
                    void showToast(string text)
                    //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)
                    {
                        Toast.MakeText(Application.Activity, text, ToastLength.Short).Show();
                        //和原生监控界面交互、发送开锁成功通知
                        NSNotificationCenter.DefaultCenter.PostNotificationName("lcCallDelegateOpenDoorSuccess", null);
                    }
                    public void OnAnswerAction()
                    else
                    {
                        showToast("接听");
                        Utlis.WriteLine("POST 开锁失败 code: " + revertObj.Code);
                    }
                    public void OnHangUpAction(int callDuration)
                    {
                        showToast("挂断 通话时长:" + callDuration);
                    }
                    public void OnRejectCallAction()
                    {
                        showToast("拒接");
                    }
                    public void OnScreenshotSuccessfulAction(Bitmap p0)
                    {
                        showToast("截图");
                    }
                    public void OnUnlockAction()
#else
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        HDLLinphoneKit.Instance.OnOpenSuccess();
                    }
                }
                void setOnHDLLinphoneCallListener()
                {
                    HDLLinphoneKit.Instance.OnHDLLinphoneCallListener = new HDLLinphoneCallListener();
                }
                    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;
    }
}