WIP:费率设置,启动充电

This commit is contained in:
ZZ 2021-07-27 14:55:03 +08:00
parent a2089e517f
commit ec72b1ee39
8 changed files with 225 additions and 30 deletions

View File

@ -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<OrderData> 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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<String, Object> cacheStation = REDIS.getCacheMap(skey);
cacheStation.put("rateModelId", rateModelId);
cacheStation.put("rateModel", rateModel);
REDIS.setCacheObject(skey, cacheStation);
return R.ok();
}

View File

@ -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<String, Object> req = sp.getParameters();
String pileNo = (String) req.get("pileNo");
Map<String, Integer> cachePile = REDIS.getCacheMap(pileNo);
Integer stationId = cachePile.get("stationId");
Map<String, Object> 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<String, Object> 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);
}
}

View File

@ -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) {

View File

@ -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));
}
}