|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.web.gb28181; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.exception.ControllerException; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.bean.Device; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestParam; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestController; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.sip.InvalidArgumentException; | 
|---|
|  |  |  | import javax.sip.SipException; | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * API兼容:设备控制 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @CrossOrigin | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | @RequestMapping(value = "/api/v1/control") | 
|---|
|  |  |  | public class ApiControlController { | 
|---|
|  |  |  | 
|---|
|  |  |  | private SIPCommander cmder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private IVideoManagerStorager storager; | 
|---|
|  |  |  | private IVideoManagerStorage storager; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 设备控制 - 云台控制 | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @RequestMapping(value = "/ptz") | 
|---|
|  |  |  | private JSONObject list(String serial,String command, | 
|---|
|  |  |  | private void list(String serial,String command, | 
|---|
|  |  |  | @RequestParam(required = false)Integer channel, | 
|---|
|  |  |  | @RequestParam(required = false)String code, | 
|---|
|  |  |  | @RequestParam(required = false)Integer speed){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (logger.isDebugEnabled()) { | 
|---|
|  |  |  | logger.debug(String.format("模拟接口> 设备云台控制 API调用,deviceId:%s ,channelId:%s ,command:%d ,speed:%d ", | 
|---|
|  |  |  | serial, code, command, speed)); | 
|---|
|  |  |  | logger.debug("模拟接口> 设备云台控制 API调用,deviceId:{} ,channelId:{} ,command:{} ,speed:{} ", | 
|---|
|  |  |  | serial, code, command, speed); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (channel == null) {channel = 0;} | 
|---|
|  |  |  | if (speed == null) {speed = 0;} | 
|---|
|  |  |  | Device device = storager.queryVideoDevice(serial); | 
|---|
|  |  |  | int leftRight = 0; | 
|---|
|  |  |  | int upDown = 0; | 
|---|
|  |  |  | int inOut = 0; | 
|---|
|  |  |  | switch (command) { | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "device[ " + serial + " ]未找到"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int cmdCode = 0; | 
|---|
|  |  |  | switch (command){ | 
|---|
|  |  |  | case "left": | 
|---|
|  |  |  | leftRight = 1; | 
|---|
|  |  |  | cmdCode = 2; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "right": | 
|---|
|  |  |  | leftRight = 2; | 
|---|
|  |  |  | cmdCode = 1; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "up": | 
|---|
|  |  |  | upDown = 1; | 
|---|
|  |  |  | cmdCode = 8; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "down": | 
|---|
|  |  |  | upDown = 2; | 
|---|
|  |  |  | cmdCode = 4; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "upleft": | 
|---|
|  |  |  | upDown = 1; | 
|---|
|  |  |  | leftRight = 1; | 
|---|
|  |  |  | cmdCode = 10; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "upright": | 
|---|
|  |  |  | upDown = 1; | 
|---|
|  |  |  | leftRight = 2; | 
|---|
|  |  |  | cmdCode = 9; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "downleft": | 
|---|
|  |  |  | upDown = 2; | 
|---|
|  |  |  | leftRight = 1; | 
|---|
|  |  |  | cmdCode = 6; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "downright": | 
|---|
|  |  |  | upDown = 2; | 
|---|
|  |  |  | leftRight = 2; | 
|---|
|  |  |  | cmdCode = 5; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "zoomin": | 
|---|
|  |  |  | inOut = 2; | 
|---|
|  |  |  | cmdCode = 16; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "zoomout": | 
|---|
|  |  |  | inOut = 1; | 
|---|
|  |  |  | cmdCode = 32; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "stop": | 
|---|
|  |  |  | cmdCode = 0; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 默认值 50 | 
|---|
|  |  |  | cmder.ptzCmd(device, code, leftRight, upDown, inOut, speed==0 ? 129 : speed, 50); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | cmder.frontEndCmd(device, code, cmdCode, speed, speed, speed); | 
|---|
|  |  |  | } catch (SipException | InvalidArgumentException | ParseException e) { | 
|---|
|  |  |  | logger.error("[命令发送失败] 云台控制: {}", e.getMessage()); | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 设备控制 - 预置位控制 | 
|---|
|  |  |  | * @param serial 设备编号 | 
|---|
|  |  |  | * @param code 通道编号,通过 /api/v1/device/channellist 获取的 ChannelList.ID, 该参数和 channel 二选一传递即可 | 
|---|
|  |  |  | * @param channel 通道序号, 默认值: 1 | 
|---|
|  |  |  | * @param command 控制指令 允许值: set, goto, remove | 
|---|
|  |  |  | * @param preset 预置位编号(1~255) | 
|---|
|  |  |  | * @param name 预置位名称, command=set 时有效 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @RequestMapping(value = "/preset") | 
|---|
|  |  |  | private void list(String serial,String command, | 
|---|
|  |  |  | @RequestParam(required = false)Integer channel, | 
|---|
|  |  |  | @RequestParam(required = false)String code, | 
|---|
|  |  |  | @RequestParam(required = false)String name, | 
|---|
|  |  |  | @RequestParam(required = false)Integer preset){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (logger.isDebugEnabled()) { | 
|---|
|  |  |  | logger.debug("模拟接口> 预置位控制 API调用,deviceId:{} ,channelId:{} ,command:{} ,name:{} ,preset:{} ", | 
|---|
|  |  |  | serial, code, command, name, preset); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (channel == null) {channel = 0;} | 
|---|
|  |  |  | Device device = storager.queryVideoDevice(serial); | 
|---|
|  |  |  | if (device == null) { | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "device[ " + serial + " ]未找到"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int cmdCode = 0; | 
|---|
|  |  |  | switch (command){ | 
|---|
|  |  |  | case "set": | 
|---|
|  |  |  | cmdCode = 129; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "goto": | 
|---|
|  |  |  | cmdCode = 130; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "remove": | 
|---|
|  |  |  | cmdCode = 131; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | cmder.frontEndCmd(device, code, cmdCode, 0, preset, 0); | 
|---|
|  |  |  | } catch (SipException | InvalidArgumentException | ParseException e) { | 
|---|
|  |  |  | logger.error("[命令发送失败] 预置位控制: {}", e.getMessage()); | 
|---|
|  |  |  | throw new ControllerException(ErrorCode.ERROR100.getCode(), "命令发送失败: " + e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|