diff --git a/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/dto/ConnectorStatusInfo.java b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/dto/ConnectorStatusInfo.java new file mode 100644 index 00000000..155ed190 --- /dev/null +++ b/xhpc-modules/xhpc-common/src/main/java/com/xhpc/common/dto/ConnectorStatusInfo.java @@ -0,0 +1,21 @@ +package com.xhpc.common.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ConnectorStatusInfo { + + public final static int OFF_LINE = 0;//离网 + public final static int FREE = 1;//空闲 + public final static int CONNECTED = 2;//占用(未充电) + public final static int CHARGING = 3;//占用(充电中) + public final static int BOOKED = 4;//占用(预约锁定) + public final static int ERROR = 255;//故障 + + private String connectorID; + private String operatorID; + private Integer status; + +} diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/XhpcPPApplication.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/XhpcPPApplication.java index 4d183115..42503d3a 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/XhpcPPApplication.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/XhpcPPApplication.java @@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ImportResource; +import org.springframework.scheduling.annotation.EnableScheduling; /** * 充电桩服务 @@ -14,6 +15,7 @@ import org.springframework.context.annotation.ImportResource; @EnableFeignClients @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @ImportResource(locations = {"classpath:svcmainlogic.xml"}) +@EnableScheduling public class XhpcPPApplication { public static void main(String[] args) { diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/HBLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/HBLogic.java index 09bcc386..2f4589c5 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/HBLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/HBLogic.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; +import java.util.Calendar; import java.util.Map; import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; @@ -30,10 +31,12 @@ public class HBLogic implements ServiceLogic { String pileNo = (String) req.get("pileNo"); String gunId = (String) req.get("gunId"); String gunStatus = (String) req.get("gunStatus"); - int gunStatusInt = Integer.parseInt(gunStatus); + Long gunStatusInt = Long.parseLong(gunStatus); String gunkey = "gun:".concat(pileNo).concat(gunId); - Map cacheGun = REDIS.getCacheMap(gunkey); - cacheGun.put("status", stable[gunStatusInt]); + Map cacheGun = REDIS.getCacheMap(gunkey); + cacheGun.put("status", stable[gunStatusInt.intValue()]); + cacheGun.put("statusInt", gunStatusInt); + cacheGun.put("hbtime", String.valueOf(Calendar.getInstance().getTimeInMillis())); REDIS.setCacheMap(gunkey, cacheGun); String skey = gunkey.concat(".seqhex"); String seq = seqHex(skey); diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java index 5eb898f0..56438e1d 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/logic/RealtimeDataLogic.java @@ -60,7 +60,8 @@ public class RealtimeDataLogic implements ServiceLogic { String terminalId = pileNo.concat(gunId); String gunkey = "gun:".concat(terminalId); Map cacheGun = REDIS.getCacheMap(gunkey); - String statusplain = stable[Integer.parseInt(realtimeData.getStatus())]; + int statusInt = Integer.parseInt(realtimeData.getStatus()); + String statusplain = stable[statusInt]; cacheGun.put("pileGunStatus", pvgstable[Integer.parseInt(realtimeData.getPileGunStatus())]); cacheGun.put("vehicleGunStatus", pvgstable[Integer.parseInt(realtimeData.getVehicleGunStatus())]); String hardwareFault = realtimeData.getHardwareFault(); @@ -86,6 +87,7 @@ public class RealtimeDataLogic implements ServiceLogic { statusOrSOC = statusplain; } Integer tr = reverseHexInt(realtimeData.getRemainingTime()); + cacheGun.put("statusInt", statusInt); cacheGun.put("status", statusOrSOC); cacheGun.put("remainingTime", tr); REDIS.setCacheMap(gunkey, cacheGun); 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 6bd02534..bee36709 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 @@ -1,5 +1,6 @@ package com.xhpc.pp.logic; +import com.xhpc.common.dto.ConnectorStatusInfo; import com.xhpc.pp.config.EarlierBeanConf; import com.xhpc.pp.tx.ServiceParameter; import com.xhpc.pp.tx.ServiceResult; @@ -56,10 +57,11 @@ public class RegisterLogic implements ServiceLogic { String gunkey = "gun:".concat(pileNo.concat(gunId)); Map cacheGun = REDIS.getCacheMap(gunkey); cacheGun.put("status", DISCONNECTED); + cacheGun.put("statusInt", ConnectorStatusInfo.OFF_LINE); cacheGun.put("svcSrv", localIPAndPort); REDIS.setCacheMap(gunkey, cacheGun); + cachePileGunSvcSrv(gunkey); } - cachePileGunSvcSrv(pileNo); log.info("pile (re)registered ({}) ", pileNo); } String skey = "pile:".concat(pileNo).concat(".seqhex"); @@ -69,11 +71,11 @@ public class RegisterLogic implements ServiceLogic { return new ServiceResult(HexUtils.toBytes(resultStr), resultCode); } - private void cachePileGunSvcSrv(String key) { + private void cachePileGunSvcSrv(String gunkey) { - String svcKey = "svcSrvPile:".concat(getLocalIPAndPort()); + String svcKey = "svcSrvGuns:".concat(getLocalIPAndPort()); Set svcPileGuns = REDIS.getCacheSet(svcKey); - svcPileGuns.add(key); + svcPileGuns.add(gunkey); REDIS.setCacheSet(svcKey, svcPileGuns); } 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 4e50fb28..560177f5 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 @@ -7,6 +7,11 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; +import java.util.Map; + +import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; +import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED; + @Lazy(false) @Component public class ChargingPileEventHandler implements ClientEventHandler { @@ -27,9 +32,9 @@ public class ChargingPileEventHandler implements ClientEventHandler { public void lostConnection(ClientHandler handler) { String pileNo = ChargingPileServer.getPileNo(handler); -// if (pileNo != null) { -// ChargingPileServer.removeHandler(pileNo); -// } + if (pileNo != null) { + ChargingPileServer.disconnPileNGuns(pileNo); + } log.info("<- ({}) - [{}] - {} ->", pileNo, handler.getName(), handler.getSocket().getRemoteSocketAddress().toString()); } @@ -38,11 +43,11 @@ public class ChargingPileEventHandler implements ClientEventHandler { public void closingConnection(ClientHandler handler) { String pileNo = ChargingPileServer.getPileNo(handler); -// ChargingPileServer.removeHandler(pileNo); -// String pkey = "pile:".concat(pileNo); -// Map cachePile = REDIS.getCacheMap(pkey); -// cachePile.put("status", DISCONNECTED); -// REDIS.setCacheMap(pkey, cachePile); + ChargingPileServer.removeHandler(pileNo); + String pkey = "pile:".concat(pileNo); + Map cachePile = REDIS.getCacheMap(pkey); + cachePile.put("status", DISCONNECTED); + REDIS.setCacheMap(pkey, cachePile); handler.closeConnection(); 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 22491592..5d23c27d 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 @@ -75,15 +75,17 @@ public class ChargingPileServer { pileMap.put(handler.getName(), pileNo); } - public static void removeHandler(String pileNo) { + public static void disconnPileNGuns(String pileNo) { ClientHandler handler = handlerMap.remove(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); - REDIS.setCacheMap(pkey, cachePile); + putemDisconn(pileNo, cachePile); + } + + private static void putemDisconn(String pileNo, Map cachePile) { String stationTermStatusKey = "stationTerminalStatus:".concat(cachePile.get("stationId").toString()); for (int i = 1; i <= (int) cachePile.get("gunNum"); i++) { String gunkey = "gun:".concat(pileNo).concat(String.format("%02d", i)); @@ -104,6 +106,18 @@ 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); + String pkey = "pile:".concat(pileNo); + Map cachePile = REDIS.getCacheMap(pkey); + cachePile.put("status", DISCONNECTED); + REDIS.setCacheMap(pkey, cachePile); + putemDisconn(pileNo, cachePile); log.info("remove handler [{}] for ({})", handler.getName(), pileNo); pileMap.remove(handler.getName()); versionMapper.remove(handler.getName()); diff --git a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/HBCheckTask.java b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/HBCheckTask.java index 68d51b77..4f2bd754 100644 --- a/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/HBCheckTask.java +++ b/xhpc-modules/xhpc-power-pile/src/main/java/com/xhpc/pp/server/HBCheckTask.java @@ -3,15 +3,35 @@ package com.xhpc.pp.server; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import java.io.IOException; +import java.util.Calendar; +import java.util.List; +import java.util.Map; + +import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS; +import static com.xhpc.common.dto.ConnectorStatusInfo.OFF_LINE; +import static com.xhpc.pp.config.EarlierBeanConf.getLocalIPAndPort; +import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED; @Component public class HBCheckTask { - @Scheduled(fixedRate = 1000) - protected void run() throws IOException { + @Scheduled(fixedRate = 10000) + protected void run() { - System.out.println("hb check"); + System.out.println("hbchk.."); + String svcSrvKey = "svcSrvGuns".concat(getLocalIPAndPort()); + List cacheGunkeyList = REDIS.getCacheList(svcSrvKey); + Long now = Calendar.getInstance().getTimeInMillis(); + for (String gunkey : cacheGunkeyList) { + Map cacheGun = REDIS.getCacheMap(gunkey); + Long hbtime = (Long) cacheGun.get("hbtime"); + hbtime = hbtime == null ? 0 : hbtime; + if ((now - hbtime) > 60000) { + cacheGun.put("statusInt", OFF_LINE); + cacheGun.put("status", DISCONNECTED); + REDIS.setCacheMap(gunkey, cacheGun); + } + } } }