Commit 1dff0a2b authored by yangyang's avatar yangyang

对接 碳银电站接口

parent c671dc3b
...@@ -162,19 +162,23 @@ public class TanYinApiUtils { ...@@ -162,19 +162,23 @@ public class TanYinApiUtils {
// 记录请求日志。 // 记录请求日志。
log.debug("请求 => 碳银{}接口,url:{},参数:{},headers:{}", desc, url, paramsJsonStr, JSON.toJSONString(headers)); log.debug("请求 => 碳银{}接口,url:{},参数:{},headers:{}", desc, url, paramsJsonStr, JSON.toJSONString(headers));
HttpResponse response; HttpResponse response = null;
try { try {
// 发送POST请求,带上参数和headers,并执行。 // 发送POST请求,带上参数和headers,并执行。
response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute(); response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute();
// 解析响应并返回。 // 解析响应并返回。
return parseResponse(desc, response, resultClass); return parseResponse(desc, response, resultClass);
} catch (BusinessException businessException) { } catch (BusinessException businessException) {
if (businessException.isTokenError()) {
// 在业务异常情况下,尝试刷新令牌并重试请求。 // 在业务异常情况下,尝试刷新令牌并重试请求。
// 刷新 Access_token // 刷新 Access_token
headers.put("Authorization", "Bearer " + refreshAccessToken(clientKey, clientSecret)); headers.put("Authorization", "Bearer " + refreshAccessToken(clientKey, clientSecret));
response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute(); response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute();
// 解析刷新令牌后的响应并返回。 // 解析刷新令牌后的响应并返回。
return parseResponse(desc, response, resultClass); return parseResponse(desc, response, resultClass);
}
log.warn(String.format("异常 => 碳银%s接口,参数: %s,响应: %s", desc, paramsJsonStr, response != null ? response.body() : businessException.getMessage()));
return null;
} catch (Exception e) { } catch (Exception e) {
// 记录未预期的异常日志。 // 记录未预期的异常日志。
log.warn(String.format("异常 => 碳银%s接口,参数: %s", desc, paramsJsonStr), e); log.warn(String.format("异常 => 碳银%s接口,参数: %s", desc, paramsJsonStr), e);
...@@ -208,24 +212,25 @@ public class TanYinApiUtils { ...@@ -208,24 +212,25 @@ public class TanYinApiUtils {
// 记录请求日志 // 记录请求日志
log.info("请求 => 碳银{}接口,url:{},参数:{},headers:{}", desc, url, paramsJsonStr, JSON.toJSONString(headers)); log.info("请求 => 碳银{}接口,url:{},参数:{},headers:{}", desc, url, paramsJsonStr, JSON.toJSONString(headers));
HttpResponse response; HttpResponse response = null;
try { try {
// 发送POST请求,设置请求体和请求头 // 发送POST请求,设置请求体和请求头
response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute(); response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute();
// 记录响应日志 // 记录响应日志
log.info("响应 => 碳银{}接口,参数:{},httpCode:{}, response:{}", desc, paramsJsonStr, response.getStatus(), response.body()); log.info("响应 => 碳银{}接口,参数:{},httpCode:{}, response:{}", desc, paramsJsonStr, response.getStatus(), response.body());
// 解析响应,返回分页信息和请求结果 // 解析响应,返回分页信息和请求结果
return parsePageResponse(desc, response, resultClass); return parsePageResponse(desc, response, resultClass);
} catch (BusinessException businessException) { } catch (BusinessException businessException) {
if (businessException.isTokenError()) {
// 在业务异常情况下,尝试刷新令牌并重试请求。 // 在业务异常情况下,尝试刷新令牌并重试请求。
// 刷新 Access_token // 刷新 Access_token
headers.put("Authorization", "Bearer " + refreshAccessToken(clientKey, clientSecret)); headers.put("Authorization", "Bearer " + refreshAccessToken(clientKey, clientSecret));
// 重新发送请求并解析响应 // 重新发送请求并解析响应
response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute(); response = HttpUtil.createPost(url).body(paramsJsonStr, MediaType.APPLICATION_JSON_UTF8_VALUE).headerMap(headers, true).execute();
return parsePageResponse(desc, response, resultClass); return parsePageResponse(desc, response, resultClass);
}
log.warn(String.format("异常 => 碳银%s接口,参数: %s,响应: %s", desc, paramsJsonStr, response != null ? response.body() : businessException.getMessage()));
return null;
} catch (Exception e) { } catch (Exception e) {
// 记录异常日志 // 记录异常日志
log.warn(String.format("异常 => 碳银%s接口,参数: %s", desc, paramsJsonStr), e); log.warn(String.format("异常 => 碳银%s接口,参数: %s", desc, paramsJsonStr), e);
...@@ -261,12 +266,8 @@ public class TanYinApiUtils { ...@@ -261,12 +266,8 @@ public class TanYinApiUtils {
// 检查响应是否成功并存在有效数据,如果不成功且错误码为401,则抛出业务异常;其他情况返回null。 // 检查响应是否成功并存在有效数据,如果不成功且错误码为401,则抛出业务异常;其他情况返回null。
if (!baseResult.getSuccess() || Objects.isNull(baseResult.getData())) { if (!baseResult.getSuccess() || Objects.isNull(baseResult.getData())) {
if ("401".equals(baseResult.getCode())) {
// Token 过期
throw new BusinessException(baseResult.getCode().toString(), baseResult.getMsg()); throw new BusinessException(baseResult.getCode().toString(), baseResult.getMsg());
} }
return null;
}
// 解析响应数据部分,根据其类型进行相应的处理和转换。 // 解析响应数据部分,根据其类型进行相应的处理和转换。
JSON result = (JSON) baseResult.getData(); JSON result = (JSON) baseResult.getData();
......
package com.yeejoin.amos.api.householdapi.constant; package com.yeejoin.amos.api.householdapi.constant;
import java.util.HashMap;
/** /**
* 碳银常量类 * 碳银常量类
* <p> * <p>
...@@ -35,4 +37,18 @@ public class TanYinConstant { ...@@ -35,4 +37,18 @@ public class TanYinConstant {
*/ */
public static String refreshAccessTokenUrl = "/v1/auth/refreshAccessToken"; public static String refreshAccessTokenUrl = "/v1/auth/refreshAccessToken";
public static final HashMap<String, String> stationStatus = new HashMap<String, String>() {
{
put("0", "在线");
put("1", "离线");
put("2", "异常");
}
};
public static final HashMap<String, String> intoNetWorkStatus = new HashMap<String, String>() {
{
put("0", "普通并网");
}
};
} }
...@@ -12,18 +12,27 @@ import org.typroject.tyboot.core.foundation.exception.BaseException; ...@@ -12,18 +12,27 @@ import org.typroject.tyboot.core.foundation.exception.BaseException;
*/ */
public class BusinessException extends BaseException { public class BusinessException extends BaseException {
private String code = "";
public BusinessException(String message) { public BusinessException(String message) {
super(message, BusinessException.class.getSimpleName(), "错误的请求."); super(message, BusinessException.class.getSimpleName(), "错误的请求.");
this.code = "-1";
this.httpStatus = 500; this.httpStatus = 500;
} }
public BusinessException(String message, String errorCode) { public BusinessException(String errorCode, String message) {
super(message, BusinessException.class.getSimpleName(), message); super(message, BusinessException.class.getSimpleName(), message);
if (StringUtils.isBlank(errorCode)) { if (StringUtils.isBlank(errorCode)) {
this.httpStatus = 500; this.httpStatus = 500;
this.code = errorCode;
} else { } else {
this.httpStatus = Integer.parseInt(errorCode); this.httpStatus = Integer.parseInt(errorCode);
this.code = errorCode;
}
} }
public boolean isTokenError() {
return "401".equals(this.code);
} }
} }
...@@ -37,6 +37,10 @@ public class TanYinInveterInfo { ...@@ -37,6 +37,10 @@ public class TanYinInveterInfo {
@TableField ("gmt_create") @TableField ("gmt_create")
private String gmtCreate; private String gmtCreate;
@ApiModelProperty ("ID")
@TableField ("idString")
private String idString;
@ApiModelProperty ("功率") @ApiModelProperty ("功率")
@TableField ("watt") @TableField ("watt")
private String watt; private String watt;
...@@ -81,10 +85,50 @@ public class TanYinInveterInfo { ...@@ -81,10 +85,50 @@ public class TanYinInveterInfo {
@TableField ("vol4") @TableField ("vol4")
private String vol4; private String vol4;
@ApiModelProperty ("输出电压 1")
@TableField ("vol5")
private String vol5;
@ApiModelProperty ("输出电压 2")
@TableField ("vol6")
private String vol6;
@ApiModelProperty ("输出电压 3")
@TableField ("vol7")
private String vol7;
@ApiModelProperty ("输出电压 3")
@TableField ("vol8")
private String vol8;
@ApiModelProperty ("输出电压 3")
@TableField ("vol9")
private String vol9;
@ApiModelProperty ("输入电压 6") @ApiModelProperty ("输入电压 6")
@TableField ("vol10") @TableField ("vol10")
private String vol10; private String vol10;
@ApiModelProperty ("输出电压 3")
@TableField ("vol11")
private String vol11;
@ApiModelProperty ("输出电压 3")
@TableField ("vol12")
private String vol12;
@ApiModelProperty ("输出电压 3")
@TableField ("vol13")
private String vol13;
@ApiModelProperty ("输出电压 3")
@TableField ("vol14")
private String vol14;
@ApiModelProperty ("输出电压 3")
@TableField ("vol15")
private String vol15;
@ApiModelProperty ("输入电流 1") @ApiModelProperty ("输入电流 1")
@TableField ("amp0") @TableField ("amp0")
private String amp0; private String amp0;
...@@ -106,33 +150,49 @@ public class TanYinInveterInfo { ...@@ -106,33 +150,49 @@ public class TanYinInveterInfo {
private String amp4; private String amp4;
@ApiModelProperty ("输入电流 6") @ApiModelProperty ("输入电流 6")
@TableField ("amp10")
private String amp10;
@ApiModelProperty ("输出电压 1")
@TableField ("vol5")
private String vol5;
@ApiModelProperty ("输出电压 2")
@TableField ("vol6")
private String vol6;
@ApiModelProperty ("输出电压 3")
@TableField ("vol7")
private String vol7;
@ApiModelProperty ("输出电流 1")
@TableField ("amp5") @TableField ("amp5")
private String amp5; private String amp5;
@ApiModelProperty ("输出电流 2") @ApiModelProperty ("输入电流 7")
@TableField ("amp6") @TableField ("amp6")
private String amp6; private String amp6;
@ApiModelProperty ("输出电流 3") @ApiModelProperty ("输入电流 8")
@TableField ("amp7") @TableField ("amp7")
private String amp7; private String amp7;
@ApiModelProperty ("输入电流 9")
@TableField ("amp8")
private String amp8;
@ApiModelProperty ("输入电流 10")
@TableField ("amp9")
private String amp9;
@ApiModelProperty ("输入电流 11")
@TableField ("amp10")
private String amp10;
@ApiModelProperty ("输入电流 12")
@TableField ("amp11")
private String amp11;
@ApiModelProperty ("输入电流 13")
@TableField ("amp12")
private String amp12;
@ApiModelProperty ("输入电流 14")
@TableField ("amp13")
private String amp13;
@ApiModelProperty ("输入电流 15")
@TableField ("amp14")
private String amp14;
@ApiModelProperty ("输入电流 16")
@TableField ("amp15")
private String amp15;
@ApiModelProperty ("温度(单位:摄氏度)") @ApiModelProperty ("温度(单位:摄氏度)")
@TableField ("temperature") @TableField ("temperature")
private String temperature; private String temperature;
......
...@@ -73,3 +73,9 @@ dataRequstScheduled.Sunlight=0 0/50 * * * * ...@@ -73,3 +73,9 @@ dataRequstScheduled.Sunlight=0 0/50 * * * *
dataRequstScheduled.GoodWe=0 0/3 * * * * dataRequstScheduled.GoodWe=0 0/3 * * * *
dataRequstScheduled.Sofar=0 0/50 * * * * dataRequstScheduled.Sofar=0 0/50 * * * *
# 碳银
tanYin.api.apiUrl=https://userauth.tanwin.cn
tanYin.api.clientSecret=rKrWVa2sXsSZeNAOW43v
tanYin.api.clientKey=yx10001
dataRequestScheduled.tanYin=0 0/10 * * * *
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment