diff --git a/build.gradle b/build.gradle index 61535ae..b287658 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/com/yablochkov/ocppstub/Communicator.java b/src/main/java/com/yablochkov/ocppstub/Communicator.java index 2ef3762..ab54a7c 100644 --- a/src/main/java/com/yablochkov/ocppstub/Communicator.java +++ b/src/main/java/com/yablochkov/ocppstub/Communicator.java @@ -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; diff --git a/src/main/java/com/yablochkov/ocppstub/SessionService.java b/src/main/java/com/yablochkov/ocppstub/SessionService.java index bfb7d88..5afa14e 100644 --- a/src/main/java/com/yablochkov/ocppstub/SessionService.java +++ b/src/main/java/com/yablochkov/ocppstub/SessionService.java @@ -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 cache = new HashMap<>(); + @Getter private final Map cache = new HashMap<>(); public void register(UUID sessionIndex, SessionInformation information) { log.info("Register new session {}", sessionIndex); cache.put(sessionIndex, information); diff --git a/src/main/java/com/yablochkov/ocppstub/rest/ResetController.java b/src/main/java/com/yablochkov/ocppstub/rest/ResetController.java new file mode 100644 index 0000000..9c6128d --- /dev/null +++ b/src/main/java/com/yablochkov/ocppstub/rest/ResetController.java @@ -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 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); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 8b13789..9d250ef 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1 +1,3 @@ - +springdoc: + swagger-ui: + path: "/api-docs.html"