From ec72b1ee39b2975748a833c39e4e01023edcc824 Mon Sep 17 00:00:00 2001 From: ZZ Date: Tue, 27 Jul 2021 14:55:03 +0800 Subject: [PATCH] =?UTF-8?q?WIP:=E8=B4=B9=E7=8E=87=E8=AE=BE=E7=BD=AE,?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=85=85=E7=94=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xhpc/common/api/PowerPileService.java | 7 +- .../common/data/down/StartChargingData.java | 21 +++- .../common/data/redis/CacheRateModel.java | 116 ++++++++++++++++++ .../pp/controller/ChargingController.java | 44 ++++++- .../xhpc/pp/controller/StationController.java | 5 +- .../xhpc/pp/logic/RateModelRequestLogic.java | 29 ++--- .../xhpc/pp/server/ChargingPileServer.java | 4 +- .../com/xhpc/pp/utils/security/HexUtils.java | 29 +++++ 8 files changed, 225 insertions(+), 30 deletions(-) create mode 100644 xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheRateModel.java diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/PowerPileService.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/PowerPileService.java index 8bf767ed..f15a0506 100644 --- a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/PowerPileService.java +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/PowerPileService.java @@ -4,6 +4,7 @@ import com.ruoyi.common.core.constant.ServiceNameConstants; import com.ruoyi.common.core.domain.R; import com.xhpc.common.api.factory.PowerPileFallbackFactory; import com.xhpc.common.data.down.StartChargingData; +import com.xhpc.common.data.redis.CacheRateModel; import com.xhpc.common.data.up.OrderData; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.validation.annotation.Validated; @@ -17,7 +18,7 @@ import java.util.Set; @FeignClient(contextId = "powerPileService", value = ServiceNameConstants.PILE_SERVICE, fallbackFactory = PowerPileFallbackFactory.class) public interface PowerPileService { - @PostMapping(value = "/charging/order") + @PostMapping(value = "/charging/start") R startCharging(@Validated @RequestBody StartChargingData startChargingData); @PostMapping(value = "/pile/whitelist") @@ -26,7 +27,7 @@ public interface PowerPileService { @DeleteMapping(value = "/pile/whitelist/{pileNo}") R deletePileWhitelist(@PathVariable("pileNo") String pileNo); - @DeleteMapping(value = "/station/rateModel/{stationId}/{rateModelId}") - R setStationRateModelId(@PathVariable("stationId") Long stationId, @PathVariable("rateModelId") Long rateModelId); + @PostMapping(value = "/station/rateModel/{stationId}/{rateModelId}") + R setStationRateModelId(@PathVariable("stationId") Long stationId, @PathVariable("rateModelId") Long rateModelId, @RequestBody CacheRateModel rateModel); } diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/down/StartChargingData.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/down/StartChargingData.java index 5873cc67..f9c30dcc 100644 --- a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/down/StartChargingData.java +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/down/StartChargingData.java @@ -12,9 +12,10 @@ public class StartChargingData { @NotBlank(message = "枪号不能为空") private String gunId; //枪号 @Min(value = 5, message = "账户余额不能小于5") - private Double balance; //账户余额 - private String logicCardNo; //逻辑卡号 - private String physicCardNo; //物理卡号 + private Integer balance; //账户余额(精度:小数点后2位)=实际金额(元)×100 + private String logicCardNo = "0000000000000000"; //逻辑卡号 + private String physicCardNo = "0000000000000000"; //物理卡号 + private String version; //协议版本号 public String getOrderNo() { @@ -46,12 +47,12 @@ public class StartChargingData { this.gunId = gunId; } - public Double getBalance() { + public Integer getBalance() { return balance; } - public void setBalance(Double balance) { + public void setBalance(Integer balance) { this.balance = balance; } @@ -76,4 +77,14 @@ public class StartChargingData { this.physicCardNo = physicCardNo; } + public String getVersion() { + + return version; + } + + public void setVersion(String version) { + + this.version = version; + } + } diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheRateModel.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheRateModel.java new file mode 100644 index 00000000..f107755f --- /dev/null +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheRateModel.java @@ -0,0 +1,116 @@ +package com.xhpc.common.data.redis; + +public class CacheRateModel { + + private String t1Price; //尖电价 所有价格精度 + private String t1SvcPrice; //尖服务费价格 + private String t2Price; //峰电价 + private String t2SvcPrice; //峰服务费价格 + private String t3Price; //平电价 + private String t3SvcPrice; //平服务费价格 + private String t4Price; //谷电价 + private String t4SvcPrice; //谷服务费价格 + private String lossRate; //计损比例 + private String[] tfPricesSeq; //48个字符串48个时段,每个时段的值为4个值之一 → 00: 尖费率 01: 峰费率 02: 平费率 03: 谷费率 + + public String getT1Price() { + + return t1Price; + } + + public void setT1Price(String t1Price) { + + this.t1Price = t1Price; + } + + public String getT1SvcPrice() { + + return t1SvcPrice; + } + + public void setT1SvcPrice(String t1SvcPrice) { + + this.t1SvcPrice = t1SvcPrice; + } + + public String getT2Price() { + + return t2Price; + } + + public void setT2Price(String t2Price) { + + this.t2Price = t2Price; + } + + public String getT2SvcPrice() { + + return t2SvcPrice; + } + + public void setT2SvcPrice(String t2SvcPrice) { + + this.t2SvcPrice = t2SvcPrice; + } + + public String getT3Price() { + + return t3Price; + } + + public void setT3Price(String t3Price) { + + this.t3Price = t3Price; + } + + public String getT3SvcPrice() { + + return t3SvcPrice; + } + + public void setT3SvcPrice(String t3SvcPrice) { + + this.t3SvcPrice = t3SvcPrice; + } + + public String getT4Price() { + + return t4Price; + } + + public void setT4Price(String t4Price) { + + this.t4Price = t4Price; + } + + public String getT4SvcPrice() { + + return t4SvcPrice; + } + + public void setT4SvcPrice(String t4SvcPrice) { + + this.t4SvcPrice = t4SvcPrice; + } + + public String getLossRate() { + + return lossRate; + } + + public void setLossRate(String lossRate) { + + this.lossRate = lossRate; + } + + public String[] getTfPricesSeq() { + + return tfPricesSeq; + } + + public void setTfPricesSeq(String[] tfPricesSeq) { + + this.tfPricesSeq = tfPricesSeq; + } + +} diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/ChargingController.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/ChargingController.java index cf0dcd77..2fee6217 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/ChargingController.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/ChargingController.java @@ -4,17 +4,30 @@ import com.ruoyi.common.core.domain.R; import com.xhpc.common.api.PowerPileService; import com.xhpc.common.data.down.StartChargingData; import com.xhpc.common.data.up.OrderData; +import com.xhpc.pp.logic.FieldLogic; +import com.xhpc.pp.server.ChargingPileServer; +import com.xhpc.pp.utils.security.HexUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.quickserver.net.server.ClientHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; + +import static com.xhpc.pp.server.ChargingPileServer.default_version; +import static com.xhpc.pp.utils.security.HexUtils.toHexInt; + @RestController public class ChargingController { @Autowired private PowerPileService powerPileService; + @Autowired + private FieldLogic fieldLogic; + @PostMapping("test/pile/charging/order") public Object test(@Validated @RequestBody StartChargingData startChargingData) { @@ -23,10 +36,37 @@ public class ChargingController { return r; } - @PostMapping("charging/order") + @PostMapping("charging/start") public Object startCharging(@Validated @RequestBody StartChargingData startChargingData) { - return new OrderData(); + byte[] msg = translate(startChargingData); + ClientHandler clientHandler = ChargingPileServer.getHandler(startChargingData.getPileNo()); + if (clientHandler == null) return R.fail(); + try { + clientHandler.sendClientBinary(msg); + return R.ok(); + } catch (IOException e) { + e.printStackTrace(); + return R.fail(e.getMessage()); + } + } + + private byte[] translate(StartChargingData startChargingData) { + + byte[] data = new byte[0]; + data = ArrayUtils.addAll(data, HexUtils.toBytes("6830007C0034")); + String version = startChargingData.getVersion(); + if (default_version.equals(version)) { + data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getOrderNo())); + data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getPileNo())); + data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getGunId())); + data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getLogicCardNo())); + data = ArrayUtils.addAll(data, HexUtils.toBytes(startChargingData.getPhysicCardNo())); + data = ArrayUtils.addAll(data, HexUtils.toBytes(toHexInt(startChargingData.getBalance()))); + } else { + // not defined or implemented yet + } + return data; } } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java index 62c45718..c8c45d89 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java @@ -1,8 +1,10 @@ package com.xhpc.pp.controller; import com.ruoyi.common.core.domain.R; +import com.xhpc.common.data.redis.CacheRateModel; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @@ -13,11 +15,12 @@ import static com.xhpc.pp.server.ChargingPileServer.REDIS; public class StationController { @PostMapping("station/rateModel/{stationId}/{rateModelId}") - public Object setStationRateModel(@PathVariable Long stationId, @PathVariable Long rateModelId) { + public Object setStationRateModel(@PathVariable Long stationId, @PathVariable Long rateModelId, @RequestBody CacheRateModel rateModel) { String skey = "station:".concat(stationId.toString()); Map cacheStation = REDIS.getCacheMap(skey); cacheStation.put("rateModelId", rateModelId); + cacheStation.put("rateModel", rateModel); REDIS.setCacheObject(skey, cacheStation); return R.ok(); } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelRequestLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelRequestLogic.java index 4d721006..f70a73dd 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelRequestLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelRequestLogic.java @@ -1,5 +1,6 @@ package com.xhpc.pp.logic; +import com.xhpc.common.data.redis.CacheStation; import com.xhpc.pp.tx.ServiceParameter; import com.xhpc.pp.tx.ServiceResult; import com.xhpc.pp.tx.logic.ServiceLogic; @@ -25,24 +26,18 @@ public class RateModelRequestLogic implements ServiceLogic { Map req = sp.getParameters(); String pileNo = (String) req.get("pileNo"); - Map cachePile = REDIS.getCacheMap(pileNo); - Integer stationId = cachePile.get("stationId"); - Map stations = REDIS.getCacheMap("stations"); - Object cacheStation = stations.get(stationId); //todo - String resultCode = ServiceResult.OK; - String hexCode = ServiceResult.HEX_OK; - -// Integer rateModelIdCache = cacheStation.get("rateModelId"); -// Object rateModelId = station.getRateModelId(); -// if (rateModelId==null) { -// hexCode = ServiceResult.HEX_FAIL; -// resultCode = ServiceResult.FAIL; -// } + Map cachePile = REDIS.getCacheMap(pileNo); + String stationId = (String) cachePile.get("stationId"); + CacheStation cacheStation = REDIS.getCacheObject("station:".concat(stationId)); + Long rateModelId = cacheStation.getRateModelId(); + if (rateModelId == null) { + return new ServiceResult((byte[]) null, ServiceResult.FAIL); + } String resultStr = "680E00000006".concat(pileNo) - .concat(String.format("%04d", "rateModelIdCache")) -// .concat(rateModel) - .concat(hexCode); + .concat(String.format("%04d", rateModelId)) +// .concat(rateModel) // todo + .concat(ServiceResult.HEX_OK); resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr)); - return new ServiceResult(HexUtils.toBytes(resultStr), resultCode); + return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK); } } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileServer.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileServer.java index 4ef3b8be..2a457121 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileServer.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileServer.java @@ -131,9 +131,9 @@ public class ChargingPileServer { return version; } - public static ClientHandler getHandler(String pile) { + public static ClientHandler getHandler(String pileNo) { - return handlerMap.get(pile); + return handlerMap.get(pileNo); } public static String getPileNo(ClientHandler handler) { diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/HexUtils.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/HexUtils.java index 87a23230..ca7b1252 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/HexUtils.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/HexUtils.java @@ -11,6 +11,27 @@ public class HexUtils { */ private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + public static String toHexInt(Integer dec) { + + return toHex(toIntBytes(String.format("%08d", dec), 8)); + } + + public static int reverseHexInt(String hex) { + + byte[] data = toBytes(reverseHex(hex)); + return toInteger(data, 0, data.length); + } + + public static String reverseHex(String hex) { + + int len = hex.length(); + String revers = ""; + for (int i = len; i > 0; i = i - 2) { + revers = revers.concat(hex.substring(i - 2, i)); + } + return revers; + } + public static String toHex(byte[] data) { StringBuilder stringBuilder = new StringBuilder(); @@ -145,4 +166,12 @@ public class HexUtils { return res; } + public static void main(String[] args) { + + byte[] data1 = toBytes(reverseHex("A0860100")); + System.out.println(toInteger(data1, 0, 4)); + System.out.println(reverseHexInt("A0860100")); + System.out.println(toHexInt(100000)); + } + }