Commit c88ea1e6 by 叶立庆

[+]增加转换数值和字节码工具类

1 parent b547e099
...@@ -461,6 +461,37 @@ public class Ocpp16Helper { ...@@ -461,6 +461,37 @@ public class Ocpp16Helper {
return decimalValue; return decimalValue;
} }
/**
* 数值类型转换成指定字节码长度的字节数组【协议所需要的字节数组】
* @param decimalValue 需要转换的值
* @param byteCount 字节长度
* @return
*/
public static byte[] bigDecimalToByteArray(BigDecimal decimalValue, int byteCount) {
// 获取 BigDecimal 的整数部分的 BigInteger
BigInteger unscaledValue = decimalValue.unscaledValue();
// 将 BigInteger 转换为字节数组
byte[] byteArray = unscaledValue.toByteArray();
// 如果字节数组长度超过 byteCount,则进行截断或填充
if (byteArray.length > byteCount) {
// 截断字节数组
byte[] truncatedArray = new byte[byteCount];
System.arraycopy(byteArray, byteArray.length - byteCount, truncatedArray, 0, byteCount);
return ByteUtils.reverse(truncatedArray);
} else if (byteArray.length < byteCount) {
// 填充字节数组
byte[] paddedArray = new byte[byteCount];
int paddingLength = byteCount - byteArray.length;
System.arraycopy(byteArray, 0, paddedArray, paddingLength, byteArray.length);
return ByteUtils.reverse(paddedArray);
} else {
// 字节数组长度正好等于 byteCount
return ByteUtils.reverse(byteArray);
}
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -27,6 +27,6 @@ public class StartMachineRespHandler implements TcpHandler<StartMachineResp, Sta ...@@ -27,6 +27,6 @@ public class StartMachineRespHandler implements TcpHandler<StartMachineResp, Sta
data.setGunNumber((byte) 1); data.setGunNumber((byte) 1);
data.setFailureReason((byte) 0); data.setFailureReason((byte) 0);
resp.setData(data); resp.setData(data);
return resp; return null;
} }
} }
...@@ -16,6 +16,8 @@ import org.junit.Test; ...@@ -16,6 +16,8 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.math.BigDecimal;
/** /**
* @Description 运营交互帧类型码数据定义协议测试类 * @Description 运营交互帧类型码数据定义协议测试类
* @Author yelq * @Author yelq
...@@ -30,11 +32,11 @@ public class RemoteChargingTest { ...@@ -30,11 +32,11 @@ public class RemoteChargingTest {
@Before @Before
public void beforeTest() throws InterruptedException { public void beforeTest() throws InterruptedException {
Ocpp16Helper.scan(); // Ocpp16Helper.scan();
String host = "localhost"; // String host = "localhost";
int port = 8888; // int port = 8888;
server = ServerMain.start(host,port); // server = ServerMain.start(host,port);
Thread.sleep(10000); // Thread.sleep(10000);
} }
@After @After
...@@ -63,7 +65,7 @@ public class RemoteChargingTest { ...@@ -63,7 +65,7 @@ public class RemoteChargingTest {
public void startMachineReqTest() throws Exception { public void startMachineReqTest() throws Exception {
StartMachineReq req = new StartMachineReq(); StartMachineReq req = new StartMachineReq();
StartMachineReqData data = new StartMachineReqData(); StartMachineReqData data = new StartMachineReqData();
data.setTransactionSerialNumber(Ocpp16Helper.strToBcd("32010200000000111511161555350261", data.getTransactionSerialNumber())); data.setTransactionSerialNumber(Ocpp16Helper.strToBcd("32010200000000111511161555350260", data.getTransactionSerialNumber()));
data.setPileId(Ocpp16Helper.strToBcd("32010200000001",data.getPileId())); data.setPileId(Ocpp16Helper.strToBcd("32010200000001",data.getPileId()));
data.setGunNumber((byte) 1); data.setGunNumber((byte) 1);
data.setLogicalCardNumber(Ocpp16Helper.strToBcd("0000001000000573",data.getLogicalCardNumber())); data.setLogicalCardNumber(Ocpp16Helper.strToBcd("0000001000000573",data.getLogicalCardNumber()));
...@@ -72,6 +74,11 @@ public class RemoteChargingTest { ...@@ -72,6 +74,11 @@ public class RemoteChargingTest {
byte[] accountBytes = Ocpp16Helper.binToBytes(Ocpp16Helper.hexToBinStr("A0860100")); byte[] accountBytes = Ocpp16Helper.binToBytes(Ocpp16Helper.hexToBinStr("A0860100"));
data.setAccountBalance(accountBytes); data.setAccountBalance(accountBytes);
req.setData(data); req.setData(data);
Ocpp16Helper.scan();
String host = "localhost";
int port = 8888;
server = ServerMain.start(host,port);
Thread.sleep(10000);
TcpCallback<TcpRsp> callback = server.sendSync(req); TcpCallback<TcpRsp> callback = server.sendSync(req);
TcpRsp tcpRsp = callback.get(); TcpRsp tcpRsp = callback.get();
log.info("callback:{}",tcpRsp); log.info("callback:{}",tcpRsp);
......
...@@ -3,8 +3,12 @@ package cn.com.glsx.charging.gbt.ocpp16; ...@@ -3,8 +3,12 @@ package cn.com.glsx.charging.gbt.ocpp16;
import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain; import cn.com.glsx.charging.modules.gbt.ocpp16.ClientMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper; import cn.com.glsx.charging.modules.gbt.ocpp16.Ocpp16Helper;
import cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain; import cn.com.glsx.charging.modules.gbt.ocpp16.ServerMain;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpCallback;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.client.TcpClient;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.StartMachineReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TransactionRecordsReqData; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.data.TransactionRecordsReqData;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.StartMachineReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.TransactionRecordsReq; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.protocol.TransactionRecordsReq;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server.TcpServer;
import cn.com.glsx.charging.util.ByteUtils; import cn.com.glsx.charging.util.ByteUtils;
...@@ -15,6 +19,8 @@ import org.junit.Test; ...@@ -15,6 +19,8 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.math.BigDecimal;
/** /**
* @Description todo * @Description todo
* @Author yelq * @Author yelq
...@@ -59,7 +65,7 @@ public class TransactionRecordsTest { ...@@ -59,7 +65,7 @@ public class TransactionRecordsTest {
TransactionRecordsReq req = new TransactionRecordsReq(); TransactionRecordsReq req = new TransactionRecordsReq();
TransactionRecordsReqData data = new TransactionRecordsReqData(); TransactionRecordsReqData data = new TransactionRecordsReqData();
data.setTransactionSerialNumber(Ocpp16Helper.strToBcd("55031412782305012018061910262392",data.getTransactionSerialNumber())); data.setTransactionSerialNumber(Ocpp16Helper.strToBcd("55031412782305012018061910262392",data.getTransactionSerialNumber()));
data.setPileId(Ocpp16Helper.strToBcd("55031412782305",data.getPileId())); data.setPileId(Ocpp16Helper.strToBcd("32010200000001",data.getPileId()));
data.setGunNumber((byte) 1); data.setGunNumber((byte) 1);
data.setStartTime(ByteUtils.fromHex("98B70E11100314")); data.setStartTime(ByteUtils.fromHex("98B70E11100314"));
data.setEndTime(ByteUtils.fromHex("98B70E11100314")); data.setEndTime(ByteUtils.fromHex("98B70E11100314"));
...@@ -83,7 +89,8 @@ public class TransactionRecordsTest { ...@@ -83,7 +89,8 @@ public class TransactionRecordsTest {
data.setTotalEndValue(ByteUtils.fromHex("00000000")); data.setTotalEndValue(ByteUtils.fromHex("00000000"));
data.setTotalElectricity(ByteUtils.fromHex("00000000")); data.setTotalElectricity(ByteUtils.fromHex("00000000"));
data.setTotalElectricityLoss(ByteUtils.fromHex("00000000")); data.setTotalElectricityLoss(ByteUtils.fromHex("00000000"));
data.setTotalAmount(ByteUtils.fromHex("00000000")); // data.setTotalAmount(ByteUtils.fromHex("00000000"));
data.setTotalAmount(Ocpp16Helper.bigDecimalToByteArray(new BigDecimal("235.0100"),4));
data.setVehicleIdentifier(Ocpp16Helper.strToAscii("0000000000000000000000000000000001",data.getVehicleIdentifier())); data.setVehicleIdentifier(Ocpp16Helper.strToAscii("0000000000000000000000000000000001",data.getVehicleIdentifier()));
data.setTransactionIdentifier((byte) 0x01); data.setTransactionIdentifier((byte) 0x01);
data.setTransactionDateTime(ByteUtils.fromHex("98B70E11100314")); data.setTransactionDateTime(ByteUtils.fromHex("98B70E11100314"));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!