From 4665e59741fe84769f1e8e9116135971247bc985 Mon Sep 17 00:00:00 2001 From: ZZ Date: Mon, 26 Jul 2021 18:18:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1:=E6=A1=A9=E5=8F=B7=E7=99=BD?= =?UTF-8?q?=E5=90=8D=E5=8D=95,=E7=94=B5=E7=AB=99=E8=B4=B9=E7=8E=87?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=AE=BE=E7=BD=AE/=E4=BF=AE=E6=94=B9(?= =?UTF-8?q?=E5=8F=82=E8=80=83showDoc=E6=96=B0=E5=A2=9E=E6=96=87=E6=A1=A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xhpc/common/api/PowerPileService.java | 15 ++++++- .../com/xhpc/common/data/redis/CachePile.java | 39 +++++++++++++++++++ .../xhpc/common/data/redis/CacheStation.java | 39 +++++++++++++++++++ .../com/xhpc/pp/config/EarlierBeanConf.java | 29 ++++++++++++++ .../pp/controller/ChargingController.java | 2 +- .../xhpc/pp/controller/PileController.java | 31 +++++++++++++++ .../xhpc/pp/controller/StationController.java | 25 ++++++++++++ .../java/com/xhpc/pp/logic/RegisterLogic.java | 18 +++------ .../pp/server/ChargingPileBinaryHandler.java | 26 ++++++++----- .../pp/server/ChargingPileEventHandler.java | 7 +++- .../xhpc/pp/server/ChargingPileServer.java | 24 ++++-------- .../src/main/resources/bootstrap.yml | 2 +- 12 files changed, 214 insertions(+), 43 deletions(-) create mode 100644 xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CachePile.java create mode 100644 xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheStation.java create mode 100644 xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/PileController.java create mode 100644 xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java 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 c9cc637e..8bf767ed 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 @@ -7,13 +7,26 @@ import com.xhpc.common.data.down.StartChargingData; import com.xhpc.common.data.up.OrderData; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import java.util.Set; + @FeignClient(contextId = "powerPileService", value = ServiceNameConstants.PILE_SERVICE, fallbackFactory = PowerPileFallbackFactory.class) public interface PowerPileService { - @PostMapping(value = "/pile/charging/order") + @PostMapping(value = "/charging/order") R startCharging(@Validated @RequestBody StartChargingData startChargingData); + @PostMapping(value = "/pile/whitelist") + R addPileWhitelist(@RequestBody Set whitelist); + + @DeleteMapping(value = "/pile/whitelist/{pileNo}") + R deletePileWhitelist(@PathVariable("pileNo") String pileNo); + + @DeleteMapping(value = "/station/rateModel/{stationId}/{rateModelId}") + R setStationRateModelId(@PathVariable("stationId") Long stationId, @PathVariable("rateModelId") Long rateModelId); + } diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CachePile.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CachePile.java new file mode 100644 index 00000000..59a638c1 --- /dev/null +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CachePile.java @@ -0,0 +1,39 @@ +package com.xhpc.common.data.redis; + +public class CachePile { + + private Long chargingStationId; + private Long rateModelId; + private Long pileNo; + + public Long getChargingStationId() { + + return chargingStationId; + } + + public void setChargingStationId(Long chargingStationId) { + + this.chargingStationId = chargingStationId; + } + + public Long getRateModelId() { + + return rateModelId; + } + + public void setRateModelId(Long rateModelId) { + + this.rateModelId = rateModelId; + } + + public Long getPileNo() { + + return pileNo; + } + + public void setPileNo(Long pileNo) { + + this.pileNo = pileNo; + } + +} diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheStation.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheStation.java new file mode 100644 index 00000000..d51807c0 --- /dev/null +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/data/redis/CacheStation.java @@ -0,0 +1,39 @@ +package com.xhpc.common.data.redis; + +public class CacheStation { + + private Long chargingPileId; + private Long chargingStationId; + private Long rateModelId; + + public Long getChargingPileId() { + + return chargingPileId; + } + + public void setChargingPileId(Long chargingPileId) { + + this.chargingPileId = chargingPileId; + } + + public Long getChargingStationId() { + + return chargingStationId; + } + + public void setChargingStationId(Long chargingStationId) { + + this.chargingStationId = chargingStationId; + } + + public Long getRateModelId() { + + return rateModelId; + } + + public void setRateModelId(Long rateModelId) { + + this.rateModelId = rateModelId; + } + +} diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/config/EarlierBeanConf.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/config/EarlierBeanConf.java index 82125871..d5011592 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/config/EarlierBeanConf.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/config/EarlierBeanConf.java @@ -1,10 +1,19 @@ package com.xhpc.pp.config; +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.xhpc.pp.logic.RegisterLogic; import com.xhpc.pp.utils.SpringContextHolder; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; +import java.util.List; +import java.util.Map; + import static com.ruoyi.common.core.utils.GetIpAndPort.getLocalIP; +import static com.xhpc.pp.server.ChargingPileServer.REDIS; @Configuration public class EarlierBeanConf { @@ -20,6 +29,26 @@ public class EarlierBeanConf { this.springContextHolder = springContextHolder; } + public static boolean ifreg(String pileNo) throws NacosException { + + Map cachePile = REDIS.getCacheMap("pile:".concat(pileNo)); + String server = (String) cachePile.get("server"); + if (server != null) { + String nacosServer = REDIS.getCacheObject("nacos").toString(); + NamingService namingService = NacosFactory.createNamingService(nacosServer); + List ppInstances = namingService.getAllInstances("xhpc-power-pile"); + String status = (String) cachePile.get("status"); + for (Instance i : ppInstances) { + if (i.getIp().concat("#").concat(Integer.valueOf(i.getPort()).toString()).equals(server)) { + if (!RegisterLogic.DISCONNECTED.equals(status)) { + return false; + } + } + } + } + return true; + } + @Value("${server.port}") public void setPort(String port) { 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 172aad4f..cf0dcd77 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 @@ -23,7 +23,7 @@ public class ChargingController { return r; } - @PostMapping("pile/charging/order") + @PostMapping("charging/order") public Object startCharging(@Validated @RequestBody StartChargingData startChargingData) { return new OrderData(); 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 new file mode 100644 index 00000000..ce6849e7 --- /dev/null +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/PileController.java @@ -0,0 +1,31 @@ +package com.xhpc.pp.controller; + +import com.ruoyi.common.core.domain.R; +import org.springframework.web.bind.annotation.*; + +import java.util.Set; + +import static com.xhpc.pp.server.ChargingPileServer.REDIS; + +@RestController +public class PileController { + + @PostMapping("pile/whitelist") + public Object addWhitelist(@RequestBody Set whitelist) { + + Set cacheWhitelist = REDIS.getCacheSet("PILE_WHITELIST"); + cacheWhitelist.addAll(whitelist); + REDIS.setCacheSet("PILE_WHITELIST", cacheWhitelist); + return R.ok(); + } + + @DeleteMapping("pile/whitelist/{pileNo}") + public Object deleteWhitelist(@PathVariable String pileNo) { + + Set cacheWhitelist = REDIS.getCacheSet("PILE_WHITELIST"); + cacheWhitelist.remove(pileNo); + REDIS.setCacheSet("PILE_WHITELIST", cacheWhitelist); + return R.ok(); + } + +} diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java new file mode 100644 index 00000000..62c45718 --- /dev/null +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/controller/StationController.java @@ -0,0 +1,25 @@ +package com.xhpc.pp.controller; + +import com.ruoyi.common.core.domain.R; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +import static com.xhpc.pp.server.ChargingPileServer.REDIS; + +@RestController +public class StationController { + + @PostMapping("station/rateModel/{stationId}/{rateModelId}") + public Object setStationRateModel(@PathVariable Long stationId, @PathVariable Long rateModelId) { + + String skey = "station:".concat(stationId.toString()); + Map cacheStation = REDIS.getCacheMap(skey); + cacheStation.put("rateModelId", rateModelId); + REDIS.setCacheObject(skey, cacheStation); + return R.ok(); + } + +} 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 54b774b6..29a54aaa 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 @@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; -import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -31,23 +30,18 @@ public class RegisterLogic implements ServiceLogic { String resultCode = ServiceResult.OK; String hexCode = ServiceResult.HEX_OK; String pileNo = (String) req.get("pileNo"); - Set pileSnPool = REDIS.getCacheSet("PILE_SN_POOL"); -// ? = REDIS.getCacheSet(pileNo); todo - //todo set rate model to cache - if (!pileSnPool.contains(pileNo)) { + Set whitelist = REDIS.getCacheSet("PILE_WHITELIST"); + if (!whitelist.contains(pileNo)) { hexCode = ServiceResult.HEX_FAIL; resultCode = ServiceResult.FAIL; } int gunNum = Integer.parseInt(req.get("gunNum").toString()); for (int gunN = 1; gunN <= gunNum; gunN++) { String gunId = String.format("%02d", (int) gunN); - Map cacheGun = REDIS.getCacheMap(pileNo.concat(gunId)); - if (cacheGun == null) { - cacheGun = new HashMap<>(); - } - int seq = (int) cacheGun.getOrDefault("seq", 0); - cacheGun.put("seq", seq); - REDIS.setCacheMap(pileNo.concat(gunId), cacheGun); + String pileGun = "gun:".concat(pileNo.concat(gunId)); + Map cacheGun = REDIS.getCacheMap(pileGun); + cacheGun.put("seq", 0); + REDIS.setCacheMap(pileGun, cacheGun); } String resultStr = "680C00000002".concat(pileNo).concat(hexCode); resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr)); 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 0a6a1466..6a8427f0 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 @@ -1,5 +1,7 @@ package com.xhpc.pp.server; +import com.alibaba.nacos.api.exception.NacosException; +import com.xhpc.pp.config.EarlierBeanConf; import com.xhpc.pp.domain.ServiceField; import com.xhpc.pp.logic.FieldLogic; import com.xhpc.pp.logic.ServiceMainLogic; @@ -17,7 +19,6 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.*; -import static com.ruoyi.common.core.utils.GetIpAndPort.getLocalIP; import static com.xhpc.pp.config.EarlierBeanConf.getLocalIPAndPort; import static com.xhpc.pp.logic.RegisterLogic.REGISTERED; import static com.xhpc.pp.server.ChargingPileServer.REDIS; @@ -68,12 +69,12 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { } process(handler, d); } - } catch (TxException e) { + } catch (TxException | NacosException e) { log.error(e.getMessage(), e); } } - private void process(ClientHandler handler, byte[] data) throws TxException, IOException { + private void process(ClientHandler handler, byte[] data) throws TxException, IOException, NacosException { String serviceName = HexUtils.toHex(data, 5, 6); String version = ChargingPileServer.getVersion(handler.getName()); @@ -95,17 +96,24 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler { } } - private void reg(ClientHandler handler, String pileNo, Map req) { + private void reg(ClientHandler handler, String pileNo, Map req) throws NacosException { + if (!EarlierBeanConf.ifreg(pileNo)) { + log.info("pile already registered >>>> [{}] ", pileNo); + return; + } ChargingPileServer.putHandler(pileNo, handler); ChargingPileServer.putVersion(handler.getName(), (String) req.get("version")); - Map pileCache = REDIS.getCacheMap(pileNo); + String pileKey = "pile:".concat(pileNo); + Map pileCache = REDIS.getCacheMap(pileKey); pileCache.put("status", REGISTERED); pileCache.put("server", getLocalIPAndPort()); - REDIS.setCacheMap(pileNo, pileCache); - Set pilesAtHost = REDIS.getCacheSet(getLocalIP()); - pilesAtHost.add(pileNo); - log.info("pile registered >>>> [{}] ", pileNo); + REDIS.setCacheMap(pileKey, pileCache); + String svcKey = "ppSvcSrv:".concat(getLocalIPAndPort()); + Set svcPiles = REDIS.getCacheSet(svcKey); + svcPiles.add(pileKey); + REDIS.setCacheSet(svcKey, svcPiles); + log.info("pile registering >>>> [{}] ", pileNo); } private List parseDataList(byte[] data) { 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 4a0e2a68..7a3c2ea0 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,16 +27,19 @@ public class ChargingPileEventHandler implements ClientEventHandler { public void lostConnection(ClientHandler handler) { String pileNo = ChargingPileServer.getPileNo(handler); - log.info("lost connection -> [{}] {} <- {}", - pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString()); if (pileNo != null) { ChargingPileServer.removeHandler(pileNo); } + log.info("lost connection -> [{}] {} <- {}", + pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString()); } @Override public void closingConnection(ClientHandler handler) { + String pileNo = ChargingPileServer.getPileNo(handler); + ChargingPileServer.removeHandler(pileNo); + handler.closeConnection(); log.info("closing connection -> " + handler.getName() + " <-"); } 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 704bb7f7..4ef3b8be 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 @@ -19,9 +19,7 @@ import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.IOException; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED; @@ -50,13 +48,6 @@ public class ChargingPileServer { public void init() { REDIS = redisService; - - REDIS.deleteObject("PILE_SN_POOL"); - Set PILE_SN_POOL = new HashSet<>(); - //todo add pile sn whitelist - PILE_SN_POOL.add("55031412782305"); - REDIS.setCacheSet("PILE_SN_POOL", PILE_SN_POOL); - } @Autowired @@ -96,9 +87,10 @@ public class ChargingPileServer { public static void removeHandler(String pileNo) { ClientHandler handler = handlerMap.remove(pileNo); - Map cacheMap = REDIS.getCacheMap(pileNo); - cacheMap.put("Status", DISCONNECTED); - REDIS.setCacheMap(pileNo, cacheMap); + String pileKey = "pile:".concat(pileNo); + Map cacheMap = REDIS.getCacheMap(pileKey); + cacheMap.put("status", DISCONNECTED); + REDIS.setCacheMap(pileKey, cacheMap); if (handler != null) { log.info("remove handler [{}] for [{}]", handler.getName(), pileNo); pileMap.remove(handler.getName()); @@ -108,12 +100,10 @@ public class ChargingPileServer { public static void sendClientMsg(String pileNo, byte[] msg) { - if (!HexUtils.toHex(msg).startsWith("00101005")) { - log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(msg)); - } - if (pileNo.length() < 16) { + log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(msg)); + if (pileNo.length() < 14) { pileNo = "0000000000000000" + pileNo; - pileNo = pileNo.substring(pileNo.length() - 16); + pileNo = pileNo.substring(pileNo.length() - 14); } ClientHandler handler = handlerMap.get(pileNo); if (handler == null || !handler.isOpen()) { diff --git a/xhpc-modules/xhpc-power-pile/src/main/resources/bootstrap.yml b/xhpc-modules/xhpc-power-pile/src/main/resources/bootstrap.yml index 8f5496ea..8c12f14b 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/resources/bootstrap.yml +++ b/xhpc-modules/xhpc-power-pile/src/main/resources/bootstrap.yml @@ -4,7 +4,7 @@ ppsvc: # Tomcat server: - port: ${random.int(1300,1400)} + port: 1300 # Spring spring: