aijinhui
2023-09-07 2fa66c208202f420a5f001cf0d67e5aa1bf94578
ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
@@ -1,27 +1,26 @@
package com.ruoyi.device.hiksdk.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.SdkOperate;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.device.camera.domain.CameraCmd;
import com.ruoyi.device.camera.service.IArdCamerasService;
import com.ruoyi.device.channel.domain.ArdChannel;
import com.ruoyi.device.channel.service.IArdChannelService;
import com.ruoyi.device.hiksdk.common.GlobalVariable;
import com.ruoyi.device.camera.domain.ArdCameras;
import com.ruoyi.device.hiksdk.config.MinioClientSingleton;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.GisUtil;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK;
import com.ruoyi.utils.gis.GisUtil;
import com.ruoyi.device.hiksdk.sdk.HCNetSDK;
import com.ruoyi.device.hiksdk.service.IHikClientService;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.LoginResultCallBack;
import com.ruoyi.storage.minio.utils.MinioUtils;
import com.ruoyi.device.hiksdk.sdk.LoginResultCallBack;
import com.ruoyi.utils.minio.MinioUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Base64;
@@ -30,11 +29,9 @@
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import static com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK.*;
import static com.ruoyi.device.hiksdk.sdk.HCNetSDK.*;
/**
 * @ClassName: hikClientServiceImpl
@@ -47,8 +44,10 @@
@Service
public class HikClientServiceImpl implements IHikClientService {
    @Value("${minio.endpoint}")
    private static String endpoint;
    @Resource
    IArdCamerasService ardCamerasService;
    private IArdCamerasService ardCamerasService;
    private static HCNetSDK hCNetSDK;
@@ -263,7 +262,7 @@
    public boolean pTZControlWithSpeed(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        Integer speed = cmd.getSpeed();
        Integer code = cmd.getCode();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
@@ -349,23 +348,31 @@
    @SdkOperate
    public boolean setFocusPos(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        Integer dwFocusPos = cmd.getDwFocusPos();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        // 获取参数
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_FOCUSMODE_CFG focusmodeCfg = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = focusmodeCfg.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        focusmodeCfg.dwFocusPos = dwFocusPos;
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_FOCUSMODECFG, channelNum, point, focusmodeCfg.size(), ibrBytesReturned);
        HCNetSDK.NET_DVR_STD_CONFIG struStdCfg = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_GIS_INFO struGisInfo = new HCNetSDK.NET_DVR_GIS_INFO();
        struGisInfo.struPtzPosEx.dwFocus=dwFocusPos;
        struStdCfg.read();
        IntByReference lchannel = new IntByReference(channelNum);
        struStdCfg.lpInBuffer= struGisInfo.getPointer();
        struStdCfg.lpCondBuffer = lchannel.getPointer();
        struStdCfg.dwCondSize = 4;
        struStdCfg.dwInSize = struGisInfo.size();
        struStdCfg.write();//设置前之前要write()
        boolean bool = hCNetSDK.NET_DVR_SetSTDConfig(userId, NET_DVR_GET_GISINFO, struStdCfg);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.error("设置聚焦值失败,请稍后重试" + code);
            log.error("设置GIS信息数据失败,请稍后重试" + code);
            return false;
        } else {
            return true;
        }
        return bool;
    }
    /**
@@ -376,28 +383,31 @@
    @Override
    public Map<String, Object> getFocusPos(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return new HashMap<>();
        }
        // 获取参数
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_FOCUSMODE_CFG focusmodeCfg = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = focusmodeCfg.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_FOCUSMODECFG, channelNum, point, focusmodeCfg.size(), ibrBytesReturned);
        if (bool) {
            focusmodeCfg.read();
            Map<String, Object> map = new HashMap<>();
            map.put("dwFocusPos", focusmodeCfg.dwFocusPos);
            map.put("byFocusDefinitionDisplay ", focusmodeCfg.byFocusDefinitionDisplay);
            map.put("dwRelativeFocusPos", focusmodeCfg.dwRelativeFocusPos);
            return map;
        } else {
        HCNetSDK.NET_DVR_STD_CONFIG struStdCfg = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_GIS_INFO struGisInfo = new HCNetSDK.NET_DVR_GIS_INFO();
        struStdCfg.read();
        IntByReference lchannel = new IntByReference(channelNum);
        struStdCfg.lpCondBuffer = lchannel.getPointer();
        struStdCfg.dwCondSize = 4;
        struStdCfg.lpOutBuffer = struGisInfo.getPointer();
        struStdCfg.dwOutSize = struGisInfo.size();
        struStdCfg.write();//设置前之前要write()
        boolean bool = hCNetSDK.NET_DVR_GetSTDConfig(userId, NET_DVR_GET_GISINFO, struStdCfg);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.debug("获取聚焦值失败,请稍后重试" + code);
            log.error("获取GIS信息数据失败,请稍后重试" + code);
            return new HashMap<>();
        } else {
            struGisInfo.read();
            Map<String, Object> map = new HashMap<>();
            map.put("dwFocus",struGisInfo.struPtzPosEx.dwFocus);
            return map;
        }
    }
@@ -410,7 +420,7 @@
    @SdkOperate
    public boolean setPreset(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        Integer PresetIndex = cmd.getPresetIndex();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
@@ -433,7 +443,7 @@
    @SdkOperate
    public boolean gotoPreset(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        Integer PresetIndex = cmd.getPresetIndex();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
@@ -458,7 +468,7 @@
    @Override
    public String getVideoResolution(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return " ";
        }
@@ -563,7 +573,7 @@
    @Override
    public Map<String, Object> getPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return null;
        }
@@ -614,7 +624,7 @@
    @SdkOperate
    public boolean setPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        Map<String, Double> ptz = cmd.getPtzMap();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
@@ -647,7 +657,7 @@
    @SdkOperate
    public boolean guideTargetPosition(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -734,7 +744,7 @@
    @Override
    public int getPTZLockInfo(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return -1;
        }
@@ -766,7 +776,7 @@
    @SdkOperate
    public boolean setZeroPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -798,7 +808,7 @@
    @Override
    public Map<String, Object> getPtzScope(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return new HashMap<>();
        }
@@ -847,7 +857,7 @@
    public boolean controlDefogcfg(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -893,7 +903,7 @@
    public boolean controlInfrarecfg(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -941,7 +951,7 @@
    public boolean controlFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -974,9 +984,17 @@
        return bool;
    }
    /**
     * @描述 获取聚焦模式
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/18 13:07
     * @修改人和其它信息
     */
    public String getFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return "";
        }
@@ -1006,7 +1024,7 @@
    public boolean controlPTHeateRpwron(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -1039,7 +1057,7 @@
    public boolean controlCameraDeicing(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
@@ -1076,7 +1094,7 @@
     */
    public String captureJPEGPicture(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return "";
        }
@@ -1119,7 +1137,7 @@
    @SdkOperate
    public String picCutCate(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return "";
        }
@@ -1149,16 +1167,13 @@
            byte[] array = jpegBuffer.array();
            //存储到minio
            String BucketName = "pic";
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String ObjectName = time + "/" + uuid + ".jpeg";
            String ContentType = "image/JPEG";
            String ObjectName = "capture/" + IdUtils.simpleUUID() + ".jpeg";
            InputStream input = new ByteArrayInputStream(array);
            String url = "";
            try {
                boolean b = MinioUtils.uploadObject(BucketName, ObjectName, input, input.available(), ContentType);
                boolean b = MinioUtil.uploadObject(BucketName, ObjectName, input, input.available(), MimeTypeUtils.IMAGE_JPEG);
                if (b) {
                    url = MinioUtils.getBucketObjectUrl(BucketName, ObjectName);
                    url = MinioUtil.getBucketObjectUrl(BucketName, ObjectName);
                    log.debug("上传文件成功!" + url);
                }
            } catch (IOException ex) {
@@ -1184,7 +1199,7 @@
    public String record(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer channelNum = cmd.getChannelNum();
            Integer channelNum = cmd.getChanNo();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            boolean enable = cmd.isEnable();
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
@@ -1246,7 +1261,7 @@
    public void recordStart(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer channelNum = cmd.getChannelNum();
            Integer channelNum = cmd.getChanNo();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return;
@@ -1301,7 +1316,7 @@
        String url = "";
        try {
            String cameraId = cmd.getCameraId();
            Integer channelNum = cmd.getChannelNum();
            Integer channelNum = cmd.getChanNo();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return "";
@@ -1337,14 +1352,11 @@
            log.debug("录像停止");
            //存入minio
            String BucketName = cmd.getRecordBucketName();
            String ObjectName = cmd.getRecordObjectName();
            String ContentType = "video/MP4";
            String ObjectName = cmd.getRecordObjectName() + ".mp4";
            FileInputStream stream = new FileInputStream(path);
            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String recordName = cameraId + "/" + time + "/" + ObjectName + ".mp4";
            boolean b = MinioUtils.uploadObject(BucketName, recordName, stream, stream.available(), ContentType);
            boolean b = MinioUtil.uploadObject(BucketName, ObjectName, stream, stream.available(), "video/MP4");
            if (b) {
                url = MinioClientSingleton.domainUrl + "/" + BucketName + "/" + recordName;
                url = MinioUtil.getBucketObjectUrl(BucketName, ObjectName);
                log.debug("上传文件成功!" + url);
            }
            return url;
@@ -1358,7 +1370,7 @@
    public void recordStopNotToMinio(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer channelNum = cmd.getChannelNum();
            Integer channelNum = cmd.getChanNo();
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return;
            }
@@ -1406,7 +1418,7 @@
                // 获取通道号
                for (int iChannum = 0; iChannum < chanNum; iChannum++) {
                    ArdChannel ardChannel = new ArdChannel();
                    int channum = iChannum + startDChan+1;
                    int channum = iChannum + startDChan + 1;
                    HCNetSDK.NET_DVR_PICCFG_V40 strPicCfg = new HCNetSDK.NET_DVR_PICCFG_V40();
                    strPicCfg.dwSize = strPicCfg.size();
                    strPicCfg.write();
@@ -1430,4 +1442,47 @@
        }
        return channelList;
    }
    /**
     * @描述 获取GIS信息数据
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:36
     * @修改人和其它信息 0-解锁 1-锁定
     */
    @Override
    public Map<String, Object> getGisInfo(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChanNo();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return new HashMap<>();
        }
        // 获取参数
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        HCNetSDK.NET_DVR_STD_CONFIG struStdCfg = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_GIS_INFO struGisInfo = new HCNetSDK.NET_DVR_GIS_INFO();
        struStdCfg.read();
        IntByReference lchannel = new IntByReference(channelNum);
        struStdCfg.lpCondBuffer = lchannel.getPointer();
        struStdCfg.dwCondSize = 4;
        struStdCfg.lpOutBuffer = struGisInfo.getPointer();
        struStdCfg.dwOutSize = struGisInfo.size();
        struStdCfg.write();//设置前之前要write()
        boolean bool = hCNetSDK.NET_DVR_GetSTDConfig(userId, NET_DVR_GET_GISINFO, struStdCfg);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.error("获取GIS信息数据失败,请稍后重试" + code);
            return new HashMap<>();
        } else {
            struGisInfo.read();
            Map<String, Object> map = new HashMap<>();
            map.put("p", struGisInfo.struPtzPos.fPanPos);
            map.put("t", struGisInfo.struPtzPos.fTiltPos<0?struGisInfo.struPtzPos.fTiltPos+360:struGisInfo.struPtzPos.fTiltPos);
            map.put("z", struGisInfo.struPtzPos.fZoomPos);
            map.put("fHorFieldAngle", struGisInfo.fHorizontalValue);// 水平视场角
            map.put("fVerFieldAngle", struGisInfo.fVerticalValue);// 垂直视场角
            return map;
        }
    }
}