XiangTan_JiaoJie_Bak/DM_Weight/HIKVISION/CHKFunction.cs

251 lines
8.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DM_Weight.util;
using PreviewDemo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using DM_Weight.Port;
using log4net;
using System.Runtime.InteropServices;
namespace DM_Weight.HIKVISION
{
public class CHKFunction
{
private readonly ILog logger = LogManager.GetLogger(typeof(CHKFunction));
private bool m_bInitSDK = false;
private Int32 m_lRealHandle = -1;
public static int HKUserId = -1;
private uint iLastErr = 0;
private string str;
public CHCNetSDK.NET_DVR_USER_LOGIN_INFO struLogInfo;
public CHCNetSDK.NET_DVR_DEVICEINFO_V40 DeviceInfo;
public CHCNetSDK.NET_DVR_TIME m_struTimeCfg;
private System.ComponentModel.Container components = null;
public CHKFunction()
{
logger.Info("CHKFunction");
HIKInit();
HIKLogin();
}
public bool HIKInit()
{
try
{
logger.Info("HIKInit");
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
logger.Info($"HIKInit-{m_bInitSDK}");
if (m_bInitSDK == false)
{
//MessageBox.Show("NET_DVR_Init error!");
//return;
logger.Info("NET_DVR_Init error!");
}
else
{
//保存SDK日志 To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
}
}
catch (Exception ex)
{
logger.Info($"HIKInit Exception:{ex.Message}");
}
return m_bInitSDK;
}
public int HIKLogin()
{
try
{
logger.Info("HIKLogin");
string ip = ReadApp.ReadAppSetting("HIKIP");
string port = ReadApp.ReadAppSetting("HIKPort");
string userName = ReadApp.ReadAppSetting("HIKUser");
string password = ReadApp.ReadAppSetting("HIKPassword");
if (HKUserId < 0)
{
struLogInfo = new CHCNetSDK.NET_DVR_USER_LOGIN_INFO();
//设备IP地址或者域名
byte[] byIP = System.Text.Encoding.Default.GetBytes(ip);
struLogInfo.sDeviceAddress = new byte[129];
byIP.CopyTo(struLogInfo.sDeviceAddress, 0);
//设备用户名
byte[] byUserName = System.Text.Encoding.Default.GetBytes(userName);
struLogInfo.sUserName = new byte[64];
byUserName.CopyTo(struLogInfo.sUserName, 0);
//设备密码
byte[] byPassword = System.Text.Encoding.Default.GetBytes(password);
struLogInfo.sPassword = new byte[64];
byPassword.CopyTo(struLogInfo.sPassword, 0);
struLogInfo.wPort = ushort.Parse(port);//设备服务端口号
//if (LoginCallBack == null)
//{
// LoginCallBack = new CHCNetSDK.LOGINRESULTCALLBACK(cbLoginCallBack);//注册回调函数
//}
//struLogInfo.cbLoginResult = LoginCallBack;
struLogInfo.bUseAsynLogin = false; //是否异步登录0- 否1- 是
DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V40();
//登录设备 Login the device
HKUserId = CHCNetSDK.NET_DVR_Login_V40(ref struLogInfo, ref DeviceInfo);
if (HKUserId < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_Login_V40 failed, error code= " + iLastErr; //登录失败,输出错误号
logger.Info(str);
}
else
{
//登录成功
//MessageBox.Show("Login Success!");
logger.Info("Login Success!");
}
}
else
{
//注销登录 Logout the device
if (m_lRealHandle >= 0)
{
//MessageBox.Show("Please stop live view firstly");
logger.Info("Please stop live view firstly");
}
if (!CHCNetSDK.NET_DVR_Logout(HKUserId))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_Logout failed, error code= " + iLastErr;
logger.Info(str);
}
HKUserId = -1;
}
}
catch (Exception ex)
{
logger.Info($"HIKLogin Exception:{ex.Message}");
}
return HKUserId;
}
public void HIKLoginOut()
{
try
{
if (m_lRealHandle >= 0)
{
bool stopRealPlay = CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
logger.Info($"录像机NET_DVR_StopRealPlay接口返回{stopRealPlay}");
}
if (HKUserId >= 0)
{
bool logout = CHCNetSDK.NET_DVR_Logout(HKUserId);
logger.Info($"录像机NET_DVR_Logout接口返回{logout}");
}
if (m_bInitSDK == true)
{
bool cleanUp = CHCNetSDK.NET_DVR_Cleanup();
logger.Info($"录像机NET_DVR_Cleanup接口返回{cleanUp}");
}
}
catch (Exception ex)
{
logger.Info($"HIKLoginOut Exception{ex.Message}");
}
}
public bool HIKStartDVRRecord()
{
bool isStart = false;
try
{
isStart = CHCNetSDK.NET_DVR_StartDVRRecord(HKUserId, 0xffff, 0);
logger.Info($"录像机NET_DVR_StartDVRRecord接口返回{isStart}");
}
catch (Exception ex)
{
logger.Info($"HIKStartDVRRecord Exception{ex.Message}");
}
return isStart;
}
public bool HIKStopDVRRecord()
{
bool isStop = false;
try
{
isStop = CHCNetSDK.NET_DVR_StopDVRRecord(HKUserId, 0xffff);
logger.Info($"录像机NET_DVR_StopDVRRecord接口返回{isStop}");
}
catch (Exception ex)
{
logger.Info($"HIKStopDVRRecord Exception{ex.Message}");
}
return isStop;
}
public void HIK_DVR_TIME()
{
try
{
UInt32 dwReturn = 0;
Int32 nSize = Marshal.SizeOf(m_struTimeCfg);
IntPtr ptrTimeCfg = Marshal.AllocHGlobal(nSize);
Marshal.StructureToPtr(m_struTimeCfg, ptrTimeCfg, false);
if (CHCNetSDK.NET_DVR_GetDVRConfig(HKUserId, CHCNetSDK.NET_DVR_GET_TIMECFG, -1, ptrTimeCfg, (UInt32)nSize, ref dwReturn))
{
m_struTimeCfg = (CHCNetSDK.NET_DVR_TIME)Marshal.PtrToStructure(ptrTimeCfg, typeof(CHCNetSDK.NET_DVR_TIME));
logger.Info($"录像机时间接口{Convert.ToString(m_struTimeCfg.dwYear)}- {Convert.ToString(m_struTimeCfg.dwMonth)}- {Convert.ToString(m_struTimeCfg.dwDay)}- {Convert.ToString(m_struTimeCfg.dwHour)}- {Convert.ToString(m_struTimeCfg.dwMinute)}- {Convert.ToString(m_struTimeCfg.dwSecond)}");
}
}
catch (Exception ex)
{
logger.Info($"HIK_DVR_TIME Exception{ex.Message}");
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
//public void Dispose()
//{
// if (m_lRealHandle >= 0)
// {
// bool stopRealPlay= CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
// }
// if (HKUserId >= 0)
// {
// CHCNetSDK.NET_DVR_Logout(HKUserId);
// }
// if (m_bInitSDK == true)
// {
// CHCNetSDK.NET_DVR_Cleanup();
// }
//}
}
}