dengzedong
4 天以前 a81b4970d13ccbf38e1f4967a56675b8d9ed152b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 {
    private static Map<String, HttpApiEntity> 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, "");
 
        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()));
            }
        }
    }
}