潘志宝
2024-11-14 afa8fc57084c423218c6816b710dfb2f0b87ea89
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/video/hikvision/utils/HIKDevice.java
@@ -1,5 +1,9 @@
package com.iailab.module.data.video.hikvision.utils;
import com.alibaba.fastjson.JSONException;
import com.iailab.module.data.video.config.VideoConfiguration;
import com.iailab.module.data.video.dto.CameraDTO;
import com.iailab.module.data.video.enums.CameraStatusEnum;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
@@ -8,8 +12,8 @@
import javax.annotation.PreDestroy;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@@ -21,6 +25,7 @@
    // 用户登录返回句柄
    public static int lUserID;  
    //构造函数
    public HIKDevice(){
       if (hCNetSDK == null) {
@@ -38,6 +43,7 @@
            }
        }
    }
   /**
    * 初始化资源配置
    */
@@ -69,42 +75,34 @@
       *      userName-用户名
       *      passWord-密码
       * */
//   public String loginDevice() throws JSONException {
//      if (!getDeviceStatus()) {
//         int init = initDevices();
//         if (init == 1) {
//            return "初始化失败";
//         }
//         ArrayList<String> list = mZnfxCodeService.getDriverInfo("driverinfo");
//         if (list != null && list.size() > 0) {
//            String driverip = list.get(0);
//            String driverport = list.get(1);
//            String username = list.get(2);
//            String password = list.get(3);
//            String returnVal = Login_V40(driverip, Short.parseShort(driverport), username, password);
//            System.out.println(returnVal);
//            return returnVal;
//         } else {
//            return "获取设备信息失败!";
//         }
//      }
//      else
//      {
//         String result = captureJPEGPicture();
//         System.out.println(result);
//         return result;
//      }
//
//   }
    public String loginDevice(CameraDTO camera) throws JSONException {
        if (!getDeviceStatus(camera.getId())) {
            int init = initDevices();
            if (init == 1) {
                return "初始化失败";
            }
            String driverip = camera.getIp();
            String driverport = camera.getPort();
            String username = camera.getUsername();
            String password = camera.getPassword();
            String returnVal = Login_V40(driverip, Short.parseShort(driverport), username, password, camera);
            System.out.println(returnVal);
            return returnVal;
        } else {
            String result = captureJPEGPicture(camera);
            System.out.println(result);
            return result;
        }
    }
    /**
    *
    * @param m_sDeviceIP 设备ip地址
    * @param wPort       端口号,设备网络SDK登录默认端口8000
    * @param m_sUsername 用户名
    * @param m_sPassword 密码
    */
   public String Login_V40(String m_sDeviceIP,short wPort,String m_sUsername,String m_sPassword) {
    public String Login_V40(String m_sDeviceIP, short wPort, String m_sUsername, String m_sPassword, CameraDTO camera) {
       /* 注册 */
       // 设备登录信息
       HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
@@ -125,30 +123,23 @@
       lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
       if (lUserID == -1) {
//          updateZnfxPostalStatus(PostalStatus.ABNORMAL.value());
//          mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.ABNORMAL.value());
            VideoConfiguration.staticCameraService.setStatus(camera.getId(), CameraStatusEnum.OFFLINE.getCode());
           System.out.println("登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
           return "登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError();
       } else {
           System.out.println("登录成功!");
           // read()后,结构体中才有对应的数据 
           m_strDeviceInfo.read();  
//           updateZnfxPostalStatus(PostalStatus.NORMAL.value());
//           mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.NORMAL.value());
            String result = captureJPEGPicture();
            VideoConfiguration.staticCameraService.setStatus(camera.getId(), CameraStatusEnum.ONLIEN.getCode());
            String result = captureJPEGPicture(camera);
           return result;
       }
   }
   //注销设备
    public String logoutDevice()
    {
    public String logoutDevice() {
        NativeLong nlUserID = new NativeLong(lUserID);
        if (!hCNetSDK.NET_DVR_Logout(nlUserID))
        {
        if (!hCNetSDK.NET_DVR_Logout(nlUserID)) {
            System.out.println("注销失败,设备ID:"+ nlUserID.intValue() + "错误码:"+hCNetSDK.NET_DVR_GetLastError());
            return "{\"result\":\"loginDevice Fail\",\"userID\":"+nlUserID.intValue()+",\"errorCode\":"+hCNetSDK.NET_DVR_GetLastError()+"}";
        }else{
@@ -156,33 +147,27 @@
            return "{\"result\":\"logoutDevice Success\",\"userID\":"+nlUserID.intValue()+",\"errorCode\":0}";
        }
    }
   /**
    * 设备抓图(无预览)
    * 单帧设备抓取保存的图片为JPG,具体注意问题需要详细查看【设备网络sdk使用手册】
     *
    * @return
    */
   public String captureJPEGPicture()
   {
//         ArrayList<String> list = mZnfxCodeService.getDriverInfo("driverpic");
         List<String> list = new ArrayList<>();
         list.add("tlChannel");
         list.add("tlPicSize");
         list.add("tlPicQuality");
             if (list != null && list.size() > 0) {
               int tlChannel = Integer.parseInt(list.get(0));
               int tlPicSize = Integer.parseInt(list.get(1));
               int tlPicQuality = Integer.parseInt(list.get(2));
    public String captureJPEGPicture(CameraDTO camera) {
        int tlChannel = camera.getChannel();
        String tlPicSize = VideoConfiguration.staticPicSize;
        String tlPicQuality = VideoConfiguration.staticPicQuality;
               NativeLong nlUserID = new NativeLong(lUserID);
               NativeLong nChannel = new NativeLong(tlChannel);
               HCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new HCNetSDK.NET_DVR_JPEGPARA();
               lpJpegPara.wPicSize = (short)tlPicSize;
               lpJpegPara.wPicQuality = (short)tlPicQuality;
        lpJpegPara.wPicSize = Short.parseShort(tlPicSize);
        lpJpegPara.wPicQuality = Short.parseShort(tlPicQuality);
               // 参数必须写入到结构体,否则在保存图片的时候异常图片
               lpJpegPara.write();
               String tFiledirName = CommonUtils.getCurrDate();//yyyyMMdd
        String tFilename = CommonUtils.getCurrDate();//yyyyMMdd
               // 创建文件名称
               String tDatePath = MessageCode.DvrPicSavePath+"/"+tFiledirName;
        String tDatePath = VideoConfiguration.staticHkCapturePath + tFilename;
               // 判断文件夹是否创建
               File tFile = new File(tDatePath);
               if (!tFile.exists()){
@@ -194,36 +179,31 @@
               // 全路径
               String tPath = tDatePath+"/"+tImageName;
               // 开始抓图
               if (!hCNetSDK.NET_DVR_CaptureJPEGPicture(nlUserID, nChannel, lpJpegPara, tPath))
               {
        if (!hCNetSDK.NET_DVR_CaptureJPEGPicture(nlUserID, nChannel, lpJpegPara, tPath)) {
                  return "失败编码:"+hCNetSDK.NET_DVR_GetLastError() ;
               }else {
                  Long tImageId = CommonUtils.Getnum();//生成流水号
//                  saveZnfxImageInfo(tFiledirName,tImageName,tPath,tImageId);
            Map<String, String> params = new HashMap<>();
            params.put("strFileName", tPath);
            params.put("cmdSerial", camera.getCode());
            params.put("brand", "HK");
            VideoConfiguration.staticCameraService.dealCapture(params);
                  return "success>>>>>>>>>>>" + tImageName;
               }
             }
             else
             {
                return "login fail>>>>>>>>>>>";
             }
   }
   /**
     * 检查设备是否在线
     *
     * @return
     */
   private boolean getDeviceStatus() {
    private boolean getDeviceStatus(String id) {
      boolean isOnLine = hCNetSDK.NET_DVR_RemoteControl(lUserID, 20005, null, 0);
//      if (!isOnLine) {
////         updateZnfxPostalStatus(PostalStatus.ABNORMAL.value()); //离线
////         insertZnfxPostalStatusLog(PostalStatus.ABNORMAL.value());
//         mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.ABNORMAL.value());
//         return false;
//      } else {
//         mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.NORMAL.value());
////         updateZnfxPostalStatus(PostalStatus.NORMAL.value());//在线
//      }
      if (!isOnLine) {
            VideoConfiguration.staticCameraService.setStatus(id, CameraStatusEnum.OFFLINE.getCode());
         return false;
      } else {
            VideoConfiguration.staticCameraService.setStatus(id, CameraStatusEnum.ONLIEN.getCode());
      }
      return true;
   }
//   /**