启动充电条件判断;TODO:计费模型下发

This commit is contained in:
ZZ 2021-08-05 19:53:15 +08:00
parent a02e12dd9c
commit 5459059c63
5 changed files with 76 additions and 28 deletions

View File

@ -3,9 +3,12 @@ package com.xhpc.pp.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.HttpUtils;
import com.xhpc.common.data.down.StartChargingData;
import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.pp.logic.RateModelRequestLogic;
import com.xhpc.pp.server.ChargingPileServer;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.utils.security.CRCCalculator;
@ -23,8 +26,7 @@ import java.util.Map;
import static com.xhpc.common.data.redis.SeqUtil.seqHex;
import static com.xhpc.pp.logic.RegisterLogic.REGISTERED;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.server.ChargingPileServer.default_version;
import static com.xhpc.pp.server.ChargingPileServer.*;
import static com.xhpc.pp.utils.security.HexUtils.toHexInt;
@RestController
@ -49,22 +51,55 @@ public class ChargingController {
String pileNo = startChargingData.getPileNo();
String pkey = "pile:".concat(pileNo);
Map<String, String> cachePile = REDIS.getCacheMap(pkey);
R<Object> r = R.ok();
if (cachePile.isEmpty()) {
return R.fail("充电桩未注册");
r = R.fail("充电桩未注册");
}
String status = cachePile.get("status");
if (!REGISTERED.equals(status)) {
return R.fail("充电桩离线");
r = R.fail("充电桩离线");
}
String svcSrv = 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);
int code = responseJson.getInteger("code");
if (code != 200) {
return R.fail(code, responseJson.getString("msg"));
if (r.getCode() != 200) {
Long pileRateModelId = Long.valueOf(cachePile.get("rateModelId"));
Long stationId = Long.valueOf(cachePile.get("stationId"));
ChargingStationDto cacheStation = REDIS.getCacheObject("station:".concat(stationId.toString()));
Long stationRateModelId = cacheStation.getRateModelId();
if (!pileRateModelId.equals(stationRateModelId)) {
if (cachePile.get("status").equals("Registered")) {
CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:".concat(stationRateModelId.toString()));
String rateModel = RateModelRequestLogic.translate(cacheRateModel);
String rateModelMsg = "680E0000000A".concat(pileNo)
.concat(String.format("%04d", stationRateModelId))
.concat(rateModel)
.concat(ServiceResult.HEX_OK);
rateModelMsg = rateModelMsg.concat(CRCCalculator.calcCrc(rateModel));
ClientHandler handler = getHandler(pileNo);
if (handler != null) {
try {
handler.sendClientBinary(HexUtils.toBytes(rateModelMsg));
r = R.fail("费率模型已下发,请稍后再次尝试启动充电");
} catch (IOException e) {
log.debug(e.getMessage());
r = R.fail("费率模型下发失败,未更新或下发等待设置中");
}
}
}
r = R.fail("费率模型未更新或下发");
} else {
String svcSrv = 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);
int code = responseJson.getInteger("code");
if (code != 200) {
r = R.fail(code, responseJson.getString("msg"));
} else {
r = R.ok(responseJson.get("data"), responseJson.getString("msg"));
}
}
}
return R.ok(responseJson.get("data"), responseJson.getString("msg"));
return r;
}
@PutMapping("charging/stop/{pileNo}/{gunId}/{version}")
@ -72,25 +107,30 @@ public class ChargingController {
String pkey = "pile:".concat(pileNo);
Map<String, String> cachePile = REDIS.getCacheMap(pkey);
R<Object> r = R.ok();
if (cachePile.isEmpty()) {
return R.fail("充电桩未注册");
r = R.fail("充电桩未注册");
}
String status = cachePile.get("status");
if (!REGISTERED.equals(status)) {
return R.fail("充电桩离线");
r = R.fail("充电桩离线");
}
String svcSrv = cachePile.get("svcSrv");
String response = HttpUtils.get(fmt(svcSrv)
.concat("/native/charging/stop/")
.concat(pileNo).concat("/")
.concat(gunId).concat("/")
.concat(version));
JSONObject responseJson = (JSONObject) JSON.parse(response);
int code = responseJson.getInteger("code");
if (code != 200) {
return R.fail(code, responseJson.getString("msg"));
if (r.getCode() == 200) {
String svcSrv = cachePile.get("svcSrv");
String response = HttpUtils.get(fmt(svcSrv)
.concat("/native/charging/stop/")
.concat(pileNo).concat("/")
.concat(gunId).concat("/")
.concat(version));
JSONObject responseJson = (JSONObject) JSON.parse(response);
int code = responseJson.getInteger("code");
if (code != 200) {
r = R.fail(code, responseJson.getString("msg"));
} else {
r = R.ok(responseJson.get("data"), responseJson.getString("msg"));
}
}
return R.ok(responseJson.get("data"), responseJson.getString("msg"));
return r;
}
private String fmt(String svcSrv) {

View File

@ -38,6 +38,7 @@ public class PileController {
for (String pileNo : pileNoSet) {
String pkey = "pile:".concat(pileNo);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
cachePile.put("rateModelId", cacheStation.getRateModelId());
cachePile.put("stationId", stationId);
REDIS.setCacheMap(pkey, cachePile);
}

View File

@ -28,9 +28,9 @@ public class TestController {
StartChargingData d = new StartChargingData();
d.setPileNo(pno); //55031412782305
d.setOrderNo(ono); //00000000000000000000000123456789
d.setBalance(b); //300
d.setGunId(gid); //02
d.setOrderNo(ono); //00000000000000000000000123456789
return powerPileService.startCharging(d);
}

View File

@ -12,6 +12,9 @@ import org.springframework.stereotype.Component;
import java.util.Map;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
import static com.xhpc.pp.tx.ServiceResult.HEX_OK;
@Lazy
@Component("PileRateModelConfigReplyDataLogic")
public class PileRateModelConfigReplyDataLogic implements ServiceLogic {
@ -24,7 +27,11 @@ public class PileRateModelConfigReplyDataLogic implements ServiceLogic {
Map<String, Object> req = sp.getParameters();
ObjectMapper objectMapper = new ObjectMapper();
PileConfigReplyData pileRateModelConfigReplyData = objectMapper.convertValue(req, PileConfigReplyData.class);
//todo
if (pileRateModelConfigReplyData.getConfigResult().equals(HEX_OK)) {
String pileNo = pileRateModelConfigReplyData.getPileNo();
Map<String, Object> cacheMap = REDIS.getCacheMap("pile:".concat(pileNo));
// todo
}
return new ServiceResult(false);
}

View File

@ -45,7 +45,7 @@ public class RateModelRequestLogic implements ServiceLogic {
return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
}
private String translate(CacheRateModel cacheRateModel) {
public static String translate(CacheRateModel cacheRateModel) {
String rateModel = "";
rateModel = rateModel.concat(toHexInt(cacheRateModel.getT1Price()))