package com.persagy.cameractl.controller; import com.alibaba.fastjson.JSONObject; import com.persagy.cameractl.cache.SceneConfigCache; import com.persagy.cameractl.model.NvrSceneRelation; import com.persagy.cameractl.model.SceneConfig; import com.persagy.cameractl.service.PtzMain; import com.persagy.cameractl.service.windows.Nvr9CameraExecuteApi; import com.persagy.cameractl.utils.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.OutputStream; import java.nio.file.Files; import java.util.*; import java.util.Map.Entry; @Slf4j @CrossOrigin @RestController public class HelloController { @Autowired private PtzMain ptzMain; @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public @ResponseBody String helloName(@PathVariable("name") String name) { return ResultTools.errorResult(name); } /** * 摄像头操作接口(底层调用SDK),包括:摄像头控制、回放、查询SDK日志。客户端回放流程为:客户端先调用该接口,成功后再调用vplayf接口播放MP4 * opertype为control时,参数:command、cameraIp、cameraPort、userName、password、channel、 * speed * * opertype为playback时,参数:cameraIp、cameraPort、userName、password、channel、 * startDateStr、endDateStr * * opertype为searchlog时,参数:cameraIp、cameraPort、userName、password、channel、 * startDateStr、endDateStr */ @RequestMapping(value = "/sdk/{opertype}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public String ptzOperation(@PathVariable("opertype") String operType, @RequestBody Camera _camera) { // 拆分rtsp://(Admin:123@)0.0.0.0(:0)(/xxx/xxxxxx)(?xxx=xxxx) 中的ip地址 if (StringUtils.isNotEmpty(_camera.ipcIp) && _camera.ipcIp.startsWith("rtsp://")) { String val = _camera.ipcIp.substring(7); int idx = val.indexOf('@'); if (idx > -1) { val = val.substring(idx + 1); } idx = val.indexOf('/'); if (idx > -1) { val = val.substring(0, idx); } idx = val.indexOf(':'); if (idx > -1) { val = val.substring(0, idx); } idx = val.indexOf('?'); if (idx > -1) { val = val.substring(0, idx); } _camera.ipcIp = val; } // 2022-07-04 视频必须转码 _camera.transcoding = true; ResultClass result = ptzMain.ptzOper(_camera, operType); switch (String.valueOf(result.name)) { // 调用成功 case "true": return ResultTools.dataResult(result.resultData); // 调用失败 default: return ResultTools.errorResult(result.reason); } } /** * 请求视频文件接口 */ @RequestMapping(value = "/vplayf/{file}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public void vplay(@PathVariable("file") String file, HttpServletResponse response, HttpServletRequest request) { String filePath = OtherTools.getLocalVideoFilePath(file); if (filePath.equals("")) return; try { File videoFile = new File(filePath); if (!videoFile.exists()) return; response.setContentType(String.format("video/%s", OtherTools.getFileSuffix(file))); response.setHeader("Content-Disposition", "attachment; filename=\"" + file + "\""); response.setContentLength((int) videoFile.length()); // 不支持分段请求 response.setHeader("Accept-Ranges", "none"); response.setHeader("Etag", "W/\"" + StringTools.getUUID() + "\""); OutputStream os = response.getOutputStream(); Files.copy(videoFile.toPath(), os); // 先声明的流后关掉! os.flush(); os.close(); } catch (Exception e) { log.error("播放" + file + "异常:", e); } } /** * 获取场景 * * @param oper * @return * @date 2021年11月4日 下午12:11:52 */ @GetMapping(value = "/config/{oper}") public Map scene(@PathVariable("oper") String oper) { Map result = new HashMap(); result.put(ResultTools.key_result, ResultTools.result_success); List dataList = new ArrayList(); Map sceneConfig = SceneConfigCache.SCENE_CONFIG; Set> entrySet = sceneConfig.entrySet(); Iterator> iterator = entrySet.iterator(); switch (oper) { case "scene": Set temp = new HashSet(); while (iterator.hasNext()) { Entry next = iterator.next(); NvrSceneRelation sceneRelation = next.getValue(); String key = sceneRelation.getGroup() + "_" + sceneRelation.getScene(); if (!temp.contains(key)) { JSONObject jsonObject = new JSONObject(); jsonObject.put("group", sceneRelation.getGroup()); jsonObject.put("scene", sceneRelation.getScene()); dataList.add(jsonObject); temp.add(key); } } break; case "json": while (iterator.hasNext()) { Entry next = iterator.next(); dataList.add(next.getValue()); } break; default: break; } result.put(ResultTools.key_data, dataList); return result; } /** * 调用软件平台接口,根据摄像头监控点位获取预览流url、回放流url、云台控制 * operType: preview 预览 playback 回放 control 云台控制 other 支持传入接口名称和参数,方便调试 tvwall 获取视频墙列表 * tvwalllayout 获取某个视频墙的布局 toup 投屏,即上墙操作 exittoup 退出投屏 * kaichuang 在电视墙屏幕上进行开窗 setwinsource 设置窗口视频源 * 预览参数: * cameraIndexCode(视频通道编码,例1000000$1$0$0)、streamType(码流类型,可不传,不传时默认1; 0:主码流 1:子码流 2:第三码流)、levelNo(级联号,没有时不传) * 回放参数: * cameraIndexCode(视频通道编码,例1000000$1$0$0)、streamType(码流类型,可不传,不传时默认1; 0:主码流 1:子码流 2:第三码流)、levelNo(级联号,没有时不传) * startDateStr(开始时间,2021-04-01 11:12:13)、endDateStr(结束时间,2021-04-01 11:12:13) * 云台控制参数: * cameraIndexCode(视频通道编码,例1000000$1$0$0)、command(控制命令)、speed(速度,值范围1~8,默认8) * * other 根据传入接口名称来调用对应的大华软件平台接口,参数: * url 调用的大华软件平台接口名称,例:admin/rest/api * paramStr 自定义参数,格式:{_type:"get | post",interfaceId:"可为空",isToken:"1 url上需要拼接token | 0 url上不需要拼接token"} * jsonParam 传给大华软件平台的接口参数;例:"{\"param\":{\"category\":1}}" * * 获取视频墙列表参数 * 无 * 获取某个视频墙的布局参数 * tvWallId 视频墙ID * 投屏参数 * layoutCode one(只有一个窗口)、left-up-one(六个窗口,左上角是大窗口)、right-down-one(六个窗口,右下角是大窗口)、right-up-one(六个窗口,右上角是大窗口)、left-down-one(六个窗口,左下角是大窗口)、multi-one(九宫格窗口) * cameraIndexArr 视频墙数据源数组,每一项均为cameraIndexCode,数组的顺序和窗口顺序保持一致 */ @RequestMapping(value = "/vision/{opertype}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public String hikvisionOper(@PathVariable("opertype") String operType, @RequestBody SceneConfig sceneConfig) { log.info("调用NVR9平台接口开始,操作类型:{},场景配置:{}", operType, sceneConfig); long start = System.currentTimeMillis(); Nvr9CameraExecuteApi dahCameraExecuteApi = new Nvr9CameraExecuteApi(); ResultClass executeResult = operType.equals("currentVideo") ? dahCameraExecuteApi.currentVideo(sceneConfig.getNvrSceneRelation()) : operType.equals("currentlunx") ? dahCameraExecuteApi.currentlunx(sceneConfig.getNvrSceneRelation()) : operType.equals("transitionScene") ? dahCameraExecuteApi.transitionScene(sceneConfig) : operType.equals("toup") ? dahCameraExecuteApi.toup(sceneConfig) : operType.equals("lunx") ? dahCameraExecuteApi.lunx(sceneConfig) : new ResultClass(false, "非法请求"); switch (String.valueOf(executeResult.name)) { case "true": log.info("调用NVR9平台接口success,返回值:{},耗时:{} ms", ResultTools.dataResult(executeResult.resultData), (System.currentTimeMillis() - start)); return ResultTools.dataResult(executeResult.resultData); default: log.warn("调用NVR9平台接口success,返回值:{},耗时:{} ms", ResultTools.dataResult(executeResult.reason), (System.currentTimeMillis() - start)); return ResultTools.errorResult(executeResult.reason); } } }