实时数据与结算订单数据解析

This commit is contained in:
ZZ 2021-08-06 17:58:40 +08:00
parent e1b78d116b
commit e5fb887b9a
10 changed files with 671 additions and 13 deletions

View File

@ -472,10 +472,20 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @return
*/
public static String numFormat(Long userId, int channelType, int type) {
StringBuffer num = new StringBuffer();
DecimalFormat decimalFormat = new DecimalFormat("0000000");
int random = (int) (1 + Math.random() * (10 - 1 + 1));
num.append(decimalFormat.format(userId)).append(DateUtils.timePath()).append(channelType).append(type).append(random);
return num.toString();
}
public static String capitalize(String str) {
if (str == null || str.isEmpty()) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}

View File

@ -0,0 +1,350 @@
package com.xhpc.common.data.redis;
import com.xhpc.common.data.up.BaseData;
//充电订单实时数据
public class CacheOrderData extends BaseData {
private String orderNo; //交易流水号
private String pileNo; //桩号
private String gunId; //枪号
private String startTime; //开始时间CP56Time2a格式
private String endTime; //结束时间CP56Time2a格式
private Integer t1Price; //尖单价
private Integer t1PowerQuantity; //尖电量
private Integer t1LossQuantity; //计损尖电量
private Integer t1Cost; //尖金额
private Integer t2Price; //峰单价
private Integer t2PowerQuantity; //峰电量
private Integer t2LossQuantity; //计损峰电量
private Integer t2Cost; //峰金额
private Integer t3Price; //平单价
private Integer t3PowerQuantity; //平电量
private Integer t3LossQuantity; //计损平电量
private Integer t3Cost; //平金额
private Integer t4Price; //谷单价
private Integer t4PowerQuantity; //谷电量
private Integer t4LossQuantity; //计损谷电量
private Integer t4Cost; //谷金额
private Integer electricMeterStart; //电表总起值
private Integer electricMeterEnd; //电表总止值
private Integer totalPowerQuantity; //总电量
private Integer totalLossPowerQuantity; //计损总电量
private Integer cost; //消费金额
private String vinNormal; //VIN 此处 VIN 码和充电时 VIN码不同正序直接上传无需补0和反序
private String transactionFlag; //交易标识
private String transactionDatetime; //交易日期时间CP56Time2a格式
private String stopReason; //停止原因
private String cardNo; //物理卡号
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getPileNo() {
return pileNo;
}
public void setPileNo(String pileNo) {
this.pileNo = pileNo;
}
public String getGunId() {
return gunId;
}
public void setGunId(String gunId) {
this.gunId = gunId;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public Integer getT1Price() {
return t1Price;
}
public void setT1Price(Integer t1Price) {
this.t1Price = t1Price;
}
public Integer getT1PowerQuantity() {
return t1PowerQuantity;
}
public void setT1PowerQuantity(Integer t1PowerQuantity) {
this.t1PowerQuantity = t1PowerQuantity;
}
public Integer getT1LossQuantity() {
return t1LossQuantity;
}
public void setT1LossQuantity(Integer t1LossQuantity) {
this.t1LossQuantity = t1LossQuantity;
}
public Integer getT1Cost() {
return t1Cost;
}
public void setT1Cost(Integer t1Cost) {
this.t1Cost = t1Cost;
}
public Integer getT2Price() {
return t2Price;
}
public void setT2Price(Integer t2Price) {
this.t2Price = t2Price;
}
public Integer getT2PowerQuantity() {
return t2PowerQuantity;
}
public void setT2PowerQuantity(Integer t2PowerQuantity) {
this.t2PowerQuantity = t2PowerQuantity;
}
public Integer getT2LossQuantity() {
return t2LossQuantity;
}
public void setT2LossQuantity(Integer t2LossQuantity) {
this.t2LossQuantity = t2LossQuantity;
}
public Integer getT2Cost() {
return t2Cost;
}
public void setT2Cost(Integer t2Cost) {
this.t2Cost = t2Cost;
}
public Integer getT3Price() {
return t3Price;
}
public void setT3Price(Integer t3Price) {
this.t3Price = t3Price;
}
public Integer getT3PowerQuantity() {
return t3PowerQuantity;
}
public void setT3PowerQuantity(Integer t3PowerQuantity) {
this.t3PowerQuantity = t3PowerQuantity;
}
public Integer getT3LossQuantity() {
return t3LossQuantity;
}
public void setT3LossQuantity(Integer t3LossQuantity) {
this.t3LossQuantity = t3LossQuantity;
}
public Integer getT3Cost() {
return t3Cost;
}
public void setT3Cost(Integer t3Cost) {
this.t3Cost = t3Cost;
}
public Integer getT4Price() {
return t4Price;
}
public void setT4Price(Integer t4Price) {
this.t4Price = t4Price;
}
public Integer getT4PowerQuantity() {
return t4PowerQuantity;
}
public void setT4PowerQuantity(Integer t4PowerQuantity) {
this.t4PowerQuantity = t4PowerQuantity;
}
public Integer getT4LossQuantity() {
return t4LossQuantity;
}
public void setT4LossQuantity(Integer t4LossQuantity) {
this.t4LossQuantity = t4LossQuantity;
}
public Integer getT4Cost() {
return t4Cost;
}
public void setT4Cost(Integer t4Cost) {
this.t4Cost = t4Cost;
}
public Integer getElectricMeterStart() {
return electricMeterStart;
}
public void setElectricMeterStart(Integer electricMeterStart) {
this.electricMeterStart = electricMeterStart;
}
public Integer getElectricMeterEnd() {
return electricMeterEnd;
}
public void setElectricMeterEnd(Integer electricMeterEnd) {
this.electricMeterEnd = electricMeterEnd;
}
public Integer getTotalPowerQuantity() {
return totalPowerQuantity;
}
public void setTotalPowerQuantity(Integer totalPowerQuantity) {
this.totalPowerQuantity = totalPowerQuantity;
}
public Integer getTotalLossPowerQuantity() {
return totalLossPowerQuantity;
}
public void setTotalLossPowerQuantity(Integer totalLossPowerQuantity) {
this.totalLossPowerQuantity = totalLossPowerQuantity;
}
public Integer getCost() {
return cost;
}
public void setCost(Integer cost) {
this.cost = cost;
}
public String getVinNormal() {
return vinNormal;
}
public void setVinNormal(String vinNormal) {
this.vinNormal = vinNormal;
}
public String getTransactionFlag() {
return transactionFlag;
}
public void setTransactionFlag(String transactionFlag) {
this.transactionFlag = transactionFlag;
}
public String getTransactionDatetime() {
return transactionDatetime;
}
public void setTransactionDatetime(String transactionDatetime) {
this.transactionDatetime = transactionDatetime;
}
public String getStopReason() {
return stopReason;
}
public void setStopReason(String stopReason) {
this.stopReason = stopReason;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
}

View File

@ -0,0 +1,206 @@
package com.xhpc.common.data.redis;
import com.xhpc.common.data.up.BaseData;
public class CacheRealtimeData extends BaseData {
private String orderNo; //交易流水号
private String pileNo; //桩号
private String gunId; //枪号
private String status; //状态
private String pileGunStatus; //枪是否归位
private String vehicleGunStatus; //是否插枪
private Integer workingVoltage; //输出电压
private Integer workingCurrent; //输出电流
private Integer gunLineTemperature; //枪线温度
private String gunLineNumber; //枪线编码
private Integer soc; //
private Integer maxTemperature; //电池组最高温度
private Integer chargingTime; //累计充电时间
private Integer remainingTime; //剩余时间
private Integer chargingDegree; //充电度数
private Integer lossChargingDegree; //记损充电度数
private Integer amountCharged; //已充金额
private String hardwareFault; //硬件故障
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getPileNo() {
return pileNo;
}
public void setPileNo(String pileNo) {
this.pileNo = pileNo;
}
public String getGunId() {
return gunId;
}
public void setGunId(String gunId) {
this.gunId = gunId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPileGunStatus() {
return pileGunStatus;
}
public void setPileGunStatus(String pileGunStatus) {
this.pileGunStatus = pileGunStatus;
}
public String getVehicleGunStatus() {
return vehicleGunStatus;
}
public void setVehicleGunStatus(String vehicleGunStatus) {
this.vehicleGunStatus = vehicleGunStatus;
}
public Integer getWorkingVoltage() {
return workingVoltage;
}
public void setWorkingVoltage(Integer workingVoltage) {
this.workingVoltage = workingVoltage;
}
public Integer getWorkingCurrent() {
return workingCurrent;
}
public void setWorkingCurrent(Integer workingCurrent) {
this.workingCurrent = workingCurrent;
}
public Integer getGunLineTemperature() {
return gunLineTemperature;
}
public void setGunLineTemperature(Integer gunLineTemperature) {
this.gunLineTemperature = gunLineTemperature;
}
public String getGunLineNumber() {
return gunLineNumber;
}
public void setGunLineNumber(String gunLineNumber) {
this.gunLineNumber = gunLineNumber;
}
public Integer getSoc() {
return soc;
}
public void setSoc(Integer soc) {
this.soc = soc;
}
public Integer getMaxTemperature() {
return maxTemperature;
}
public void setMaxTemperature(Integer maxTemperature) {
this.maxTemperature = maxTemperature;
}
public Integer getChargingTime() {
return chargingTime;
}
public void setChargingTime(Integer chargingTime) {
this.chargingTime = chargingTime;
}
public Integer getRemainingTime() {
return remainingTime;
}
public void setRemainingTime(Integer remainingTime) {
this.remainingTime = remainingTime;
}
public Integer getChargingDegree() {
return chargingDegree;
}
public void setChargingDegree(Integer chargingDegree) {
this.chargingDegree = chargingDegree;
}
public Integer getLossChargingDegree() {
return lossChargingDegree;
}
public void setLossChargingDegree(Integer lossChargingDegree) {
this.lossChargingDegree = lossChargingDegree;
}
public Integer getAmountCharged() {
return amountCharged;
}
public void setAmountCharged(Integer amountCharged) {
this.amountCharged = amountCharged;
}
public String getHardwareFault() {
return hardwareFault;
}
public void setHardwareFault(String hardwareFault) {
this.hardwareFault = hardwareFault;
}
}

View File

@ -50,20 +50,27 @@ public class ChargingController {
String pileNo = startChargingData.getPileNo();
String pkey = "pile:".concat(pileNo);
Map<String, String> cachePile = REDIS.getCacheMap(pkey);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
R<Object> r = R.ok();
if (cachePile.isEmpty()) {
r = R.fail("充电桩未注册");
}
String status = cachePile.get("status");
String status = cachePile.get("status").toString();
if (!REGISTERED.equals(status)) {
r = R.fail("充电桩离线");
}
if (r.getCode() != 200) {
Long pileRateModelId = Long.valueOf(cachePile.get("rateModelId"));
Long stationId = Long.valueOf(cachePile.get("stationId"));
if (r.getCode() == 200) {
Long stationId = Long.valueOf(cachePile.get("stationId").toString()); //todo
ChargingStationDto cacheStation = REDIS.getCacheObject("station:".concat(stationId.toString()));
Long stationRateModelId = cacheStation.getRateModelId();
Long pileRateModelId;
String pileRateModelIdStr = (String) cachePile.get("rateModelId");
if (pileRateModelIdStr == null) {
pileRateModelId = stationRateModelId;
} else {
pileRateModelId = Long.parseLong(pileRateModelIdStr);
}
cachePile.put("rateModelId", pileRateModelId.toString());
if (!pileRateModelId.equals(stationRateModelId)) {
if (cachePile.get("status").equals("Registered")) {
@ -87,7 +94,7 @@ public class ChargingController {
}
r = R.fail("费率模型未更新或下发");
} else {
String svcSrv = cachePile.get("svcSrv");
String svcSrv = (String) cachePile.get("svcSrv");
JSONObject json = (JSONObject) JSON.toJSON(startChargingData);
String response = HttpUtils.post(fmt(svcSrv).concat("/native/charging/start"), json);
JSONObject responseJson = (JSONObject) JSON.parse(response);

View File

@ -1,17 +1,25 @@
package com.xhpc.pp.logic;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.data.redis.CacheOrderData;
import com.xhpc.common.data.up.OrderData;
import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.logic.ServiceLogic;
import com.xhpc.pp.utils.security.CRCCalculator;
import com.xhpc.pp.utils.security.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.utils.security.CacheDataUtils.reflectTranslate;
@Lazy
@Component("OrderDataLogic")
public class OrderDataLogic implements ServiceLogic {
@ -24,8 +32,23 @@ public class OrderDataLogic implements ServiceLogic {
Map<String, Object> req = sp.getParameters();
ObjectMapper objectMapper = new ObjectMapper();
OrderData orderData = objectMapper.convertValue(req, OrderData.class);
//todo
return new ServiceResult(false);
String orderNo = orderData.getOrderNo();
String orderkey = "order:".concat(orderNo);
Map<String, Object> cacheOrder = REDIS.getCacheMap(orderkey);
cacheOrder.put("status", "完成结算");
cacheOrder.put("orderData", translate(orderData));
REDIS.setCacheMap(orderkey, cacheOrder);
String resultStr = "681500000040".concat(orderNo).concat(ServiceResult.HEX_OK);
resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
}
private CacheOrderData translate(OrderData orderData) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException, InstantiationException {
Class<CacheOrderData> codclz = CacheOrderData.class;
Class<OrderData> odclz = OrderData.class;
Field[] codfields = codclz.getDeclaredFields();
return (CacheOrderData) reflectTranslate(orderData, codclz, odclz, codfields);
}
}

View File

@ -30,7 +30,7 @@ public class PileRateModelConfigReplyDataLogic implements ServiceLogic {
if (pileRateModelConfigReplyData.getConfigResult().equals(HEX_OK)) {
String pileNo = pileRateModelConfigReplyData.getPileNo();
Map<String, Object> cacheMap = REDIS.getCacheMap("pile:".concat(pileNo));
// todo
cacheMap.get("");//todo
}
return new ServiceResult(false);
}

View File

@ -1,6 +1,7 @@
package com.xhpc.pp.logic;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.data.redis.CacheRealtimeData;
import com.xhpc.common.data.up.RealtimeData;
import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
@ -10,12 +11,15 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.utils.security.CacheDataUtils.reflectTranslate;
import static com.xhpc.pp.utils.security.HexUtils.toBits;
@Lazy
@ -59,11 +63,11 @@ public class RealtimeDataLogic implements ServiceLogic {
String orderkey = "order:".concat(orderNo);
if (!orderNo.equals("00000000000000000000000000000000")) {
Map<String, Object> order = REDIS.getCacheMap(orderkey);
List<RealtimeData> realtimeDataList = (List<RealtimeData>) order.get("realtimeDataList");
List<CacheRealtimeData> realtimeDataList = (List<CacheRealtimeData>) order.get("realtimeDataList");
if (realtimeDataList == null) {
realtimeDataList = new ArrayList<>();
}
realtimeDataList.add(realtimeData);
realtimeDataList.add(translate(realtimeData));
order.put("soc", realtimeData.getSoc());
order.put("status", statusplain);
order.put("realtimeDataList", realtimeDataList);
@ -72,4 +76,12 @@ public class RealtimeDataLogic implements ServiceLogic {
return new ServiceResult(false);
}
private CacheRealtimeData translate(RealtimeData realtimeData) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException, InstantiationException {
Class<CacheRealtimeData> crdclz = CacheRealtimeData.class;
Class<RealtimeData> rdclz = RealtimeData.class;
Field[] crdfields = crdclz.getDeclaredFields();
return (CacheRealtimeData) reflectTranslate(realtimeData, crdclz, rdclz, crdfields);
}
}

View File

@ -0,0 +1,31 @@
package com.xhpc.pp.utils.security;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import static com.xhpc.common.core.utils.StringUtils.capitalize;
import static com.xhpc.pp.utils.security.Cp56Time2a.getCP56time2a;
public class CacheDataUtils {
public static Object reflectTranslate(Object srcobj, Class tarclz, Class srcclz, Field[] tarfields) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
Object tarobj = tarclz.getConstructors()[0].newInstance();
for (Field tarfield : tarfields) {
String tarFieldName = tarfield.getName();
String srcval = (String) srcclz.getMethod("get".concat(capitalize(tarFieldName))).invoke(srcobj);
Object tarval;
if (tarfield.getType().getSimpleName().equals("Integer")) {
tarval = HexUtils.reverseHexInt(srcval);
} else if (tarFieldName.contains("ime")) {
tarval = getCP56time2a(srcval);
} else {
tarval = srcval;
}
tarclz.getMethod("set".concat(capitalize(tarFieldName)), tarfield.getType()).invoke(tarobj, tarval);
}
return tarobj;
}
}

View File

@ -0,0 +1,19 @@
package com.xhpc.pp.utils.security;
public class Cp56Time2a {
public static void main(String[] args) {
String date = getCP56time2a("F0D2210F060815");
System.out.println(date);
}
public static String getCP56time2a(String str) {
return "20" + Integer.parseInt(str.substring(12, 14), 16) + "-" + Integer.parseInt(str.substring(10, 12), 16)
+ "-" + Integer.parseInt(str.substring(8, 10), 16) + "T" + Integer.parseInt(str.substring(6, 8), 16)
+ ":" + Integer.parseInt(str.substring(4, 6), 16) + ":"
+ Integer.parseInt(str.substring(2, 4) + "" + str.substring(0, 2), 16) / 1000;
}
}

View File

@ -180,8 +180,8 @@ public class HexUtils {
public static void main(String[] args) {
// byte[] data1 = toBytes(reverseHex("A0860100"));
// System.out.println(toInteger(data1, 0, 4));
byte[] data1 = toBytes(reverseHex("FC080000"));
System.out.println(toInteger(data1, 0, 4));
// System.out.println(reverseHexInt("A0860100"));
// System.out.println(toHexInt(100000));
System.out.println(toBits("1000"));