Commit 089f05e2 authored by 周昊's avatar 周昊
Browse files

1、icc对接接口

parent f3045b08
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
package com.ruoyi.algorithm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.model.v202010.OauthPublicKeyResponse;
import com.ruoyi.algorithm.domain.AlgorithmBase;
import com.ruoyi.algorithm.domain.dto.AlgorithmBaseListDto;
import com.ruoyi.algorithm.service.IAlgorithmBaseService;
@@ -8,6 +15,7 @@ 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.icc.IccUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@@ -86,4 +94,11 @@ public class AlgorithmBaseController extends BaseController {
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(algorithmBaseService.deleteAlgorithmBaseByIds(ids));
    }

    private static final String PUBLIC_KEY_URL = "https://124.160.33.135:4077/evo-apigw/evo-oauth/1.0.0/oauth/public-key";

    @GetMapping("/test")
    public String test() throws ClientException {
        return IccUtil.StartPlaybackByTime();
    }
}
+0 −25
Original line number Diff line number Diff line
package com.ruoyi.icc;

import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 周昊
 * @desc ...
 * @date 2023-05-17 15:27:24
 */
@Configuration
public class IccConfig {
    @Bean
    public IClient iccDefaultClient() throws ClientException {
        return new DefaultClient();
    }
    /*//如果你项目没有办法增加iccSdk.properties配置文件,你也可以
    @Bean
    public IClient iccDefaultClient() throws ClientException {
        return new DefaultClient("host", "username", "password", "clientId", "clientSecret");
    }*/
}
+126 −0
Original line number Diff line number Diff line
package com.ruoyi.icc;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.http.IccTokenResponse;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.dahuatech.icc.oauth.model.v202010.OauthPublicKeyResponse;
import com.dahuatech.icc.util.SignUtil;
import com.ruoyi.icc.domain.IccConfigDomain;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 周昊
 * @desc ...
 * @date 2023-05-18 15:40:57
 */
public class IccUtil {

    private static final String HOST = "https://124.160.33.135:4077";

    private static final String PUBLIC_KEY_URL = HOST + "/evo-apigw/evo-oauth/1.0.0/oauth/public-key";

    private static final String TOKEN_URL = HOST + "/evo-apigw/evo-oauth/1.0.0/oauth/extend/token";

    private static final String START_VIDEO_URL = "/evo-apigw/admin/API/MTS/Video/StartVideo";

    private static final String START_Playback_URL = "/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime";

    public static String publicKey() throws ClientException {
        IccHttpHttpRequest pubRequest = new IccHttpHttpRequest(PUBLIC_KEY_URL, Method.GET);
        String pubBody = pubRequest.execute();
        OauthPublicKeyResponse keyResp = (OauthPublicKeyResponse) BeanUtil.toBean(pubBody, OauthPublicKeyResponse.class);
        JSONObject entries = JSONUtil.parseObj(pubBody);
        JSONObject data = entries.get("data", JSONObject.class);
        String publicKey = data.get("publicKey", String.class);
        return publicKey;
    }

    public static String userPasswordAuth() throws ClientException {
        String publicKey = publicKey();
        Map<String, Object> map = new HashMap();
        map.put("grant_type", "password");
        map.put("username", IccConfigDomain.getUsername());
        map.put("password", SignUtil.encryptRSA(IccConfigDomain.getPassword(), publicKey));
        map.put("client_id", IccConfigDomain.getClientId());
        map.put("client_secret", IccConfigDomain.getClientSecret());
        map.put("public_key", publicKey);
        IccHttpHttpRequest pr = new IccHttpHttpRequest(TOKEN_URL, Method.POST, JSONUtil.toJsonStr(map));
        String prBody = pr.execute();
        IccTokenResponse keyResp = (IccTokenResponse) BeanUtil.toBean(prBody, IccTokenResponse.class);
        JSONObject entries = JSONUtil.parseObj(prBody);
        JSONObject data = entries.get("data", JSONObject.class);
        return "bearer " + data.get("access_token", String.class);
    }

    public static String startVideo(String channelId) throws ClientException {
        IClient iClient = new DefaultClient();
        /**
         * 1、请求地址是统一网关入口,以 /evo-apigw 开头
         * 2、方法参见 @see com.dahuatech.hutool.http.Method
         */
        GeneralRequest generalRequest = new GeneralRequest(START_VIDEO_URL, Method.POST);
        // set http post body
        generalRequest.body("{\n" +
                "  \"data\": {\n" +
                "    \"channelId\": \"" + channelId + "\",\n" +
                "    \"dataType\": \"1\",\n" +
                "    \"streamType\": \"1\"\n" +
                "  }\n" +
                "}");
        // set header
        generalRequest.header("Authorization", userPasswordAuth());
        // 发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());

        JSONObject entries = JSONUtil.parseObj(generalResponse);
        JSONObject result = entries.get("result", JSONObject.class);
        JSONObject data = result.get("data", JSONObject.class);
        String url = data.get("url", String.class);
        String token = data.get("token", String.class);
        url = url.substring(0, url.indexOf("|"));
        return url + "?token=" + token;
    }

    public static String StartPlaybackByTime(String channelId, Long startTime, Long endTime) throws ClientException {
        IClient iClient = new DefaultClient();
        /**
         * 1、请求地址是统一网关入口,以 /evo-apigw 开头
         * 2、方法参见 @see com.dahuatech.hutool.http.Method
         */
        GeneralRequest generalRequest = new GeneralRequest(START_Playback_URL, Method.POST);
        // set http post body
        generalRequest.body("{\n" +
                "  \"data\": {\n" +
                "    \"channelId\": \"" + channelId + "\",\n" +
                "    \"recordSource\": \"2\",\n" +
                "    \"startTime\": \"" + startTime + "\",\n" +
                "    \"endTime\": \"" + endTime + "\",\n" +
                "    \"streamType\": \"1\",\n" +
                "    \"recordType\": \"1\"\n" +
                "  }\n" +
                "}");
        // set header
        generalRequest.header("Authorization", userPasswordAuth());
        // 发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());

        JSONObject entries = JSONUtil.parseObj(generalResponse);
        JSONObject result = entries.get("result", JSONObject.class);
        JSONObject data = result.get("data", JSONObject.class);
        String url = data.get("url", String.class);
        String token = data.get("token", String.class);
        url = url.substring(url.indexOf("|") + 1);
        return url + "?token=" + token;
    }

}
+86 −0
Original line number Diff line number Diff line
package com.ruoyi.icc.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author 周昊
 * @desc ...
 * @date 2023-05-18 17:04:22
 */
@Component
@ConfigurationProperties(prefix = "icc.sdk")
public class IccConfigDomain {
    private static String host;
    private static String clientId;
    private static String clientSecret;
    private static String pwdClientId;
    private static String pwdClientSecret;
    private static String username;
    private static String password;
    private static String grantType;

    public static String getHost() {
        return host;
    }

    public void setHost(String host) {
        IccConfigDomain.host = host;
    }

    public static String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        IccConfigDomain.clientId = clientId;
    }

    public static String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        IccConfigDomain.clientSecret = clientSecret;
    }

    public String getPwdClientId() {
        return pwdClientId;
    }

    public void setPwdClientId(String pwdClientId) {
        IccConfigDomain.pwdClientId = pwdClientId;
    }

    public String getPwdClientSecret() {
        return pwdClientSecret;
    }

    public void setPwdClientSecret(String pwdClientSecret) {
        IccConfigDomain.pwdClientSecret = pwdClientSecret;
    }

    public static String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        IccConfigDomain.username = username;
    }

    public static String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        IccConfigDomain.password = password;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        IccConfigDomain.grantType = grantType;
    }
}
+0 −24
Original line number Diff line number Diff line
package com.ruoyi.icc;

import cn.hutool.core.bean.BeanUtil;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.dahuatech.icc.oauth.model.v202010.OauthPublicKeyResponse;

/**
 * @author 周昊
 * @desc ...
 * @date 2023-05-17 15:28:50
 */
public class test {
    public static void main(String[] args) throws ClientException {
//        IccHttpHttpRequest pubRequest = new IccHttpHttpRequest(PUBLIC_KEY_URL, Method.GET);
//        String pubBody = pubRequest.execute();
//        OauthPublicKeyResponse keyResp = (OauthPublicKeyResponse) BeanUtil.toBean(pubBody, OauthPublicKeyResponse.class);
    }
}
Loading