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.App;
using Android.Content;
using Android.Graphics;
using Android.Widget;
using Com.Hdl.Hdllinphonesdk;
#else
using Foundation;
using UIKit;
using Shared.IOS.HDLLinphoneSDK;
#endif
namespace Shared
{
///
/// 目前是每个住宅对应一个SIP账号,切换住宅需要重新获取SIP账号
///
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;
}
}
///
/// 当前住宅是否支持可视对讲
///
public bool IsEnable = false;
///
/// 是否自动跳转呼叫页面,
/// false:收到推送并且已经收到sip呼叫才跳转呼叫页面
/// true:原生收到呼叫马上跳转呼叫页面,不等待推送过来
///
bool IsAutoJumpCallView = false;
///
/// 是否初始化了SDK
///
bool IsInitSdk;
///
/// 当前登录的Sip账号
///
HDLSipInfo currentHDLSipInfo;
///
/// 可视对讲设备参数
///
HDLCallVideoInfo mHDLCallVideoInfo;
#region ■ -- 初始化SDK_______________________________
///
/// 初始化SDK
///
void InitLinphone()
{
//防止重复初始化
if (IsInitSdk) return;
IsInitSdk = true;
#if __IOS__
Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().InitalLinPhone();
//设置收到来电后、是否需自动跳转呼叫页面方案
Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().IsAutoJumpCallView = IsAutoJumpCallView;
//设置Listener监听
mOnHDLLinphoneCallDelegate = new OnHDLLinphoneCallDelegate(this);
Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().HdlLinphoneCallDelegate = mOnHDLLinphoneCallDelegate;
#else
HDLLinphoneKit.Instance.InitLinphone(Application.Activity);
//设置收到来电后、是否需自动跳转呼叫页面方案
HDLLinphoneKit.Instance.AutoJumpCallView = IsAutoJumpCallView;
//设置Listener监听
setOnHDLLinphoneCallListener();
#endif
}
///
/// 设置sip登录账号
///
///
public void SetAccountAndLogin(HDLSipInfo mHDLSipInfo)
{
this.currentHDLSipInfo = mHDLSipInfo;
if (mHDLSipInfo == null) return;
#if __IOS__
//Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().Login("6666", "85521566", "116.62.26.215:5060");
Shared.IOS.HDLLinphoneSDK.HDLLinPhoneSDK.Instance().Login(mHDLSipInfo.sipAccount, mHDLSipInfo.sipPasswd, mHDLSipInfo.realm);
#else
HDLLinphoneKit.Instance.SetAccountAndLogin(mHDLSipInfo.sipAccount, mHDLSipInfo.sipPasswd, mHDLSipInfo.realm);
#endif
}
///
/// 注销所有账号
///
public void LogoutAllAccount() {
//没初始化过则返回
if (!IsInitSdk) return;
this.currentHDLSipInfo = null;
this.mHDLCallVideoInfo = null;
#if __IOS__
HDLLinPhoneSDK.Instance().LogoutAllLinphoneUser();
#else
HDLLinphoneKit.Instance.Logout();
#endif
}
///
/// 清除配置表
///
public void ClearProxyConfig()
{
#if __IOS__
HDLLinPhoneSDK.Instance().ClearAllConfigs();
#else
HDLLinphoneKit.Instance.ClearProxyConfig();
#endif
}
#endregion
#region ■ -- 获取sip账号_______________________________
///
/// 获取当前住宅的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, bool clearCallInfo = true)
{
try
{
//先清空呼叫和监视设备信息
if (clearCallInfo)
{
InitCallInfo(null);
}
HDLSipInfo mHDLSipInfo = GetHDLSipInfo(homeId);
if (mHDLSipInfo != null)
{
Application.RunOnMainThread(() =>
{
InitLinphone();
mHDLSipInfo.homeId = homeId;
SetAccountAndLogin(mHDLSipInfo);
Utlis.WriteLine("CALL 获取SIP账号成功:" + mHDLSipInfo.sipAccount);
});
}
}
catch { }
}
#endregion
#region ■ -- 动作回调和原生交互,提交记录到云端_______________________________
///
/// 初始化呼叫参数
///
///
///
void InitCallInfo(HDLCallVideoInfo mHDLCallVideoInfo)
{
this.mHDLCallVideoInfo = mHDLCallVideoInfo;
}
///
/// 判断callId是否为空
///
///
bool CheckmHDLCallVideoInfoIsNullOrEmpty()
{
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);//当前呼叫id
dic.Add("interphoneType", mHDLCallVideoInfo.InterphoneType);//可视对讲厂家类型
dic.Add("homeId", mHDLCallVideoInfo.HomeId);//住宅ID
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());
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 ■ -- 跳转监控页面_______________________________
///
/// 跳转监控页面
///
///
public void ShowESVideoMonitor(ESVideoInfo mESVideoInfo)
{
InitLinphone();
//sip账号为空
if (string.IsNullOrEmpty(mESVideoInfo.deviceSipAccount))
{
HDLCommon.Current.ShowAlert(Language.StringByID(StringId.shebeibucunzai));
return;
}
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__
HDLLinPhoneSDK.Instance().CallWithUserName(mHDLCallVideoInfo.DeviceSipAccount, mHDLCallVideoInfo.DeviceName);
#else
//1.先呼叫设备
Com.Hdl.Hdllinphonesdk.HDLLinphoneKit.Instance.CallTo(mHDLCallVideoInfo.DeviceSipAccount, true);
//2.跳转打开监控页面
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 ■ -- 跳转呼叫页面_______________________________
///
/// 收到推送,后判断呼叫住宅是否为当前住宅,不是的话重新获取SIP账号并登录
///
///
public void ShowESVideoIntercom(ESVideoInfo mESVideoInfo)
{
InitLinphone();
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 (currentHDLSipInfo != null && currentHDLSipInfo.homeId == mESVideoInfo.HomeId)
{
Utlis.WriteLine("CALL 收到推送 推送住宅和之前注册登录的住宅一致");
CheckCallStateAndGotoPage();
}
else
{
//注意:之前没登录SIP账号或者住宅不一致的情况下需要重新获取SIP账号
//获取呼叫住宅的SIP账号并登录
Utlis.WriteLine("CALL 收到推送 其他住宅,重新获取呼叫住宅的SIP账号并登录");
new Thread(() =>
{
GetHDLSipInfoAndInitSDK(mESVideoInfo.HomeId, false);
})
{ IsBackground = false }.Start();
//
CheckCallStateAndGotoPage();
}
}
///
/// 检测来电状态,判断是否马上打开呼叫页面,如果还没来电则开启线程检测等待
///
///
public void CheckCallStateAndGotoPage()
{
//如果开启了自动跳转方案,则无需继续下面处理、无需手动跳转
if (IsAutoJumpCallView) return;
//先结束掉之前的线程
EndCheckIncomingCallThread();
//检测是否来电了如果是直接调整呼叫页面如果不是则开启线程等待判断
if (IsIncomingReceivedCallState()) {
Utlis.WriteLine("CALL 已经来电了。。。打开呼叫页面");
GoToHDLLinphoneIntercomActivity();
}
else
{
//如果还没来电 开启线程检测
StartCheckIncomingCallThread();
}
}
///
/// 是否来电状态
///
///
bool IsIncomingReceivedCallState() {
#if __IOS__
return HDLLinPhoneSDK.Instance().IsIncomingReceivedCallState;
#else
return HDLLinphoneKit.Instance.IsIncomingReceivedCallState;
#endif
}
///
/// 跳转打开原生的呼叫页面
///
void GoToHDLLinphoneIntercomActivity() {
#if __IOS__
HDLLinPhoneSDK.Instance().GotoHDLLinphoneIntercomVC(mHDLCallVideoInfo.DeviceName);
#else
var intent = new Intent(Shared.Application.Activity, typeof(Com.Hdl.Hdllinphonesdk.Activity.HDLLinphoneIntercomActivity));
if (mHDLCallVideoInfo != null)
{
intent.PutExtra(HDLLinphoneKit.KeyTitleName, mHDLCallVideoInfo.DeviceName);
}
Shared.Application.Activity.StartActivity(intent);
#endif
}
///
/// 检测来电线程
///
Thread checkIncomingCallThread = null;
///
/// 检测线程持续时间
///
const int MAX_THREAD_TIME = 30;
///
/// 线程结束
///
int threadTime = MAX_THREAD_TIME;
///
/// 开启检测来电线程
///
void StartCheckIncomingCallThread()
{
try
{
Utlis.WriteLine("CALL 还没来电,开启线程");
threadTime = MAX_THREAD_TIME;
//结束之前的线程
EndCheckIncomingCallThread();
//新建线程
checkIncomingCallThread = new Thread(() =>
{
try
{
//15S后自动结束线程
while (threadTime > 0)
{
threadTime--;
Utlis.WriteLine("CALL 检测中...." + threadTime);
Thread.Sleep(1000);
//来电了,跳转呼叫页面
if (IsIncomingReceivedCallState())
{
Utlis.WriteLine("CALL 来电了。。。打开呼叫页面");
threadTime = 0;//跳出循环,关闭线程
Application.RunOnMainThread(() =>
{
GoToHDLLinphoneIntercomActivity();
});
}
}
}
catch { }
});
//开启线程
checkIncomingCallThread.Start();
}
catch { }
}
///
/// 结束线程
///
void EndCheckIncomingCallThread()
{
if (checkIncomingCallThread != null)
{
try
{
checkIncomingCallThread.Interrupt();
}
catch {
}
checkIncomingCallThread = null;
Utlis.WriteLine("CALL 先结束线程");
}
}
///
/// 判断线程是否启动了
///
///
public bool CheckIncomingCallThreadIsNull()
{
return checkIncomingCallThread == null || checkIncomingCallThread.ThreadState != ThreadState.Running;
}
#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 imgBitmap)
{
//showToast("截图成功");
}
///
/// 开锁事件
///
public void OnUnlockAction()
{
hdlLinphone.HDLUnlockAction();
}
///
/// 来电事件
///
///
public void OnIncomingCall(string userName)
{
Utlis.WriteLine("OnIncomingCall :" + userName);
}
}
///
/// 设置原生的Listener监听
///
void setOnHDLLinphoneCallListener()
{
HDLLinphoneKit.Instance.OnHDLLinphoneCallListener = new HDLLinphoneCallListener(this);
}
///
/// 清楚通知栏所有通知
///
void CancelAllNotification()
{
NotificationManager nMgr = (NotificationManager)Application.Activity.GetSystemService(Context.NotificationService);
nMgr.CancelAll();
}
#endif
#endregion
#region ■ -- iOS相关操作_______________________________
#if __IOS__
#region OnHDLLinphoneCallDelegate
///
///
///
OnHDLLinphoneCallDelegate mOnHDLLinphoneCallDelegate;
///
///
///
public class OnHDLLinphoneCallDelegate : HDLLinphoneCallDelegate
{
[Weak] HDLLinphone hdlLinphone;
public OnHDLLinphoneCallDelegate(HDLLinphone mHDLLinphone)
{
this.hdlLinphone = mHDLLinphone;
}
///
/// 接听
///
public override void OnAnswerAction()
{
hdlLinphone.HDLUpdateCallStatus(CallStatusType.RECEIVED, InterphoneType.HDL);
}
///
/// 挂断
///
///
public override void OnHangUpAction(int callDuration)
{
hdlLinphone.HDLUpdateCallStatus(CallStatusType.RECEIVED, InterphoneType.HDL, callDuration);
}
///
/// 拒接
///
public override void OnRejectCallAction()
{
hdlLinphone.HDLUpdateCallStatus(CallStatusType.REJECT, InterphoneType.HDL);
}
///
/// 截图成功
///
///
public override void OnScreenshotSuccessfulAction(UIImage image)
{
////NSData imageData = UIImagePNGRepresentation(image); UIImage
//NSData imageData = image.AsPNG();
//byte[] dataBytes = new byte[imageData.Length];
//System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, dataBytes, 0, Convert.ToInt32(imageData.Length));
////image.g
////hdlLinphone.ScreenshotSuccessfulAction(dataBytes);
}
///
/// 开锁成功
///
public override void OnUnlockAction()
{
hdlLinphone.HDLUnlockAction();
}
///
/// 来电中...
///
public override void OnIncomingCall(string userName)
{
Utlis.WriteLine("OnIncomingCall :" + userName);
}
}
#endregion
#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 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;
}
}