JAVA对接海康门禁实现设置门计划模版

前言

此功能开发是基于基础环境搭建完之后的功能代码,如果没有搭建基础环境还需看下网上的教程自己搭建一下,如有疑问或者逻辑问题可私信博主!

实战代码

1、实体类

java 复制代码
package com.sansint.avideo.domain;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author DJY
 * @date 2024/1/15
 */
@Data
public class DoorPlanParamInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 周计划
     */
    private List<DoorPlanWeekInfo> planWeekInfos;
    /**
     * 计划模板编号,为0表示取消关联,恢复默认状态(普通状态)
     */
    private String doorTemplateNo;
    /**
     * 模版名称
     */
    private String templateName;
    /**
     * 设备id
     */
    private Long deviceId;

}

2、业务代码

现在代码只是部分思路代码,可借鉴及提供思路,其他关联业务部分代码可私信!

java 复制代码
 /***
     * 设置门计划模版
     * @param doorTemplate 门计划模版对象
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Integer> setDoorPlanTemplate(DoorPlanParamInfo doorTemplate) {
        DeviceInfoNew deviceInfo = deviceInfoNewMapper.selectOneById(doorTemplate.getDeviceId());
        Assert.isTrue(!Objects.isNull(deviceInfo), "设置门计划模版,设备为空!!!!");
        Integer lUserID = lUserIDMap.get(deviceInfo.getDeviceIp());
        if (Objects.isNull(lUserID) || lUserID < NumberConstant.ZERO) {
            //登陆设备
            UserManageService.login_V40(deviceInfo.getDeviceIp(), deviceInfo.getDeviceUserName(), deviceInfo.getDevicePassword(), Short.parseShort(deviceInfo.getDevicePort()));
            lUserID = lUserIDMap.get(deviceInfo.getDeviceIp());
        }
        //设置门状态计划参数
        HCNetSDK.NET_DVR_DOOR_STATUS_PLAN struDoorStatus = new HCNetSDK.NET_DVR_DOOR_STATUS_PLAN();
        struDoorStatus.read();
        struDoorStatus.dwSize = struDoorStatus.size();
        struDoorStatus.dwTemplateNo = Integer.parseInt(doorTemplate.getDoorTemplateNo());
        struDoorStatus.write();
        boolean b_SetDoorStatus = hCNetSDK.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.NET_DVR_SET_DOOR_STATUS_PLAN, NumberConstant.ONE, struDoorStatus.getPointer(), struDoorStatus.size());
        if (!b_SetDoorStatus) {
            System.out.println("设置门状态计划参数,错误码为" + hCNetSDK.NET_DVR_GetLastError());
            return R.fail("设置门状态计划参数,错误码为" + hCNetSDK.NET_DVR_GetLastError());
        } else {
            System.out.println("设置门状态计划参数成功");
        }
        //设置门状态计划模板参数
        HCNetSDK.NET_DVR_PLAN_TEMPLATE struDoorTemp = new HCNetSDK.NET_DVR_PLAN_TEMPLATE();
        struDoorTemp.read();
        struDoorTemp.dwSize = struDoorTemp.size();
        struDoorTemp.byEnable = NumberConstant.ONE;   //使能
        struDoorTemp.dwWeekPlanNo = NumberConstant.ONE;  //周计划模板编号;
        byte[] byTemplateName = new byte[0];
        try {
            byTemplateName = doorTemplate.getTemplateName().getBytes("GBK");
            //计划模板名称
            for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
                struDoorTemp.byTemplateName[i] = 0;
            }
            System.arraycopy(byTemplateName, 0, struDoorTemp.byTemplateName, 0, byTemplateName.length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        struDoorTemp.write();
        boolean b_SetDoorTemp = hCNetSDK.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.NET_DVR_SET_DOOR_STATUS_PLAN_TEMPLATE, Integer.parseInt(doorTemplate.getDoorTemplateNo()), struDoorTemp.getPointer(), struDoorTemp.size());
        if (!b_SetDoorTemp) {
            System.out.println("设置门状态计划模板失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
            return R.fail("设置门状态计划模板失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
        } else {
            System.out.println("设置门状态计划模板成功");
        }
        //设置门状态周计划参数
        HCNetSDK.NET_DVR_WEEK_PLAN_CFG struDoorWeekPlan = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();
        struDoorWeekPlan.read();
        Pointer pstruDoorWeekPlan = struDoorWeekPlan.getPointer();
        IntByReference Ipint = new IntByReference(0);
        boolean b_GetPlan = hCNetSDK.NET_DVR_GetDVRConfig(lUserID, HCNetSDK.NET_DVR_GET_WEEK_PLAN_CFG, NumberConstant.ONE, pstruDoorWeekPlan, struDoorWeekPlan.size(), Ipint);
        if (!b_GetPlan) {
            System.out.println("获取门状态周计划参数失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
        } else {
            pstruDoorWeekPlan.write(0, struDoorWeekPlan.getPointer().getByteArray(0, struDoorWeekPlan.size()), 0, struDoorWeekPlan.size());
            struDoorWeekPlan.read();
            System.out.println("获取门状态周计划参数成功");
        }
        struDoorWeekPlan.read();
        struDoorWeekPlan.byEnable = NumberConstant.ONE; //是否开启:0- 否,1- 是
        //避免时间段交叉,先初始化
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 8; j++) {
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].byEnable = 0;
                //门状态模式(梯控模式),0-普通状态,2-常开状态(自由),3-常闭状态(禁用)
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].byDoorStatus = 0;
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour = 0;
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond = 0;
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour = 0;
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byMinute = 0;
                struDoorWeekPlan.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.bySecond = 0;
            }
        }
        //求出传入的周其他的周
        List<DoorPlanWeekInfo> weekInfoList = doorTemplate.getPlanWeekInfos();
        List<Integer> weekNumList = new ArrayList<>();
        //收集传入的所有的星期几的索引
        for (DoorPlanWeekInfo doorPlanWeekInfo : weekInfoList) {
            weekNumList.add(doorPlanWeekInfo.getWeekNum() - NumberConstant.ONE);
        }
        //补齐未传入的星期几的索引
        List<Integer> missingNumbers = findMissingNumbers(weekNumList);
        //一周7天
        //设置已传入的时间参数
        for (DoorPlanWeekInfo doorPlanWeekInfo : weekInfoList) {
            //获取当前日期的参数
            int day = doorPlanWeekInfo.getWeekNum() - NumberConstant.ONE;
            List<DoorPlanWeekTimeInfo> timeInfoList = doorPlanWeekInfo.getDoorPlanWeekTimeInfoList();
            if (CollectionUtils.isNotEmpty(timeInfoList) && timeInfoList.size() > NumberConstant.ZERO) {
                for (int i = 0; i < timeInfoList.size(); i++) {
                    //分割时、分,放到集合中
                    List<Byte> startTimes = Arrays.stream(timeInfoList.get(i).getStartTime().split(":")).map(Byte::valueOf).collect(Collectors.toList());
                    List<Byte> stopTimes = Arrays.stream(timeInfoList.get(i).getEndTime().split(":")).map(Byte::valueOf).collect(Collectors.toList());
                    //设置是否开启
                    if (ObjectUtil.isNull(timeInfoList.get(i).getType()) || timeInfoList.get(i).getType() == NumberConstant.TWO) {
                        struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].byEnable = NumberConstant.ZERO;
                    } else {
                        struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].byEnable = NumberConstant.ONE;
                    }
                    //门状态模式(梯控模式),0-普通状态,2-常开状态(自由),3-常闭状态(禁用)
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].byDoorStatus = timeInfoList.get(i).getDoorStatus();
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].struTimeSegment.struBeginTime.byHour = startTimes.get(NumberConstant.ZERO);
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].struTimeSegment.struBeginTime.byMinute = startTimes.get(NumberConstant.ONE);
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].struTimeSegment.struBeginTime.bySecond = NumberConstant.ZERO;
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].struTimeSegment.struEndTime.byHour = stopTimes.get(NumberConstant.ZERO);
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].struTimeSegment.struEndTime.byMinute = stopTimes.get(NumberConstant.ONE);
                    struDoorWeekPlan.struPlanCfg[day].struPlanCfgDay[i].struTimeSegment.struEndTime.bySecond = NumberConstant.ZERO;
                    //对本地表中数据进行更新下发状态
                    DoorPlanWeekTimeInfo doorPlanWeekTimeInfo = timeInfoList.get(i);
                    DoorPlanTemplate doorPlanTemplate = doorPlanTemplateMapper.selectById(doorPlanWeekTimeInfo.getId());
                    Assert.isTrue(Objects.nonNull(doorPlanTemplate),"门计划模版对象为空");
                    //获取传入的开始时间和结束时间需要对比库是否有重复
                    //根据当前设备id和日期编号查询该日期下是否有重复的时间段
                    List<DoorPlanTemplate> repeatTimeDataByDb = getRepeatTimeDataByDb(doorPlanTemplate);
                    // 判断时间段是否重叠
                    repeatTimeDataByDb.forEach(repeat -> {
                        if (!repeat.getId().equals(doorPlanTemplate.getId())) {
                            //调用方法比较
                            if (DateUtils.isOverlap(LocalTime.parse(doorPlanTemplate.getStartTime(), FORMATTER), LocalTime.parse(doorPlanTemplate.getEndTime(), FORMATTER), LocalTime.parse(repeat.getStartTime(), FORMATTER), LocalTime.parse(repeat.getEndTime(), FORMATTER))) {
                                throw new CommonException(NumberConstant.ERR_STATUS, "开始时间:" + doorPlanTemplate.getStartTime() + ",结束时间:" + doorPlanTemplate.getEndTime() + ",该日期下已有数据存在重复时间段");
                            }
                        }
                    });
                    if (!doorPlanTemplate.getStartStatus().equals(NumberConstant.ONE)){
                        doorPlanTemplate.setStartStatus(NumberConstant.ONE);
                        int updateResult = doorPlanTemplateMapper.updateById(doorPlanTemplate);
                        log.info("下发模版到设备中---》》》更新本地表中下发状态,id:{},成功条数:{}",doorPlanTemplate.getId(),updateResult);
                    }
                }

            }
        }
        //未传入的数据的值设置为0,不启用
        for (Integer missingNumber : missingNumbers) {
            //设置是否开启 1-使能,0-不使能
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].byEnable = NumberConstant.ZERO;
            //设置门状态模式(梯控模式)0-无效,1-常开状态(自由),2-常闭状态(禁用),3-普通状态(门状态计划使用)
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].byDoorStatus = NumberConstant.ZERO;
            //设置时分秒
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].struTimeSegment.struBeginTime.byHour = NumberConstant.ZERO;
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].struTimeSegment.struBeginTime.byMinute = NumberConstant.ZERO;
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].struTimeSegment.struBeginTime.bySecond = NumberConstant.ZERO;
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].struTimeSegment.struEndTime.byHour = NumberConstant.ZERO;
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].struTimeSegment.struEndTime.byMinute = NumberConstant.ZERO;
            struDoorWeekPlan.struPlanCfg[missingNumber].struPlanCfgDay[NumberConstant.ZERO].struTimeSegment.struEndTime.bySecond = NumberConstant.ZERO;
        }

        struDoorWeekPlan.write();
        boolean planResult = hCNetSDK.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.NET_DVR_SET_WEEK_PLAN_CFG, NumberConstant.ONE, pstruDoorWeekPlan, struDoorWeekPlan.size());
        Assert.isTrue(planResult,"设置状态周计划参数失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
        return R.data(NumberConstant.ONE);
    }
    public static List<Integer> findMissingNumbers(List<Integer> weekList) {
        List<Integer> missingNums = new ArrayList<>();
        for (int i = NumberConstant.ZERO; i < NumberConstant.SEVEN; i++) {
            boolean found = false;
            for (Integer integer : weekList) {
                if (i == integer) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                missingNums.add(i);
            }
        }
        return missingNums;
    }
相关推荐
九圣残炎25 分钟前
【从零开始的LeetCode-算法】3354. 使数组元素等于零
java·算法·leetcode
天天扭码1 小时前
五天SpringCloud计划——DAY1之mybatis-plus的使用
java·spring cloud·mybatis
程序猿小柒1 小时前
leetcode hot100【LeetCode 4.寻找两个正序数组的中位数】java实现
java·算法·leetcode
不爱学习的YY酱2 小时前
【操作系统不挂科】<CPU调度(13)>选择题(带答案与解析)
java·linux·前端·算法·操作系统
丁总学Java2 小时前
Maven项目打包,com.sun.tools.javac.processing
java·maven
kikyo哎哟喂2 小时前
Java 代理模式详解
java·开发语言·代理模式
duration~2 小时前
SpringAOP模拟实现
java·开发语言
小码ssim2 小时前
IDEA使用tips(LTS✍)
java·ide·intellij-idea
潜洋3 小时前
Spring Boot教程之五:在 IntelliJ IDEA 中运行第一个 Spring Boot 应用程序
java·spring boot·后端
暮志未晚Webgl3 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5