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 9ef9de60..4e3320a9 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 @@ -12,6 +12,7 @@ import com.xhpc.common.data.redis.CacheRateModel; import com.xhpc.pp.logic.RateModelRequestLogic; import com.xhpc.pp.server.ChargingPileServer; import com.xhpc.pp.utils.security.CRCCalculator; +import com.xhpc.pp.utils.security.CacheDataUtils; import com.xhpc.pp.utils.security.HexUtils; import org.apache.commons.lang3.ArrayUtils; import org.quickserver.net.server.ClientHandler; @@ -24,7 +25,6 @@ import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.Calendar; import java.util.Map; -import java.util.concurrent.TimeUnit; import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; import static com.xhpc.common.data.redis.SeqUtil.seqHex; @@ -173,10 +173,11 @@ public class ChargingController { } @PostMapping("native/charging/balance/refresh") - public R nativeRefreshBalance(@Validated @RequestBody StartChargingData refreshBalanceData) { + public R nativeRefreshBalance(@Validated @RequestBody StartChargingData refreshBalanceData) throws InterruptedException { String pileNo = refreshBalanceData.getPileNo(); String gunkey = "gun:".concat(pileNo).concat(refreshBalanceData.getGunId()); + if (CacheDataUtils.hori(gunkey)) Thread.sleep(500L);// return R.fail("等待上一条指令响应"); String skey = gunkey.concat(".seqhex"); String seq = seqHex(skey); String refreshBalanceMsg = "6817".concat(seq).concat("0042").concat(pileNo) @@ -211,7 +212,7 @@ public class ChargingController { if (!"空闲".equals(gunstatus)) { return R.fail("充电桩不在空闲正常状态,稍后再试"); } - if (hori(gunkey)) return R.fail("等待上一条指令响应"); + if (CacheDataUtils.hori(gunkey)) Thread.sleep(500L); //return R.fail("等待上一条指令响应"); String skey = gunkey.concat(".seqhex"); String seq = seqHex(skey); byte[] msg = translateStart(startChargingData, seq); @@ -231,23 +232,12 @@ public class ChargingController { cacheGun.put("orderkey", orderkey); REDIS.setCacheMap(gunkey, cacheGun); return R.ok("指令已下发至充电桩"); - } catch (IOException e) { + } catch (IOException | InterruptedException e) { log.error("send message failed. " + e.getMessage(), e); return R.fail(e.getMessage()); } } - private boolean hori(String gunkey) { - - String hori = gunkey.concat(".hori"); - String HORI = REDIS.getCacheObject(hori); - if (HORI != null) { - return true; - } - REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS); - return false; - } - @GetMapping("native/charging/stop/{pileNo}/{gunId}/{version}") public R nativeStopCharging(@PathVariable("pileNo") String pileNo, @PathVariable("gunId") String gunId, @PathVariable("version") String version) { @@ -255,15 +245,15 @@ public class ChargingController { if (clientHandler == null) return R.fail("充电桩没有连接到上次注册的服务器"); try { String gunkey = "gun:".concat(pileNo).concat(gunId); - Map cacheGun = REDIS.getCacheMap(gunkey); - if (hori(gunkey)) return R.fail("等待上一条指令响应"); + if (CacheDataUtils.hori(gunkey)) Thread.sleep(500L);//return R.fail("等待上一条指令响应"); 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("指令已下发至充电桩"); - } catch (IOException e) { + } catch (IOException | InterruptedException 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/logic/OrderDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java index ca4e6ab1..d3024c4f 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/OrderDataLogic.java @@ -60,7 +60,7 @@ public class OrderDataLogic implements ServiceLogic { return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK); } - private CacheOrderData translate(OrderData orderData) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { + private CacheOrderData translate(OrderData orderData) throws InvocationTargetException, IllegalAccessException, InstantiationException { Class codclz = CacheOrderData.class; Class odclz = OrderData.class; 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 a482bbc6..8003662e 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 @@ -27,6 +27,7 @@ import static com.xhpc.pp.server.ChargingPileServer.REDIS; 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; +import static com.xhpc.pp.utils.security.CacheDataUtils.hori2; import static com.xhpc.pp.utils.security.HexUtils.toHex; public class ChargingPileBinaryHandler implements ClientBinaryHandler { @@ -103,6 +104,11 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { setCachePileRM(pilekey); } if (result.getBinary() != null) { + String gunId = (String) req.get("gunId"); + if (gunId != null) { + String gunkey = "gun:".concat(pileNo).concat(gunId); + hori2(gunkey); + } log.info("server send msg >>>> ({}) |{}|", pileNo, toHex(result.getBinary())); handler.sendClientBinary(result.getBinary()); if (SERVICE_RMR.equals(serviceName) && OK.equals(resultCode)) { 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 856dc270..2dfc2447 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 @@ -33,10 +33,7 @@ public class ChargingPileEventHandler implements ClientEventHandler { String pileNo = ChargingPileServer.getPileNo(handler); if (pileNo != null) { - String pkey = "pile:".concat(pileNo); - Map cachePile = REDIS.getCacheMap(pkey); - cachePile.put("status", RegisterLogic.DISCONNECTED); - REDIS.setCacheMap(pkey, cachePile); + ChargingPileServer.removeHandler(pileNo); } log.info("-> ({}) - [{}] <- {}", 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 3b741f12..c7f77483 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 @@ -88,9 +88,17 @@ public class ChargingPileServer { ClientHandler handler = handlerMap.remove(pileNo); String pkey = "pile:".concat(pileNo); - Map cacheMap = REDIS.getCacheMap(pkey); - cacheMap.put("status", DISCONNECTED); - REDIS.setCacheMap(pkey, cacheMap); + Map cachePile = REDIS.getCacheMap(pkey); + cachePile.put("status", DISCONNECTED); + REDIS.setCacheMap(pkey, cachePile); + for (int i = 1; i < 10; i++) { + String gunkey = "gun:".concat(pileNo).concat(String.format("%02d", i)); + Map cacheGun = REDIS.getCacheMap(gunkey); + if (cacheGun != null) { + cacheGun.put("status", "离线"); + REDIS.setCacheMap(gunkey, cacheGun); + } + } if (handler != null) { log.info("remove handler [{}] for ({})", handler.getName(), pileNo); pileMap.remove(handler.getName()); diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java index 299ed61a..c9184bec 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/utils/security/CacheDataUtils.java @@ -4,9 +4,11 @@ import cn.hutool.core.date.DateUtil; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.TimeUnit; import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; import static com.xhpc.common.core.utils.StringUtils.capitalize; +import static com.xhpc.pp.server.ChargingPileServer.REDIS; import static com.xhpc.pp.utils.security.CP56Time2a.cp56toDate; public class CacheDataUtils { @@ -38,5 +40,21 @@ public class CacheDataUtils { return tarobj; } + public static void hori2(String gunkey) { + + String hori = gunkey.concat(".hori"); + REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS); + } + + public static boolean hori(String gunkey) { + + String hori = gunkey.concat(".hori"); + String HORI = REDIS.getCacheObject(hori); + if (HORI != null) { + return true; + } + REDIS.setCacheObject(hori, "protection film", 300L, TimeUnit.MILLISECONDS); + return false; + } }