海康Ehome协议java开发

海康Ehome协议 java开发
注:我创建了springboot项目,

1.先把必要的包导入
在这里插入图片描述
maven依赖自己来
在这里插入图片描述
2.从海康demo里把这这三个文件导入
在这里插入图片描述

3.初始化CMS注册,报警,存储

@Component
public class CodeCache {
    public static HCISUPCMS hcisupcms;
    public static HCISUPAlarm hcisupalarm;
    public static HCISUPSS hcisupss;

    @Autowired
    HikSDKProperties hikSDKProperties;

    public static String alarmUrl;
    public static String cmsUrl;
    public static String ssUrl;

    @PostConstruct
    public void init(){
        alarmUrl = hikSDKProperties.getAlarmPath();
        cmsUrl = hikSDKProperties.getCmsPath();
        ssUrl = hikSDKProperties.getSsPath();

        hcisupss = HCISUPSS.INSTANCE;
        hcisupalarm = HCISUPAlarm.INSTANCE;
        hcisupcms = HCISUPCMS.INSTANCE;
    }

}

2.在项目启动后执行

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    public HCISUPCMS hcisupcms = CodeCache.hcisupcms;
    public HCISUPAlarm hcisupalarm = CodeCache.hcisupalarm;
    public HCISUPSS hcisupss = CodeCache.hcisupss;

    @Autowired
    private HikServerProperties serverProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("通过实现ApplicationRunner接口,在spring boot项目启动后打印参数");
        //存储服务器
        ss();
        //报警服务器
        ealarm();
        cms();
    }

    /**
     * 存储监听
     */
    private void ss() {
        boolean sinit = hcisupss.NET_ESS_Init();
        if(!sinit){
            System.out.println("NET_ESS_Init失败!");
        }
        HCISUPSS.NET_EHOME_SS_LISTEN_PARAM pSSListenParam = new HCISUPSS.NET_EHOME_SS_LISTEN_PARAM();
        pSSListenParam.fnSSMsgCb = new PSS_Message_Callback();
        pSSListenParam.fnSStorageCb = new MyEHomeSSStorageCallBack();

//        String strIP = "192.168.1.186";
        String strIP = serverProperties.getIp();
        System.arraycopy(strIP.getBytes(), 0, pSSListenParam.struAddress.szIP, 0, strIP.length());

        pSSListenParam.struAddress.wPort = 7661;
        String strKMS_UserName = "test";
        System.arraycopy(strKMS_UserName.getBytes(), 0, pSSListenParam.szKMS_UserName, 0, strKMS_UserName.length());

        String strKMS_Password = "12345";
        System.arraycopy(strKMS_Password.getBytes(), 0, pSSListenParam.szKMS_Password, 0, strKMS_Password.length());

        String strAccessKey = "test";
        System.arraycopy(strAccessKey.getBytes(), 0, pSSListenParam.szAccessKey, 0, strAccessKey.length());

        String strSecretKey = "12345";
        System.arraycopy(strSecretKey.getBytes(), 0, pSSListenParam.szSecretKey, 0, strSecretKey.length());

        pSSListenParam.byHttps = 0;
        pSSListenParam.write();
        NativeLong listenSS = hcisupss.NET_ESS_StartListen(pSSListenParam);//存储监听
        if (listenSS.intValue() == -1) {
            log.error("NET_ESS_StartListen启动注册监听失败,错误号:",hcisupss.NET_ESS_GetLastError());
            return;
        }
        System.out.println("存储监听:"+listenSS.intValue());
    }
    private void ealarm() {
        hcisupalarm.NET_EALARM_Init();
        HCISUPAlarm.NET_EHOME_ALARM_LISTEN_PARAM pAlarmListenParam = new HCISUPAlarm.NET_EHOME_ALARM_LISTEN_PARAM();
        pAlarmListenParam.fnMsgCb = new MyEHomeMsgCallBack();
//        pAlarmListenParam.struAddress.szIP="192.168.1.186".getBytes();
        pAlarmListenParam.struAddress.szIP= serverProperties.getIp().getBytes();
        pAlarmListenParam.struAddress.wPort = 7662;
        pAlarmListenParam.pUserData = null;
        pAlarmListenParam.byProtocolType = 1;   //0- TCP方式(保留,暂不支持),1- UDP方式
        pAlarmListenParam.byUseCmsPort = 0;     //是否复用CMS端口:0- 不复用,非0- 复用
        //如果复用cms端口,协议类型字段无效,此时AMS的本地监听信息struAddress填本地回环地址,
        //本地回环地址通过NET_ECMS_GetSDKLocalCfg、NET_ECMS_SetSDKLocalCfg获取和设置)
        NativeLong nativeLong = hcisupalarm.NET_EALARM_StartListen(pAlarmListenParam);//报警监听
        if(nativeLong.intValue() < -1){
            hcisupalarm.NET_EALARM_Fini();
            System.out.println("报警监听失败"+hcisupcms.NET_ECMS_GetLastError());
            return;
        }
        System.out.println("报警监听"+nativeLong);
    }

    private void cms(){
        //CMS注册模块初始化
        hcisupcms.NET_ECMS_Init();

        System.out.println("版本号:"+hcisupcms.NET_ECMS_GetBuildVersion());

        HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM lpCMSListenPara = new HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM();
        lpCMSListenPara.fnCB = new RegisterCallBack();
//        lpCMSListenPara.struAddress.szIP="192.168.1.186".getBytes();
        lpCMSListenPara.struAddress.szIP= serverProperties.getIp().getBytes();
        lpCMSListenPara.struAddress.wPort = 7660;
        //启动监听,接收设备注册信息
        NativeLong nativeLong = hcisupcms.NET_ECMS_StartListen(lpCMSListenPara);
        if(nativeLong.intValue() < -1){
            log.info("注册监听失败,error code:{}",hcisupcms.NET_ECMS_GetLastError());
            hcisupcms.NET_ECMS_Fini();
            return;
        }
        int lLoginID = nativeLong.intValue();
        try {
            while(true){
                Thread.sleep(1000);
                if(lLoginID >= 0){
                    break;
                }
            }
            Thread.sleep(3000);
        }catch (Exception e){}

        log.info("NET_ECMS_StartListen启动注册监听成功!{}",nativeLong.intValue());
    }

}

3.创建回调
在这里插入图片描述
注册回调
在这里插入图片描述
报警回调
在这里插入图片描述

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 38
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值