package com.sercurityControl.proteam.util; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.securityControl.common.core.utils.DateUtils; import com.sercurityControl.proteam.domain.AnalysesEntity; import com.sercurityControl.proteam.domain.ResultDeviceData; import com.sercurityControl.proteam.domain.TasksEntity; import com.sercurityControl.proteam.domain.VideoTaskEntity; import com.sercurityControl.proteam.domain.ty.*; import lombok.extern.slf4j.Slf4j; import org.omg.IOP.ENCODING_CDR_ENCAPS; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.*; import java.net.*; import java.util.*; /** * 统一视频公交类 */ @Component @Slf4j public class TyVideoUtil { /** * 统一视频平台地址 */ private static String VIDEO_ADDRESS; /** * 统一视频平台账号 */ private static String VIDEO_USERNAME; /** * 统一视频平台密码 */ private static String VIDEO_PASSWORD; @Value("${tvideo.uvpUrl}") public void setVIDEO_ADDRESS(String VIDEO_ADDRESS) { TyVideoUtil.VIDEO_ADDRESS = VIDEO_ADDRESS; } @Value("${tvideo.uvpAk}") public void setVIDEO_USERNAME(String VIDEO_USERNAME) { TyVideoUtil.VIDEO_USERNAME = VIDEO_USERNAME; } @Value("${tvideo.uvpSk}") public void setVIDEO_PASSWORD(String VIDEO_PASSWORD) { TyVideoUtil.VIDEO_PASSWORD = VIDEO_PASSWORD; } /** * 登录统一视频平台登录接口 */ private static final String LOGIN_URL = "/uvp-backend-common/api/v1/authorization"; /** * 查询设备信息数据 */ private static final String DEVICE_INFO = "/uvp-backend-common/api/v1/resource/queryDev"; /** * 查询设备上下线记录 */ private static final String DEVICE_UP_DOWN = "/uvp-backend-maintenance/api/v1/dev/queryDevStatusDescriptionInfo"; // private static final String DEVICE_UP_DOWN="/uvp-backend-maintenance/api/v1/dev/queryDevStatusOnOffLineInfo"; /** * 实时视频分析 */ private static final String VIDEO_TASK = "/uvp-micro-service/analysis/api/v1/videoTask"; /** * 分析任务查询 */ private static final String TASKS = "/uvp-micro-service/analysis/api/v1/tasks"; /** * 分析任务控制 */ private static final String TASK_CONTROL = "/uvp-micro-service/analysis/api/v1/taskControl"; /** * 分析结果查询 */ private static final String ANALYSES = "/uvp-micro-service/analysis/api/v1/analyses"; /** * 获取算法能力 */ private static final String ABILITIES = "/v1/service/abilities"; /** * 设备前端录像查询 */ private static final String WEB_VIDEO_URL="/uvp-micro-service/ses/api/v1/queryRecord"; private static final String DOWN_IMAGE="/uvp-micro-service/download"; /** * 统一视频平台登录获取token * * @return token */ public static String getToken() { String token = null; try { Map body = new HashMap<>(16); body.put("ak", VIDEO_USERNAME); body.put("sk", Base64.getEncoder().encodeToString(VIDEO_PASSWORD.getBytes())); HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + LOGIN_URL).body(JSON.toJSONString(body)).execute(); boolean ok = execute.isOk(); if (ok) { String responseBody = execute.body(); TyToken tokens = JSON.parseObject(responseBody, TyToken.class); if (Objects.equals("true", tokens.getSuccessful())) { token = tokens.getResultValue().getToken(); return token; } } } catch (Exception e) { log.error(e.toString(), e); } return token; } /** * 获取球机消息数据 */ public static List getDeviceList(List list) { List res = new ArrayList<>(); try { String[] params = list.toArray(new String[list.size()]); Map body = new HashMap<>(16); body.put("devCodes", params); String str1 = JSON.toJSONString(body); String token = getToken(); if (token != null) { String url = VIDEO_ADDRESS + DEVICE_INFO + "?ak=" + VIDEO_USERNAME + "&token=" + token + "×tamp=" + DateUtils.getTimeStamp() + "&nonce=" + UUID.randomUUID(); HttpResponse execute = HttpUtil.createPost(url).body(str1).execute(); boolean ok = execute.isOk(); if (ok) { String responseBody = execute.body(); ResultDeviceData data = JSON.parseObject(responseBody, ResultDeviceData.class); if (Objects.equals("true", data.getSuccessful())) {// res = data.getResultValue(); return res; } } } } catch (Exception e) { log.error(e.toString(), e); } return res; } public static String getDownImage(String fileId,String token){ try { String url = VIDEO_ADDRESS + DOWN_IMAGE+"?fileid="+ fileId+ "&ak=" + VIDEO_USERNAME + "&token=" + token + "×tamp=" + DateUtils.getTimeStamp() + "&nonce=" + UUID.randomUUID(); return BinaryStreamReceiver.transImage(url); }catch (Exception e){ log.error(e.toString(), e); return ""; } } /** * 球机上下线记录 * * @param devList 统一平台编码 * @param startTime 开始时间 YYYY-MM-DD * @param endTime 结束时间 YYYY-MM-DD */ public static List getDevUpDown(List devList, String startTime, String endTime) { List list = new ArrayList<>(); try { String[] params = devList.toArray(new String[devList.size()]); String token = getToken(); String url = VIDEO_ADDRESS + DEVICE_UP_DOWN + "?ak=" + VIDEO_USERNAME + "&token=" + token + "×tamp=" + DateUtils.getTimeStamp() + "&nonce=" + UUID.randomUUID(); System.err.println("url==="+url); if (token != null) { Map body = new HashMap<>(16); body.put("devCodes", params); body.put("startTime", startTime); body.put("endTime", endTime); body.put("pageNo", 1); body.put("pageSize", 200); String str1 = JSON.toJSONString(body); HttpResponse execute = HttpUtil.createPost(url).body(str1).execute(); boolean ok = execute.isOk(); System.err.println("ok==="+ok); if (ok) { String responseBody = execute.body(); System.err.println("responseBody==="+responseBody); TDevUpDownData data = JSON.parseObject(responseBody, TDevUpDownData.class); return data.getResultValue(); } } } catch (Exception e) { log.error(e.toString(), e); } return list; } /** * 获取设备web端视频 * @return */ public static List getDevVideo(String tCode,String beginTime,String endTime){ try{ String token = getToken(); String url = VIDEO_ADDRESS + WEB_VIDEO_URL + "?ak=" + VIDEO_USERNAME + "&token=" + token + "×tamp=" + DateUtils.getTimeStamp() + "&nonce=" + UUID.randomUUID(); if (token != null) { Map body = new HashMap<>(16); body.put("code", tCode); body.put("beginTime", beginTime); body.put("endTime", endTime); body.put("pageNo", 1); body.put("pageSize", 200); String str1 = JSON.toJSONString(body); HttpResponse execute = HttpUtil.createPost(url).body(str1).execute(); boolean ok = execute.isOk(); if (ok) {//视频 String responseBody = execute.body(); TyVideoVo data = JSON.parseObject(responseBody, TyVideoVo.class); if(data!=null){ ResultValueVo vo=data.getResultValue(); if(vo!=null){ List video=vo.getItems(); return video; } } } } }catch (Exception e){ log.error(e.toString(),e); } return new ArrayList<>(); } /** * 获取算法能力 */ public static JSONObject getAlgCodes(){ try{ String token = getToken(); System.err.println("token=="+token); String url = VIDEO_ADDRESS + ABILITIES + "?ak=" + VIDEO_USERNAME + "&token=" + token + "×tamp=" + DateUtils.getTimeStamp() + "&nonce=" + UUID.randomUUID(); Map body = new HashMap<>(16); body.put("pageNo", 1); body.put("pageSize", 200); String param = JSON.toJSONString(body); System.err.println(url); HttpResponse execute = HttpUtil.createPost(url).body(param).execute(); boolean ok = execute.isOk(); System.err.println("执行结果=="+ok); if (ok) { String responseBody = execute.body(); System.err.println("执行成功返回数据=="); System.err.println(responseBody); return JsonHelper.jsonStrToJsonObj(responseBody); } }catch (Exception e){ log.error(e.toString(),e); } return null; } /** * 创建任务 设备集合 * @param devs 设备集合 * @param algCode 算法id * @return */ public static JSONObject setVideoTask(List devs,String algCode) { try { List list=new ArrayList<>(); for (String str: devs) { VideoTaskEntity.DevInfosDTO dto=new VideoTaskEntity.DevInfosDTO(); dto.setDevCode(str); dto.setPresettingIndex(0); list.add(dto); } VideoTaskEntity entity=new VideoTaskEntity(); entity.setDevInfos(list); entity.setTaskName("基建-视频智能分析任务"); entity.setTaskDesc("基建-实时视频分析任务创建"); entity.setStartTime(DateTimeHelper.getNowTime()); entity.setAlgCode(algCode); entity.setTaskControl(1); entity.setInterval(60*5); //TODO 系统id entity.setBusinessSysId(""); String token = getToken(); String url = VIDEO_ADDRESS + VIDEO_TASK + "?ak=" + VIDEO_USERNAME + "&token=" + token + "×tamp=" + DateUtils.getTimeStamp() + "&nonce=" + UUID.randomUUID(); String param = JSON.toJSONString(entity); if (token != null) { HttpResponse execute = HttpUtil.createPost(url).body(param).execute(); boolean ok = execute.isOk(); if (ok) { String responseBody = execute.body(); return JsonHelper.jsonStrToJsonObj(responseBody); } } } catch (Exception e) { log.error(e.toString(), e); } return null; } /** * 获取创建的智能分析任务信息。 * 由于创建的智能分析任务可能较多,可由用户发起多起查询,每一次获取一部分数据。 * * @param taskId 任务ID * @param algCode 请求参数算法编码 * @param taskControl 任务控制状态,0代表任务不启用,1代表任务启用 * @param currentStatus 当前任务状态:0:未开启;1:开启未运行;2:运行中;3:已完成;4:已过期; * @param startTime 任务检索开始时间 * @param endTime 任务检索结束时间 * @param pageSize 每页数量 * @param pageNo 页码 * @return json */ public static TasksEntity getTasks(String taskId, String algCode, String taskControl, String currentStatus, String startTime, String endTime, String pageNo, String pageSize) { try { String token = getToken(); String url = VIDEO_ADDRESS + TASKS + "?ak=" + VIDEO_USERNAME + "&token=" + token; Map body = new HashMap<>(16); body.put("taskId", taskId); body.put("algCode", algCode); body.put("taskControl", taskControl); body.put("currentStatus", currentStatus); body.put("startTime", startTime); body.put("endTime", endTime); body.put("pageNo", pageNo); body.put("pageSize", pageSize); String param = JSON.toJSONString(body); if (token != null) { HttpResponse execute = HttpUtil.createPost(url).body(param).execute(); boolean ok = execute.isOk(); if (ok) { String responseBody = execute.body(); JSONObject jsonObject = JsonHelper.jsonStrToJsonObj(responseBody); return JSON.parseObject(jsonObject.getString("resultValue"), TasksEntity.class); } } } catch (Exception e) { log.error(e.toString(), e); } return null; } /** * 对智能分析任务进行手动的执行、停止或删除的操作。 * * @param taskId 智能分析任务主键 * @param actionType 启动/停止状态(0:停止;1:启动;2:删除) * @return json */ public static JSONObject setTaskControl(String taskId, Integer actionType) { try { String token = getToken(); String url = VIDEO_ADDRESS + TASK_CONTROL + "?ak=" + VIDEO_USERNAME + "&token=" + token; Map body = new HashMap<>(16); body.put("taskId", taskId); body.put("actionType", actionType); String param = JSON.toJSONString(body); if (token != null) { HttpResponse execute = HttpUtil.createPost(url).body(param).execute(); boolean ok = execute.isOk(); if (ok) { String responseBody = execute.body(); return JsonHelper.jsonStrToJsonObj(responseBody); } } } catch (Exception e) { log.error(e.toString(), e); } return null; } /** * 获取创建的智能分析任务产生的结果信息。 * 由于产生的结果信息可能较多,可由用户发起多起查询,每一次获取一部分数据。 * * @param startTime 开始时间(yyyy-mm-dd hh24:mi:ss) * @param endTime 结束时间(yyyy-mm-dd hh24:mi:ss) * @param taskId 任务ID * @param devCode 设备编码 * @param taskName 任务名称 * @param algCode 分析算法类型编码 * @param pageNo 当前页码 * @param pageSize 每页显示多少条 * @return json */ public static AnalysesEntity getAnalyses(String taskId, String devCode, String taskName, String algCode, String startTime, String endTime, String pageNo, String pageSize) { try { String token = getToken(); String url = VIDEO_ADDRESS + ANALYSES + "?ak=" + VIDEO_USERNAME + "&token=" + token; Map body = new HashMap<>(16); body.put("taskId", taskId); body.put("algCode", algCode); body.put("devCode", devCode); body.put("taskName", taskName); body.put("startTime", startTime); body.put("endTime", endTime); body.put("pageNo", pageNo); body.put("pageSize", pageSize); String param = JSON.toJSONString(body); if (token != null) { HttpResponse execute = HttpUtil.createPost(url).body(param).execute(); boolean ok = execute.isOk(); if (ok) { String responseBody = execute.body(); JSONObject jsonObject = JsonHelper.jsonStrToJsonObj(responseBody); return JSON.parseObject(jsonObject.getString("resultValue"), AnalysesEntity.class); } } } catch (Exception e) { log.error(e.toString(), e); } return null; } }