package com.welampiot.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.welampiot.common.BaseResult; import com.welampiot.common.Constant; import com.welampiot.common.InterfaceResultEnum; import com.welampiot.dto.VideoMonitorDTO; import com.welampiot.dto.VideoPointInfoDTO; import com.welampiot.service.SystemConfigService; import com.welampiot.service.VideoMonitorService; import com.welampiot.service.VideoPointInfoService; import com.welampiot.utils.ToolUtils; import com.welampiot.utils.VideoUtil; import com.welampiot.utils.WebUtils; import com.welampiot.vo.VideoMonitorDetailVO; import com.welampiot.vo.VideoMonitorVO; import com.welampiot.vo.VideoPointInfoVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestController @CrossOrigin @RequestMapping("/video") public class VideoController { private final static Logger log = LoggerFactory.getLogger(VideoController.class); @Autowired private ToolUtils toolUtils; @Autowired private VideoMonitorService videoMonitorService; @Autowired private SystemConfigService systemConfigService; @Autowired private VideoPointInfoService videoPointInfoService; /** * 设置AI开关状态 * @param request * @return */ @RequestMapping(value = "/setAiStatus", method = RequestMethod.POST) public BaseResult setAiStatus(HttpServletRequest request){ int version = request.getParameter("version") == null ? 0 : Integer.parseInt(request.getParameter("version")); int lampPoleId = request.getParameter("lampPoleId") == null ? 0 : Integer.parseInt(request.getParameter("lampPoleId")); int status = request.getParameter("status") == null ? 0 : Integer.parseInt(request.getParameter("status")); VideoMonitorDTO videoMonitorDTO = new VideoMonitorDTO(); videoMonitorDTO.setLampPoleId(lampPoleId); videoMonitorDTO.setIsInspect(status); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 获取摄像头回放列表 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getRecordList", method = RequestMethod.POST) public BaseResult getRecordList(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); long startTime = System.currentTimeMillis() - 24 * 3600 * 1000; ArrayList videoMonitorDTOS = new ArrayList<>(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while (System.currentTimeMillis() > startTime + 20 * 60 * 1000) { VideoMonitorDTO videoMonitorDTO1 = new VideoMonitorDTO(); videoMonitorDTO1.setStartTime(simpleDateFormat.format(new Date(startTime))); videoMonitorDTO1.setEndTime(simpleDateFormat.format(new Date(startTime + 20 * 60 * 1000))); videoMonitorDTOS.add(videoMonitorDTO1); } VideoMonitorVO videoMonitorVO = new VideoMonitorVO(); videoMonitorVO.setList(videoMonitorDTOS); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorVO); } /** * 获取回放播放地址 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getRecordPlay", method = RequestMethod.POST) public BaseResult getRecordPlay(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); Integer id = videoMonitorDTO.getId(); String startTime = videoMonitorDTO.getStartTime(); String endTime = videoMonitorDTO.getEndTime(); if (startTime == null || endTime == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(id); String gbAddress = oneById.getGbAddress(); String gbPlayBackAddress = VideoUtil.getGbPlayBackAddress(videoMonitorService, systemConfigService, gbAddress, startTime, endTime); VideoMonitorDTO videoMonitorDTO1 = new VideoMonitorDTO(); videoMonitorDTO1.setAddress(gbPlayBackAddress); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorDTO1); } /** * 获取NVR通道值信息 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getChannelValue", method = RequestMethod.POST) public BaseResult getChannelValue(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); Integer id = videoMonitorDTO.getId(); if (id == null || id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(id); String url = Constant.GB_HOST_ADDRESS+"/api/devices"; String s = WebUtils.requestGet(url); JSONArray jsonArray = JSON.parseArray(s); JSONObject data = null; if (jsonArray != null){ for (Object j: jsonArray) { JSONObject j1 = (JSONObject) j; String deviceId = (String) j1.get("deviceId"); if (deviceId.equalsIgnoreCase(oneById.getGbAddress())){ data = j1; } } } ArrayList objects = new ArrayList<>(); if (data != null){ for (Object channelMap : (JSONArray) data.get("channelMap")) { JSONObject channelMap1 = (JSONObject) channelMap; VideoMonitorDTO videoMonitorDTO1 = new VideoMonitorDTO(); videoMonitorDTO1.setName((String) channelMap1.get("name")); videoMonitorDTO1.setChannelId((String) channelMap1.get("channelId")); objects.add(videoMonitorDTO1); } } return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,objects); } /** * NVR获取回放列表 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getNvrRecordList", method = RequestMethod.POST) public BaseResult getNvrRecordList(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); long startTime = System.currentTimeMillis() - 24 * 3600 * 1000; ArrayList videoMonitorDTOS = new ArrayList<>(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while (System.currentTimeMillis() > startTime + 20 * 60 * 1000) { VideoMonitorDTO videoMonitorDTO1 = new VideoMonitorDTO(); videoMonitorDTO1.setStartTime(simpleDateFormat.format(new Date(startTime))); videoMonitorDTO1.setEndTime(simpleDateFormat.format(new Date(startTime + 20 * 60 * 1000))); videoMonitorDTOS.add(videoMonitorDTO1); } VideoMonitorVO videoMonitorVO = new VideoMonitorVO(); videoMonitorVO.setList(videoMonitorDTOS); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorVO); } /** * NVR获取回放地址 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getPlayBackAddressByChannel", method = RequestMethod.POST) public BaseResult getPlayBackAddressByChannel(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); if (videoMonitorDTO.getId() == null || videoMonitorDTO.getChannel() == null || videoMonitorDTO.getStartTime() == null || videoMonitorDTO.getEndTime() == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(videoMonitorDTO.getId()); String nvrGbPlayBackAddress = VideoUtil.getNvrGbPlayBackAddress(videoMonitorService, systemConfigService, oneById.getGbAddress(), videoMonitorDTO.getStartTime(), videoMonitorDTO.getEndTime(), videoMonitorDTO.getChannel()); VideoMonitorDTO videoMonitorDTO1 = new VideoMonitorDTO(); videoMonitorDTO1.setAddress(nvrGbPlayBackAddress); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorDTO1); } /** * NVR获取通道播放地址 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getPlayAddressByChannel", method = RequestMethod.POST) public BaseResult getPlayAddressByChannel(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); if (videoMonitorDTO.getId() == null || videoMonitorDTO.getChannel() == null || videoMonitorDTO.getStartTime() == null || videoMonitorDTO.getEndTime() == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(videoMonitorDTO.getId()); String nvrGbPlayBackAddress = VideoUtil.getNvrGbPlayAddress(videoMonitorService, systemConfigService, oneById.getGbAddress(), videoMonitorDTO.getChannel()); VideoMonitorDTO videoMonitorDTO1 = new VideoMonitorDTO(); videoMonitorDTO1.setAddress(nvrGbPlayBackAddress); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorDTO1); } /** * 设置报警位 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/setEmergencyLocation", method = RequestMethod.POST) public BaseResult setEmergencyLocation(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); if (videoMonitorDTO.getId() == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(videoMonitorDTO.getId()); String url = Constant.GB_HOST_ADDRESS+"/api/postion/"+oneById.getGbAddress()+"/34020000001320000001"; String param = "{\"code\":1,\"postion\":1}"; WebUtils.requestPost(url, param); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 查看报警位 * @param videoMonitorDTO * @return */ @RequestMapping(value = "/getEmergencyLocation", method = RequestMethod.POST) public BaseResult getEmergencyLocation(VideoMonitorDTO videoMonitorDTO){ int version = videoMonitorDTO.getVersion(); if (videoMonitorDTO.getId() == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(videoMonitorDTO.getId()); String url = Constant.GB_HOST_ADDRESS+"/api/postion/"+oneById.getGbAddress()+"/34020000001320000001"; String param = "{\"code\":2,\"postion\":1}"; WebUtils.requestPost(url, param); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 视屏监控列表 * @param request 路段,关键字搜索,分页 * @return 视屏监控列表 */ @RequestMapping(value = "/getList", method = RequestMethod.POST) public BaseResult getList(HttpServletRequest request) { Integer version = (Integer) toolUtils.getRequestContent(request,"version",1); Integer page = (Integer) toolUtils.getRequestContent(request,"page",1); Integer count = (Integer) toolUtils.getRequestContent(request,"count",1); Integer areaId = (Integer) toolUtils.getRequestContent(request,"areaId",1); Integer sectionId = (Integer) toolUtils.getRequestContent(request,"sectionId",1); String keyword = (String) toolUtils.getRequestContent(request,"keyword",2); if (page == 0) page = 1; if (count == 0) count = 16; VideoMonitorVO vo = new VideoMonitorVO(); vo.setLimit(count * (page - 1)); vo.setOffset(count); vo.setAreaId(areaId); vo.setSectionId(sectionId); vo.setKeyword(keyword); vo.setSectionList(toolUtils.getSectionList(request)); List videoList = videoMonitorService.getVideoListByVO(vo); Integer total = videoMonitorService.getVideoTotal(vo); VideoMonitorVO videoMonitorVO = new VideoMonitorVO(); videoMonitorVO.setList(videoList); videoMonitorVO.setTotal((int) Math.ceil(total / (double) count)); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorVO); } /** * 视屏监控详情 * @param vo id * @return 视屏监控详情 */ @RequestMapping(value = "/info", method = RequestMethod.POST) public BaseResult info(VideoMonitorVO vo) { if (vo.getVersion() == null) vo.setVersion(0); Integer version = vo.getVersion(); Integer id = vo.getId(); if (id == null || id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO videoMonitorDTO = videoMonitorService.getVideoMonitorDetailsById(vo); if (videoMonitorDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version); VideoMonitorDetailVO videoMonitorDetailVO = new VideoMonitorDetailVO(); BeanUtils.copyProperties(videoMonitorDTO,videoMonitorDetailVO); if (videoMonitorDetailVO.getType() != null && videoMonitorDetailVO.getType() == 1) { videoMonitorDetailVO.setTypeStr("NVR"); } else { videoMonitorDetailVO.setTypeStr("IPC"); } return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoMonitorDetailVO); } /** * 添加/编辑视屏监控 * @param vo 视屏监控字段 * @return 添加/编辑视屏监控 */ @RequestMapping(value = "/save", method = RequestMethod.POST) public BaseResult save(VideoMonitorDetailVO vo) { if (vo.getVersion() == null) vo.setVersion(0); Integer version = vo.getVersion(); Integer id = vo.getId(); if (vo.getName() == null || vo.getName().trim().isEmpty()) return toolUtils.response(InterfaceResultEnum.LACK_NAME_ERROR,version); if (vo.getSectionId() == null || vo.getSectionId() == 0) return toolUtils.response(InterfaceResultEnum.LACK_SECTION_ERROR,version); if (vo.getAreaId() == null || vo.getAreaId() == 0) return toolUtils.response(InterfaceResultEnum.LACK_AREA_ERROR,version); if (vo.getDevId() == null || vo.getDevId().isEmpty()) return toolUtils.response(InterfaceResultEnum.LACK_DEV_SN_ERROR,version); if (vo.getPassword() == null || vo.getPassword().isEmpty()) return toolUtils.response(InterfaceResultEnum.LACK_DEV_CODE_ERROR,version); if (vo.getDeviceType() == null) return toolUtils.response(InterfaceResultEnum.LACK_DEV_TYPE_ERROR,version); if (vo.getType() == null) return toolUtils.response(InterfaceResultEnum.LACK_VIDEO_TYPE_ERROR,version); VideoMonitorDTO videoMonitorDTO = new VideoMonitorDTO(); BeanUtils.copyProperties(vo,videoMonitorDTO); VideoMonitorDTO dto = new VideoMonitorDTO(); if (id == null || id == 0) { // 添加 dto.setDevId(vo.getDevId()); if (videoMonitorService.checkVideoDevId(dto) > 0) return toolUtils.response(InterfaceResultEnum.DEV_SN_UNIQUE_ERROR,version); videoMonitorService.addVideoMonitorData(videoMonitorDTO); } else { // 编辑 dto.setId(id); dto.setDevId(vo.getDevId()); if (videoMonitorService.checkVideoDevId(dto) > 0) return toolUtils.response(InterfaceResultEnum.DEV_SN_UNIQUE_ERROR,version); videoMonitorService.updateVideoMonitorData(videoMonitorDTO); } return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 删除视屏监控 * @param vo id * @return 删除视屏监控 */ @RequestMapping(value = "/del", method = RequestMethod.POST) public BaseResult del(VideoMonitorVO vo) { if (vo.getVersion() == null) vo.setVersion(0); Integer version = vo.getVersion(); String videoId = vo.getVideoId(); if (videoId == null || videoId.trim().isEmpty()) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); String[] split = videoId.split(","); for (String s : split) { Integer id = Integer.valueOf(s); videoMonitorService.deleteVideoMonitorById(id); } return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 获取视屏监控点位信息 * @param vo id * @return 获取视屏监控点位信息 */ @RequestMapping(value = "/getPointInfo", method = RequestMethod.POST) public BaseResult getPointInfo(VideoPointInfoVO vo) { if (vo.getVersion() == null) vo.setVersion(0); Integer version = vo.getVersion(); Integer id = vo.getId(); if (id == null || id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); List videoPointList = videoPointInfoService.getVideoPointListByVideoId(id); VideoPointInfoVO videoPointInfoVO = new VideoPointInfoVO(); videoPointInfoVO.setList(videoPointList); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,videoPointInfoVO); } /** * 设置点位信息 * @param vo 点位属性 * @return 设置点位信息 */ @RequestMapping(value = "/setPointInfo", method = RequestMethod.POST) public BaseResult setPointInfo(VideoPointInfoVO vo) { if (vo.getVersion() == null) vo.setVersion(0); Integer version = vo.getVersion(); Integer point = vo.getPoint(); Integer time = vo.getTime(); Integer videoId = vo.getVideoId(); if (point == null || time == null || videoId == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoPointInfoDTO videoPointInfoDTO = new VideoPointInfoDTO(); BeanUtils.copyProperties(vo,videoPointInfoDTO); videoPointInfoService.addVideoPointData(videoPointInfoDTO); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 设置点位状态 * @param vo 点位属性 * @return 设置点位状态 */ @RequestMapping(value = "/setPointStatus", method = RequestMethod.POST) public BaseResult setPointStatus(VideoPointInfoVO vo) { if (vo.getVersion() == null) vo.setVersion(0); Integer version = vo.getVersion(); Integer point = vo.getPoint(); Integer status = vo.getStatus(); Integer videoId = vo.getVideoId(); if (point == null || status == null || videoId == null) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoPointInfoDTO videoPointInfoDTO = new VideoPointInfoDTO(); BeanUtils.copyProperties(vo,videoPointInfoDTO); videoPointInfoService.updateVideoPointStatus(videoPointInfoDTO); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 云台控制 * @param request * @return */ @RequestMapping(value = "/yunStart", method = RequestMethod.POST) public BaseResult yunStart(HttpServletRequest request){ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1); Integer direction = (Integer) toolUtils.getRequestContent(request,"direction",1); String deviceSerial = (String) toolUtils.getRequestContent(request,"deviceSerial",2); if (deviceSerial.length() == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); String requestData = "{\"moveSpeed\":130,\"zoomSpeed\":130"; switch (direction){ case 0: requestData += ",\"upDown\":1,\"leftRight\":0,\"inOut\":0"; case 1: requestData += ",\"upDown\":2,\"leftRight\":0,\"inOut\":0"; case 2: requestData += ",\"upDown\":0,\"leftRight\":1,\"inOut\":0"; case 3: requestData += ",\"upDown\":0,\"leftRight\":2,\"inOut\":0"; case 4: requestData += ",\"upDown\":1,\"leftRight\":1,\"inOut\":0"; case 5: requestData += ",\"upDown\":2,\"leftRight\":1,\"inOut\":0"; case 6: requestData += ",\"upDown\":1,\"leftRight\":2,\"inOut\":0"; case 7: requestData += ",\"upDown\":2,\"leftRight\":2,\"inOut\":0"; case 8: requestData += ",\"upDown\":0,\"leftRight\":0,\"inOut\":2"; case 9: requestData += ",\"upDown\":0,\"leftRight\":0,\"inOut\":1"; } requestData += "}"; VideoMonitorDTO oneByDevId = videoMonitorService.getOneByDevId(deviceSerial); String url1 = Constant.GB_HOST_ADDRESS+"/api/ptz/"+oneByDevId.getGbAddress()+"/34020000001320000001"; WebUtils.requestPost(url1, requestData); try { Thread.sleep(500); } catch (InterruptedException e) { // throw new RuntimeException(e); } requestData = "{\"upDown\":0,\"leftRight\":0,\"inOut\":0,\"moveSpeed\":0,\"zoomSpeed\":0}"; WebUtils.requestPost(url1, requestData); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } /** * 播放视屏监控 * @param request * @return */ @RequestMapping(value = "/videoPlay", method = RequestMethod.POST) public BaseResult videoPlay(HttpServletRequest request){ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1); Integer id = (Integer) toolUtils.getRequestContent(request,"id",1); if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(id); String gbPlayAddress = VideoUtil.getGbPlayAddress(oneById.getGbAddress()); oneById.setHlsHd(gbPlayAddress); oneById.setRtmp(gbPlayAddress); oneById.setRtmp2(gbPlayAddress); oneById.setRtmpHD2(gbPlayAddress); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,oneById); } /** * 播放视屏监控 * @param request * @return */ @RequestMapping(value = "/videoPlayAsy", method = RequestMethod.POST) public BaseResult videoPlayAsy(HttpServletRequest request){ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1); Integer id = (Integer) toolUtils.getRequestContent(request,"id",1); if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(id); String gbPlayAddress = VideoUtil.getGbPlayAddress(oneById.getGbAddress()); oneById.setHlsHd(gbPlayAddress); oneById.setRtmp(gbPlayAddress); oneById.setRtmp2(gbPlayAddress); oneById.setRtmpHD2(gbPlayAddress); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,oneById); } /** * 查看点位 * @param request * @return */ @RequestMapping(value = "/viewPoint", method = RequestMethod.POST) public BaseResult viewPoint(HttpServletRequest request){ Integer version = (Integer) toolUtils.getRequestContent(request,"version",1); Integer id = (Integer) toolUtils.getRequestContent(request,"videoId",1); Integer point = (Integer) toolUtils.getRequestContent(request,"point",1); if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version); VideoMonitorDTO oneById = videoMonitorService.getOneById(id); point = point + 1; String url = Constant.GB_HOST_ADDRESS+"/api/postion/"+oneById.getGbAddress()+"/34020000001320000001"; String param = "{\"code\":2,\"postion\":"+point+"}"; WebUtils.requestPost(url, param); return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version); } }