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() { } /// /// /// private static HDLLinphone m_Current = null; /// /// /// public static HDLLinphone Current { get { if (m_Current == null) { m_Current = new HDLLinphone(); } return m_Current; } } #region ■ -- 初始化SDK_______________________________ /// /// 初始化SDK /// 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 } /// /// 当前登录的Sip账号 /// HDLSipInfo currentHDLSipInfo; /// /// 设置sip登录账号 /// /// 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 ■ -- 动作回调和原生交互,提交记录到云端_______________________________ /// /// 可视对讲设备参数 /// public HDLCallVideoInfo mHDLCallVideoInfo; /// /// 初始化呼叫参数 /// /// /// public void InitCallInfo(HDLCallVideoInfo mHDLCallVideoInfo) { this.mHDLCallVideoInfo = mHDLCallVideoInfo; } /// /// 判断callId是否为空 /// /// bool CheckmHDLCallVideoInfoIsNullOrEmpty() { //return false; return (mHDLCallVideoInfo == null || string.IsNullOrEmpty(mHDLCallVideoInfo.CallId)); } /// /// 截图成功 /// public void ScreenshotSuccessfulAction(byte[] dataBytes) { //Utlis.WriteLine("ScreenshotSuccessfulAction"); if (CheckmHDLCallVideoInfoIsNullOrEmpty()) return; new Thread(() => { //var imageName = Guid.NewGuid().ToString(); Dictionary dic = new Dictionary(); 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 ■ -- 新接听和开锁接口_______________________________ /// /// 开锁 /// public void HDLUnlockAction() { //Utlis.WriteLine("UnlockAction"); if (mHDLCallVideoInfo == null) return; new Thread(() => { Dictionary dic = new Dictionary(); 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(); } /// /// 更改通话状态 /// public void HDLUpdateCallStatus(CallStatusType callStatusType, InterphoneType interphoneType, int callDuration = 0) { if (CheckmHDLCallVideoInfoIsNullOrEmpty()) return; new Thread(() => { Dictionary dic = new Dictionary(); 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账号_______________________________ /// /// 是否初始化了SDK /// public bool isInitSdk; /// /// 获取当前住宅的SIP账号 /// public HDLSipInfo GetHDLSipInfo(string homeId) { Dictionary d = new Dictionary(); 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(resultObj.Data.ToString()); return info; } } else { return null; } } /// /// 获取当前住宅Sip账号信息并初始化LinphoneSDK和登录 /// /// 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 ■ -- 跳转监控页面_______________________________ /// /// 跳转监控页面 /// /// 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 ■ -- 跳转呼叫页面_______________________________ /// /// 跳转呼叫页面 /// /// 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__ /// /// 接听、挂断、开锁等动作监听处理 /// 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(); } } /// /// 设置原生的Listener监听 /// void setOnHDLLinphoneCallListener() { HDLLinphoneKit.Instance.OnHDLLinphoneCallListener = new HDLLinphoneCallListener(this); } #endif #endregion } /// /// SIP可视对讲参数 /// public class HDLCallVideoInfo { /// /// 设备序列号,通讯必要字段 /// public string DeviceId = string.Empty; /// /// 设备名称 /// public string DeviceName = string.Empty; /// /// 设备Sip账号 /// public string DeviceSipAccount = string.Empty; /// /// 呼叫记录Id /// public string CallId = string.Empty; /// /// 住宅Id /// public string HomeId = string.Empty; /// /// 账号 /// public string Username = string.Empty; /// /// 密码 /// public string Password = string.Empty; /// /// Domain /// public string Domain = string.Empty; /// /// 类型 /// public string InterphoneType; } /// /// SIP账号相关信息 /// public class HDLSipInfo { /// /// Sip服务器 /// public string proxy { get; set; } /// /// 设备域 /// public string realm { get; set; } /// /// sip账号 /// public string sipAccount { get; set; } /// /// sipPasswd sip密码 /// public string sipPasswd { get; set; } /// /// 住宅ID /// public string homeId; } }