| | |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | 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 |
| | |
| | | @Slf4j |
| | | public class RealTimeEnergyConsumptionServiceImpl implements RealTimeEnergyConsumptionService { |
| | | |
| | | private static final long REMOTE_CACHE_TTL_SECONDS = 10L; |
| | | |
| | | 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_HISTORY_URL = URL + "/elitechAccess/v2/getHistoryData"; |
| | | |
| | | private static final String KET_ID = "75804708"; |
| | | |
| | |
| | | |
| | | private static final String PASS_WORD = "y17775163675"; |
| | | |
| | | private static final String REAL_TIME_CACHE_PREFIX = "JCLY:REAL_TIME:"; |
| | | |
| | | private static final String HISTORY_CACHE_PREFIX = "JCLY:HISTORY:"; |
| | | |
| | | @Autowired |
| | | private RedisTemplate<String, String> redisTemplate; |
| | | |
| | | /** |
| | | * 根据 paramCode 提取探头参数。 |
| | | */ |
| | | private static JSONObject getProbeParam(JSONArray paramList, String targetCode) { |
| | | if (paramList == null) { |
| | | return new JSONObject(); |
| | |
| | | return new JSONObject(); |
| | | } |
| | | |
| | | /** |
| | | * 实时获取温湿度、光照、二氧化碳数据。 |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | JSONArray historyList = JSON.parseArray(String.valueOf(resultMap.get("data"))); |
| | | if (historyList == null || historyList.isEmpty()) { |
| | | return resultList; |
| | | } |
| | | |
| | | 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<>(); |
| | |
| | | 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()) { |
| | |
| | | return cleanedToken.isEmpty() ? null : cleanedToken; |
| | | } |
| | | |
| | | public static String getRealTimeData(String token, List<String> guidList) { |
| | | 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", 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, TimeUnit.SECONDS); |
| | | } |
| | | } |