|
|
@@ -1,5 +1,6 @@
|
|
|
package com.yunfeiyun.agmp.iots.core.http;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.date.DateUnit;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import com.alibaba.fastjson2.*;
|
|
|
@@ -8,15 +9,24 @@ import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
|
|
|
import com.yunfeiyun.agmp.common.exception.BizException;
|
|
|
import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
|
|
|
import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
|
|
|
import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
|
|
|
import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotEzvizuser;
|
|
|
import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
import com.yunfeiyun.agmp.iots.common.annotate.HttpCore;
|
|
|
+import com.yunfeiyun.agmp.iots.common.modal.IotDeviceconnResVo;
|
|
|
import com.yunfeiyun.agmp.iots.device.common.ezviz.EzvizApi;
|
|
|
import com.yunfeiyun.agmp.iots.device.mapper.IotEzvizdevicebindMapper;
|
|
|
import com.yunfeiyun.agmp.iots.device.mapper.IotEzvizuserMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
+import org.apache.http.conn.ssl.TrustStrategy;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.ssl.SSLContexts;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -27,6 +37,10 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
@@ -36,30 +50,58 @@ import java.util.concurrent.TimeUnit;
|
|
|
* 监控萤石云平台HttpClient(对接设备:海康,大华)
|
|
|
* */
|
|
|
@Slf4j
|
|
|
-@Component
|
|
|
@HttpCore(serviceName = ServiceNameConst.SERVICE_EZVIZ_MINITOR)
|
|
|
-public class EzvizHttpClient {
|
|
|
+public class EzvizHttpClient extends HttpClient{
|
|
|
+
|
|
|
private RestTemplate restTemplate;
|
|
|
- @Resource
|
|
|
- private RedisCacheManager redisCacheManager ;
|
|
|
- @Resource
|
|
|
- private IotEzvizuserMapper iotEzvizuserMapper;
|
|
|
- @Resource
|
|
|
- private IotEzvizdevicebindMapper iotEzvizdevicebindMapper;
|
|
|
+ private String ezvizAppKey ;
|
|
|
+ private String ezvizAppSecret;
|
|
|
+ private String hikCloudClientId;
|
|
|
+ private String hikCloudClientSecret;
|
|
|
+ private RedisCacheManager redisCacheManager;
|
|
|
|
|
|
- @PostConstruct
|
|
|
- public void start() throws InterruptedException{
|
|
|
- HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
- factory.setConnectTimeout(1000*60*3);
|
|
|
- factory.setReadTimeout(1000*60*3);
|
|
|
- factory.setConnectionRequestTimeout(1000*60*3);
|
|
|
- restTemplate = new RestTemplate(factory);
|
|
|
+ @Override
|
|
|
+ public void init(IotDeviceconnResVo iotDeviceconnResVo, JSONObject configJson) {
|
|
|
+ try {
|
|
|
+ super.init(iotDeviceconnResVo,configJson);
|
|
|
+ initRestTemplate();
|
|
|
+ ezvizAppKey = configJson.getString("ezvizAppKey");
|
|
|
+ ezvizAppSecret = configJson.getString("ezvizAppSecret");
|
|
|
+ hikCloudClientId = configJson.getString("hikCloudClientId");
|
|
|
+ hikCloudClientSecret = configJson.getString("hikCloudClientSecret");
|
|
|
+ redisCacheManager = SpringUtils.getBean(RedisCacheManager.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("萤石云httpClient初始化失败",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void initRestTemplate() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
|
|
|
+ TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
|
|
|
+ SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
|
|
+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
|
|
+ HttpClientBuilder clientBuilder = HttpClients.custom();
|
|
|
+ CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslsf).build();
|
|
|
+ HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
|
|
|
+ requestFactory.setConnectTimeout(1000 * 10 );
|
|
|
+ requestFactory.setReadTimeout(1000 * 10 );
|
|
|
+ requestFactory.setConnectionRequestTimeout(1000 * 10 );
|
|
|
+ requestFactory.setHttpClient(httpClient);
|
|
|
+ restTemplate = new RestTemplate(requestFactory);
|
|
|
+ }
|
|
|
+ public String getAccessToken(){
|
|
|
+ String accessToken = redisCacheManager.getCacheObject(RedisCacheKey.IOT_EZVIZ_HTTP_TOKEN,ezvizAppKey);
|
|
|
+ if(StringUtils.isEmpty(accessToken)){
|
|
|
+ accessToken = refreshToken();
|
|
|
+ }
|
|
|
+ if(StringUtils.isEmpty(accessToken)){
|
|
|
+ throw new IotBizException(IotErrorCode.GET_TOKEN_FAIL);
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
}
|
|
|
|
|
|
- public String refreshToken(String appKey,String appSecret){
|
|
|
+ public String refreshToken(){
|
|
|
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
|
|
|
- paramsMap.add("appKey",appKey);
|
|
|
- paramsMap.add("appSecret",appSecret);
|
|
|
+ paramsMap.add("appKey",ezvizAppKey);
|
|
|
+ paramsMap.add("appSecret",ezvizAppSecret);
|
|
|
MultiValueMap<String, String> headers = new HttpHeaders();
|
|
|
headers.add("Content-Type","application/x-www-form-urlencoded");
|
|
|
HttpEntity<?> httpEntity = new HttpEntity<>(paramsMap,headers);
|
|
|
@@ -72,34 +114,17 @@ public class EzvizHttpClient {
|
|
|
String accessToken = data.getString("accessToken");
|
|
|
Date expireTime = new Date(data.getLong("expireTime"));
|
|
|
long timeOut = DateUtil.between(new Date(),expireTime, DateUnit.SECOND);
|
|
|
- redisCacheManager.setCacheObject(RedisCacheKey.IOT_EZVIZ_HTTP_TOKEN,appKey,accessToken,timeOut, TimeUnit.SECONDS);
|
|
|
+ redisCacheManager.setCacheObject(RedisCacheKey.IOT_EZVIZ_HTTP_TOKEN,ezvizAppKey,accessToken,timeOut, TimeUnit.SECONDS);
|
|
|
return accessToken;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
- public String getAccessToken(String deviceCode){
|
|
|
- log.info("设备查询萤石云用户信息:【{}】",deviceCode);
|
|
|
- IotEzvizuser iotEzvizuser = iotEzvizuserMapper.selectIotEzvizuserByDeviceCode(deviceCode);
|
|
|
- log.info("萤石云用户信息:【{}】",iotEzvizuser);
|
|
|
- String accessToken = redisCacheManager.getCacheObject(RedisCacheKey.IOT_EZVIZ_HTTP_TOKEN,iotEzvizuser.getAppKey());
|
|
|
- if(StringUtils.isEmpty(accessToken)){
|
|
|
- accessToken = refreshToken(iotEzvizuser.getAppKey(), iotEzvizuser.getAppSecret());
|
|
|
- }
|
|
|
- if(StringUtils.isEmpty(accessToken)){
|
|
|
- throw new IotBizException(IotErrorCode.GET_TOKEN_FAIL);
|
|
|
- }
|
|
|
- return accessToken;
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
|
|
|
public Object postExchange( String url, JSONObject body, Class<?> responseType){
|
|
|
- String channelNo = body.getString("channelNo");
|
|
|
- String devCode = body.getString("deviceSerial");
|
|
|
- if(StringUtils.isNotEmpty(channelNo)){
|
|
|
- devCode = devCode+"-"+body.getString("channelNo");
|
|
|
- }
|
|
|
- String accessToken = getAccessToken(devCode);
|
|
|
+ String accessToken = refreshToken();
|
|
|
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
|
|
|
paramsMap.setAll(JSONObject.parseObject(body.toJSONString(JSONWriter.Feature.IgnoreNoneSerializable), HashMap.class));
|
|
|
paramsMap.add("accessToken",accessToken);
|
|
|
@@ -122,12 +147,8 @@ public class EzvizHttpClient {
|
|
|
}
|
|
|
|
|
|
public Object getExchange( String url, JSONObject body, Class<?> responseType){
|
|
|
- String channelNo = body.getString("channelNo");
|
|
|
- String devCode = body.getString("deviceSerial");
|
|
|
- if(StringUtils.isNotEmpty(channelNo)){
|
|
|
- devCode = devCode+"-"+channelNo;
|
|
|
- }
|
|
|
- String accessToken = getAccessToken(devCode);
|
|
|
+
|
|
|
+ String accessToken = refreshToken();
|
|
|
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
|
|
|
paramsMap.setAll(JSONObject.parseObject(body.toJSONString(JSONWriter.Feature.IgnoreNoneSerializable), HashMap.class));
|
|
|
paramsMap.add("accessToken",accessToken);
|