You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ocpp16-csms-stub/src/main/java/com/yablochkov/ocppstub/rest/ResetController.java

86 lines
3.4 KiB

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 com.yablochkov.ocppstub.TransactionService;
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.RemoteStartTransactionConfirmation;
import eu.chargetime.ocpp.model.core.RemoteStopTransactionConfirmation;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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;
@Autowired private TransactionService transactionService;
@Lazy @Autowired private OcppStub ocppStub;
@GetMapping("/list")
public Map<UUID, SessionInformation> findClients() {
return sessionService.getCache();
}
@PutMapping("/reset/{identity}")
@ResponseStatus(HttpStatus.OK)
public ResetConfirmation reset(
@PathVariable("identity") final String identity, @RequestBody final ResetRequest request) {
// TODO REFACTOR THIS
log.info("Send reset to {}, parameters {}", identity, request);
var session = sessionService.getCache()
.entrySet().stream()
.filter((entry) -> Objects.equals(entry.getValue().getIdentifier(), identity))
.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);
}
@PostMapping("/start/{identity}")
public RemoteStartTransactionConfirmation startTransaction(@PathVariable("identity") final String identity) {
return transactionService.remoteStart(identity);
}
@DeleteMapping("/stop/{identity}")
public RemoteStopTransactionConfirmation stopTransaction(@PathVariable("identity") final String identity) {
return transactionService.remoteStop(identity);
}
}