From 8b92da6cfa951ece4c9e5403445036b9f2d8a1e5 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期一, 13 四月 2026 14:16:07 +0800
Subject: [PATCH] feat(jcly): 添加新的设备GUID常量并扩展实时数据获取功能
---
src/main/java/com/ruoyi/http/service/impl/RealTimeEnergyConsumptionServiceImpl.java | 252 ++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 202 insertions(+), 50 deletions(-)
diff --git a/src/main/java/com/ruoyi/http/service/impl/RealTimeEnergyConsumptionServiceImpl.java b/src/main/java/com/ruoyi/http/service/impl/RealTimeEnergyConsumptionServiceImpl.java
index 6b10ba2..be25fb3 100644
--- a/src/main/java/com/ruoyi/http/service/impl/RealTimeEnergyConsumptionServiceImpl.java
+++ b/src/main/java/com/ruoyi/http/service/impl/RealTimeEnergyConsumptionServiceImpl.java
@@ -6,12 +6,17 @@
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.Collections;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
/**
* @author :yys
@@ -21,11 +26,17 @@
@Slf4j
public class RealTimeEnergyConsumptionServiceImpl implements RealTimeEnergyConsumptionService {
+ private static final long REMOTE_CACHE_TTL_SECONDS = 10L; // 缂撳瓨TTL锛�10绉�
+
+ private static final long REMOTE_CACHE_TTL_SECONDS_30 = 30L; // 缂撳瓨TTL锛�30绉�
+
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 REAL_TIME_URL = "/elitechAccess/v2/getRealTimeData";
+
+ private static final String REAL_HISTORY_URL = URL + "/elitechAccess/v2/getHistoryData";
private static final String KET_ID = "75804708";
@@ -35,96 +46,237 @@
private static final String PASS_WORD = "y17775163675";
- private static final String DEVICE_GUID = "90444196515214284663";
+ private static final String REAL_TIME_CACHE_PREFIX = "JCLY:REAL_TIME:";
+ private static final String HISTORY_CACHE_PREFIX = "JCLY:HISTORY:";
- /**
- * 鏍规嵁paramCode鎻愬彇鎺㈠ご鍙傛暟
- * @param paramList 璁惧鍙傛暟鏁扮粍
- * @param targetCode 鐩爣鎺㈠ご缂栫爜
- * @return 鎺㈠ご鍙傛暟瀵硅薄锛堝寘鍚玭ame/value/unit锛�
- */
+ @Autowired
+ private RedisTemplate<String, String> redisTemplate;
+
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(); // 鏈尮閰嶅埌杩斿洖绌哄璞★紝閬垮厤绌烘寚閽�
+ return new JSONObject();
}
- /**
- * 瀹炴椂鑾峰彇娓╂箍搴︼紝浜屾哀鍖栫⒊鏁版嵁
- */
- public static void main(String[] args) {
- String realTimeData = getRealTimeData(getToken());
- Map<String, Object> map = JSON.parseObject(realTimeData, Map.class);
- if(map.get("code").equals(0)){
- // 1. 瑙f瀽澶栧眰data涓篔SON鏁扮粍锛堟帴鍙h繑鍥炵殑璁惧鍒楄〃锛�
- JSONArray deviceList = JSON.parseArray(map.get("data").toString());
- // 2. 閬嶅巻璁惧鍒楄〃锛堟澶勪粎鍙栫涓�涓澶囷紝鑻ユ湁澶氫釜璁惧鍙惊鐜鐞嗭級
- if (!deviceList.isEmpty()) {
- JSONObject deviceObj = deviceList.getJSONObject(0);
- // 3. 瑙f瀽璁惧鍐呯殑鍙傛暟鏁扮粍锛堟墍鏈塸aramCode瀵瑰簲鐨勫弬鏁帮級
- JSONArray paramList = deviceObj.getJSONArray("data");
+ public List<Map<String, String>> getHistoryData(String guid, long startTime, long endTime) {
+ List<Map<String, String>> resultList = new ArrayList<>();
+ String historyData = requestHistoryData(getToken(), guid, startTime, endTime);
+ Map<String, Object> resultMap = JSON.parseObject(historyData, Map.class);
+ if (!Integer.valueOf(0).equals(resultMap.get("code"))) {
+ return resultList;
+ }
- // 4. 瀹氫箟鐩爣鎺㈠ご鐨刾aramCode锛屾寜闇�鎵╁睍
- String[] targetCodes = {"0100", "0110", "0120", "0130"};
- for (String code : targetCodes) {
- // 5. 閬嶅巻鍙傛暟鏁扮粍锛屽尮閰嶇洰鏍噋aramCode
- for (int i = 0; i < paramList.size(); i++) {
- JSONObject paramObj = paramList.getJSONObject(i);
- String currentCode = paramObj.getString("paramCode");
- if (code.equals(currentCode)) {
- // 6. 鎻愬彇鏍稿績瀛楁锛堝�笺�佸崟浣嶃�佹帰澶村悕绉帮級
- String paramName = paramObj.getString("paramName"); // 鎺㈠ご1/鎺㈠ご2...
- String value = paramObj.getString("value"); // 鏁板�硷紙345.80/24.90...锛�
- String unitCode = paramObj.getString("unitCode"); // 鍗曚綅锛圠ux/鈩�/%RH/ppm锛�
+ JSONArray historyList = JSON.parseArray(String.valueOf(resultMap.get("data")));
+ if (historyList == null || historyList.isEmpty()) {
+ return resultList;
+ }
- // 7. 涓氬姟澶勭悊锛氭墦鍗�/璧嬪��/瀛樺偍绛夛紙鎸夐渶淇敼锛�
- System.out.println(paramName + "锛�" + value + " " + unitCode);
- // 鍖归厤鍒板悗鐩存帴璺冲嚭鍐呭眰寰幆锛屾彁鍗囨晥鐜�
+ for (int i = 0; i < historyList.size(); i++) {
+ JSONObject historyObj = historyList.getJSONObject(i);
+ Map<String, String> historyItem = new HashMap<>();
+ historyItem.put("guid", firstNonBlank(
+ historyObj.getString("deviceGuid"),
+ historyObj.getString("guid"),
+ guid
+ ));
+ historyItem.put("subUId", stringValue(historyObj.get("subUid")));
+ historyItem.put("monitorTimeStamp", stringValue(historyObj.get("monitorTimeStamp")));
+ historyItem.put("monitorTimeStr", historyObj.getString("monitorTimeStr"));
+ historyItem.put("position", historyObj.getString("position"));
+ historyItem.put("address", historyObj.getString("address"));
+
+ JSONArray paramList = historyObj.getJSONArray("data");
+ if (paramList != null && !paramList.isEmpty()) {
+ for (int j = 0; j < paramList.size(); j++) {
+ JSONObject paramObj = paramList.getJSONObject(j);
+ String paramName = paramObj.getString("paramName");
+ String paramCode = paramObj.getString("paramCode");
+ String value = paramObj.getString("value");
+ String unitCode = paramObj.getString("unitCode");
+ String fullValue = value == null ? null : value + (unitCode == null ? "" : unitCode);
+ switch (paramName) {
+ case "鎺㈠ご1":
+ historyItem.put("light", fullValue);
break;
- }
+ case "鎺㈠ご2":
+ historyItem.put("temperature", fullValue);
+ break;
+ case "鎺㈠ご3":
+ historyItem.put("humidity", fullValue);
+ break;
+ case "鎺㈠ご4":
+ historyItem.put("co2", fullValue);
+ break;
+ default:
+ if (paramCode != null) {
+ historyItem.put(paramCode, fullValue);
+ }
+ break;
}
}
}
+ resultList.add(historyItem);
}
+ return resultList;
+ }
+
+ 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<>();
+ String deviceGuid = firstNonBlank(
+ deviceObj.getString("deviceGuid"),
+ deviceObj.getString("guid"),
+ deviceObj.getString("devGuid"),
+ deviceObj.getString("sn")
+ );
+ if (deviceGuid != null) {
+ deviceData.put("guid", deviceGuid);
+ }
+ 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 static String getToken(){
+ 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));
+ 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 (map.get("code").equals(0)) {
+ 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;
}
- public static String getRealTimeData(String token){
+ private String sanitizeToken(String token) {
+ if (token == null) {
+ return null;
+ }
+ String cleanedToken = token.replace("\0", "").trim();
+ return cleanedToken.isEmpty() ? null : cleanedToken;
+ }
+
+ private String firstNonBlank(String... values) {
+ for (String value : values) {
+ if (value != null && !value.trim().isEmpty()) {
+ return value;
+ }
+ }
+ return null;
+ }
+
+ private String stringValue(Object value) {
+ return value == null ? null : String.valueOf(value);
+ }
+
+ public 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", Collections.singletonList(DEVICE_GUID));
- log.info("璇锋眰鍙傛暟锛歿}", JSON.toJSONString( param));
- String result = HttpUtils.sendPostJson(URL + REAL_TIME_URL, JSON.toJSONString(param),token);
+ param.put("deviceGuids", guidList);
+ log.info("璇锋眰鍙傛暟锛歿}", JSON.toJSONString(param));
+ String cacheKey = REAL_TIME_CACHE_PREFIX + JSON.toJSONString(param);
+ String cachedResult = sanitizeToken(redisTemplate.opsForValue().get(cacheKey));
+ if (cachedResult != null) {
+ log.info("鍛戒腑瀹炴椂鏁版嵁缂撳瓨锛歿}", cacheKey);
+ return cachedResult;
+ }
+ String result = HttpUtils.sendPostJson(URL + REAL_TIME_URL, JSON.toJSONString(param), token);
log.info("杩斿洖缁撴灉锛歿}", result);
+ cacheRemoteResponse(cacheKey, result);
return result;
}
+ public String requestHistoryData(String token, String guid, long startTime, long endTime) {
+ Map<String, Object> param = new HashMap<>();
+ param.put("keyId", KET_ID);
+ param.put("keySecret", KEY_SECRET);
+ param.put("deviceGuid", guid);
+ param.put("startTime", startTime);
+ param.put("endTime", endTime);
+ log.info("鍘嗗彶鏁版嵁璇锋眰鍙傛暟锛歿}", JSON.toJSONString(param));
+ String cacheKey = HISTORY_CACHE_PREFIX + JSON.toJSONString(param);
+ String cachedResult = sanitizeToken(redisTemplate.opsForValue().get(cacheKey));
+ if (cachedResult != null) {
+ log.info("鍛戒腑鍘嗗彶鏁版嵁缂撳瓨锛歿}", cacheKey);
+ return cachedResult;
+ }
+ String result = HttpUtils.sendPostJson(REAL_HISTORY_URL, JSON.toJSONString(param), token);
+ log.info("鍘嗗彶鏁版嵁杩斿洖缁撴灉锛歿}", result);
+ cacheRemoteResponse(cacheKey, result);
+ return result;
+ }
-
-
-
+ private void cacheRemoteResponse(String cacheKey, String result) {
+ if (result == null || result.trim().isEmpty()) {
+ return;
+ }
+ redisTemplate.opsForValue().set(cacheKey, result, REMOTE_CACHE_TTL_SECONDS_30, TimeUnit.SECONDS);
+ }
}
--
Gitblit v1.9.3