feat: Reset
This commit is contained in:
@@ -23,6 +23,7 @@ dependencies {
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
implementation group: 'eu.chargetime.ocpp', name: 'v1_6', version: '1.0.1'
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.3.0'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.yablochkov.ocppstub;
|
||||
|
||||
import eu.chargetime.ocpp.model.Request;
|
||||
import eu.chargetime.ocpp.model.core.HeartbeatRequest;
|
||||
import eu.chargetime.ocpp.model.core.ResetConfirmation;
|
||||
import eu.chargetime.ocpp.model.core.StatusNotificationRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.yablochkov.ocppstub;
|
||||
|
||||
import eu.chargetime.ocpp.YablSessionInformation;
|
||||
import eu.chargetime.ocpp.model.SessionInformation;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -13,7 +14,7 @@ import java.util.UUID;
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SessionService {
|
||||
private final Map<UUID,SessionInformation> cache = new HashMap<>();
|
||||
@Getter private final Map<UUID,SessionInformation> cache = new HashMap<>();
|
||||
public void register(UUID sessionIndex, SessionInformation information) {
|
||||
log.info("Register new session {}", sessionIndex);
|
||||
cache.put(sessionIndex, information);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.yablochkov.ocppstub.rest;
|
||||
|
||||
import static eu.chargetime.ocpp.model.core.ResetStatus.Rejected;
|
||||
|
||||
import com.yablochkov.ocppstub.OcppStub;
|
||||
import com.yablochkov.ocppstub.SessionService;
|
||||
import eu.chargetime.ocpp.NotConnectedException;
|
||||
import eu.chargetime.ocpp.OccurenceConstraintException;
|
||||
import eu.chargetime.ocpp.UnsupportedFeatureException;
|
||||
import eu.chargetime.ocpp.model.Confirmation;
|
||||
import eu.chargetime.ocpp.model.SessionInformation;
|
||||
import eu.chargetime.ocpp.model.core.ResetConfirmation;
|
||||
import eu.chargetime.ocpp.model.core.ResetRequest;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/reset")
|
||||
@Slf4j
|
||||
public class ResetController {
|
||||
@Autowired private SessionService sessionService;
|
||||
@Lazy @Autowired private OcppStub ocppStub;
|
||||
@GetMapping("/list")
|
||||
public Map<UUID, SessionInformation> findClients() {
|
||||
return sessionService.getCache();
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResetConfirmation updateBook(
|
||||
@PathVariable("id") final String id, @RequestBody final ResetRequest request) {
|
||||
var session = sessionService.getCache()
|
||||
.entrySet().stream()
|
||||
.filter((entry) -> Objects.equals(entry.getValue().getIdentifier(), id))
|
||||
.map(Entry::getKey)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
|
||||
try {
|
||||
var stage = ocppStub.send(session, request);
|
||||
|
||||
Confirmation confirmation = stage.toCompletableFuture().get();
|
||||
|
||||
if (confirmation instanceof ResetConfirmation resetConfirmation) {
|
||||
return resetConfirmation;
|
||||
}
|
||||
} catch (OccurenceConstraintException | UnsupportedFeatureException | NotConnectedException |
|
||||
ExecutionException | InterruptedException e) {
|
||||
log.error("Problem with reset request");
|
||||
}
|
||||
|
||||
return new ResetConfirmation(Rejected);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: "/api-docs.html"
|
||||
|
||||
Reference in New Issue
Block a user