liurunyu
1 天以前 83d9b0de6d127cf0f2822c51139fa4e15a3326e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.dy.pipIrrRemote.video.ys;
 
import com.dy.common.schedulerTask.SchedulerTaskSupport;
import com.dy.pipIrrRemote.video.YsAppCtrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
/**
 * @Author: liurunyu
 * @Date: 2025/6/7 11:40
 * @Description
 */
@Slf4j
@Component
public class YsAppClient {
 
    private static final String JobName = "VideoYsJob" ;
    private static final String JobGroupName = "VideoYsGroup" ;
    private static final Integer ThreadPoolMaxCount = 1 ;//线程池线程最大个数
    private static final Integer ThreadPoolPriority = 5 ;//线程优先级
    private static final boolean quartzJobRunOneTimes = true ;//定时任务只执行一次
 
    @Value("${ys.accessTokenExpireDay: 7}")
    protected Integer accessTokenExpireDay;//AccessToken过期时间,单位天
 
    @Value("${ys.appKey}")
    protected String appKey;
 
    @Value("${ys.secret}")
    protected String secret;
 
    @Value("${ys.requestAccessTokenUrl}")
    protected String requestAccessTokenUrl;
 
    @Autowired
    protected RestTemplate restTemplate ;
 
    @Autowired
    protected YsAppCtrl ysVideoCtrl;
 
    private Long computeNetGetAccessTokenAt(){
        Long millis = (System.currentTimeMillis() + (accessTokenExpireDay * 24 * 60 * 60 * 1000) ) ;
        // millis = 10000L ;
        return millis ;
    }
 
    /**
     * 获得AccessToken
     */
    public void getAccessToken(){
        if((appKey != null && appKey.trim().length() > 0)
                && (secret != null && secret.trim().length() > 0)
                && (requestAccessTokenUrl != null && requestAccessTokenUrl.trim().length() > 0)){
            MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
            formData.add("appKey", appKey);
            formData.add("appSecret", secret);
            YsAccessTokenResponse response = this.postRequest2Ys(restTemplate, requestAccessTokenUrl, formData);
            if(response != null){
                switch (response.code){
                    case "200" :{
                        this.dealSuccessOfRequestAccessTokenFromYs(response) ;
                        break ;
                    }
                    default: {
                        this.dealErrorOfRequestAccessTokenFromYs(response) ;
                        break ;
                    }
                }
            }
        }
    }
    private void dealSuccessOfRequestAccessTokenFromYs(YsAccessTokenResponse response){
        if(response != null && response.data != null){
            ysVideoCtrl.saveAccessTokenOfYs(response.data.accessToken, response.data.expireTime);
        }
    }
 
    private void dealErrorOfRequestAccessTokenFromYs(YsAccessTokenResponse response){
        log.error("从萤石开放平台获得AccessToken失败,错误码:{},错误信息:{}", response.code, response.msg);
    }
    /**
     * 设置下次获得AccessToken
     */
    public void reSetNextGetAccessToken(Long getAccessTokenAt){
        if(getAccessTokenAt == null || getAccessTokenAt.longValue() == 0){
            getAccessTokenAt = this.computeNetGetAccessTokenAt() ;
        }
        //毫秒变成秒
        int futureSecond = (int)((getAccessTokenAt - System.currentTimeMillis()) / 1000) ;
        try {
            SchedulerTaskSupport.setThreadPoolPro(ThreadPoolMaxCount , ThreadPoolPriority);
            //因为要重复加工作任务,所以先把上次加的同组同名任务删除
            SchedulerTaskSupport.deleteJob(JobName , JobGroupName) ;
            // 只执行一次的任务
            SchedulerTaskSupport.addSecondlyJob(JobName , JobGroupName, YsAccessTokenQuartzJob.class, null, futureSecond, 1, 0) ;
        } catch (Exception e) {
            log.error("设置从萤石开放平台定时获得AccessToken任务时发生异常", e);
        }
    }
    /**
     * 向萤石开放平台发送Post请求
     * @param restTemplate SpringBoot的RestTemplate
     * @param toMwUrl web请求Url
     * @param body 数据
     * @return
     */
    private YsAccessTokenResponse postRequest2Ys(RestTemplate restTemplate, String toMwUrl, Object body) {
        String url = UriComponentsBuilder.fromUriString(toMwUrl)
                .build()
                .toUriString();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
 
        HttpEntity<?> httpEntity = new HttpEntity<>(body, headers);
        ResponseEntity<YsAccessTokenResponse> resEntity = null;
        try {
            // 通过Post方式调用接口
            resEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, YsAccessTokenResponse.class);
        } catch (Exception e) {
            log.error("从萤石开放平台定时获得AccessToken任务执行时发生异常", e);
        }
        if(resEntity != null){
            return resEntity.getBody();
        }
        return null ;
    }
 
}