package com.iailab.module.data.channel.http.collector.asdb; import com.alibaba.fastjson.JSON; import com.iailab.framework.common.constant.CommonConstant; import com.iailab.framework.common.util.http.HttpUtils; import com.iailab.module.data.channel.http.collector.asdb.vo.HttpAsdbRespDataVO; 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.*; /** * AnSteelDB采集 * * @author Jay */ @Slf4j @Component public class HttpCollectorForAsdb { private static Map apiMap = new HashMap<>(); @Autowired private HttpApiService httpApiService; @Autowired private RedisTemplate redisTemplate; public static final long offset = 10; private HttpApiEntity getHttpApi(String id) { if (apiMap.containsKey(id)) { return apiMap.get(id); } HttpApiEntity httpApi = httpApiService.info(id); apiMap.put(id, httpApi); return httpApi; } 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, ""); List dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class); log.info("存入IailabData缓存: " + catchKey); dataList.forEach(item -> { redisTemplate.opsForValue().set(catchKey, item.getValue().toString(), offset, TimeUnit.SECONDS); }); for (HttpAsdbRespDataVO data : dataList){ if (tagNo.equals(data.getPoint())){ value = data.getValue(); break; } } return value; } public Map getTagValues(String sourceId, List tagNames) { Map 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 getTagValues(List params, Date collectTime) { Map 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 result, List params) { String responseStr = HttpUtils.sendGet(url, null, ""); List dataList = JSON.parseArray(responseStr, HttpAsdbRespDataVO.class); Map valueGroup = new HashMap<>(); for (HttpAsdbRespDataVO data : dataList) { valueGroup.put(data.getPoint(), data); } for (Object[] item : params) { if (valueGroup.containsKey(item[1].toString())) { HttpAsdbRespDataVO data = valueGroup.get(item[1].toString()); result.put(TagUtils.genTagId(DataSourceType.HTTP.getCode(), sourceName, data.getPoint()), data.getValue()); } } } }