package com.sz.admin.monitor.sdk;


import com.mybatisflex.core.query.QueryWrapper;
import com.sz.admin.monitor.pojo.po.Camera;
import com.sz.admin.monitor.pojo.po.Nvr;
import com.sz.admin.monitor.pojo.po.RecordFile;
import com.sz.admin.monitor.pojo.vo.ptz.PTZVO;
import com.sz.admin.monitor.pojo.vo.watchful.WatchfulVO;
import com.sz.admin.monitor.sdk.hkSdk.HCNetSDK;
import com.sz.admin.monitor.sdk.hkSdk.HkNVR;
import com.sz.admin.monitor.service.CameraService;
import com.sz.admin.monitor.service.DeviceService;
import com.sz.admin.monitor.service.IpChannelService;
import com.sz.admin.monitor.service.NvrService;
import com.sz.admin.monitor.utils.ThreeDPtz;
import com.sz.admin.monitor.utils.VcrlUserIdContainer;
import com.sz.core.util.BeanCopyUtils;
import com.sz.mysql.DataScopeProperties;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所有NVR类型的管理类
 *
 * @author zxsong
 * @since 2024-1-4 10:29:15
 */
@Component
@Slf4j
public class ManageNVR {
    public static final String INVOKE_POINT_CMD = "39";
    @Resource
    private HkNVR hkNVR;
    @Resource
    private NvrService nvrService;
    @Resource
    private CameraService cameraService;

    @Resource
    private VcrlUserIdContainer vcrlUserIdContainer;
    @Resource
    private DataScopeProperties dataScopeProperties;

    /**
     * 项目启动注册所有sdk和登录所有nvr
     */
    @Bean
    public Object nvrInit() {
        return null;
//        //海康sdk注册
//        HCNetSDK hcNetSDK = hkNVR.registerPlugin();
//        Nvr nvr = new Nvr();
//        nvr.setDriver(0);
//        // 从数据库中查询出所有的nvr
//        List<Nvr> devicesList = nvrService.list(QueryWrapper.create(nvr));
//        if (null == devicesList) {
//            return hcNetSDK;
//        }
//        // 遍历每个nvr，进行一个注册
//        for (Nvr n : devicesList) {
//            int lUserId = hkNVR.loginVcr(n.getIp(), n.getPort().toString(), n.getAccount(), n.getPassword());
//            if (lUserId == -1) {
//                // 如果注册失败，将状态设置为离线
//                nvrService.upStatusById(n.getId(), 0);
//            } else {
//                //将vcr的唯一Id和登录回调句柄luserid存储
//                log.info("NVR登录成功，ID：{}", lUserId);
//                vcrlUserIdContainer.addData(n.getId().intValue(), lUserId);
//                nvrService.upStatusById(n.getId(), 1);
//            }
//        }
//        return hcNetSDK;
    }

//    @Bean
//    public Object dhNvrInit() {
//        //大华sdk注册
//        NetSDKLib netSDKLib = dhNVR.registerPlugin(null, null);
//
//        IpDevice ipDevices = new IpDevice();
//        ipDevices.setDriver(1);
//        List<IpDevice> devicesListHk = ipDevicesService.list(QueryWrapper.create(ipDevices));
//        if (null == devicesListHk) {
//            return netSDKLib;
//        }
//        for (IpDevice devices : devicesListHk) {
//            NetSDKLib.LLong lLong = dhNVR.loginVcr2(devices.getIpAddress(), String.valueOf(devices.getAdminPort()), devices.getUsername(), devices.getPassword());
//            if (lLong.toString().equals("0")) {
//                ipDevicesService.upStatusById(devices.getId(), 0);
//            } else {
//                //将vcr的唯一Id和登录回调句柄luserid存储
//                vcrlUserIdContainer.addData(devices.getId(), lLong);
//                ipDevicesService.upStatusById(devices.getId(), 1);
//            }
//        }
//        return netSDKLib;
//    }


    /**
     * 新增nvr时登录
     *
     * @param ip
     * @param port
     * @param userName
     * @param password
     * @param deviceType
     * @return
     */
    public Object loginOne(Long id, String ip, String port, String userName, String password, String deviceType) {
        Object loginVcr = 0;
        if ("0".equals(deviceType)) {
            // 海康登录
            loginVcr = hkNVR.loginVcr(ip, port, userName, password);
        } else if ("1".equals(deviceType)) {
            // 大华登录
           // loginVcr = dhNVR.loginVcr2(ip, port, userName, password);
        } else {
            return 0;
        }
        Nvr devices = nvrService.getOne(QueryWrapper
                .create()
                .select()
                .from(Nvr.class)
                .eq(Nvr::getId, id));
        vcrlUserIdContainer.addData(devices.getId().intValue(), loginVcr);
        return loginVcr;
    }

    /**
     * 登陆单个camera
     *
     * @param ip
     * @param port
     * @param userName
     * @param password
     * @param deviceType
     * @return
     */
    public int loginOneCamera(String ip, String port, String userName, String password, String deviceType) {
        int loginVcr = 0;
        if ("0".equals(deviceType)) {
            loginVcr = hkNVR.loginVcr(ip, port, userName, password);
        } else if ("1".equals(deviceType)) {
          //  loginVcr = dhNVR.loginVcr(ip, port, userName, password);
        } else {
            return 0;
        }
        return loginVcr;
    }

    /**
     * 云台控制
     *
     * @param channelId 通道id
     * @param dwCmd 云台控制命令
     * @param speed 速度
     * @param dwStop 停止命令
     */
    public boolean ptzCon(int channelId, String dwCmd, String speed, String dwStop) {
        // 根据通道id获取摄像机
        Camera camera = cameraService.getById(channelId);
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr nvr = nvrService.getById(camera.getNvrId());
        //
        if (0 == nvr.getDriver()) {
            return hkNVR.ptzControl(userId, camera.getChannelId(), dwCmd, dwStop, Integer.parseInt(speed));
        } else {
            return false;
        }

    }


    /**
     * 预置位的设置调用
     *设置预置位 = “记录”当前位置，给它一个号码。
     * 调用预置位 = “跳转”到那个号码对应的位置
     * @param
     * @param ptzPresetCmd
     * @param dwPresetIndex
     * @return
     */
    public boolean ptzPresets(int channelId, int ptzPresetCmd, String dwPresetIndex,String pointName) {
        Camera camera = cameraService.getById(channelId);
        if (camera == null) {
            return false;
        }
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr nvr = nvrService.getById(camera.getNvrId());
        if (userId == null) {
            log.error("录像机不在线:{}-{}", nvr.getIp(), nvr.getName());
            return false;
        }
        if (0 == nvr.getDriver()) {
            boolean b = hkNVR.ptzPreset(userId, camera.getChannelId(), ptzPresetCmd, dwPresetIndex, pointName);
            if (!b) {
                int channelUserId = hkNVR.login_V40(camera.getIpAddress(), nvr.getPort().shortValue(), nvr.getAccount(), nvr.getPassword());
                boolean preset = hkNVR.ptzPreset(channelUserId, 1, ptzPresetCmd, dwPresetIndex, pointName);
                hkNVR.loginOut(channelUserId, camera.getIpAddress());
                return preset;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 获取海康大华通道信息
     *
     * @param lUserID  登录的句柄
     * @param ipDeviceId  设备的id
     * @param deviceType  设备的类型 0表示海康，1表示大华
     * @return 通道列表
     * @throws UnsupportedEncodingException
     */
    public List<Camera> getChannel(Object lUserID, long ipDeviceId, int deviceType) throws UnsupportedEncodingException {
        List<Camera> cameraList = new ArrayList<>();
        ModelMapper modelMapper = new ModelMapper();
        if (deviceType == 0) {
            List<Camera> ipChannelInfo = hkNVR.getIPChannelInfo(Integer.parseInt(lUserID.toString()), ipDeviceId);
            for (Camera objectIpChannel : ipChannelInfo) {
                Camera camera = modelMapper.map(objectIpChannel, Camera.class);
                cameraList.add(camera);
            }
        } else {
            // 获取大华通道信息
//            List<Camera> ipChannelInfo = dhNVR.getIPChannelInfo(lUserID, ipDeviceId);
//            for (Camera objectIpChannel : ipChannelInfo) {
//                Camera ipChannel = modelMapper.map(objectIpChannel, Camera.class);
//                cameraList.add(ipChannel);
//            }
        }
        return cameraList;
    }

    /**
     * 抓图
     *
     * @param cameraId
     * @return
     */
    public synchronized String capturePic(int cameraId, String fileName) {
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        return "!23";
        Camera camera = cameraService.getById(cameraId);
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr nvr = nvrService.getById(camera.getNvrId());
        if(camera.getChannelDriver() == 0){
            int loginOne = this.loginOneCamera(camera.getIpAddress(), String.valueOf(nvr.getPort()),
                nvr.getAccount(), nvr.getPassword(), String.valueOf(0));
            String path;
            if (loginOne == -1){
                // 登录失败
                path = hkNVR.capturePic(userId, camera.getChannelId(), fileName);
            }else {
                path = hkNVR.capturePic(loginOne, camera.getVideoType(), fileName);
                if (path.contains("抓图失败")) {
                    log.error("抓图失败:{}", camera.getIpAddress());
                    path = hkNVR.capturePic(userId, camera.getChannelId(),fileName);
                }
                hkNVR.loginOut(loginOne, camera.getIpAddress());
            }
            return path;
        }else {
            return hkNVR.capturePic(userId, camera.getChannelId(),fileName);
        }
    }


    /**
     * 判断两个ip是否在同一网段
     *
     * @param ip1
     * @param ip2
     * @return
     */
    public boolean isSameNetworkSimple(String ip1, String ip2) {
        String[] parts1 = ip1.split("\\.");
        String[] parts2 = ip2.split("\\.");
        return parts1[0].equals(parts2[0])
                && parts1[1].equals(parts2[1])
                && parts1[2].equals(parts2[2]);
    }

    /**
     * 3Dptz功能
     *通过鼠标在NVR或IPC（网络摄像机）的实时视频画面上进行交互操作，
     * 来控制摄像头的云台转动和镜头变焦，实现一种直观、精确的定位方式
     * @param channelId
     * @param threeDPtz
     * @return
     */
    public boolean threeDPtz(int channelId, ThreeDPtz threeDPtz) {
        Camera camera = cameraService.getById(channelId);
        Integer lUserID = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr nvr = nvrService.getById(camera.getNvrId());
        if (0 == nvr.getDriver()) {
            return hkNVR.threeDPositioning(lUserID, camera.getChannelId(), threeDPtz);
        } else {
            return false;
        }
    }


    /**
     * 云台守望功能
     * 当云台摄像机（PTZ摄像机）在一段时间内没有接收到任何控制指令后，会自动回到一个预先设定好的默认位置（预置点）并保持静止。
     * @param channelId 通道id
     * @param openOrNo 0表示关闭，1表示开启
     * @param watchTime 云台守望时间，单位为秒
     */
    public boolean watchful(int channelId, int openOrNo, int watchTime, int presetIndex) {
        Camera camera = cameraService.getById(channelId);
        Integer lUserId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr nvr = nvrService.getById(camera.getNvrId());
        if (camera.getChannelDriver() != 0 ||camera.getType() == 0 || camera.getType() == 3){
            return false;
        }
        if(openOrNo == 1){
            if (0 == nvr.getDriver()) {
                boolean ptzPresets = this.ptzPresets(channelId, 39, String.valueOf(presetIndex), "");
                if (ptzPresets){
                    try {
                        // 延时一秒钟
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 开启云台守卫功能
                    boolean b = hkNVR.enableParkActionToPreset(lUserId, camera.getChannelId(), watchTime, presetIndex);
                    if(b){
                        //  更新该摄像机的守望位为1 表示开启了守望功能
                        cameraService.updateWatchfulStateByIp(camera.getIpAddress(), 1);
                    }
                    return b;
                }
            }
            return false;
        }else {
            if (0 == nvr.getDriver()) {
                boolean b = hkNVR.disableParkAction(lUserId, camera.getChannelId());
                if(b){
                    // 更新该摄像机的守望位为0 表示关闭了守望功能
                    cameraService.updateWatchfulStateByIp(camera.getIpAddress(), 0);
                }
                return b;
            } else {
                return false;
            }
        }

    }


    /**
     * 获取海康的云台守望的参数信息
     *
     * @param channelId 通道id
     * @return 云台守望数据
     */
    public WatchfulVO getWatchful(Long channelId) {
        Camera camera = cameraService.getById(channelId);
        log.info("摄像头参数信息：{}", camera);
        int userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        HCNetSDK.NET_DVR_PTZ_PARKACTION_CFG watchCfg = HkNVR.getWatchCfg(HCNetSDK.INSTANCE, userId, camera.getChannelId());
        log.info("守望参数：{}", watchCfg);
        WatchfulVO watchfulVO = new WatchfulVO();
        if (watchCfg != null) {
            watchfulVO.setWatchfulState((int) watchCfg.byEnable);
            watchfulVO.setWatchfulIndex((int) watchCfg.wID);
            watchfulVO.setWatchfulTime(watchCfg.dwParkTime);
            return watchfulVO;
        }
        return watchfulVO;
    }

    /**
     * 获取图片分辨率
     *
     * @param imagePath
     * @return
     */
    public int[] getImageResolution(String imagePath) {
        try {
            BufferedImage image = ImageIO.read(new File(imagePath));
            if (image == null) {
                throw new RuntimeException("读取图片分辨率失败,测温抓图失败");
            }
            int width = image.getWidth();
            int height = image.getHeight();
            return new int[]{width, height};
        } catch (IOException e) {
            throw new RuntimeException("读取图片分辨率失败: " + imagePath, e);
        }
    }

    /**
     * 获取红外探测器分辨率
     *
     * @param dataPath
     * @return
     */
    public int[] guessResolution(String dataPath) {
        File file = new File(dataPath);
        if (!file.exists()) {
            return null;
        }
        long fileSize = file.length();
        if (fileSize % 4 != 0) {
            return null;
        }
        long points = fileSize / 4;
        if (points == 80L * 60) return new int[]{80, 60};
        if (points == 120L * 90) return new int[]{120, 90};
        if (points == 160L * 120) return new int[]{160, 120};
        if (points == 256L * 192) return new int[]{256, 192};
        if (points == 320L * 240) return new int[]{320, 240};
        if (points == 384L * 288) return new int[]{384, 288};
        if (points == 512L * 384) return new int[]{512, 384};
        if (points == 512L * 480) return new int[]{512, 480};
        if (points == 640L * 480) return new int[]{640, 480};
        if (points == 640L * 512) return new int[]{640, 512};
        if (points == 1024L * 768) return new int[]{1024, 768};
        return null;
    }

    /**
     * 根据红外二进制原始图片 获取全屏温度
     *
     * @param srcPath
     * @param width
     * @return
     */
    public static String handleImageData(String srcPath, int width) {
        int k = srcPath.indexOf(".");
        if (k == -1) {
            return null;
        }
        // 目标文件
        String desPath = srcPath.substring(0, k) + ".json";

        File file = new File(srcPath);
        if (!file.exists()) {
            return null;
        }
        // 65, 13, -88, 65, 44, -117, -87, 65, 13, 30, -87, 65, 35, -96, -89, 65, -87, -21, -91, 65, 77, -92, -92
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[4];
            ArrayList<String> list = new ArrayList<>();
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                // 处理读取的字节数据
                int l;
                l = buffer[0];
                l &= 0xff;
                l |= ((long) buffer[1] << 8);
                l &= 0xffff;
                l |= ((long) buffer[2] << 16);
                l &= 0xffffff;
                l |= ((long) buffer[3] << 24);
                String tempData = Float.intBitsToFloat(l) + "";
                String number = String.format("%.2f", Double.parseDouble(tempData));
                list.add(number);
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(desPath));
            for (int i = 0; i < list.size(); i++) {
                String s = list.get(i);
                bw.write(s);
                bw.write(",");
                int j = i + 1;
                if (j % width == 0) {
                    bw.newLine();
                }
            }
            bw.close();
            return desPath;

        } catch (IOException e) {
            e.printStackTrace();

        }
        return null;
    }
//@Data
//    public static class MaxValueWithCoordinates {
//        public double maxValue;
//        public int maxX;
//        public int maxY;
//        public String filePath;
//        public MaxValueWithCoordinates(double maxValue, int maxX, int maxY,String filePath) {
//            this.maxValue = maxValue;
//            this.maxX = maxX;
//            this.maxY = maxY;
//            this.filePath = filePath;
//        }
//
//        @Override
//        public String toString() {
//            return "MaxValue: " + maxValue + " at (" + maxX + ", " + maxY + ")";
//        }
//    }

    @Data
    public static class MaxMinValueWithCoordinates {
        public double maxValue;
        public int maxX;
        public int maxY;

        public double minValue;
        public int minX;
        public int minY;

        public String filePath;

        public MaxMinValueWithCoordinates(double maxValue, int maxX, int maxY,
                                          double minValue, int minX, int minY,
                                          String filePath) {
            this.maxValue = maxValue;
            this.maxX = maxX;
            this.maxY = maxY;
            this.minValue = minValue;
            this.minX = minX;
            this.minY = minY;
            this.filePath = filePath;
        }

        @Override
        public String toString() {
            return "MaxValue: " + maxValue + " at (" + maxX + ", " + maxY + "), "
                    + "MinValue: " + minValue + " at (" + minX + ", " + minY + ")";
        }
    }


    /**
     * 获取框最大温度或则点
     *
     * @param path
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return
     */
//    public static MaxValueWithCoordinates getJsonHeightTemp(String path, int x1, int y1, int x2, int y2) {
//        File file = new File(path);
//        try {
//            FileReader fileReader = new FileReader(file);
//            BufferedReader reader = new BufferedReader(fileReader);
//            String line;
//            ArrayList<String[]> list = new ArrayList<>();
//            while ((line = reader.readLine()) != null) {
//                String[] split = line.split(",");
//                list.add(split);
//            }
//            reader.close();
//            fileReader.close();
//
//            double max = Double.NEGATIVE_INFINITY;
//            int maxX = -1;
//            int maxY = -1;
//
//            for (int i = y1; i <= y2 && i < list.size(); i++) {
//                for (int j = x1; j <= x2 && j < list.get(i).length; j++) {
//                    String[] strings = list.get(i);
//                    String s = strings[j];
//                    double value = Double.parseDouble(s);
//
//                    if (value > max) {
//                        max = value;
//                        maxX = j;
//                        maxY = i;
//                    }
//                }
//            }
//
//            return new MaxValueWithCoordinates(max, maxX, maxY,null);
//        } catch (Exception e) {
//            e.printStackTrace(); // 打印异常堆栈信息
//        }
//        return new MaxValueWithCoordinates(-1.0, -1, -1,null);
//    }
    public static MaxMinValueWithCoordinates getJsonHeightTemp(String path, int x1, int y1, int x2, int y2) {
        File file = new File(path);

        try (FileReader fileReader = new FileReader(file);
             BufferedReader reader = new BufferedReader(fileReader)) {

            String line;
            ArrayList<String[]> list = new ArrayList<>();
            while ((line = reader.readLine()) != null) {
                list.add(line.split(","));
            }

            double max = Double.NEGATIVE_INFINITY;
            int maxX = -1, maxY = -1;

            double min = Double.POSITIVE_INFINITY;
            int minX = -1, minY = -1;

            for (int i = y1; i <= y2 && i < list.size(); i++) {
                String[] row = list.get(i);
                for (int j = x1; j <= x2 && j < row.length; j++) {
                    String s = row[j];
                    if (s == null || s.isEmpty()) continue; // 防御：空值跳过

                    double value;
                    try {
                        value = Double.parseDouble(s);
                    } catch (NumberFormatException ex) {
                        continue; // 防御：脏数据跳过
                    }

                    if (value > max) {
                        max = value;
                        maxX = j;
                        maxY = i;
                    }

                    if (value < min) {
                        min = value;
                        minX = j;
                        minY = i;
                    }
                }
            }

            return new MaxMinValueWithCoordinates(max, maxX, maxY, min, minX, minY, path);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 检测录像机在线状态
     *
     * @param deviceId
     * @return
     */
    public int getDeviceStatus(Integer deviceId, Integer driver) {
        int deviceStatus = 0;
        Nvr ipDevice = nvrService.getById(deviceId);
        if (driver == 0) {
            deviceStatus = hkNVR.getDeviceStatus(ipDevice);
        } else if (driver == 1) {
            // TODO 检查大华的设备在线状态
          //  deviceStatus = dhNVR.getDeviceStatus(ipDevice);
        }
        return deviceStatus;
    }

    /**
     *  视频录像
     * @param fileName 文件名称
     * @param channelId 通道ID
     * @param isRecord 是否录像
     * @return
     */
    public String videotaped(String fileName, int channelId, boolean isRecord) {
        Camera camera = cameraService.getById(channelId);
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr nvr = nvrService.getById(camera.getNvrId().intValue());
        if (0 == nvr.getDriver()) {
            return hkNVR.videotaped(userId, camera.getChannelId(), isRecord, fileName);
        }
        return "";
    }

    public List<RecordFile> findFileByDate(int channelId, LocalDateTime startTime, LocalDateTime endTime) throws UnsupportedEncodingException {
        Camera camera = cameraService.getById(channelId);
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Nvr ipDevices = nvrService.getById(camera.getNvrId().intValue());
        if (0 == ipDevices.getDriver()) {
            return hkNVR.findRecordFile(userId, camera.getChannelId(), startTime, endTime);
        }
        return null;
    }

    /**
     * 获取nvr的硬盘问题
     *
     * @return
     */
    public Map<String, String> getDevCfg(String ip, int port, String user, String psw) {
        return hkNVR.getDevCfg(ip, port, user, psw);
    }


    /**
     * 获取通道录像起止时间
     */
    public List<RecordFile> getChannelCfg(int channelId, LocalDateTime startTime, LocalDateTime endTime) {
        Camera camera = cameraService.getById(channelId);
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        return hkNVR.getChannelyRecordTime(userId, camera.getChannelId(), startTime, endTime);
    }

    /**
     * 检查通道是否支持3Dptz
     * @param channelId
     * @return
     */
    public boolean checkCameraThreePTZ(Integer channelId){
        Camera camera = cameraService.getById(channelId);
        if (camera.getChannelDriver() != 0 || camera.getType() == 0 || camera.getType() == 3){
            return false;
        }
        Nvr ipDevices = nvrService.getById(camera.getNvrId().intValue());
        int userId = hkNVR.loginVcr(camera.getIpAddress(), String.valueOf(8000), ipDevices.getAccount(), ipDevices.getPassword());
        boolean checkCameraThree = hkNVR.checkCameraThree(userId);
        hkNVR.loginOut(userId,camera.getIpAddress());
        return checkCameraThree;
    }
    // 返回最后操作的错误码
    public int getLastError()
    {
        return hkNVR.getLastError();
    }

    // 定义获取 PTZ 坐标的专用函数
    public PTZVO getPTZLocation(Long id) {
        Camera camera = cameraService.getById(id);
        Integer userId = (Integer) vcrlUserIdContainer.getlUserId(camera.getNvrId().intValue());
        Map<String, Integer> map = hkNVR.NET_DVR_GetPTZPos(userId, camera.getChannelId());
        return BeanCopyUtils.copy(map, PTZVO.class);
    }
}
