服务:桩号白名单,电站费率模型设置/修改(参考showDoc新增文档)

This commit is contained in:
ZZ 2021-07-26 18:18:11 +08:00
parent 838ed627ba
commit 4665e59741
12 changed files with 214 additions and 43 deletions

View File

@ -7,13 +7,26 @@ import com.xhpc.common.data.down.StartChargingData;
import com.xhpc.common.data.up.OrderData; import com.xhpc.common.data.up.OrderData;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.annotation.Validated; 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import java.util.Set;
@FeignClient(contextId = "powerPileService", value = ServiceNameConstants.PILE_SERVICE, fallbackFactory = PowerPileFallbackFactory.class) @FeignClient(contextId = "powerPileService", value = ServiceNameConstants.PILE_SERVICE, fallbackFactory = PowerPileFallbackFactory.class)
public interface PowerPileService { public interface PowerPileService {
@PostMapping(value = "/pile/charging/order") @PostMapping(value = "/charging/order")
R<OrderData> startCharging(@Validated @RequestBody StartChargingData startChargingData); R<OrderData> startCharging(@Validated @RequestBody StartChargingData startChargingData);
@PostMapping(value = "/pile/whitelist")
R addPileWhitelist(@RequestBody Set<String> 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);
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,10 +1,19 @@
package com.xhpc.pp.config; 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 com.xhpc.pp.utils.SpringContextHolder;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; 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.ruoyi.common.core.utils.GetIpAndPort.getLocalIP;
import static com.xhpc.pp.server.ChargingPileServer.REDIS;
@Configuration @Configuration
public class EarlierBeanConf { public class EarlierBeanConf {
@ -20,6 +29,26 @@ public class EarlierBeanConf {
this.springContextHolder = springContextHolder; this.springContextHolder = springContextHolder;
} }
public static boolean ifreg(String pileNo) throws NacosException {
Map<String, Object> 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<Instance> 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}") @Value("${server.port}")
public void setPort(String port) { public void setPort(String port) {

View File

@ -23,7 +23,7 @@ public class ChargingController {
return r; return r;
} }
@PostMapping("pile/charging/order") @PostMapping("charging/order")
public Object startCharging(@Validated @RequestBody StartChargingData startChargingData) { public Object startCharging(@Validated @RequestBody StartChargingData startChargingData) {
return new OrderData(); return new OrderData();

View File

@ -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<String> whitelist) {
Set<String> 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<String> cacheWhitelist = REDIS.getCacheSet("PILE_WHITELIST");
cacheWhitelist.remove(pileNo);
REDIS.setCacheSet("PILE_WHITELIST", cacheWhitelist);
return R.ok();
}
}

View File

@ -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<String, Object> cacheStation = REDIS.getCacheMap(skey);
cacheStation.put("rateModelId", rateModelId);
REDIS.setCacheObject(skey, cacheStation);
return R.ok();
}
}

View File

@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -31,23 +30,18 @@ public class RegisterLogic implements ServiceLogic {
String resultCode = ServiceResult.OK; String resultCode = ServiceResult.OK;
String hexCode = ServiceResult.HEX_OK; String hexCode = ServiceResult.HEX_OK;
String pileNo = (String) req.get("pileNo"); String pileNo = (String) req.get("pileNo");
Set<String> pileSnPool = REDIS.getCacheSet("PILE_SN_POOL"); Set<String> whitelist = REDIS.getCacheSet("PILE_WHITELIST");
// ? = REDIS.getCacheSet(pileNo); todo if (!whitelist.contains(pileNo)) {
//todo set rate model to cache
if (!pileSnPool.contains(pileNo)) {
hexCode = ServiceResult.HEX_FAIL; hexCode = ServiceResult.HEX_FAIL;
resultCode = ServiceResult.FAIL; resultCode = ServiceResult.FAIL;
} }
int gunNum = Integer.parseInt(req.get("gunNum").toString()); int gunNum = Integer.parseInt(req.get("gunNum").toString());
for (int gunN = 1; gunN <= gunNum; gunN++) { for (int gunN = 1; gunN <= gunNum; gunN++) {
String gunId = String.format("%02d", (int) gunN); String gunId = String.format("%02d", (int) gunN);
Map<String, Object> cacheGun = REDIS.getCacheMap(pileNo.concat(gunId)); String pileGun = "gun:".concat(pileNo.concat(gunId));
if (cacheGun == null) { Map<String, Object> cacheGun = REDIS.getCacheMap(pileGun);
cacheGun = new HashMap<>(); cacheGun.put("seq", 0);
} REDIS.setCacheMap(pileGun, cacheGun);
int seq = (int) cacheGun.getOrDefault("seq", 0);
cacheGun.put("seq", seq);
REDIS.setCacheMap(pileNo.concat(gunId), cacheGun);
} }
String resultStr = "680C00000002".concat(pileNo).concat(hexCode); String resultStr = "680C00000002".concat(pileNo).concat(hexCode);
resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr)); resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));

View File

@ -1,5 +1,7 @@
package com.xhpc.pp.server; 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.domain.ServiceField;
import com.xhpc.pp.logic.FieldLogic; import com.xhpc.pp.logic.FieldLogic;
import com.xhpc.pp.logic.ServiceMainLogic; import com.xhpc.pp.logic.ServiceMainLogic;
@ -17,7 +19,6 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.*; 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.config.EarlierBeanConf.getLocalIPAndPort;
import static com.xhpc.pp.logic.RegisterLogic.REGISTERED; import static com.xhpc.pp.logic.RegisterLogic.REGISTERED;
import static com.xhpc.pp.server.ChargingPileServer.REDIS; import static com.xhpc.pp.server.ChargingPileServer.REDIS;
@ -68,12 +69,12 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
} }
process(handler, d); process(handler, d);
} }
} catch (TxException e) { } catch (TxException | NacosException e) {
log.error(e.getMessage(), 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 serviceName = HexUtils.toHex(data, 5, 6);
String version = ChargingPileServer.getVersion(handler.getName()); String version = ChargingPileServer.getVersion(handler.getName());
@ -95,17 +96,24 @@ public class ChargingPileBinaryHandler implements ClientBinaryHandler {
} }
} }
private void reg(ClientHandler handler, String pileNo, Map<String, Object> req) { private void reg(ClientHandler handler, String pileNo, Map<String, Object> req) throws NacosException {
if (!EarlierBeanConf.ifreg(pileNo)) {
log.info("pile already registered >>>> [{}] ", pileNo);
return;
}
ChargingPileServer.putHandler(pileNo, handler); ChargingPileServer.putHandler(pileNo, handler);
ChargingPileServer.putVersion(handler.getName(), (String) req.get("version")); ChargingPileServer.putVersion(handler.getName(), (String) req.get("version"));
Map<String, Object> pileCache = REDIS.getCacheMap(pileNo); String pileKey = "pile:".concat(pileNo);
Map<String, Object> pileCache = REDIS.getCacheMap(pileKey);
pileCache.put("status", REGISTERED); pileCache.put("status", REGISTERED);
pileCache.put("server", getLocalIPAndPort()); pileCache.put("server", getLocalIPAndPort());
REDIS.setCacheMap(pileNo, pileCache); REDIS.setCacheMap(pileKey, pileCache);
Set<String> pilesAtHost = REDIS.getCacheSet(getLocalIP()); String svcKey = "ppSvcSrv:".concat(getLocalIPAndPort());
pilesAtHost.add(pileNo); Set<String> svcPiles = REDIS.getCacheSet(svcKey);
log.info("pile registered >>>> [{}] ", pileNo); svcPiles.add(pileKey);
REDIS.setCacheSet(svcKey, svcPiles);
log.info("pile registering >>>> [{}] ", pileNo);
} }
private List<byte[]> parseDataList(byte[] data) { private List<byte[]> parseDataList(byte[] data) {

View File

@ -27,16 +27,19 @@ public class ChargingPileEventHandler implements ClientEventHandler {
public void lostConnection(ClientHandler handler) { public void lostConnection(ClientHandler handler) {
String pileNo = ChargingPileServer.getPileNo(handler); String pileNo = ChargingPileServer.getPileNo(handler);
log.info("lost connection -> [{}] {} <- {}",
pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString());
if (pileNo != null) { if (pileNo != null) {
ChargingPileServer.removeHandler(pileNo); ChargingPileServer.removeHandler(pileNo);
} }
log.info("lost connection -> [{}] {} <- {}",
pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString());
} }
@Override @Override
public void closingConnection(ClientHandler handler) { public void closingConnection(ClientHandler handler) {
String pileNo = ChargingPileServer.getPileNo(handler);
ChargingPileServer.removeHandler(pileNo);
handler.closeConnection();
log.info("closing connection -> " + handler.getName() + " <-"); log.info("closing connection -> " + handler.getName() + " <-");
} }

View File

@ -19,9 +19,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED; import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED;
@ -50,13 +48,6 @@ public class ChargingPileServer {
public void init() { public void init() {
REDIS = redisService; REDIS = redisService;
REDIS.deleteObject("PILE_SN_POOL");
Set<String> PILE_SN_POOL = new HashSet<>();
//todo add pile sn whitelist
PILE_SN_POOL.add("55031412782305");
REDIS.setCacheSet("PILE_SN_POOL", PILE_SN_POOL);
} }
@Autowired @Autowired
@ -96,9 +87,10 @@ public class ChargingPileServer {
public static void removeHandler(String pileNo) { public static void removeHandler(String pileNo) {
ClientHandler handler = handlerMap.remove(pileNo); ClientHandler handler = handlerMap.remove(pileNo);
Map<String, Object> cacheMap = REDIS.getCacheMap(pileNo); String pileKey = "pile:".concat(pileNo);
cacheMap.put("Status", DISCONNECTED); Map<String, Object> cacheMap = REDIS.getCacheMap(pileKey);
REDIS.setCacheMap(pileNo, cacheMap); cacheMap.put("status", DISCONNECTED);
REDIS.setCacheMap(pileKey, cacheMap);
if (handler != null) { if (handler != null) {
log.info("remove handler [{}] for [{}]", handler.getName(), pileNo); log.info("remove handler [{}] for [{}]", handler.getName(), pileNo);
pileMap.remove(handler.getName()); pileMap.remove(handler.getName());
@ -108,12 +100,10 @@ public class ChargingPileServer {
public static void sendClientMsg(String pileNo, byte[] msg) { public static void sendClientMsg(String pileNo, byte[] msg) {
if (!HexUtils.toHex(msg).startsWith("00101005")) {
log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(msg)); log.info("server send msg >>>> [{}] |{}|", pileNo, HexUtils.toHex(msg));
} if (pileNo.length() < 14) {
if (pileNo.length() < 16) {
pileNo = "0000000000000000" + pileNo; pileNo = "0000000000000000" + pileNo;
pileNo = pileNo.substring(pileNo.length() - 16); pileNo = pileNo.substring(pileNo.length() - 14);
} }
ClientHandler handler = handlerMap.get(pileNo); ClientHandler handler = handlerMap.get(pileNo);
if (handler == null || !handler.isOpen()) { if (handler == null || !handler.isOpen()) {

View File

@ -4,7 +4,7 @@ ppsvc:
# Tomcat # Tomcat
server: server:
port: ${random.int(1300,1400)} port: 1300
# Spring # Spring
spring: spring: