第一次接触sdk,走了很多弯路,所以记录一下,希望可以帮助更多人
其中最重要的就是回调函数,他会把我们的动态报警的报警信息返回过来,让我们可以拿到报警信息去做一些业务方面的功能
public class ImplAlarmService implements IAlarmService,NetSDKLib.fMessCallBack {
@Override
public String ss1s() {
return "null";
}
public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
enum AlarmStatus {
ALARM_START, ALARM_STOP
}
// alarm event info list
static Vector<AlarmEventInfo> data = new Vector<AlarmEventInfo>();
@Override
public boolean invoke(int lCommand, NetSDKLib.LLong lLoginID,
Pointer pStuEvent, int dwBufLen, String strDeviceIP,
NativeLong nDevicePort, Pointer dwUser) {
switch (lCommand) {
case NetSDKLib.NET_ALARM_ALARM_EX:
case NetSDKLib.NET_MOTION_ALARM_EX:
case NetSDKLib.NET_VIDEOLOST_ALARM_EX:
case NetSDKLib.NET_SHELTER_ALARM_EX:
case NetSDKLib.NET_DISKFULL_ALARM_EX:
case NetSDKLib.NET_DISKERROR_ALARM_EX: {
byte []alarm = new byte[dwBufLen];
pStuEvent.read(0, alarm, 0, dwBufLen);
for (int i = 0; i < dwBufLen; i++) {
if (alarm[i] == 1) {
AlarmEventInfo alarmEventInfo = new AlarmEventInfo(i, lCommand, AlarmStatus.ALARM_START);
if (!data.contains(alarmEventInfo)) {
data.add(alarmEventInfo);
System.out.println("报警开始:数据"+data.elementAt(0).type+",时间:"
+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data.elementAt(0).date)+",Ip地址:"+strDeviceIP);
/**
* 向物联网平台发送post数据
*/
System.out.println(WanwuhulianApi.start(strDeviceIP));
continue;
}
}else {
AlarmEventInfo alarmEventInfo = new AlarmEventInfo(i, lCommand, AlarmStatus.ALARM_STOP);
if (data.remove(alarmEventInfo)) {
System.out.println("报警结束"+strDeviceIP);
// eventQueue.postEvent(new AlarmListenEvent(target, alarmEventInfo));
}
}
}
break;
}
default:
break;
}
return true;
}
// struct of alarm event
static class AlarmEventInfo {
public static long index = 0;
public long id;
public int chn;
public int type;
public Date date;
public AlarmStatus status;
public AlarmEventInfo(int chn, int type, AlarmStatus status) {
this.chn = chn;
this.type = type;
this.status = status;
this.date = new Date();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AlarmEventInfo showInfo = (AlarmEventInfo) o;
return chn == showInfo.chn && type == showInfo.type;
}
}
}
通过初始化,登录,订阅报警,开始报警,只要看demo就可以实现,我们可以把demo的jf那个类删了,那个类是可视化的实现,删了之后结构就比较清晰了。
其中报警的方法,最主要就是以上的实现类和其中的invoke重写方法,我的理解是通过case可以找的相关报警类型,每次摄像头触发报警,就会调用一次,直接拿取数据就ok了