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.stereotype.Service;
|
|
import java.util.Collections;
|
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";
|
|
private static final String DEVICE_GUID = "90444196515214284663";
|
|
|
/**
|
* 根据paramCode提取探头参数
|
* @param paramList 设备参数数组
|
* @param targetCode 目标探头编码
|
* @return 探头参数对象(包含name/value/unit)
|
*/
|
private static JSONObject getProbeParam(JSONArray paramList, String targetCode) {
|
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 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. 解析外层data为JSON数组(接口返回的设备列表)
|
JSONArray deviceList = JSON.parseArray(map.get("data").toString());
|
// 2. 遍历设备列表(此处仅取第一个设备,若有多个设备可循环处理)
|
if (!deviceList.isEmpty()) {
|
JSONObject deviceObj = deviceList.getJSONObject(0);
|
// 3. 解析设备内的参数数组(所有paramCode对应的参数)
|
JSONArray paramList = deviceObj.getJSONArray("data");
|
|
// 4. 定义目标探头的paramCode,按需扩展
|
String[] targetCodes = {"0100", "0110", "0120", "0130"};
|
for (String code : targetCodes) {
|
// 5. 遍历参数数组,匹配目标paramCode
|
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"); // 单位(Lux/℃/%RH/ppm)
|
|
// 7. 业务处理:打印/赋值/存储等(按需修改)
|
System.out.println(paramName + ":" + value + " " + unitCode);
|
// 匹配到后直接跳出内层循环,提升效率
|
break;
|
}
|
}
|
}
|
}
|
}
|
System.out.println();
|
}
|
|
public static String getToken(){
|
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 (map.get("code").equals(0)) {
|
Object token = map.get("data");
|
log.info("token:{}", token);
|
return token.toString();
|
}
|
return result;
|
}
|
|
public static String getRealTimeData(String token){
|
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);
|
log.info("返回结果:{}", result);
|
return result;
|
}
|
|
|
|
|
|
}
|