package com.ruoyi.web.controller.camera;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.alarm.Alarm;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hksdk.HCNetSDK;
import com.ruoyi.system.domain.EquipmentConfiguration;
import com.ruoyi.system.domain.PersonnelManagement;
import com.ruoyi.system.domain.vo.CameraInfoVo;
import com.ruoyi.system.service.CameraService;
import com.ruoyi.system.service.IEquipmentConfigurationService;
import com.ruoyi.system.service.IPersonnelManagementService;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 安防管理Controller
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/camera")
public class CameraController extends BaseController {

    @Autowired
    private CameraService cameraService;
    @Autowired
    private IEquipmentConfigurationService equipmentConfigurationService;

    /**
     * 查询安防设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:camera:list')")
    @GetMapping("/list")
    public AjaxResult list(CameraInfoVo cameraInfoVo) {
        return cameraService.selectCameraList(cameraInfoVo);
    }


    /**
     * 获取设备视频地址
     */
    @PreAuthorize("@ss.hasPermi('system:camera:video')")
    @GetMapping("/video/{cameraIndexCode}")
    public AjaxResult video(@PathVariable String cameraIndexCode) {
        return cameraService.video(cameraIndexCode);
    }

    /**
     * 设备关闭
     */
    @GetMapping("/video/closeDz")
    public AjaxResult closeDz(EquipmentConfiguration equipmentConfiguration) {
        equipmentConfiguration = equipmentConfigurationService.selectEquipmentConfigurationByEquipmentName(equipmentConfiguration.getEquipmentName());
        String ip = equipmentConfiguration.getEquipmentIp();
        short prot = (short) Short.parseShort(equipmentConfiguration.getEquipmentPort());
        String login = equipmentConfiguration.getLoginName();
        String pwd = equipmentConfiguration.getLoginPwd();

        int tdh = Alarm.login_V40( ip, prot, login, pwd);;  //登录设备
        String ret="落竿失败!";
        try {
            HCNetSDK.NET_DVR_BARRIERGATE_CFG cfg = new HCNetSDK.NET_DVR_BARRIERGATE_CFG();
            // cfg.read();
            cfg.dwChannel=tdh;  //通道号
            cfg.dwSize=cfg.size(); //结构体大小
            cfg.byLaneNo=1; //道闸号：0-表示无效值(设备需要做有效值判断)，1-道闸1
            cfg.byBarrierGateCtrl=0; // 控制参数：0-关闭道闸，1-开启道闸，2-停止道闸，3-锁定道闸
            cfg.byRes[0]=0; //保留，置为0
            //起竿操作
            Pointer name = cfg.getPointer();
            cfg.write();
            boolean a=Alarm.controlsDz(tdh, 3128,  name, cfg.size());
            if(a) {
                ret="落竿成功!";
            }else {
                ret="落竿失败!"+Alarm.controlsDzError();
                //失败重新布防, 可以不布防
                //new HikVisionService().run();
            }
            System.out.println("关闸结果--"+a+"-- 提示内容:"+ret);
            //注销用户
            boolean b=Alarm.logout(tdh);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(ret);
    }

    /**
     * 设备打开
     */
    @GetMapping("/video/openDz")
    public AjaxResult openDz(EquipmentConfiguration equipmentConfiguration) {
        equipmentConfiguration = equipmentConfigurationService.selectEquipmentConfigurationByEquipmentName(equipmentConfiguration.getEquipmentName());
        String ip = equipmentConfiguration.getEquipmentIp();
        short prot = (short) Short.parseShort(equipmentConfiguration.getEquipmentPort());
        String login = equipmentConfiguration.getLoginName();
        String pwd = equipmentConfiguration.getLoginPwd();

        int tdh = Alarm.login_V40( ip, prot, login, pwd);;  //登录设备
        //Alarm.setAlarm();
        String ret="起竿失败!";
        try {
            HCNetSDK.NET_DVR_BARRIERGATE_CFG cfg = new HCNetSDK.NET_DVR_BARRIERGATE_CFG();
            // cfg.read();
            cfg.dwChannel=tdh;  //通道号
            cfg.dwSize=cfg.size(); //结构体大小
            cfg.byLaneNo=1; //道闸号：0-表示无效值(设备需要做有效值判断)，1-道闸1
            cfg.byBarrierGateCtrl=1; // 控制参数：0-关闭道闸，1-开启道闸，2-停止道闸，3-锁定道闸
            cfg.byRes[0]=0; //保留，置为0
            //起竿操作
            Pointer name = cfg.getPointer();
            cfg.write();
            boolean a=Alarm.controlsDz(tdh, 3128,  name, cfg.size());
            if(a) {
                ret="起竿成功!";
            }else {
                ret="起竿失败!"+Alarm.controlsDzError();
                //失败重新布防, 可以不布防
                //new HikVisionService().run();
            }
            System.out.println("开闸结果--"+a+"-- 提示内容:"+ret);
            //注销用户
            boolean b=Alarm.logout(tdh);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(ret);
    }


}
