2024-06-29 16:49:17 +08:00

84 lines
3.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RenZhiUtil {
public static final String host = "http://www.0531yun.com";
/**
* 根据设备地址查询实时数据
*
* @param devSns
* @param jnrzckAccount
* @param jnrzckPw
* @return
*/
public static JSONArray getRealTimeDataByDeviceAddr(String devSns, String jnrzckAccount, String jnrzckPw) {
String token = getToken(jnrzckAccount, jnrzckPw);
String url = StrUtil.format("{}/api/data/getRealTimeDataByDeviceAddr?deviceAddrs={}", host, devSns);
log.info("根据设备地址查询实时数据url:{},authorization:{}", url, token);
String rsp = HttpRequest.get(url)
.header("authorization", token)
.timeout(5000)//超时,毫秒
.execute().body();
JSONObject rspJo = JSONObject.parseObject(rsp);
log.info("根据设备地址查询实时数据rs:{}", rspJo.toJSONString());
try {
return rspJo.getJSONArray("data");
} catch (Exception e) {
throw new RuntimeException("根据设备地址查询实时数据失败:", e);
}
}
/**
* 获取报警数据列表
*
* @param devSn
* @param nodeId
* @return
*/
public static JSONArray getAlarmRecordList(String devSn, String token, Integer nodeId, String startTime, String endTime) {
String url = StrUtil.format("{}/api/data/alarmRecordList?deviceAddr={}&nodeId={}&startTime={}&endTime={}", host, devSn, nodeId, startTime, endTime);
log.info("获取报警数据列表url:{},authorization:{}", url, token);
String rsp = HttpRequest.get(url)
.header("authorization", token)
.timeout(5000)//超时,毫秒
.execute().body();
JSONObject rspJo = JSONObject.parseObject(rsp);
log.info("获取报警数据列表rs:{}", rspJo.toJSONString());
try {
return rspJo.getJSONArray("data");
} catch (Exception e) {
throw new RuntimeException("获取报警数据列表失败:", e);
}
}
/**
* 根据用户名和密码获取 token
*
* @param jnrzckAccount
* @param jnrzckPw
* @return
*/
public static String getToken(String jnrzckAccount, String jnrzckPw) {
String url = StrUtil.format("{}/api/getToken?loginName={}&password={}", host, jnrzckAccount, jnrzckPw);
log.info("url:{}", url);
String rsp = HttpUtil.get(url);
JSONObject rspJo = JSONObject.parseObject(rsp);
log.info("rs:{}", rspJo.toJSONString());
try {
return rspJo.getJSONObject("data").getString("token");
} catch (Exception e) {
throw new RuntimeException("获取token失败", e);
}
}
}