Commit 1c1b5932 by 谢海旭

添加 Ocpp16Helper.strToBcd 方法

1 parent 06bcbf71
...@@ -22,6 +22,7 @@ import cn.com.glsx.charging.util.PluralCache; ...@@ -22,6 +22,7 @@ import cn.com.glsx.charging.util.PluralCache;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
@Slf4j @Slf4j
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
...@@ -176,6 +177,35 @@ public class Ocpp16Helper { ...@@ -176,6 +177,35 @@ public class Ocpp16Helper {
} }
/** /**
* 字符串转单个bcd字节
*
* @param str
* @return
*/
public static byte strToBcd(String str) {
if (!StringUtils.hasLength(str)) {
throw new IllegalArgumentException("str cannot be empty");
}
int len = str.length();
if (len > 2) {
throw new IllegalArgumentException("Invalid str");
}
if (len == 1) {
str = "0" + str; // 补齐偶数位
}
char highChar = str.charAt(0);
char lowChar = str.charAt(1);
int high = Character.digit(highChar, 10);
int low = Character.digit(lowChar, 10);
return (byte) ((high << 4) | low);
}
/**
* 字符串 转ascii字节数组 * 字符串 转ascii字节数组
* 不足指定长度,默认后面补零 * 不足指定长度,默认后面补零
* *
......
...@@ -31,17 +31,22 @@ public class ClientSimulateTest { ...@@ -31,17 +31,22 @@ public class ClientSimulateTest {
req.getData().setPileId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPileId())); req.getData().setPileId(Ocpp16Helper.strToBcd("55031412782305", req.getData().getPileId()));
req.getData().setPointType((byte) 0); req.getData().setPointType((byte) 0);
req.getData().setGunCount((byte) 2); req.getData().setGunCount((byte) 2);
req.getData().setSoftVersion(Ocpp16Helper.strToBcd("v4.1.50", req.getData().getSoftVersion()));
req.getData().setProtocolVersion((byte) 10); req.getData().setProtocolVersion((byte) 10);
req.getData().setSoftVersion(Ocpp16Helper.strToAscii("v4.1.50", req.getData().getSoftVersion()));
req.getData().setNetwork((byte) 0x03);
req.getData().setSim(Ocpp16Helper.strToBcd("01010101010101010101", req.getData().getSim()));
req.getData().setOperator((byte) 0x04);
client.sendSync(req); client.sendSync(req);
// 定期心跳 // 定期心跳
while (true) { while (true) {
HeartbeatReq heartbeat = new HeartbeatReq();
HeartbeatReqData hdata = new HeartbeatReqData(); HeartbeatReqData hdata = new HeartbeatReqData();
hdata.setPileId(req.getData().getPileId()); hdata.setPileId(req.getData().getPileId());
hdata.setGunNo(Ocpp16Helper.strToBcd("2"));
hdata.setStatus((byte) 0); hdata.setStatus((byte) 0);
HeartbeatReq heartbeat = new HeartbeatReq();
heartbeat.setData(hdata); heartbeat.setData(hdata);
client.sendSync(heartbeat); client.sendSync(heartbeat);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!