From 75b6fbd0781ac55cabb576335a4508188dac3b9f Mon Sep 17 00:00:00 2001 From: ZZ Date: Sat, 7 May 2022 14:33:57 +0800 Subject: [PATCH] manual config time n rate model --- .../com/xhpc/common/api/PowerPileService.java | 3 + .../api/factory/PowerPileFallbackFactory.java | 6 ++ .../pp/controller/ChargingController.java | 8 +- .../xhpc/pp/controller/PileController.java | 96 +++++++++++++++---- .../logic/PileTimeConfigReplyDataLogic.java | 2 +- .../logic/RateModelConfigReplyDataLogic.java | 5 + .../pp/server/ChargingPileBinaryHandler.java | 11 ++- 7 files changed, 101 insertions(+), 30 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 97530d2b..66232819 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 @@ -40,4 +40,7 @@ public interface PowerPileService { @PathVariable("rateModelId") @Param("rateModelId") Long rateModelId, @RequestBody @Param("rateModel") CacheRateModel rateModel); + @PostMapping("/pile/{pileNo}/timeNRateModel") + R configTimeNRateModel(@PathVariable("pileNo") String pileNo); + } 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 7c29e046..793c8598 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 @@ -57,6 +57,12 @@ public class PowerPileFallbackFactory implements FallbackFactory getRR(String response) { + public static R getRR(String response) { R r; JSONObject responseJson = (JSONObject) JSON.parse(response); if (responseJson == null) { - r = R.fail("Native服务调用失败"); + r = R.fail("原生服务调用失败"); } else { int code = responseJson.getInteger("code"); log.debug(response); @@ -133,7 +133,7 @@ public class ChargingController { return r; } - private R checkPile(Map cachePile) { + public static R checkPile(Map cachePile) { if (cachePile == null) { return R.fail("充电桩未注册"); @@ -186,7 +186,7 @@ public class ChargingController { return r; } - private String fmt(String svcSrv) { + public static String fmt(String svcSrv) { String[] split = svcSrv.split("#"); return "http://".concat(split[0]).concat(":").concat(split[1]); 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 96319ea3..3f0e102b 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 @@ -1,17 +1,24 @@ package com.xhpc.pp.controller; +import cn.hutool.core.date.DateUtil; import com.xhpc.common.api.dto.ChargingStationDto; import com.xhpc.common.core.domain.R; +import com.xhpc.common.core.utils.HttpUtils; +import com.xhpc.pp.logic.RateModelRequestLogic; import com.xhpc.pp.utils.HexUtils; import lombok.extern.slf4j.Slf4j; 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 java.util.*; +import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; +import static cn.hutool.core.util.NumberUtil.isInteger; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; +import static com.xhpc.common.data.redis.StaticBeanUtil.seqHex; +import static com.xhpc.pp.controller.ChargingController.*; +import static com.xhpc.pp.server.ChargingPileBinaryHandler.getTimeBin; import static com.xhpc.pp.server.ChargingPileServer.getHandler; import static com.xhpc.pp.server.ChargingPileServer.removeHandler; @@ -47,47 +54,96 @@ public class PileController { cachePile.put("version", version); REDIS.setCacheMap(pkey, cachePile); } + log.info("station [{}] pile whitelist add: [{}]", stationId, Arrays.toString(pileNoSet.toArray())); return R.ok(); } @DeleteMapping("pile/whitelist/delete/{stationId}") public Object deleteWhitelist(@PathVariable("stationId") Long stationId, @RequestBody Set pileNoSet) { -// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); -// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); REDIS.deleteSetVal("PILE_WHITELIST", pileNoSet.toArray()); String stationKey = "station:".concat(stationId.toString()); -// System.out.println(">>>>>>>>>>>>>>>>>>pileNoSet.toArray()>>>>>>>>>>>>>>>>"+stationKey); ChargingStationDto cacheStation = REDIS.getCacheObject(stationKey); cacheStation.getPiles().removeAll(pileNoSet); REDIS.setCacheObject(stationKey, cacheStation); + log.info("station [{}] pile whitelist delete: [{}]", stationId, Arrays.toString(pileNoSet.toArray())); return R.ok(); } - @PostMapping("native/pile/{pileNo}/rateModel") - public Object configRateModel(@PathVariable("pileNo") String pileNo, @RequestBody String msg) { + + @PostMapping("pile/{pileNo}/timeNRateModel") + public R configTimeNRateModel(@PathVariable("pileNo") String pileNo) { + + String pkey = "pile:".concat(pileNo); + Map cachePile = REDIS.getCacheMap(pkey); + R r = checkPile(cachePile); + if (r.getCode() == 200) { + String svcSrv = (String) cachePile.get("svcSrv"); + String response = HttpUtils.post(fmt(svcSrv).concat("/native/pile/").concat(pileNo).concat("/timeNRateModel")); + r = getRR(response); + } + return r; + } + + + @PostMapping("native/pile/{pileNo}/timeNRateModel") + public R nativeConfigTimeNRateModel(@PathVariable("pileNo") String pileNo) { ClientHandler handler = getHandler(pileNo); - R r; + R r = R.fail("校时校费下发失败,充电桩未注册."); if (handler != null && handler.isOpen()) { if (!handler.isOpen()) { log.error("send message failed. [{}]({}) connection lost", handler.getName(), pileNo); removeHandler(pileNo); - r = R.fail("充电桩连接已断开,请稍后再试"); + r = R.fail("充电桩连接已断开,请稍后再试."); } else { - try { - log.debug("[{}] - server send msg >>>> ({}) |{}|", handler.getName(), pileNo, msg); - handler.sendClientBinary(HexUtils.toBytes(msg)); - r = R.ok(null, "网络延迟,请重试"); - } catch (IOException e) { - log.error("费率模型下发失败[{}]:".concat(e.getMessage()).concat(".无法启动充电"), pileNo); - removeHandler(pileNo); - r = R.fail("电桩网络通信失败,请重试"); + Collection stationKeys = REDIS.keys("station:*"); + for (String stationKey : stationKeys) { + ChargingStationDto stationDto = REDIS.getCacheObject(stationKey); + Long rateModelIdStation = stationDto.getRateModelId(); + if (rateModelIdStation != null) { + Set pns = stationDto.getPiles(); + if (pns != null) for (String pn : pns) { + if (pn.equals(pileNo)) { + String pk = "pile:".concat(pileNo); + Long rateModelIdPile = REDIS.getCacheMapValue(pk, "rateModelId"); + if (!rateModelIdStation.equals(rateModelIdPile)) { + String gkPattern = ("gun:").concat(pileNo).concat("*"); + Collection gks = REDIS.keys(gkPattern); + boolean charging = false; + for (String gk : gks) { + if (gk.length() == 20 && isInteger(REDIS.getCacheMapValue(gk, "status"))) { + charging = true; + break; + } + } + if (!charging) { + String rsmsg = RateModelRequestLogic.translate(pileNo, rateModelIdStation, "0058", + REDIS.getCacheMapValue(pk, "version")); + try { + handler.sendClientBinary(HexUtils.toBytes(rsmsg)); + log.info("pile[{}] rate model[{}] sent ⚪ {}", pileNo, rateModelIdStation, rsmsg); + String skey = pk.concat(".seqhex"); + Thread.sleep(500); + Date date = Calendar.getInstance().getTime(); + String timebin = getTimeBin(seqHex(skey), pileNo, date); + handler.sendClientBinary(HexUtils.toBytes(timebin)); + REDIS.setCacheMapValue(pk, "manConfTime", DateUtil.format(date, + NORM_DATETIME_FORMAT)); + r = R.fail("校时校费下发成功."); + } catch (IOException | InterruptedException e) { + r = R.fail("校时|校费下发失败,请重试."); + } + } else { + r = R.fail("校时校费下发失败,充电桩在充电中."); + } + } + break; + } + } + } } } - } else { - log.error("费率模型下发失败[{}].无法启动充电", pileNo); - r = R.fail("启动失败,请稍后重试"); } return r; } diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java index 8869294c..f8859d4c 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/PileTimeConfigReplyDataLogic.java @@ -40,7 +40,7 @@ public class PileTimeConfigReplyDataLogic implements ServiceLogic { String pk = "pile:".concat(pileNo); String configTime = DateUtil.format(cp56toDate(pileTimeConfigReplyData.getSetTime()), NORM_DATETIME_FORMAT); Map cachePile = REDIS.getCacheMap(pk); - cachePile.put("configTime", configTime); + cachePile.put("configTimeReply", configTime); REDIS.setCacheMap(pk, cachePile); log.debug("({}) set time success√: [{}]", pileNo, configTime); String remark = "充电桩同步时钟"; diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelConfigReplyDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelConfigReplyDataLogic.java index ebdcd499..812f430b 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelConfigReplyDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RateModelConfigReplyDataLogic.java @@ -1,5 +1,6 @@ package com.xhpc.pp.logic; +import cn.hutool.core.date.DateUtil; import com.fasterxml.jackson.databind.ObjectMapper; import com.xhpc.common.api.dto.ChargingStationDto; import com.xhpc.common.data.up.PileConfigReplyData; @@ -15,8 +16,10 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.Calendar; import java.util.Map; +import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; import static com.xhpc.pp.tx.ServiceResult.*; @@ -53,6 +56,8 @@ public class RateModelConfigReplyDataLogic implements ServiceLogic { cachePile.put("rateModelId", cacheStation.getRateModelId()); result = OK; } + String configTime = DateUtil.format(Calendar.getInstance().getTime(), NORM_DATETIME_FORMAT); + cachePile.put("configRmReply", configTime); REDIS.setCacheMap(pkey, cachePile); } 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 d9d6da81..a673b72c 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 @@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.*; +import java.util.concurrent.TimeUnit; import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; @@ -132,13 +133,14 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { if (SERVICE_HB.equals(serviceName)) { if (OK.equals(resultCode)) { pileNo = ChargingPileServer.getPileNo(handler); - Boolean tcfg = (Boolean) cachePile.get("tcfg"); + String tcfgkey = "pile:".concat(pileNo).concat(".tcfg"); + Boolean tcfg = REDIS.getCacheObject(tcfgkey); if (tcfg == null || !tcfg) { String skey = pilekey.concat(".seqhex"); Thread.sleep(500); Date date = Calendar.getInstance().getTime(); String timebin = getTimeBin(seqHex(skey), pileNo, date); - cachePile.put("configTime", DateUtil.format(date, NORM_DATETIME_FORMAT)); + cachePile.put("hbConfTime", DateUtil.format(date, NORM_DATETIME_FORMAT)); REDIS.setCacheMap(pilekey, cachePile); log.debug("server send time config msg >>>> ({}) |{}|", pileNo, timebin); if (!handler.isOpen()) { @@ -149,8 +151,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { if (gunId != null) { hori2(pileNo, gunId); } - cachePile.put("tcfg", true); - REDIS.setCacheMap(pilekey, cachePile); + REDIS.setCacheObject(tcfgkey, true, 1L, TimeUnit.DAYS); } } } else { @@ -161,7 +162,7 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { } } - private String getTimeBin(String seqhex, String pileNo, Date date) { + public static String getTimeBin(String seqhex, String pileNo, Date date) { String timebin = "6812".concat(seqhex).concat("0056").concat(pileNo).concat(toCp56Hex(date)); timebin = timebin.concat(CRCCalculator.calcCrc(timebin));