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 ;
|
}
|
|
}
|