01 使用 海康SDK 对车辆监控设备进行布防
前言
最近朋友的需求, 是需要使用 海康sdk 连接海康设备, 进行数据的获取, 比如 进出车辆, 进出人员
这一部分是 对接海康车辆设备 获取相关布防事件, 并进行入库 的相关处理
测试用例
主要的处理如下
1. 设备登陆, 不同的设备可能兼容的 登陆方式不一样, 我这里设备需要使用 NET_DRV_Login_V30
2. 设置布防回调函数
3. 对设备进行布防
4. 布防期间 sleep, 由布防回调函数处理
5. 撤销布防, 退出登录, 进入 下一个循环
private static void attachDeviceAndMonitor(String devPrefix) {
int attachDeviceAndMonitor = getIntConfigByDevPreference("global", "attachDeviceAndMonitor", 0);
if (attachDeviceAndMonitor == 0) {
return;
}
try {
int sleepInMillis = getIntConfigByDevPreference(devPrefix, "alarmWaitSleepInMs", 10000);
while (isRunning) {
int lUserID = -1;
int lHandle = -1;
String vehicleLogPrefix = "-1";
try {
//---------------------------------------
// 注册设备
//登录参数,包括设备地址、登录用户、密码等
NET_DVR_USER_LOGIN_INFO struLoginInfo = new NET_DVR_USER_LOGIN_INFO();
struLoginInfo.bUseAsynLogin = false; //同步登录方式
String host = getStrConfigByDevPreference(devPrefix, "ip", "192.168.1.13");
short port = (short) getIntConfigByDevPreference(devPrefix, "port", 8000);
String sUserName = getStrConfigByDevPreference(devPrefix, "username", "admin"); //设备登录用户名
String sPassword = getStrConfigByDevPreference(devPrefix, "password", "abcd1234"); //设备登录密码
String tableName = getStrConfigByDevPreference(devPrefix, "tableName", "stats_vehicle_flow_log"); //存储明细表名
//设备信息, 输出参数
vehicleLogPrefix = devPrefix + ":" + host;
NET_DVR_DEVICEINFO_V30 struDeviceInfoV30 = new NET_DVR_DEVICEINFO_V30();
lUserID = hCNetSDK.NET_DVR_Login_V30(host, port, sUserName, sPassword, struDeviceInfoV30);
System.out.println(vehicleLogPrefix + "登陆结果为 : " + lUserID);
if (lUserID < 0) {
System.out.println(vehicleLogPrefix + "登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
Thread.sleep(sleepInMillis);
continue;
}
int vehicleDevFetchCountPerLogin = getIntConfigByDevPreference(devPrefix, "vehicleDevFetchCountPerLogin", 10);
for (int i = 0; i < vehicleDevFetchCountPerLogin; i++) {
if (!isRunning) {
break;
}
try {
System.out.println(String.format("%s 在监听, 当前时间 %s", vehicleLogPrefix, DateFormatUtils.format(new Date())));
//设置报警回调函数
/*注:多台设备对接时也只需要调用一次设置一个回调函数,不支持不同设备的事件在不同的回调函数里面返回*/
hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(new StatsVehicleFlowCallback(devPrefix, host, tableName), null);
//启用布防
NET_DVR_SETUPALARM_PARAM_V50 struAlarmParam = new NET_DVR_SETUPALARM_PARAM_V50();
struAlarmParam.dwSize = struAlarmParam.size();
struAlarmParam.byLevel = (byte) getIntConfigByDevPreference(devPrefix, "byLevel", 1); //布防等级
struAlarmParam.byAlarmInfoType = (byte) getIntConfigByDevPreference(devPrefix, "byAlarmInfoType", 1); // 智能交通报警信息上传类型:0- 老报警信息(NET_DVR_PLATE_RESULT),1- 新报警信息(NET_ITS_PLATE_RESULT)
struAlarmParam.byDeployType = (byte) getIntConfigByDevPreference(devPrefix, "byDeployType", 1); //布防类型:0-客户端布防,1-实时布防
struAlarmParam.write();
//其他报警布防参数不需要设置,不支持
lHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V50(lUserID, struAlarmParam, null, 0);
System.out.println(vehicleLogPrefix + "NET_DVR_SetupAlarmChan_V50_byLevel : " + struAlarmParam.byLevel
+ " ,byAlarmInfoType : " + struAlarmParam.byAlarmInfoType + ", byDeployType : " + struAlarmParam.byDeployType + " 结果为 : " + lHandle);
if (lHandle < 0) {
System.out.println(String.format(vehicleLogPrefix + "NET_DVR_SetupAlarmChan_V41 error, %d\n", hCNetSDK.NET_DVR_GetLastError()));
Thread.sleep(sleepInMillis);
continue;
}
Thread.sleep(sleepInMillis);
} catch (Exception e) {
e.printStackTrace();
} finally {
//撤销布防上传通道
if (lHandle >= 0) {
if (!hCNetSDK.NET_DVR_CloseAlarmChan_V30(lHandle)) {
System.out.println(String.format(vehicleLogPrefix + "NET_DVR_CloseAlarmChan_V30 error, %d\n", hCNetSDK.NET_DVR_GetLastError()));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//注销用户
if (lUserID >= 0) {
hCNetSDK.NET_DVR_Logout(lUserID);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
hCNetSDK.NET_DVR_Cleanup();
}
}
private static String getStrConfigByDevPreference(String devPrefix, String configKey, String defaultValue) {
String configValue = ConfigUtils.getString(String.format("%s.%s", devPrefix, configKey));
if (configValue != null) {
return configValue;
}
return ConfigUtils.getString(configKey, defaultValue);
}
private static int getIntConfigByDevPreference(String devPrefix, String configKey, int defaultValue) {
int configValue = ConfigUtils.getInt(String.format("%s.%s", devPrefix, configKey));
if (configValue != 0) {
return configValue;
}
return ConfigUtils.getInt(configKey, defaultValue);
}
StatsVehicleFlowCallback 处理如下, 对于事件信息的入库处理
public class StatsVehicleFlowCallback implements FMSGCallBack_V31 {
private String devPrefix;
private String host;
private String devLogPrefix;
private String tableName;
public StatsVehicleFlowCallback(String devPrefix, String host, String tableName) {
this.devPrefix = devPrefix;
this.host = host;
devLogPrefix = devPrefix + ":" + host;
this.tableName = tableName;
}
@Override
public boolean invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
long startTs = System.currentTimeMillis();
int insertResult = -1;
try {
StatsVehicleFlowLog vehicleFlowLog = new StatsVehicleFlowLog();
Date now = new Date();
vehicleFlowLog.setTheDay(DateFormatUtils.format(now, "yyyy-MM-dd"));
vehicleFlowLog.setVehiclePrefix(devPrefix);
vehicleFlowLog.setHost(host);
vehicleFlowLog.setAlarmType(String.valueOf(lCommand));
vehicleFlowLog.setRemark("");
vehicleFlowLog.setCreatedAt(DateFormatUtils.format(now));
String insertSql = MysqlUtils.assembleInsertSql(tableName, vehicleFlowLog.toJSON());
insertResult = MysqlUtils.executeUpdate(insertSql);
} catch (Exception e) {
e.printStackTrace();
} finally {
long spentTs = System.currentTimeMillis() - startTs;
System.out.println(String.format(devLogPrefix + " 接收到信息 : %s, 接收时间 : %s, 入库结果 : %s, 开销 : %s", lCommand, DateFormatUtils.format(new Date()), insertResult, spentTs));
}
return true;
}
}
最终记录数据如下
完