From e48e29a776ff5e857c573c2d1c32ddc6f7e4b239 Mon Sep 17 00:00:00 2001 From: ZZ Date: Thu, 5 Aug 2021 13:14:50 +0800 Subject: [PATCH] stop charging --- .../com/xhpc/common/api/PowerPileService.java | 3 + .../api/factory/PowerPileFallbackFactory.java | 6 ++ .../pp/controller/ChargingController.java | 66 +++++++++++++++++-- .../pp/server/ChargingPileBinaryHandler.java | 10 +-- .../pp/server/ChargingPileEventHandler.java | 5 +- .../xhpc/pp/server/ChargingPileServer.java | 6 +- 6 files changed, 78 insertions(+), 18 deletions(-) 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 076626ce..9bcbd66c 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 @@ -18,6 +18,9 @@ public interface PowerPileService { @PostMapping("/charging/start") R startCharging(@Validated @RequestBody StartChargingData startChargingData); + @PutMapping("charging/stop/{pileNo}/{gunId}/{version}") + R stopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version); + @PostMapping("/pile/whitelist/add/{stationId}") R addPileWhitelist(@PathVariable("stationId") Long stationId, @RequestBody Set pileNoSet); diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/factory/PowerPileFallbackFactory.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/factory/PowerPileFallbackFactory.java index 74bbdea8..0e355de9 100644 --- a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/factory/PowerPileFallbackFactory.java +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/api/factory/PowerPileFallbackFactory.java @@ -28,6 +28,12 @@ public class PowerPileFallbackFactory implements FallbackFactory pileNoSet) { 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 71e2d884..d403b89f 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 @@ -16,9 +16,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.Map; @@ -69,6 +67,32 @@ public class ChargingController { return R.ok(responseJson.get("data"), responseJson.getString("msg")); } + @PutMapping("charging/stop/{pileNo}/{gunId}/{version}") + public Object stopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) { + + String pkey = "pile:".concat(pileNo); + Map cachePile = REDIS.getCacheMap(pkey); + if (cachePile.isEmpty()) { + return R.fail("充电桩未注册"); + } + String status = cachePile.get("status"); + if (!REGISTERED.equals(status)) { + return 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")); + } + return R.ok(responseJson.get("data"), responseJson.getString("msg")); + } + private String fmt(String svcSrv) { String[] split = svcSrv.split("#"); @@ -85,7 +109,7 @@ public class ChargingController { String gunkey = "gun:".concat(pileNo).concat(startChargingData.getGunId()); String skey = gunkey.concat(".seqhex"); String seq = seqHex(skey); - byte[] msg = translateSucc(startChargingData, seq); + byte[] msg = translateStart(startChargingData, seq); clientHandler.sendClientBinary(msg); return R.ok(); } catch (IOException e) { @@ -94,8 +118,25 @@ public class ChargingController { } } + @GetMapping("native/charging/stop/{pileNo}/{gunId}/{version}") + public Object nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) { - private byte[] translateSucc(StartChargingData startChargingData, String seq) { + ClientHandler clientHandler = ChargingPileServer.getHandler(pileNo); + if (clientHandler == null) return R.fail("充电桩没有连接到上次注册的服务器"); + try { + String gunkey = "gun:".concat(pileNo).concat(gunId); + String skey = gunkey.concat(".seqhex"); + String seq = seqHex(skey); + byte[] msg = translateStop(pileNo, gunId, version, seq); + clientHandler.sendClientBinary(msg); + return R.ok(); + } catch (IOException e) { + log.error("send message failed. " + e.getMessage(), e); + return R.fail(e.getMessage()); + } + } + + private byte[] translateStart(StartChargingData startChargingData, String seq) { byte[] data = new byte[0]; data = ArrayUtils.addAll(data, HexUtils.toBytes("6830".concat(seq).concat("0034"))); @@ -112,8 +153,21 @@ public class ChargingController { } String msg = HexUtils.toHex(data).concat(ServiceResult.HEX_OK); msg = msg.concat(CRCCalculator.calcCrc(msg)); - log.info("charging order[{}], send msg to pile: {}", startChargingData.getOrderNo(), msg); + log.info("start charging order[{}], send msg >>>> |{}|", startChargingData.getOrderNo(), msg); return HexUtils.toBytes(msg); } + private byte[] translateStop(String pileNo, String gunId, String version, String seq) { + + byte[] data = new byte[0]; + data = ArrayUtils.addAll(data, HexUtils.toBytes("680C".concat(seq).concat("0036"))); + if (default_version.equals(version)) { + data = ArrayUtils.addAll(data, HexUtils.toBytes(pileNo)); + data = ArrayUtils.addAll(data, HexUtils.toBytes(gunId)); + } + String msg = HexUtils.toHex(data).concat(ServiceResult.HEX_OK); + msg = msg.concat(CRCCalculator.calcCrc(msg)); + log.info("stop charging, send msg to terminal ({}{}) >>>> {}", pileNo, gunId, msg); + return HexUtils.toBytes(msg); + } } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java index 8aad7205..a288e7c1 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileBinaryHandler.java @@ -54,7 +54,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { String pileNo = ChargingPileServer.getPileNo(handler); log.info("received data <<<< {}, from pile <<<< {}", dataStr, pileNo); if (d.length <= 2 || !dataStr.startsWith("68")) { - log.info("received invalid data <<<< {}, len[{}]", dataStr, d.length); + log.info("received invalid data <<<< |{}|, len[{}]", dataStr, d.length); continue; } int len = HexUtils.toInteger(d, 1, 2); @@ -91,7 +91,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { // TODO: 2021/7/28 }*/ if (result.getBinary() != null) { - log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(result.getBinary())); + log.info("server send msg >>>> ({}) |{}|", pileNo, HexUtils.toHex(result.getBinary())); handler.sendClientBinary(result.getBinary()); } } @@ -99,7 +99,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { private void reg(ClientHandler handler, String pileNo, Map req) throws NacosException { if (!EarlierBeanConf.ifreg(pileNo)) { - log.info("pile already registered >>>> [{}] ", pileNo); + log.info("pile already registered >>>> ({}) ", pileNo); return; } ChargingPileServer.putHandler(pileNo, handler); @@ -116,7 +116,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { String gunkey = pileNo.concat(gunId); cachePileGunSvcSrv("svcSrvGun:", gunkey); } - log.info("pile registering >>>> [{}] ", pileNo); + log.info("pile registering >>>> ({}) ", pileNo); } private void cachePileGunSvcSrv(String prefix, String key) { @@ -135,7 +135,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { int start = 0; while (start < data.length) { if (len > data.length) { - log.error("incorrect input data[{}] len[{}]", data, data.length); + log.error("incorrect input data|{}| len[{}]", data, data.length); break; } dataList.add(ArrayUtils.subarray(data, start, start + len)); diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileEventHandler.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileEventHandler.java index 7a3c2ea0..163d8a8f 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileEventHandler.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/ChargingPileEventHandler.java @@ -27,10 +27,7 @@ public class ChargingPileEventHandler implements ClientEventHandler { public void lostConnection(ClientHandler handler) { String pileNo = ChargingPileServer.getPileNo(handler); - if (pileNo != null) { - ChargingPileServer.removeHandler(pileNo); - } - log.info("lost connection -> [{}] {} <- {}", + log.info("lost connection -> ({}) [{}] <- {}", pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString()); } 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 668a9404..de10558f 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 @@ -92,7 +92,7 @@ public class ChargingPileServer { cacheMap.put("status", DISCONNECTED); REDIS.setCacheMap(pkey, cacheMap); if (handler != null) { - log.info("remove handler [{}] for [{}]", handler.getName(), pileNo); + log.info("remove handler [{}] for ({})", handler.getName(), pileNo); pileMap.remove(handler.getName()); versionMapper.remove(handler.getName()); } @@ -100,14 +100,14 @@ public class ChargingPileServer { public static void sendClientMsg(String pileNo, byte[] msg) { - log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(msg)); + log.info("server send msg >>>> ({}) |{}|", pileNo, HexUtils.toHex(msg)); if (pileNo.length() < 14) { pileNo = "0000000000000000" + pileNo; pileNo = pileNo.substring(pileNo.length() - 14); } ClientHandler handler = handlerMap.get(pileNo); if (handler == null || !handler.isOpen()) { - log.error("send message failed. [{}] connection lost", pileNo); + log.error("send message failed. ({}) connection lost", pileNo); removeHandler(pileNo); return; }