This commit is contained in:
Lemon
2024-08-02 14:09:18 +08:00
34 changed files with 1218 additions and 349 deletions

View File

@@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@@ -13,6 +14,7 @@ import java.util.List;
* 单数为 桩 -> 平台
* 双数为 平台 -> 桩
*/
@Slf4j
public enum YKCFrameTypeCode {
LOGIN_CODE(0x01, "充电桩登录认证"),
@@ -114,8 +116,8 @@ public enum YKCFrameTypeCode {
this.value = value;
}
private int code;
private String value;
private int code; // 帧类型code
private String value; // 帧类型名称
public int getCode() {
return code;
@@ -129,15 +131,6 @@ public enum YKCFrameTypeCode {
return BytesUtil.intToBytesLittle(code, 1);
}
public static YKCFrameTypeCode fromCode(byte code) {
for (YKCFrameTypeCode item : YKCFrameTypeCode.values()) {
if (item.getCode() == code) {
return item;
}
}
return null;
}
public static String getFrameTypeStr(String frameType) {
for (YKCFrameTypeCode item : YKCFrameTypeCode.values()) {
String str = YKCUtils.frameType2Str(item.getBytes());
@@ -281,6 +274,7 @@ public enum YKCFrameTypeCode {
REMOTE_ACCOUNT_BALANCE_UPDATE(REMOTE_ACCOUNT_BALANCE_UPDATE_CODE.getCode(), REMOTE_ACCOUNT_BALANCE_UPDATE_ANSWER_CODE.getCode()),
;
// 请求帧类型
private int requestFrameType;
@@ -307,34 +301,25 @@ public enum YKCFrameTypeCode {
return BytesUtil.intToBytesLittle(requestFrameType, 1);
}
public byte[] getResponseFrameBytes() {
return BytesUtil.intToBytesLittle(responseFrameType, 1);
}
PileAnswersRelation(int requestFrameType, int responseFrameType) {
this.requestFrameType = requestFrameType;
this.responseFrameType = responseFrameType;
}
// 根据请求帧类型 获取应答帧类型 int类型
public static int getResponseFrameTypeByRequestFrameType(int requestFrameType) {
/**
* 根据响应帧类型 查找对应的 请求帧类型
*/
public static String getRequestFrameType(String responseFrameType) {
for (PileAnswersRelation relation : PileAnswersRelation.values()) {
if (relation.getRequestFrameType() == requestFrameType) {
return relation.getResponseFrameType();
if (StringUtils.equals(responseFrameType, YKCUtils.frameType2Str(relation.getResponseFrameBytes()))) {
return YKCUtils.frameType2Str(relation.getRequestFrameBytes());
}
}
return 0;
}
// 根据请求帧类型 获取应答帧类型 byte[]类型
public static byte[] getResponseFrameTypeBytes(byte[] requestFrameType) {
int frameType = BytesUtil.bytesToInt(requestFrameType);
return BytesUtil.intToBytes(getResponseFrameTypeByRequestFrameType(frameType), 1);
}
// 需要获取应答的帧类型
public static List<String> getRequestFrameTypeList() {
List<String> resultList = Lists.newArrayList();
for (PileAnswersRelation relation : PileAnswersRelation.values()) {
resultList.add(YKCUtils.frameType2Str(relation.getRequestFrameBytes()));
}
return resultList;
return null;
}
}

View File

@@ -171,7 +171,7 @@ public enum ReturnCodeEnum {
CODE_CREATE_RESERVED_ERROR("00400013", "创建预约失败"),
CODE_UPDATE_RESERVED_STATUS_ERROR("00400014", "修改预约充电状态失败"),
CODE_UPDATE_RESERVED_STATUS_ERROR("00400014", "修改预约充电信息失败"),
CODE_QUERY_RESERVED_LIST_ERROR("00400015", "查询预约充电列表失败"),

View File

@@ -0,0 +1,11 @@
package com.jsowell.common.protocol;
import lombok.Data;
@Data
public abstract class Message {
protected Byte messageType;
}

View File

@@ -0,0 +1,26 @@
package com.jsowell.common.protocol;
import com.jsowell.common.protocol.Message;
import com.jsowell.common.protocol.RpcRequest;
import com.jsowell.common.protocol.RpcResponse;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MessageConstant {
public final static Byte rpcRequest = 1;
public final static Byte rpcResponse = 2;
public static Map<Byte, Class<? extends Message>> messageTypeMap = new ConcurrentHashMap<>();
static {
messageTypeMap.put(rpcRequest, RpcRequest.class);
messageTypeMap.put(rpcResponse, RpcResponse.class);
}
public static Class<? extends Message> getMessageClass(Byte messageType){
return messageTypeMap.get(messageType);
}
}

View File

@@ -0,0 +1,22 @@
package com.jsowell.common.protocol;
import com.jsowell.common.protocol.Message;
import lombok.Data;
import lombok.ToString;
import java.util.UUID;
@Data
@ToString
public class RpcRequest extends Message {
private String id;
private String param;
public RpcRequest() {
this.id = UUID.randomUUID().toString();
super.messageType = MessageConstant.rpcRequest;
}
}

View File

@@ -0,0 +1,19 @@
package com.jsowell.common.protocol;
import com.jsowell.common.protocol.Message;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class RpcResponse extends Message {
private String id;
private String result;
public RpcResponse() {
super.messageType = MessageConstant.rpcResponse;
}
}

View File

@@ -0,0 +1,72 @@
package com.jsowell.common.protocol;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class SyncPromise {
// 用于接收结果
// private RpcResponse rpcResponse;
private byte[] rpcResult;
private final CountDownLatch countDownLatch = new CountDownLatch(1);
// 用于判断是否超时
private boolean isTimeout = false;
public boolean isTimeout() {
return isTimeout;
}
public byte[] getRpcResult() {
return rpcResult;
}
public void setRpcResult(byte[] rpcResult) {
this.rpcResult = rpcResult;
}
/**
* 同步等待返回结果
*/
// public RpcResponse get(long timeout, TimeUnit unit) throws InterruptedException {
// // 等待阻塞超时时间内countDownLatch减到0将提前唤醒以此作为是否超时判断
// boolean earlyWakeUp = countDownLatch.await(timeout, unit);
//
// if(earlyWakeUp) {
// // 超时时间内countDownLatch减到0提前唤醒说明已有结果
// return rpcResponse;
// } else {
// // 超时时间内countDownLatch没有减到0自动唤醒说明超时时间内没有等到结果
// isTimeout = true;
// return null;
// }
// }
public byte[] get2(long timeout, TimeUnit unit) throws InterruptedException {
// 等待阻塞超时时间内countDownLatch减到0将提前唤醒以此作为是否超时判断
boolean earlyWakeUp = countDownLatch.await(timeout, unit);
if(earlyWakeUp) {
// 超时时间内countDownLatch减到0提前唤醒说明已有结果
return rpcResult;
} else {
// 超时时间内countDownLatch没有减到0自动唤醒说明超时时间内没有等到结果
isTimeout = true;
return null;
}
}
public void wake() {
countDownLatch.countDown();
}
// public RpcResponse getRpcResponse() {
// return rpcResponse;
// }
// public void setRpcResponse(RpcResponse rpcResponse) {
// this.rpcResponse = rpcResponse;
// }
}

View File

@@ -0,0 +1,17 @@
package com.jsowell.common.util;
import com.jsowell.common.protocol.SyncPromise;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class RpcUtil {
private final static Map<String, SyncPromise> syncPromiseMap = new ConcurrentHashMap<>();
public static Map<String, SyncPromise> getSyncPromiseMap() {
return syncPromiseMap;
}
}