diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/dto/ChargingStationDto.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/dto/ChargingStationDto.java index 16987d28..1bf54c0a 100644 --- a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/dto/ChargingStationDto.java +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/dto/ChargingStationDto.java @@ -30,7 +30,7 @@ public class ChargingStationDto { public Long getRateModelId() { - return rateModelId; + return rateModelId == null ? -1L : rateModelId; } public void setRateModelId(Long rateModelId) { 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 605f9d38..2d569a83 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 @@ -37,7 +37,6 @@ public class ChargingController { @Autowired private PowerPileService powerPileService; - @PostMapping("test/pile/charging/order") public Object test(@Validated @RequestBody StartChargingData startChargingData) { @@ -45,8 +44,8 @@ public class ChargingController { return r; } - @PostMapping("charging/start") - public Object startCharging(@Validated @RequestBody StartChargingData startChargingData) { + @PostMapping("charging/balance/refresh") + public Object refreshBalance(@Validated @RequestBody StartChargingData startChargingData) { String pileNo = startChargingData.getPileNo(); String pkey = "pile:".concat(pileNo); @@ -81,17 +80,75 @@ public class ChargingController { if (handler != null) { try { handler.sendClientBinary(HexUtils.toBytes(rateModelMsg)); - r = R.fail("费率模型已下发,请稍后再次尝试启动充电"); + r = R.ok("余额更新已下发"); } catch (IOException e) { - log.debug(e.getMessage()); - r = R.fail("费率模型下发失败,未更新或下发等待设置中"); + r = R.fail("余额更新下发失败:".concat(e.getMessage())); } + } else { + r = R.fail("费率模型下发失败,充电桩离线"); + } + } else { + r = R.fail("充电桩离线,余额更新下发失败"); + } + } else { + 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); + 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; + } + + @PostMapping("charging/start") + public Object startCharging(@Validated @RequestBody StartChargingData startChargingData) { + + String pileNo = startChargingData.getPileNo(); + String pkey = "pile:".concat(pileNo); + Map cachePile = REDIS.getCacheMap(pkey); + R r = R.ok(); + if (cachePile == null) { + return R.fail("充电桩未注册"); + } + String status = cachePile.get("status").toString(); + if (!REGISTERED.equals(status)) { + return R.fail("充电桩离线"); + } + if (r.getCode() == 200) { + String svcSrv = (String) cachePile.get("svcSrv"); + Long stationId = (Long) cachePile.get("stationId"); //todo + ChargingStationDto cacheStation = REDIS.getCacheObject("station:".concat(stationId.toString())); + Long stationRateModelId = cacheStation.getRateModelId(); + Long pileRateModelId = (Long) cachePile.get("rateModelId"); + cachePile.put("rateModelId", pileRateModelId); + if (!stationRateModelId.equals(pileRateModelId)) { + if (cachePile.get("status").toString().equals(REGISTERED)) { + CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:".concat(stationRateModelId.toString())); + String rateModel = RateModelRequestLogic.translate(cacheRateModel); + String rateModelMsg = "680E0000000A".concat(pileNo) + .concat(String.format("%04X", stationRateModelId)) + .concat(rateModel) + .concat(ServiceResult.HEX_OK); + rateModelMsg = rateModelMsg.concat(CRCCalculator.calcCrc(rateModel)); + String response = HttpUtils.post(fmt(svcSrv).concat("/native/pile/".concat(pileNo).concat("/rateModel")), rateModelMsg); + JSONObject responseJson = (JSONObject) JSON.parse(response); + assert responseJson != null; + int code = responseJson.getInteger("code"); + if (code != 200) { + r = R.fail(code, responseJson.getString("msg")); + } else { + r = R.fail(responseJson.get("data"), responseJson.getString("msg")); } } else { r = R.fail("充电桩离线,费率模型未更新或下发"); } } else { - 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); diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/PileController.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/PileController.java index 06f7f9b5..11b39ace 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/PileController.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/PileController.java @@ -2,12 +2,16 @@ package com.xhpc.pp.controller; import com.xhpc.common.api.dto.ChargingStationDto; import com.xhpc.common.core.domain.R; +import com.xhpc.pp.utils.security.HexUtils; +import org.quickserver.net.server.ClientHandler; import org.springframework.web.bind.annotation.*; +import java.io.IOException; import java.util.Map; import java.util.Set; import static com.xhpc.pp.server.ChargingPileServer.REDIS; +import static com.xhpc.pp.server.ChargingPileServer.getHandler; @RestController public class PileController { @@ -62,10 +66,28 @@ public class PileController { } @GetMapping("terminal/{terminalSn}/status") - public Object terminalStatus(@PathVariable("terminalSn")String terminalSn) { + public Object terminalStatus(@PathVariable("terminalSn") String terminalSn) { // todo return R.ok(); } + @PostMapping("native/pile/{pileNo}/rateModel") + public Object configRateModel(@PathVariable("pileNo") String pileNo, @RequestBody String msg) { + + ClientHandler handler = getHandler(pileNo); + R r; + if (handler != null) { + try { + handler.sendClientBinary(HexUtils.toBytes(msg)); + r = R.fail("费率模型已下发"); + } catch (IOException e) { + r = R.fail("费率模型下发失败:".concat(e.getMessage())); + } + } else { + r = R.fail("费率模型下发失败,充电桩离线"); + } + return r; + } + } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RegisterLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RegisterLogic.java index 4094a263..d4fac884 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RegisterLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RegisterLogic.java @@ -39,7 +39,7 @@ public class RegisterLogic implements ServiceLogic { resultCode = ServiceResult.FAIL; } else if (!EarlierBeanConf.ifreg(pileNo)) { log.info("pile already registered ({}) ", pileNo); - hexCode = ServiceResult.HEX_FAIL; + hexCode = ServiceResult.HEX_FAIL; //todo we may need optimize it resultCode = ServiceResult.FAIL; } else { String pkey = "pile:".concat(pileNo); @@ -48,7 +48,6 @@ public class RegisterLogic implements ServiceLogic { String localIPAndPort = getLocalIPAndPort(); cachePile.put("svcSrv", localIPAndPort); REDIS.setCacheMap("pile:".concat(pileNo), cachePile); - cachePileGunSvcSrv(pileNo); int gunNum = Integer.parseInt(req.get("gunNum").toString()); for (int gunN = 1; gunN <= gunNum; gunN++) { String gunId = String.format("%02d", gunN); @@ -57,6 +56,7 @@ public class RegisterLogic implements ServiceLogic { cacheGun.put("svcSrv", localIPAndPort); REDIS.setCacheMap(gunkey, cacheGun); } + cachePileGunSvcSrv(pileNo); log.info("pile (re)registered ({}) ", pileNo); } String resultStr = "680C00000002".concat(pileNo).concat(hexCode);