From c2527da0f9231b3c9d9a00dc1f1ec19111ee53e5 Mon Sep 17 00:00:00 2001 From: ZZ Date: Thu, 9 Sep 2021 16:16:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=BF=9E=E6=8E=A5=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pp/controller/ChargingController.java | 50 ++++++++++++------- .../xhpc/pp/controller/PileController.java | 22 +++++--- .../pp/server/ChargingPileBinaryHandler.java | 25 +++++++--- .../xhpc/pp/server/ChargingPileServer.java | 6 +-- 4 files changed, 68 insertions(+), 35 deletions(-) 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 8fef4dc9..c1b53b2e 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 @@ -31,8 +31,7 @@ import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex; import static com.xhpc.pp.logic.RegisterLogic.REGISTERED; -import static com.xhpc.pp.server.ChargingPileServer.default_version; -import static com.xhpc.pp.server.ChargingPileServer.getHandler; +import static com.xhpc.pp.server.ChargingPileServer.*; import static com.xhpc.pp.utils.security.HexUtils.toHexInt; @RestController @@ -183,12 +182,18 @@ public class ChargingController { ClientHandler handler = getHandler(pileNo); R r; if (handler != null) { - try { - log.info("native refresh balance >>> {}", refreshBalanceMsg); - handler.sendClientBinary(HexUtils.toBytes(refreshBalanceMsg)); - r = R.ok("余额更新已下发"); - } catch (IOException e) { - r = R.fail("余额更新下发失败:".concat(e.getMessage())); + if (!handler.isOpen()) { + log.error("[{}]({}) connection lost", handler.getName(), pileNo); + removeHandler(pileNo); + r = R.fail("充电桩连接已断开"); + } else { + try { + log.info("native refresh balance >>> {}", refreshBalanceMsg); + handler.sendClientBinary(HexUtils.toBytes(refreshBalanceMsg)); + r = R.ok("余额更新已下发"); + } catch (IOException e) { + r = R.fail("余额更新下发失败:".concat(e.getMessage())); + } } } else { r = R.fail("余额更新下发失败,充电桩离线"); @@ -200,10 +205,13 @@ public class ChargingController { public R nativeStartCharging(@Validated @RequestBody StartChargingData startChargingData) { String pileNo = startChargingData.getPileNo(); - ClientHandler clientHandler = ChargingPileServer.getHandler(pileNo); - if (clientHandler == null) { -// ChargingPileServer.removeHandler(pileNo); + ClientHandler handler = ChargingPileServer.getHandler(pileNo); + if (handler == null) { return R.fail("充电桩没有连接到上次注册的服务器,请稍后再试一次"); + } else if (!handler.isOpen()) { + log.error("[{}]({}) connection lost", handler.getName(), pileNo); + removeHandler(pileNo); + return R.fail("充电桩连接已断开,请稍后再试一次"); } try { String gunkey = "gun:".concat(pileNo).concat(startChargingData.getGunId()); @@ -216,7 +224,7 @@ public class ChargingController { String skey = gunkey.concat(".seqhex"); String seq = seqHex(skey); byte[] msg = translateStart(startChargingData, seq); - clientHandler.sendClientBinary(msg); + handler.sendClientBinary(msg); Integer balance = startChargingData.getBalance(); String orderkey = "order:".concat(startChargingData.getOrderNo()); Map cacheOrder = REDIS.getCacheMap(orderkey); @@ -248,8 +256,8 @@ public class ChargingController { @GetMapping("native/charging/stop/{pileNo}/{gunId}/{version}") public R nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) { - ClientHandler clientHandler = ChargingPileServer.getHandler(pileNo); - if (clientHandler == null) { + ClientHandler handler = ChargingPileServer.getHandler(pileNo); + if (handler == null) { // ChargingPileServer.removeHandler(pileNo); return R.fail("充电桩没有连接到上次注册的服务器,请稍后再试一次"); } @@ -259,10 +267,16 @@ public class ChargingController { String skey = gunkey.concat(".seqhex"); String seq = seqHex(skey); byte[] msg = translateStop(pileNo, gunId, version, seq); - clientHandler.sendClientBinary(msg); - Map cacheGun = REDIS.getCacheMap(gunkey); - REDIS.setCacheMap(gunkey, cacheGun); - return R.ok("指令已下发至充电桩"); + if (!handler.isOpen()) { + log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo); + removeHandler(pileNo); + return R.ok("充电桩已离线"); + } else { + handler.sendClientBinary(msg); + Map cacheGun = REDIS.getCacheMap(gunkey); + REDIS.setCacheMap(gunkey, cacheGun); + return R.ok("停止充电指令已下发"); + } } catch (IOException e) { log.error("send message failed. " + e.getMessage(), e); return R.fail(e.getMessage()); 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 f7783a75..e8cb7125 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 @@ -16,6 +16,7 @@ import java.util.Set; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; import static com.xhpc.pp.server.ChargingPileServer.getHandler; +import static com.xhpc.pp.server.ChargingPileServer.removeHandler; @RestController @Slf4j @@ -66,13 +67,20 @@ public class PileController { ClientHandler handler = getHandler(pileNo); R r; if (handler != null) { - try { - log.info("[{}] - server send msg >>>> ({}) |{}|", handler.getName(), pileNo, msg); - handler.sendClientBinary(HexUtils.toBytes(msg)); - r = R.ok(null, "费率模型已下发,请再次启动充电"); - } catch (IOException e) { - log.error("send message failed. " + e.getMessage(), e); - r = R.fail("费率模型下发失败:".concat(e.getMessage()).concat(".无法启动充电")); + if (!handler.isOpen()) { + log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo); + removeHandler(pileNo); + r = R.fail("充电桩连接已断开,请稍后再试"); + } else { + try { + log.info("[{}] - server send msg >>>> ({}) |{}|", handler.getName(), pileNo, msg); + handler.sendClientBinary(HexUtils.toBytes(msg)); + r = R.ok(null, "费率模型已下发,请再次启动充电"); + } catch (IOException e) { + log.error("send message failed. " + e.getMessage(), e); + removeHandler(pileNo); + r = R.fail("费率模型下发失败:".concat(e.getMessage()).concat(".无法启动充电")); + } } } else { r = R.fail("费率模型下发失败,充电桩离线"); 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 adb05471..5228b6e8 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 @@ -24,6 +24,7 @@ import java.util.*; import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex; +import static com.xhpc.pp.server.ChargingPileServer.removeHandler; import static com.xhpc.pp.tx.ServiceResult.OK; import static com.xhpc.pp.utils.security.CP56Time2a.toCp56Hex; import static com.xhpc.pp.utils.security.CRCCalculator.calcCrc; @@ -109,8 +110,13 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { if (gunId != null) { hori2(pileNo, gunId); } - log.info("server send msg >>>> ({}) |{}|", pileNo, toHex(result.getBinary())); - handler.sendClientBinary(result.getBinary()); + if (!handler.isOpen()) { + log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo); + removeHandler(pileNo); + } else { + log.info("server send msg >>>> ({}) |{}|", pileNo, toHex(result.getBinary())); + handler.sendClientBinary(result.getBinary()); + } if (SERVICE_HB.equals(serviceName) && OK.equals(resultCode)) { Boolean tcfg = (Boolean) cachePile.get("tcfg"); if (!tcfg) { @@ -121,12 +127,17 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { cachePile.put("configTime", DateUtil.format(date, NORM_DATETIME_FORMAT)); REDIS.setCacheMap(pilekey, cachePile); log.info("server send time config msg >>>> ({}) |{}|", pileNo, timebin); - handler.sendClientBinary(HexUtils.toBytes(timebin)); - if (gunId != null) { - hori2(pileNo, gunId); + if (!handler.isOpen()) { + log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo); + removeHandler(pileNo); + } else { + handler.sendClientBinary(HexUtils.toBytes(timebin)); + if (gunId != null) { + hori2(pileNo, gunId); + } + cachePile.put("tcfg", true); + REDIS.setCacheMap(pilekey, cachePile); } - cachePile.put("tcfg", true); - REDIS.setCacheMap(pilekey, cachePile); } } } 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 5d23c27d..60bb2654 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 @@ -111,8 +111,8 @@ public class ChargingPileServer { public static void removeHandler(String pileNo) { ClientHandler handler = handlerMap.remove(pileNo); - handler.closeConnection(); - log.info("handler [{}] for ({}) close connection", handler.getName(), pileNo); +// handler.closeConnection(); +// log.info("handler [{}] for ({}) close connection", handler.getName(), pileNo); String pkey = "pile:".concat(pileNo); Map cachePile = REDIS.getCacheMap(pkey); cachePile.put("status", DISCONNECTED); @@ -132,7 +132,7 @@ public class ChargingPileServer { } 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", handler.getName(), pileNo); removeHandler(pileNo); return; }