|
|
@@ -1,146 +0,0 @@
|
|
|
-package com.yunfeiyun.agmp.iots.core.http;
|
|
|
-
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
-import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
-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.iot.common.constant.IotErrorCode;
|
|
|
-import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
-import com.yunfeiyun.agmp.iot.common.domain.IotFirmdev;
|
|
|
-import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
-import com.yunfeiyun.agmp.iots.device.common.hik.HikCloudApi;
|
|
|
-import com.yunfeiyun.agmp.iots.service.IIotFirmdevService;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.http.*;
|
|
|
-import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.LinkedMultiValueMap;
|
|
|
-import org.springframework.util.MultiValueMap;
|
|
|
-import org.springframework.web.client.RestTemplate;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-/**海康云眸对接*/
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class HikCloudHttpClient {
|
|
|
- private RestTemplate restTemplate;
|
|
|
- @Resource
|
|
|
- private RedisCacheManager redisCacheManager;
|
|
|
- @Resource
|
|
|
- private IIotFirmdevService iIotFirmdevService;
|
|
|
-
|
|
|
- //@PostConstruct
|
|
|
- public void init() {
|
|
|
- HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
- factory.setConnectTimeout(1000 * 60 * 3);
|
|
|
- factory.setReadTimeout(1000 * 60 * 3);
|
|
|
- factory.setConnectionRequestTimeout(1000 * 60 * 3);
|
|
|
- restTemplate = new RestTemplate(factory);
|
|
|
- }
|
|
|
-
|
|
|
- public String[] getConfig() {
|
|
|
-// try {
|
|
|
-// IotFirmdev iotFirmdev = redisCacheManager.getCacheObject(RedisCacheKey.IOT_HIK_CLOUD_API_CONFIG, ServiceNameConst.SERVICE_HIK_EZVIZ_MINITOR);
|
|
|
-// if (null == iotFirmdev) {
|
|
|
-// iotFirmdev = iIotFirmdevService.selectIotFirmdevByServiceName(ServiceNameConst.SERVICE_HIK_EZVIZ_MINITOR);
|
|
|
-// redisCacheManager.setCacheObject(RedisCacheKey.IOT_HIK_CLOUD_API_CONFIG, ServiceNameConst.SERVICE_HIK_EZVIZ_MINITOR, iotFirmdev);
|
|
|
-// }
|
|
|
-// JSONObject config = JSONObject.parseObject(iotFirmdev.getFirmdevCfg());
|
|
|
-// return new String[]{config.getString("hik_cloud_client_Id"), config.getString("hik_cloud_client_secret")};
|
|
|
-// } catch (Exception e) {
|
|
|
-// return new String[]{"null", "null"};
|
|
|
-// }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public JSONObject getMessage(String consumerId) {
|
|
|
-
|
|
|
- MultiValueMap<String, String> headers = new HttpHeaders();
|
|
|
- headers.add("Content-Type", "application/x-www-form-urlencoded");
|
|
|
- headers.add("Authorization", "bearer" + getAccessToken());
|
|
|
- MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<>();
|
|
|
- paramsMap.add("autoCommit", true);
|
|
|
- paramsMap.add("consumerId", consumerId);
|
|
|
- HttpEntity<?> httpEntity = new HttpEntity<>(paramsMap, headers);
|
|
|
- ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(HikCloudApi.GET_MESSAGE, HttpMethod.POST, httpEntity, JSONObject.class);
|
|
|
- if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
|
|
- log.info("海康多光谱相机获取光谱数据结果:{}",responseEntity);
|
|
|
- return responseEntity.getBody();
|
|
|
- }
|
|
|
- throw new BizException(ErrorCode.API_DISABLED);
|
|
|
- }
|
|
|
-
|
|
|
- public String getAccessToken() {
|
|
|
- String[] config = getConfig();
|
|
|
- String accessToken = redisCacheManager.getCacheObject(RedisCacheKey.IOT_HIK_CLOUD_HTTP_TOKEN, config[0]);
|
|
|
- if (StringUtils.isEmpty(accessToken)) {
|
|
|
- accessToken = refreshAccessToken();
|
|
|
- }
|
|
|
- if (StringUtils.isEmpty(accessToken)) {
|
|
|
- throw new IotBizException(IotErrorCode.GET_TOKEN_FAIL);
|
|
|
- }
|
|
|
- return accessToken;
|
|
|
- }
|
|
|
-
|
|
|
- public String getConsumerId() {
|
|
|
- String[] config = getConfig();
|
|
|
- String consumerId = redisCacheManager.getCacheObject(RedisCacheKey.IOT_HIK_CLOUD_MESSAGE_CHANNEL_CONSUMER_ID, config[0]);
|
|
|
- if (StringUtils.isEmpty(consumerId)) {
|
|
|
- consumerId = refreshConsumerId();
|
|
|
- } else {
|
|
|
- // 消费者标识同一时间每个账号下最多创建5个,5分钟内未使用的情况下平台上会将该标识废弃,防止多次创建消费着标识,重复使用缓存,未防止时间冲突,本地只保存4分30秒
|
|
|
- redisCacheManager.setCacheObject(RedisCacheKey.IOT_HIK_CLOUD_MESSAGE_CHANNEL_CONSUMER_ID, config[0], consumerId, 270, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
- if (StringUtils.isEmpty(consumerId)) {
|
|
|
- throw new IotBizException(IotErrorCode.GET_TOKEN_FAIL);
|
|
|
- }
|
|
|
- return consumerId;
|
|
|
- }
|
|
|
-
|
|
|
- private String refreshAccessToken() {
|
|
|
- String[] config = getConfig();
|
|
|
- MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
|
|
|
- paramsMap.set("client_id", config[0]);
|
|
|
- paramsMap.set("client_secret", config[1]);
|
|
|
- paramsMap.set("grant_type", "client_credentials");
|
|
|
- MultiValueMap<String, String> headers = new HttpHeaders();
|
|
|
- headers.add("Content-Type", "application/x-www-form-urlencoded");
|
|
|
- HttpEntity<?> httpEntity = new HttpEntity<>(paramsMap, headers);
|
|
|
- try {
|
|
|
- ResponseEntity<String> responseEntity = restTemplate.exchange(HikCloudApi.GET_ACCESS_TOKEN, HttpMethod.POST, httpEntity, String.class, new ArrayList<>());
|
|
|
- if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
|
|
- JSONObject result = JSONObject.parseObject(responseEntity.getBody());
|
|
|
- String accessToken = result.getString("access_token");
|
|
|
- redisCacheManager.setCacheObject(RedisCacheKey.IOT_HIK_CLOUD_HTTP_TOKEN, config[0], accessToken, result.getLong("expires_in"), TimeUnit.SECONDS);
|
|
|
- return accessToken;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- throw e;
|
|
|
- }
|
|
|
- throw new BizException(ErrorCode.API_DISABLED);
|
|
|
- }
|
|
|
-
|
|
|
- private String refreshConsumerId() {
|
|
|
- String[] config = getConfig();
|
|
|
- MultiValueMap<String, String> headers = new HttpHeaders();
|
|
|
- headers.add("Content-Type", "application/x-www-form-urlencoded");
|
|
|
- headers.add("Authorization", "bearer" + getAccessToken());
|
|
|
- HttpEntity<?> httpEntity = new HttpEntity<>(null, headers);
|
|
|
- ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(HikCloudApi.CREATE_CONSUMER, HttpMethod.POST, httpEntity, JSONObject.class);
|
|
|
- if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
|
|
- JSONObject result = responseEntity.getBody();
|
|
|
- if (result.get("code").equals(HttpStatus.OK.value())) {
|
|
|
- String consumerId = result.getJSONObject("data").getString("consumerId");
|
|
|
- redisCacheManager.setCacheObject(RedisCacheKey.IOT_HIK_CLOUD_MESSAGE_CHANNEL_CONSUMER_ID, config[0], consumerId, 270, TimeUnit.SECONDS);
|
|
|
- return consumerId;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-}
|