package com.iailab.module.data.channel.http.collector.asdb;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.iailab.framework.common.constant.CommonConstant;
|
import com.iailab.framework.common.util.date.DateUtils;
|
import com.iailab.framework.common.util.http.HttpUtils;
|
import com.iailab.module.data.channel.http.entity.HttpApiEntity;
|
import com.iailab.module.data.channel.http.service.HttpApiService;
|
import com.iailab.module.data.common.enums.DataSourceType;
|
import com.iailab.module.data.common.utils.TagUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.math.BigDecimal;
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2025年03月26日
|
*/
|
@Slf4j
|
@Component
|
public class HttpCollectorForAsdy {
|
|
@Autowired
|
private HttpApiService httpApiService;
|
|
@Autowired
|
private RedisTemplate redisTemplate;
|
|
public static final long offset = 10;
|
|
private HttpApiEntity getHttpApi(String id) {
|
return httpApiService.getFromCatch(id);
|
}
|
|
public BigDecimal getTagValue(String sourceId, String tagNo) {
|
BigDecimal value = CommonConstant.BAD_VALUE;
|
HttpApiEntity httpApi = this.getHttpApi(sourceId);
|
//先查缓存
|
String catchKey = "IailabData:" + httpApi.getCode() + ":" + tagNo;
|
if (redisTemplate.hasKey(catchKey)) {
|
log.info("查找IailabData缓存: " + catchKey);
|
return new BigDecimal(redisTemplate.opsForValue().get(catchKey).toString());
|
}
|
String responseStr = HttpUtils.sendGet(httpApi.getUrl(), null, "");
|
|
JSONObject jsonObject = JSON.parseObject(responseStr);
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.MILLISECOND,0);
|
calendar.set(Calendar.SECOND,0);
|
calendar.set(Calendar.MINUTE,0);
|
calendar.set(Calendar.HOUR_OF_DAY,0);
|
calendar.add(Calendar.DAY_OF_YEAR,-1);
|
String time = DateUtils.format(calendar.getTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
|
|
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
String cacheValue = "0";
|
if (entry.getValue() instanceof String) {
|
cacheValue = entry.getValue().toString();
|
}else if (entry.getValue() instanceof JSONObject) {
|
JSONObject objectValue = (JSONObject) entry.getValue();
|
if (objectValue.containsKey(time)){
|
cacheValue = objectValue.get(time).toString();
|
}
|
}
|
if (entry.getKey().equals(tagNo)) {
|
value = new BigDecimal(cacheValue);
|
}
|
log.info("存入IailabData缓存: " + catchKey);
|
redisTemplate.opsForValue().set("IailabData:" + httpApi.getCode() + ":" + entry.getKey(), cacheValue, offset, TimeUnit.SECONDS);
|
}
|
|
return value;
|
}
|
|
public Map<String, Object> getTagValues(String sourceId, List<String> tagNames) {
|
Map<String, Object> result = new HashMap<>();
|
try {
|
if (CollectionUtils.isEmpty(tagNames)) {
|
return result;
|
}
|
for (String tagName : tagNames) {
|
result.put(tagName, getTagValue(sourceId, tagName));
|
}
|
} catch (Exception ex) {
|
log.info("getCurrentValue异常");
|
ex.printStackTrace();
|
throw ex;
|
}
|
return result;
|
}
|
|
public Map<String, Object> getTagValues(List<Object[]> params, Date collectTime) {
|
Map<String, Object> result = new HashMap<>();
|
if (CollectionUtils.isEmpty(params)) {
|
return new HashMap<>();
|
}
|
try {
|
HttpApiEntity httpApi = this.getHttpApi(params.get(0)[0].toString());
|
this.getByHtp(httpApi.getUrl(), httpApi.getCode(), result, params);
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
return result;
|
}
|
|
private void getByHtp(String url, String sourceName, Map<String, Object> result, List<Object[]> params) {
|
log.info("HttpCollectorForAsdy.getByHtp:url=" + url);
|
log.info("HttpCollectorForAsdy.getByHtp:params=" + JSONArray.toJSONString(params));
|
String responseStr = HttpUtils.sendGet(url, null, "");
|
log.info("HttpCollectorForAsdy.getByHtp:responseStr=" + responseStr);
|
|
JSONObject jsonObject = JSON.parseObject(responseStr);
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.MILLISECOND,0);
|
calendar.set(Calendar.SECOND,0);
|
calendar.set(Calendar.MINUTE,0);
|
calendar.set(Calendar.HOUR_OF_DAY,0);
|
calendar.add(Calendar.DAY_OF_YEAR,-1);
|
String time = DateUtils.format(calendar.getTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
|
|
Map<String, BigDecimal> valueGroup = new HashMap<>();
|
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
String value = "0";
|
if (entry.getValue() instanceof String) {
|
value = entry.getValue().toString();
|
}else if (entry.getValue() instanceof JSONObject) {
|
JSONObject objectValue = (JSONObject) entry.getValue();
|
if (objectValue.containsKey(time)){
|
value = objectValue.get(time).toString();
|
}
|
}
|
valueGroup.put(entry.getKey(),new BigDecimal(value));
|
}
|
|
for (Object[] item : params) {
|
if (valueGroup.containsKey(item[1].toString())) {
|
result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, item[1].toString()), valueGroup.get(item[1].toString()));
|
}
|
}
|
}
|
}
|