python 调用海康SDK,登录,设置回调,布防,撤防,注销以及释放SDK

import os
import ctypes
import datetime
import time
#获取所有的库文件到一个列表
path = "/home/caobin/chike/chike/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/"
so_list = []
def add_so(path,so_list):
    files = os.listdir(path)
    for file in files:
        if not os.path.isdir(path+file):
            if file.endswith(".so"): so_list.append(path+file)
        else:
            add_so(path+file+"/",so_list)
add_so(path,so_list)

for lib in so_list:
    print(lib)

lUserID = 0
m_lAlarmHandle = 0
def callCpp(func_name,*args):
    for so_lib in so_list:
        # print(so_lib)
        try:
            lib = ctypes.cdll.LoadLibrary(so_lib)
            try:
                value = eval("lib.%s"%func_name)(*args)
                print("调用的库:"+so_lib)
                print("执行成功,返回值:"+str(value))
                return value
            except:
                continue
        except:
            print("库文件载入失败:"+so_lib)
            continue
    print("没有找到接口!")
    return False

#定义结构体
class LPNET_DVR_DEVICEINFO_V30(ctypes.Structure):
    _fields_ = [
        ("sSerialNumber", ctypes.c_byte * 48),
        ("byAlarmInPortNum", ctypes.c_byte),
        ("byAlarmOutPortNum", ctypes.c_byte),
        ("byDiskNum", ctypes.c_byte),
        ("byDVRType", ctypes.c_byte),
        ("byChanNum", ctypes.c_byte),
        ("byStartChan", ctypes.c_byte),
        ("byAudioChanNum", ctypes.c_byte),
        ("byIPChanNum", ctypes.c_byte),
        ("byZeroChanNum", ctypes.c_byte),
        ("byMainProto", ctypes.c_byte),
        ("bySubProto", ctypes.c_byte),
        ("bySupport", ctypes.c_byte),
        ("bySupport1", ctypes.c_byte),
        ("bySupport2", ctypes.c_byte),
        ("wDevType", ctypes.c_uint16),
        ("bySupport3", ctypes.c_byte),
        ("byMultiStreamProto", ctypes.c_byte),
        ("byStartDChan", ctypes.c_byte),
        ("byStartDTalkChan", ctypes.c_byte),
        ("byHighDChanNum", ctypes.c_byte),
        ("bySupport4", ctypes.c_byte),
        ("byLanguageType", ctypes.c_byte),
        ("byVoiceInChanNum", ctypes.c_byte),
        ("byStartVoiceInChanNo", ctypes.c_byte),
        ("byRes3", ctypes.c_byte * 2),
        ("byMirrorChanNum", ctypes.c_byte),
        ("wStartMirrorChanNo", ctypes.c_uint16),
        ("byRes2", ctypes.c_byte * 2)]

#报警设备信息结构体
class NET_DVR_ALARMER(ctypes.Structure):
    _fields_ = [
        ("byUserIDValid", ctypes.c_byte),
        ("bySerialValid", ctypes.c_byte),
        ("byVersionValid", ctypes.c_byte),
        ("byDeviceNameValid", ctypes.c_byte),
        ("byMacAddrValid", ctypes.c_byte),
        ("byLinkPortValid", ctypes.c_byte),
        ("byDeviceIPValid", ctypes.c_byte),
        ("bySocketIPValid", ctypes.c_byte),
        ("lUserID", ctypes.c_long),
        ("sSerialNumber", ctypes.c_byte * 48),
        ("dwDeviceVersion", ctypes.c_uint32),
        ("sDeviceName", ctypes.c_char * 32),
        ("byMacAddr", ctypes.c_char * 6),
        ("wLinkPort", ctypes.c_uint16),
        ("sDeviceIP", ctypes.c_char * 128),
        ("sSocketIP", ctypes.c_char * 128),
        ("byIpProtocol", ctypes.c_byte),
        ("byRes2", ctypes.c_byte * 11)
    ]

#####布防
class NET_DVR_SETUPALARM_PARAM(ctypes.Structure):
    _fields_ = [
        ("dwSize",ctypes.c_uint32),
        ("beLevel",ctypes.c_byte),
        ("byAlarmInfoType",ctypes.c_byte),
        ("byRetAlarmTypeV40",ctypes.c_byte),
        ("byRetDevInfoVersion",ctypes.c_byte),
        ("byRetVQDAlarmType",ctypes.c_byte),
        ("byFaceAlarmDetection",ctypes.c_byte),
        ("bySupport",ctypes.c_byte),
        ("byBrokenNetHttp",ctypes.c_byte),
        ("wTaskNo",ctypes.c_uint16),
        ("byDeployType",ctypes.c_byte),
        ("byRes1",ctypes.c_byte*3),
        ("byAlarmTypeURL",ctypes.c_byte),
        ("byCustomCtrl",ctypes.c_byte)
    ]


#用户注册设备
def NET_DVR_Login_V30(sDVRIP = "192.168.1.63",wDVRPort = 8000,sUserName = "admin",sPassword = "guoji123"):
    init_res = callCpp("NET_DVR_Init")#SDK初始化
    if init_res:
        print("SDK初始化成功")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("SDK初始化错误:" + str(error_info))
        return False

    set_overtime = callCpp("NET_DVR_SetConnectTime",5000,4)#设置超时
    if set_overtime:
        print("设置超时时间成功")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("设置超时错误信息:" + str(error_info))
        return False

    #用户注册设备
    #c++传递进去的是byte型数据,需要转成byte型传进去,否则会乱码
    sDVRIP = bytes(sDVRIP,"ascii")
    sUserName = bytes(sUserName,"ascii")
    sPassword = bytes(sPassword,"ascii")
    DeviceInfo = LPNET_DVR_DEVICEINFO_V30()
    DeviceInfoRef = ctypes.byref(DeviceInfo)
    lUserID = callCpp("NET_DVR_Login_V30",sDVRIP,wDVRPort,sUserName,sPassword,DeviceInfoRef)
    print("登录结果:"+str(lUserID))
    if lUserID == -1:
        error_info = callCpp("NET_DVR_GetLastError")
        print("登录错误信息:" + str(error_info))
        return error_info
    else:
        return lUserID


##设置回调,由于在回调函数这里char*指针传不过去,因此改成了由C++去实现
def NET_DVR_SetDVRMessageCallBack_V30():
    CALLFUNC = ctypes.CFUNCTYPE(ctypes.c_void_p,ctypes.c_long,ctypes.POINTER(NET_DVR_ALARMER))#,ctypes.c_char
    if callCpp("NET_DVR_SetDVRMessageCallBack_V30",CALLFUNC(MsgCallback),None):print("设置回调函数成功!")
    else:print("回调函数设置错误:"+ str(callCpp("NET_DVR_GetLastError")))
    # iListenHandle = callCpp("NET_DVR_StartListen_V30", sLocalIP,wLocalPort,CALLFUNC(MSGCallBack),pUserData)



#回调函数接口
def MsgCallback(lCommand,net_dvr_alarm):
    print("回调函数目前的问题:参数中参数不完整,char*数据不能传递,进行中的工作是把char*传递并转成结构体")
    print("回调函数执行,lcommand的值:"+str(lCommand))
    print("返回的结构体")
    print(net_dvr_alarm)
    print("结构体中的变量,byUserIDValid输出:"+str(net_dvr_alarm.contents.byUserIDValid))
    print(datetime.datetime.now())
    print("\n")
    return 0

#####布防
def NET_DVR_SetupAlarmChan_V41():
    struAlarmParam = NET_DVR_SETUPALARM_PARAM()
    struAlarmParamSize = ctypes.sizeof(struAlarmParam)
    print("结构体大小:"+str(struAlarmParamSize))
    struAlarmParam.dwSize = struAlarmParamSize#结构体大小
    struAlarmParam.beLevel = 1#0 一级布防,1 二级布防
    # struAlarmParam.byAlarmInfoType = 1#智能交通设备有效
    struAlarmParam.byFaceAlarmDetection = 1#人脸侦测报警
    struAlarmParam_p = ctypes.byref(struAlarmParam)
    m_lAlarmHandle = callCpp("NET_DVR_SetupAlarmChan_V41",lUserID,struAlarmParam_p)
    print("布防结果:"+str(m_lAlarmHandle))
    if m_lAlarmHandle < 0:
        print("错误信息:"+str(callCpp("NET_DVR_GetLastError")))
        return False
    else:print("布防成功")

#撤防
def NET_DVR_CloseAarmChan_V30():
    res = callCpp("NET_DVR_CloseAarmChan_V30",m_lAlarmHandle)
    if res<0:
        print("撤防失败:"+str(callCpp("NET_DVR_GetLastError")))
    else:
        print("撤防成功")

#释放SDK 资源

def NET_DVR_Cleanup():
    res = callCpp("NET_DVR_Cleanup")
    if res<0:
        print("SDK资源释放失败:"+str(callCpp("NET_DVR_GetLastError")))
    else:
        print("SDK资源成功释放!!")

####用户追注销
def NET_DVR_Logout_V30():
    if callCpp("NET_DVR_Logout_V30",lUserID):
        print("用户已经成功注销")
    else:
        print("注销失败:"+str(callCpp("NET_DVR_GetLastError")))
    init_res = callCpp("NET_DVR_Cleanup")#释放SDK资源
    if init_res:
        print("SDK资源释放成功")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("错误信息:" + str(error_info))
        return False

NET_DVR_Login_V30()
NET_DVR_SetDVRMessageCallBack_V30()
time.sleep(100)##sleep一会儿,等待SDK回调
NET_DVR_SetupAlarmChan_V41()
NET_DVR_CloseAarmChan_V30()
NET_DVR_Logout_V30()
NET_DVR_Cleanup()
发布了33 篇原创文章 · 获赞 14 · 访问量 12万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览