parent
699ae54af4
commit
4c7cb52703
@ -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" |
||||
|
Loading…
Reference in new issue