Skip to content

Commit bca07e6

Browse files
authored
Merge pull request #1872 from steve-community/1871-close-websocket-connection-if-any-after-deletion-of-a-charging-station
Close websocket connection (if any) after deletion of a charging station
2 parents dea4bbd + f0bbb97 commit bca07e6

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

src/main/java/de/rwth/idsg/steve/ocpp/ws/SessionContextStore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public interface SessionContextStore {
3838

3939
WebSocketSession getSession(String chargeBoxId);
4040

41+
void closeSessions(String chargeBoxId);
42+
4143
int getSize(String chargeBoxId);
4244

4345
int getNumberOfChargeBoxes();

src/main/java/de/rwth/idsg/steve/ocpp/ws/SessionContextStoreImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.joda.time.DateTime;
2929
import org.springframework.web.socket.WebSocketSession;
3030

31+
import java.io.IOException;
3132
import java.util.ArrayDeque;
3233
import java.util.Collections;
3334
import java.util.Deque;
@@ -132,6 +133,31 @@ public WebSocketSession getSession(String chargeBoxId) {
132133
}
133134
}
134135

136+
@Override
137+
public void closeSessions(String chargeBoxId) {
138+
Lock l = locks.get(chargeBoxId);
139+
l.lock();
140+
try {
141+
Deque<SessionContext> endpointDeque = lookupTable.get(chargeBoxId);
142+
if (endpointDeque == null) {
143+
return;
144+
}
145+
146+
// To prevent a ConcurrentModificationException, iterate over a copy of
147+
// endpointDeque when closing sessions. The close() operation can trigger an event
148+
// that modifies the deque, causing an error.
149+
for (SessionContext sessionContext : new ArrayDeque<>(endpointDeque)) {
150+
try {
151+
sessionContext.getSession().close();
152+
} catch (IOException e) {
153+
log.error("Error while closing web socket session for chargeBoxId '{}'", chargeBoxId, e);
154+
}
155+
}
156+
} finally {
157+
l.unlock();
158+
}
159+
}
160+
135161
@Override
136162
public int getSize(String chargeBoxId) {
137163
Deque<SessionContext> endpointDeque = lookupTable.get(chargeBoxId);

src/main/java/de/rwth/idsg/steve/service/ChargePointService.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,16 @@ public void updateChargePoint(ChargePointForm form) {
108108
}
109109

110110
public void deleteChargePoint(int chargeBoxPk) {
111+
var details = getDetails(chargeBoxPk);
112+
var chargeBoxId = details.getChargeBox().getChargeBoxId();
113+
111114
chargePointRepository.deleteChargePoint(chargeBoxPk);
115+
log.info("Deleted charge point with chargeBoxPk={} and chargeBoxId={}", chargeBoxPk, chargeBoxId);
116+
117+
// https://github.com/steve-community/steve/issues/1871
118+
var version = OcppProtocol.fromCompositeValue(details.getChargeBox().getOcppProtocol()).getVersion();
119+
log.info("Closing all WebSocket sessions of chargeBoxPk={} and chargeBoxId={}", chargeBoxPk, chargeBoxId);
120+
sessionContextStoreHolder.getOrCreate(version).closeSessions(chargeBoxId);
112121
}
113122

114123
// -------------------------------------------------------------------------
@@ -281,8 +290,13 @@ private static void appendList(Map<String, Deque<SessionContext>> map, List<Ocpp
281290
for (SessionContext ctx : endpointDeque) {
282291
DateTime openSince = ctx.getOpenSince();
283292

293+
Integer chargeBoxPk = primaryKeyLookup.get(chargeBoxId);
294+
if (chargeBoxPk == null) {
295+
log.warn("Could not find chargeBoxPk for chargeBoxId={}", chargeBoxId);
296+
}
297+
284298
OcppJsonStatus status = OcppJsonStatus.builder()
285-
.chargeBoxPk(primaryKeyLookup.get(chargeBoxId))
299+
.chargeBoxPk(chargeBoxPk)
286300
.chargeBoxId(chargeBoxId)
287301
.connectedSinceDT(openSince)
288302
.connectedSince(DateTimeUtils.humanize(openSince))

src/main/java/de/rwth/idsg/steve/service/OcppTagService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ public void updateOcppTag(OcppTagForm form) {
148148
}
149149

150150
public void deleteOcppTag(int ocppTagPk) {
151+
var details = getRecord(ocppTagPk);
151152
ocppTagRepository.deleteOcppTag(ocppTagPk);
153+
log.info("Deleted Ocpp Tag with ocppTagPk={} and ocppTagId={}", ocppTagPk, details.getIdTag());
152154
}
153155

154156
// -------------------------------------------------------------------------

src/main/java/de/rwth/idsg/steve/service/UserService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public void update(UserForm form) {
8888
}
8989

9090
public void delete(int userPk) {
91+
var details = getDetails(userPk);
9192
userRepository.delete(userPk);
93+
log.info("Deleted user with userPk={} and email={}", userPk, details.getUserRecord().getEMail());
9294
}
9395

9496
/**

src/main/java/de/rwth/idsg/steve/web/dto/OcppJsonStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@Builder
3333
@ToString
3434
public final class OcppJsonStatus {
35-
private final int chargeBoxPk;
35+
private final Integer chargeBoxPk;
3636
private final String chargeBoxId, connectedSince;
3737
private final String connectionDuration;
3838
private final OcppVersion version;

src/main/webapp/WEB-INF/views/ocppJsonStatus.jsp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ Connection Status for JSON Charge Points
4343
</thead>
4444
<tbody>
4545
<c:forEach items="${ocppJsonStatusList}" var="s">
46-
<tr><td><a href="${ctxPath}/manager/chargepoints/details/${s.chargeBoxPk}">${s.chargeBoxId}</a></td>
46+
<tr>
47+
<td>
48+
<c:choose>
49+
<c:when test="${not empty s.chargeBoxPk}">
50+
<a href="${ctxPath}/manager/chargepoints/details/${s.chargeBoxPk}">${s.chargeBoxId}</a>
51+
</c:when>
52+
<c:otherwise>
53+
${s.chargeBoxId}
54+
</c:otherwise>
55+
</c:choose>
56+
</td>
4757
<td>${s.version.value}</td>
4858
<td data-sort-value="${s.connectedSinceDT.millis}">${s.connectedSince}</td>
4959
<td>${s.connectionDuration}</td>

0 commit comments

Comments
 (0)