5 天以前 d17d4d1ac98865d2a71b4f24771b7b76e0839ed3
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
157
package com.ruoyi.http.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.http.service.RealTimeEnergyConsumptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author :yys
 * @date : 2026/1/27 16:02
 */
@Service
@Slf4j
public class RealTimeEnergyConsumptionServiceImpl implements RealTimeEnergyConsumptionService {
 
    private static final String URL = "https://new.e-elitech.cn/api/data-api";
 
    private static final String TOKEN_URL = "/elitechAccess/getToken";
 
    private static final String REAL_TIME_URL = "/elitechAccess/v2/getRealTimeData";
 
    private static final String KET_ID = "75804708";
 
    private static final String KEY_SECRET = "xTUGToozKpYgUPqTsZzB";
 
    private static final String USER_NAME = "用户30773662";
 
    private static final String PASS_WORD = "y17775163675";
 
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
 
    /**
     * 根据 paramCode 提取探头参数。
     */
    private static JSONObject getProbeParam(JSONArray paramList, String targetCode) {
        if (paramList == null) {
            return new JSONObject();
        }
        for (int i = 0; i < paramList.size(); i++) {
            JSONObject paramObj = paramList.getJSONObject(i);
            if (targetCode.equals(paramObj.getString("paramCode"))) {
                return paramObj;
            }
        }
        return new JSONObject();
    }
 
    /**
     * 实时获取温湿度、光照、二氧化碳数据。
     */
    public List<Map<String, String>> getRealData(List<String> guidList) {
        log.info("开始获取实时数据");
        List<Map<String, String>> listMaps = new ArrayList<>();
        String realTimeData = getRealTimeData(getToken(), guidList);
        Map<String, Object> map = JSON.parseObject(realTimeData, Map.class);
        if (!Integer.valueOf(0).equals(map.get("code"))) {
            return listMaps;
        }
 
        JSONArray deviceList = JSON.parseArray(String.valueOf(map.get("data")));
        if (deviceList == null || deviceList.isEmpty()) {
            return listMaps;
        }
 
        String[] targetCodes = {"0100", "0110", "0120", "0130"};
        for (int deviceIndex = 0; deviceIndex < deviceList.size(); deviceIndex++) {
            JSONObject deviceObj = deviceList.getJSONObject(deviceIndex);
            JSONArray paramList = deviceObj.getJSONArray("data");
            Map<String, String> deviceData = new HashMap<>();
            for (String code : targetCodes) {
                JSONObject paramObj = getProbeParam(paramList, code);
                if (paramObj.isEmpty()) {
                    continue;
                }
                String paramName = paramObj.getString("paramName");
                String value = paramObj.getString("value");
                String unitCode = paramObj.getString("unitCode");
                log.info("{}: {} {}", paramName, value, unitCode);
                switch (paramName) {
                    case "探头1":
                        deviceData.put("light", value + unitCode);
                        break;
                    case "探头2":
                        deviceData.put("temperature", value + unitCode);
                        break;
                    case "探头3":
                        deviceData.put("humidity", value + unitCode);
                        break;
                    case "探头4":
                        deviceData.put("co2", value + unitCode);
                        break;
                    default:
                        break;
                }
            }
            listMaps.add(deviceData);
        }
        return listMaps;
    }
 
    public static void main(String[] args) {
        System.out.println();
    }
 
    public String getToken() {
        String cachedToken = sanitizeToken(redisTemplate.opsForValue().get("JCLY_TOKEN:"));
        if (cachedToken != null) {
            return cachedToken;
        }
        Map<String, String> param = new HashMap<>();
        param.put("keyId", KET_ID);
        param.put("keySecret", KEY_SECRET);
        param.put("userName", USER_NAME);
        param.put("password", PASS_WORD);
        log.info("请求参数:{}", JSON.toJSONString(param));
        String result = HttpUtils.sendPostJson(URL + TOKEN_URL, JSON.toJSONString(param));
        log.info("返回结果:{}", result);
        Map<String, Object> map = JSON.parseObject(result, Map.class);
        if (Integer.valueOf(0).equals(map.get("code"))) {
            Object token = map.get("data");
            log.info("token:{}", token);
            redisTemplate.opsForValue().set("JCLY_TOKEN:", token.toString(), 60 * 60 * 24);
            return token.toString();
        }
        return result;
    }
 
    private String sanitizeToken(String token) {
        if (token == null) {
            return null;
        }
        String cleanedToken = token.replace("\0", "").trim();
        return cleanedToken.isEmpty() ? null : cleanedToken;
    }
 
    public static String getRealTimeData(String token, List<String> guidList) {
        Map<String, Object> param = new HashMap<>();
        param.put("keyId", KET_ID);
        param.put("keySecret", KEY_SECRET);
        param.put("deviceGuids", guidList);
        log.info("请求参数:{}", JSON.toJSONString(param));
        String result = HttpUtils.sendPostJson(URL + REAL_TIME_URL, JSON.toJSONString(param), token);
        log.info("返回结果:{}", result);
        return result;
    }
}