@@ -28,139 +28,223 @@ namespace Quanjiang.DigitalScholl.JobService
private ADR_RecordIBLL adrRecordIbll = new ADR_RecordBLL();
//海康考勤机回调
public CHCNetSDK.LOGINRESULTCALLBACK LoginCallBack = null;
public CHCNetSDK.LoginResultCallBack LoginCallBack = null;
private CHCNetSDK.EXCEPYIONCALLBACK m_fExceptionCB = null;
private CHCNetSDK.MSGCallBack_V31 m_falarmData_V31 = null;
public QuanjiangDigitalSchollJobService()
{
InitializeComponent();
GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnString");
try
{
InitializeComponent();
GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnString");
}
catch (Exception e)
{
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
adrDeviceLogEntity.LogData = "服务初始化失败:" + e.Message + ",详细:" + e.StackTrace;
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
}
}
protected override void OnStart(string[] args)
{
var options = new BackgroundJobServerOptions
{
ServerName = "QuanjiangDigitalSchollJobService"
};
_server = new BackgroundJobServer(options);
//海康考勤机对接
#region 海康考勤机对接
//初始化SDK
bool m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK)
try
{
//保存SDK日志 To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\HKSdkLog\\", true);
var options = new BackgroundJobServerOptions
{
ServerName = "QuanjiangDigitalSchollJobService"
};
_server = new BackgroundJobServer(options);
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["enableHK"])&&Convert.ToBoolean(ConfigurationManager.AppSettings["enableHK"]))
{
//海康考勤机对接
#region 海康考勤机对接
//初始化SDK
bool m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK)
{
//保存SDK日志 To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\HKSdkLog\\", true);
//设置透传报警信息类型
CHCNetSDK.NET_DVR_LOCAL_GENERAL_CFG struLocalCfg = new CHCNetSDK.NET_DVR_LOCAL_GENERAL_CFG();
struLocalCfg.byAlarmJsonPictureSeparate = 1;//控制JSON透传报警数据和图片是否分离,0-不分离(COMM_VCA_ALARM返回),1-分离(分离后走COMM_ISAPI_ALARM回调返回)
//设置透传报警信息类型
CHCNetSDK.NET_DVR_LOCAL_GENERAL_CFG struLocalCfg = new CHCNetSDK.NET_DVR_LOCAL_GENERAL_CFG();
struLocalCfg.byAlarmJsonPictureSeparate = 1;//控制JSON透传报警数据和图片是否分离,0-不分离(COMM_VCA_ALARM返回),1-分离(分离后走COMM_ISAPI_ALARM回调返回)
Int32 nSize = Marshal.SizeOf(struLocalCfg);
IntPtr ptrLocalCfg = Marshal.AllocHGlobal(nSize);
Marshal.StructureToPtr(struLocalCfg, ptrLocalCfg, false);
Int32 nSize = Marshal.SizeOf(struLocalCfg);
IntPtr ptrLocalCfg = Marshal.AllocHGlobal(nSize);
Marshal.StructureToPtr(struLocalCfg, ptrLocalCfg, false);
if (!CHCNetSDK.NET_DVR_SetSDKLocalCfg(17, ptrLocalCfg)) //NET_DVR_LOCAL_CFG_TYPE_GENERAL
{
string strErr = "海康NET_DVR_SetSDKLocalCfg 失败, error code= " + CHCNetSDK.NET_DVR_GetLastError();
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
adrDeviceLogEntity.LogData = strErr;
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
}
Marshal.FreeHGlobal(ptrLocalCfg);
if (!CHCNetSDK.NET_DVR_SetSDKLocalCfg(17, ptrLocalCfg)) //NET_DVR_LOCAL_CFG_TYPE_GENERAL
{
string strErr = "海康NET_DVR_SetSDKLocalCfg 失败, error code= " + CHCNetSDK.NET_DVR_GetLastError();
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
adrDeviceLogEntity.LogData = strErr;
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
}
Marshal.FreeHGlobal(ptrLocalCfg);
//设置异常消息回调函数
if (m_fExceptionCB == null)
{
m_fExceptionCB = new CHCNetSDK.EXCEPYIONCALLBACK(cbExceptionCB);
}
CHCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, IntPtr.Zero, m_fExceptionCB, IntPtr.Zero);
//设置异常消息回调函数
if (m_fExceptionCB == null)
{
m_fExceptionCB = new CHCNetSDK.EXCEPYIONCALLBACK(cbExceptionCB);
}
CHCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, IntPtr.Zero, m_fExceptionCB, IntPtr.Zero);
//设置报警回调函数
if (m_falarmData_V31 == null)
{
m_falarmData_V31 = new CHCNetSDK.MSGCallBack_V31(MsgCallback_V31);
}
CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(m_falarmData_V31, IntPtr.Zero);
//设置报警回调函数
if (m_falarmData_V31 == null)
{
m_falarmData_V31 = new CHCNetSDK.MSGCallBack_V31(MsgCallback_V31);
}
CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(m_falarmData_V31, IntPtr.Zero);
//获取设备
var devicelist = adrDeviceIbll.GetAllUnconnectedList(true);
//登录设备
foreach (var deviceEntity in devicelist)
{
CHCNetSDK.NET_DVR_USER_LOGIN_INFO struLogInfo = new CHCNetSDK.NET_DVR_USER_LOGIN_INFO();
//设备IP地址或者域名
byte[] byIP = System.Text.Encoding.Default.GetBytes(deviceEntity.IpAddress);
struLogInfo.sDeviceAddress = new byte[129];
byIP.CopyTo(struLogInfo.sDeviceAddress, 0);
//设备用户名
byte[] byUserName = System.Text.Encoding.Default.GetBytes(deviceEntity.AdminAccount);
struLogInfo.sUserName = new byte[64];
byUserName.CopyTo(struLogInfo.sUserName, 0);
//设备密码
byte[] byPassword = System.Text.Encoding.Default.GetBytes(deviceEntity.AdminPwd);
struLogInfo.sPassword = new byte[64];
byPassword.CopyTo(struLogInfo.sPassword, 0);
struLogInfo.wPort = ushort.Parse(deviceEntity.PortNumber.ToString());//设备服务端口号
struLogInfo.cbLoginResult = LoginCallBack;
struLogInfo.bUseAsynLogin = true; //是否异步登录:0- 否,1- 是
//用户数据,回调时可用
//获取大小
uint dwSize = (uint)Marshal.SizeOf(deviceEntity);
//分配内存
IntPtr ptrCond = Marshal.AllocHGlobal((int)dwSize);
//写入内存
Marshal.StructureToPtr(deviceEntity, ptrCond, false);
//用户数据,回调时可用
struLogInfo.pUser = ptrCond;
if ((struLogInfo.bUseAsynLogin == true) && (LoginCallBack == null))
//获取设备
var devicelist = adrDeviceIbll.GetAllUnconnectedList(true);
//登录设备
foreach (var deviceEntity in devicelist)
{
CHCNetSDK.NET_DVR_USER_LOGIN_INFO struLogInfo = new CHCNetSDK.NET_DVR_USER_LOGIN_INFO();
//设备IP地址或者域名
byte[] byIP = System.Text.Encoding.Default.GetBytes(deviceEntity.IpAddress);
struLogInfo.sDeviceAddress = new byte[129];
byIP.CopyTo(struLogInfo.sDeviceAddress, 0);
//设备用户名
byte[] byUserName = System.Text.Encoding.Default.GetBytes(deviceEntity.AdminAccount);
struLogInfo.sUserName = new byte[64];
byUserName.CopyTo(struLogInfo.sUserName, 0);
//设备密码
byte[] byPassword = System.Text.Encoding.Default.GetBytes(deviceEntity.AdminPwd);
struLogInfo.sPassword = new byte[64];
byPassword.CopyTo(struLogInfo.sPassword, 0);
struLogInfo.wPort = ushort.Parse(deviceEntity.PortNumber.ToString());//设备服务端口号
//用户数据,回调时可用
//获取大小
//uint dwSize = (uint)Marshal.SizeOf(deviceEntity);
////分配内存
//IntPtr ptrCond = Marshal.AllocHGlobal((int)dwSize);
////写入内存
//Marshal.StructureToPtr(deviceEntity, ptrCond, false);
////用户数据,回调时可用
//struLogInfo.pUser = ptrCond;
//struLogInfo.bUseAsynLogin = false; //是否异步登录:0- 否,1- 是
//struLogInfo.cbLoginResult = new CHCNetSDK.LoginResultCallBack(AsynLoginMsgCallback);//注册回调函数;
struLogInfo.byLoginMode = 0; //0-Private, 1-ISAPI, 2-自适应
struLogInfo.byHttps = 0; //0-不适用tls,1-使用tls 2-自适应
CHCNetSDK.NET_DVR_DEVICEINFO_V40 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V40();
DeviceInfo.struDeviceV30.sSerialNumber = new byte[CHCNetSDK.SERIALNO_LEN];
//登录设备 Login the device
var lUserID = CHCNetSDK.NET_DVR_Login_V40(ref struLogInfo, ref DeviceInfo);
//释放内存
//Marshal.FreeHGlobal(ptrCond);
//同步登录方法
string strLoginCallBack = "";
if (lUserID < 0)
{
uint iErrCode = CHCNetSDK.NET_DVR_GetLastError();
strLoginCallBack = "登录设备失败,lUserID:" + lUserID + ",错误号:" + iErrCode;
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
if (deviceEntity != null && !string.IsNullOrEmpty(deviceEntity.Id))
{
adrDeviceLogEntity.DeviceId = deviceEntity.Id;
strLoginCallBack += ",设备IP:" + deviceEntity.IpAddress;
}
adrDeviceLogEntity.LogData = strLoginCallBack;
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
}
else
{
//登录成功
strLoginCallBack = "登录设备成功,lUserID:" + lUserID;
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
if (deviceEntity != null && !string.IsNullOrEmpty(deviceEntity.Id))
{
adrDeviceLogEntity.DeviceId = deviceEntity.Id;
strLoginCallBack += ",设备IP:" + deviceEntity.IpAddress;
}
adrDeviceLogEntity.LogData = strLoginCallBack;
adrDeviceLogEntity.LogType = "0";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
//设备布防
CHCNetSDK.NET_DVR_SETUPALARM_PARAM struAlarmParam = new CHCNetSDK.NET_DVR_SETUPALARM_PARAM();
struAlarmParam.dwSize = (uint)Marshal.SizeOf(struAlarmParam);
struAlarmParam.byLevel = 1; //0- 一级布防,1- 二级布防
struAlarmParam.byAlarmInfoType = 1;//智能交通设备有效,新报警信息类型
struAlarmParam.byDeployType = 0;
struAlarmParam.byFaceAlarmDetection = 1;//1-人脸侦测
var m_lUserID = CHCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, ref struAlarmParam);
if (m_lUserID < 0)
{
string strErr = "布防失败,错误号:" + CHCNetSDK.NET_DVR_GetLastError(); //布防失败,输出错误号
adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
if (deviceEntity != null && !string.IsNullOrEmpty(deviceEntity.Id))
{
adrDeviceLogEntity.DeviceId = deviceEntity.Id;
strErr += ",设备IP:" + deviceEntity.IpAddress;
}
adrDeviceLogEntity.LogData = strErr;
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
//更新设备状态
deviceEntity.DOnlineStatus = false;
}
else
{
adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
string strNotice = "布防成功";
if (deviceEntity != null && !string.IsNullOrEmpty(deviceEntity.Id))
{
adrDeviceLogEntity.DeviceId = deviceEntity.Id;
strNotice += ",设备IP:" + deviceEntity.IpAddress;
}
adrDeviceLogEntity.LogData = strNotice;
adrDeviceLogEntity.LogType = "0";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
//更新设备状态
deviceEntity.DOnlineStatus = true;
}
//更新设备状态
adrDeviceIbll.SaveEntity(deviceEntity.Id, deviceEntity);
}
}
}
else//初始化失败
{
LoginCallBack = new CHCNetSDK.LOGINRESULTCALLBACK(cbLoginCallBack);//注册回调函数
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
adrDeviceLogEntity.LogData = "海康SDK初始化失败";
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
}
struLogInfo.byLoginMode = 0; //0-Private, 1-ISAPI, 2-自适应
struLogInfo.byHttps = 0; //0-不适用tls,1-使用tls 2-自适应
CHCNetSDK.NET_DVR_DEVICEINFO_V40 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V40();
//登录设备 Login the device
CHCNetSDK.NET_DVR_Login_V40(ref struLogInfo, ref DeviceInfo);
//释放内存
Marshal.FreeHGlobal(ptrCond);
//同步登录方法
//if (m_lUserID < 0)
//{
// iLastErr = CHCNetSDK.NET_DVR_GetLastError();
// strErr = "NET_DVR_Login_V30 failed, error code= " + iLastErr; //登录失败,输出错误号 Failed to login and output the error code
// MessageBox.Show(strErr);
//}
//else
//{
// //登录成功
// iDeviceNumber++;
// string str1 = "" + m_lUserID;
// listViewDevice.Items.Add(new ListViewItem(new string[] { str1, textBoxIP.Text, "未布防" }));//将已注册设备添加进列表
//}
#endregion
}
}
else//初始化失败
catch (Exception e)
{
ADR_DeviceLogEntity adrDeviceLogEntity = new ADR_DeviceLogEntity();
adrDeviceLogEntity.Create();
adrDeviceLogEntity.LogData = "海康SDK初始化失败";
adrDeviceLogEntity.LogData = "服务启动失败:" + e.Message + ",详细:" + e.StackTrace;
adrDeviceLogEntity.LogType = "2";
adrDeviceLogIbll.SaveEntity(null, adrDeviceLogEntity);
}
#endregion
}
/// <summary>
/// 海康登录回调
@@ -169,7 +253,7 @@ namespace Quanjiang.DigitalScholl.JobService
/// < param name="dwResult"></param>
/// <param name = "lpDeviceInfo" ></ param >
/// < param name="pUser"></param>
public void cbLoginCallBack(int lUserID, int dwResult, IntPtr lpDeviceInfo, IntPtr pUser)
public void AsynLoginMsgCallback(Int32 lUserID, UInt32 dwResult, ref CHCNetSDK.NET_DVR_DEVICEINFO_V30 lpDeviceInfo, IntPtr pUser)
{
try
{