Skip to content

Commit 6fd4697

Browse files
committed
decouple OCPP invocation from main thread by using executor service
1 parent 213c2de commit 6fd4697

2 files changed

Lines changed: 46 additions & 28 deletions

File tree

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.stereotype.Service;
3737

3838
import java.util.List;
39+
import java.util.concurrent.ScheduledExecutorService;
3940
import java.util.function.Consumer;
4041

4142
import static de.rwth.idsg.steve.utils.DateTimeUtils.toDateTime;
@@ -51,6 +52,7 @@
5152
public class ChargePointService12_Client {
5253
private static final OcppVersion VERSION = OcppVersion.V_12;
5354

55+
@Autowired private ScheduledExecutorService executorService;
5456
@Autowired private RequestTaskStore requestTaskStore;
5557
@Autowired private ChargePointService12_Dispatcher dispatcher;
5658

@@ -186,7 +188,7 @@ public int updateFirmware(UpdateFirmwareParams params) {
186188
* Executes the requests
187189
*/
188190
private void execute(List<ChargePointSelect> list, Consumer<ChargePointSelect> consumer) {
189-
list.stream().forEach(consumer);
191+
executorService.execute(() -> list.stream().forEach(consumer));
190192
}
191193

192194
// -------------------------------------------------------------------------
@@ -197,10 +199,10 @@ public int remoteStartTransaction(RemoteStartTransactionParams params) {
197199
RemoteStartTransactionRequest req = this.prepareRemoteStartTransaction(params);
198200
List<ChargePointSelect> list = params.getChargePointSelectList();
199201
RequestTask task = new RequestTask(VERSION, req, list);
200-
201202
ChargePointSelect c = list.get(0);
202-
RemoteStartTransactionResponseHandler handler = new RemoteStartTransactionResponseHandler(task, c.getChargeBoxId());
203-
dispatcher.remoteStartTransaction(c, req, handler);
203+
204+
execute(() -> dispatcher.remoteStartTransaction(c, req,
205+
new RemoteStartTransactionResponseHandler(task, c.getChargeBoxId())));
204206

205207
return requestTaskStore.add(task);
206208
}
@@ -209,10 +211,10 @@ public int remoteStopTransaction(RemoteStopTransactionParams params) {
209211
RemoteStopTransactionRequest req = this.prepareRemoteStopTransaction(params);
210212
List<ChargePointSelect> list = params.getChargePointSelectList();
211213
RequestTask task = new RequestTask(VERSION, req, list);
212-
213214
ChargePointSelect c = list.get(0);
214-
RemoteStopTransactionResponseHandler handler = new RemoteStopTransactionResponseHandler(task, c.getChargeBoxId());
215-
dispatcher.remoteStopTransaction(c, req, handler);
215+
216+
execute(() -> dispatcher.remoteStopTransaction(c, req,
217+
new RemoteStopTransactionResponseHandler(task, c.getChargeBoxId())));
216218

217219
return requestTaskStore.add(task);
218220
}
@@ -221,11 +223,18 @@ public int unlockConnector(UnlockConnectorParams params) {
221223
UnlockConnectorRequest req = this.prepareUnlockConnector(params);
222224
List<ChargePointSelect> list = params.getChargePointSelectList();
223225
RequestTask task = new RequestTask(VERSION, req, list);
224-
225226
ChargePointSelect c = list.get(0);
226-
UnlockConnectorResponseHandler handler = new UnlockConnectorResponseHandler(task, c.getChargeBoxId());
227-
dispatcher.unlockConnector(c, req, handler);
227+
228+
execute(() -> dispatcher.unlockConnector(c, req,
229+
new UnlockConnectorResponseHandler(task, c.getChargeBoxId())));
228230

229231
return requestTaskStore.add(task);
230232
}
233+
234+
/**
235+
* Executes the requests for single charge point invocation
236+
*/
237+
private void execute(Runnable r) {
238+
executorService.execute(r);
239+
}
231240
}

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
import java.util.ArrayList;
6262
import java.util.List;
63+
import java.util.concurrent.ScheduledExecutorService;
6364
import java.util.function.Consumer;
6465
import java.util.stream.Collectors;
6566

@@ -76,6 +77,7 @@
7677
public class ChargePointService15_Client {
7778
private static final OcppVersion VERSION = OcppVersion.V_15;
7879

80+
@Autowired private ScheduledExecutorService executorService;
7981
@Autowired private UserRepository userRepository;
8082
@Autowired private UserService userService;
8183
@Autowired private ReservationRepository reservationRepository;
@@ -323,7 +325,7 @@ public int sendLocalList(SendLocalListParams params) {
323325
* Executes the requests
324326
*/
325327
private void execute(List<ChargePointSelect> list, Consumer<ChargePointSelect> consumer) {
326-
list.stream().forEach(consumer);
328+
executorService.execute(() -> list.stream().forEach(consumer));
327329
}
328330

329331
// -------------------------------------------------------------------------
@@ -334,10 +336,10 @@ public int remoteStartTransaction(RemoteStartTransactionParams params) {
334336
RemoteStartTransactionRequest req = this.prepareRemoteStartTransaction(params);
335337
List<ChargePointSelect> list = params.getChargePointSelectList();
336338
RequestTask task = new RequestTask(VERSION, req, list);
337-
338339
ChargePointSelect c = list.get(0);
339-
RemoteStartTransactionResponseHandler handler = new RemoteStartTransactionResponseHandler(task, c.getChargeBoxId());
340-
dispatcher.remoteStartTransaction(c, req, handler);
340+
341+
execute(() -> dispatcher.remoteStartTransaction(c, req,
342+
new RemoteStartTransactionResponseHandler(task, c.getChargeBoxId())));
341343

342344
return requestTaskStore.add(task);
343345
}
@@ -346,10 +348,10 @@ public int remoteStopTransaction(RemoteStopTransactionParams params) {
346348
RemoteStopTransactionRequest req = this.prepareRemoteStopTransaction(params);
347349
List<ChargePointSelect> list = params.getChargePointSelectList();
348350
RequestTask task = new RequestTask(VERSION, req, list);
349-
350351
ChargePointSelect c = list.get(0);
351-
RemoteStopTransactionResponseHandler handler = new RemoteStopTransactionResponseHandler(task, c.getChargeBoxId());
352-
dispatcher.remoteStopTransaction(c, req, handler);
352+
353+
execute(() -> dispatcher.remoteStopTransaction(c, req,
354+
new RemoteStopTransactionResponseHandler(task, c.getChargeBoxId())));
353355

354356
return requestTaskStore.add(task);
355357
}
@@ -358,10 +360,10 @@ public int unlockConnector(UnlockConnectorParams params) {
358360
UnlockConnectorRequest req = this.prepareUnlockConnector(params);
359361
List<ChargePointSelect> list = params.getChargePointSelectList();
360362
RequestTask task = new RequestTask(VERSION, req, list);
361-
362363
ChargePointSelect c = list.get(0);
363-
UnlockConnectorResponseHandler handler = new UnlockConnectorResponseHandler(task, c.getChargeBoxId());
364-
dispatcher.unlockConnector(c, req, handler);
364+
365+
execute(() -> dispatcher.unlockConnector(c, req,
366+
new UnlockConnectorResponseHandler(task, c.getChargeBoxId())));
365367

366368
return requestTaskStore.add(task);
367369
}
@@ -379,9 +381,10 @@ public int reserveNow(ReserveNowParams params) {
379381

380382
ReserveNowRequest req = this.prepareReserveNow(params, reservationId);
381383
RequestTask task = new RequestTask(VERSION, req, list);
382-
ReserveNowResponseHandler handler = new ReserveNowResponseHandler(task, chargeBoxId,
383-
reservationRepository, reservationId);
384-
dispatcher.reserveNow(c, req, handler);
384+
385+
execute(() -> dispatcher.reserveNow(c, req,
386+
new ReserveNowResponseHandler(task, chargeBoxId,
387+
reservationRepository, reservationId)));
385388

386389
return requestTaskStore.add(task);
387390
}
@@ -390,13 +393,19 @@ public int cancelReservation(CancelReservationParams params) {
390393
CancelReservationRequest req = this.prepareCancelReservation(params);
391394
List<ChargePointSelect> list = params.getChargePointSelectList();
392395
RequestTask task = new RequestTask(VERSION, req, list);
393-
394396
ChargePointSelect c = list.get(0);
395-
CancelReservationResponseHandler handler =
397+
398+
execute(() -> dispatcher.cancelReservation(c, req,
396399
new CancelReservationResponseHandler(task, c.getChargeBoxId(),
397-
reservationRepository, params.getReservationId());
398-
dispatcher.cancelReservation(c, req, handler);
399-
400+
reservationRepository, params.getReservationId())));
401+
400402
return requestTaskStore.add(task);
401403
}
404+
405+
/**
406+
* Executes the requests for single charge point invocation
407+
*/
408+
private void execute(Runnable r) {
409+
executorService.execute(r);
410+
}
402411
}

0 commit comments

Comments
 (0)