HuNan_NOSqlSugar/DM_Weight/HIKVISION/CHKFunction.cs

162 lines
5.0 KiB
C#
Raw Normal View History

2024-07-06 10:01:30 +08:00
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;
namespace DM_Weight.HIKVISION
{
internal class CHKFunction
{
private bool m_bInitSDK = false;
private static int 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;
private System.ComponentModel.Container components = null;
public bool HIKInit()
{
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
//MessageBox.Show("NET_DVR_Init error!");
//return;
}
else
{
//保存SDK日志 To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);
}
return m_bInitSDK;
}
public int 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; //登录失败,输出错误号
MessageBox.Show(str);
}
else
{
//登录成功
//MessageBox.Show("Login Success!");
}
}
else
{
//注销登录 Logout the device
if (m_lRealHandle >= 0)
{
//MessageBox.Show("Please stop live view firstly");
}
if (!CHCNetSDK.NET_DVR_Logout(HKUserId))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_Logout failed, error code= " + iLastErr;
}
HKUserId = -1;
}
return HKUserId;
}
public void HIKLoginOut()
{
if (m_lRealHandle >= 0)
{
CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
}
if (HKUserId >= 0)
{
CHCNetSDK.NET_DVR_Logout(HKUserId);
}
if (m_bInitSDK == true)
{
CHCNetSDK.NET_DVR_Cleanup();
}
}
public bool HIKStartDVRRecord()
{
bool isStart= CHCNetSDK.NET_DVR_StartDVRRecord(HKUserId, 5, 2);
return isStart;
}
public bool HIKStopDVRRecord()
{
bool isStart = CHCNetSDK.NET_DVR_StopDVRRecord(HKUserId, 5);
return isStart;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
public void Dispose()
{
if (m_lRealHandle >= 0)
{
CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
}
if (HKUserId >= 0)
{
CHCNetSDK.NET_DVR_Logout(HKUserId);
}
if (m_bInitSDK == true)
{
CHCNetSDK.NET_DVR_Cleanup();
}
//base.Dispose(disposing);
}
}
}