diff --git a/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/EvcsOcppServer.java b/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/EvcsOcppServer.java index cabfb8d054..47d0539fcb 100644 --- a/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/EvcsOcppServer.java +++ b/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/EvcsOcppServer.java @@ -110,12 +110,25 @@ protected void removeEvcs(Evcs evcs) { return; } var ocppEvcs = (AbstractManagedOcppEvcsComponent) evcs; - var evcss = this.activeEvcsSessions.get(ocppEvcs.getSessionId()); - if (evcss != null) { - if (evcss.size() < 2) { - this.activeEvcsSessions.remove(ocppEvcs.getSessionId()); - } else { - this.activeEvcsSessions.get(ocppEvcs.getSessionId()).remove(ocppEvcs); + var sessionId = ocppEvcs.getSessionId(); + if (sessionId != null) { + var evcss = this.activeEvcsSessions.get(sessionId); + if (evcss != null) { + evcss.remove(ocppEvcs); + if (evcss.isEmpty()) { + this.activeEvcsSessions.remove(sessionId); + // Also remove from ocppSessions if needed + var ocppIdToRemove = ""; + for (Entry entry : this.ocppSessions.entrySet()) { + if (entry.getValue().equals(sessionId)) { + ocppIdToRemove = entry.getKey(); + break; + } + } + if (!ocppIdToRemove.isEmpty()) { + this.ocppSessions.remove(ocppIdToRemove); + } + } } } this.ocppEvcss.remove(ocppEvcs.getConfiguredOcppId()); @@ -123,8 +136,7 @@ protected void removeEvcs(Evcs evcs) { } public EvcsOcppServer() { - super(OpenemsComponent.ChannelId.values() // - ); + super(OpenemsComponent.ChannelId.values()); } @Activate diff --git a/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/MyJsonServer.java b/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/MyJsonServer.java index 66d4d78b3a..dd37cd6b2c 100644 --- a/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/MyJsonServer.java +++ b/io.openems.edge.evcs.ocpp.server/src/io/openems/edge/evcs/ocpp/server/MyJsonServer.java @@ -43,8 +43,8 @@ public class MyJsonServer { /** * The JSON OCPP server. * - *

- * Responsible for sending and receiving OCPP JSON commands. + * + *

Responsible for sending and receiving OCPP JSON commands. */ private final JSONServer server; @@ -90,6 +90,23 @@ public void newSession(UUID sessionIndex, SessionInformation information) { var ocppIdentifier = information.getIdentifier().replace("/", ""); + // Check if there is already a session for this ocppIdentifier + UUID oldSessionIndex = MyJsonServer.this.parent.ocppSessions.get(ocppIdentifier); + if (oldSessionIndex != null && !oldSessionIndex.equals(sessionIndex)) { + // Remove old session + MyJsonServer.this.logDebug("Removing old session [" + oldSessionIndex + "] for ocppIdentifier [" + + ocppIdentifier + "]"); + List oldEvcss = MyJsonServer.this.parent.activeEvcsSessions + .get(oldSessionIndex); + if (oldEvcss != null) { + for (AbstractManagedOcppEvcsComponent ocppEvcs : oldEvcss) { + ocppEvcs.lostSession(); + } + MyJsonServer.this.parent.activeEvcsSessions.remove(oldSessionIndex); + } + } + + // Update the ocppSessions with the new sessionIndex MyJsonServer.this.parent.ocppSessions.put(ocppIdentifier, sessionIndex); var presentEvcss = MyJsonServer.this.parent.ocppEvcss.get(ocppIdentifier); @@ -122,10 +139,16 @@ public void lostSession(UUID sessionIndex) { for (Entry session : MyJsonServer.this.parent.ocppSessions.entrySet()) { if (session.getValue().equals(sessionIndex)) { ocppId = session.getKey(); + break; } } - MyJsonServer.this.parent.ocppSessions.remove(ocppId); + // Before removing, check if the sessionIndex is still the one mapped to ocppId + UUID currentSessionIndex = MyJsonServer.this.parent.ocppSessions.get(ocppId); + if (currentSessionIndex != null && currentSessionIndex.equals(sessionIndex)) { + MyJsonServer.this.parent.ocppSessions.remove(ocppId); + } + MyJsonServer.this.parent.activeEvcsSessions.remove(sessionIndex); }