feat: Minor fixes
This commit is contained in:
@@ -18,6 +18,8 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,6 +29,7 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
@Slf4j
|
||||
public class TransactionService {
|
||||
private final static long REMOTE_TIMEOUT_SEC = 10;
|
||||
private final AtomicInteger transactionNumber = new AtomicInteger();
|
||||
private final Map<String,Integer> transactionMap = new HashMap<>();
|
||||
|
||||
@@ -62,11 +65,13 @@ public class TransactionService {
|
||||
.ifPresentOrElse(
|
||||
(id) -> log.info("Transaction id {} removed ({} requested)", id, transactionId),
|
||||
() -> log.info("Nothing to remove for {}", transactionId));
|
||||
|
||||
log.info("Stop by request completed");
|
||||
}
|
||||
|
||||
public RemoteStartTransactionConfirmation remoteStart(String id, Integer connectorId) {
|
||||
log.info("Try to start transaction for {}", id);
|
||||
var session = sessionService.getSessionByIdentity(id);
|
||||
public RemoteStartTransactionConfirmation remoteStart(String identity, Integer connectorId) {
|
||||
log.info("Try to start transaction for {}", identity);
|
||||
var session = sessionService.getSessionByIdentity(identity);
|
||||
log.info("Found session {}", session);
|
||||
if (Objects.nonNull(session)) {
|
||||
|
||||
@@ -79,20 +84,24 @@ public class TransactionService {
|
||||
request.setConnectorId(connectorId);
|
||||
}
|
||||
|
||||
log.info("Send start transaction request {}", request);
|
||||
var future = ocppStub.send(session, request);
|
||||
Confirmation confirmation = future.toCompletableFuture().get();
|
||||
Confirmation confirmation = future.toCompletableFuture()
|
||||
.get(REMOTE_TIMEOUT_SEC, TimeUnit.SECONDS);
|
||||
|
||||
if (confirmation instanceof RemoteStartTransactionConfirmation startConfirmation) {
|
||||
var status = startConfirmation.getStatus();
|
||||
log.info("Start transaction status {}", status);
|
||||
if (RemoteStartStopStatus.Accepted.equals(status)) {
|
||||
transactionMap.remove(id);
|
||||
transactionMap.remove(identity);
|
||||
}
|
||||
return startConfirmation;
|
||||
}
|
||||
} catch (InterruptedException | ExecutionException | OccurenceConstraintException |
|
||||
UnsupportedFeatureException | NotConnectedException e) {
|
||||
log.error("Caught exception on transaction start", e);
|
||||
} catch (TimeoutException e) {
|
||||
log.error("Remote start transaction confirmation exception, ps not responding", e);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Can't start transaction");
|
||||
@@ -130,4 +139,27 @@ public class TransactionService {
|
||||
}
|
||||
throw new RuntimeException("Can't stop transaction");
|
||||
}
|
||||
|
||||
public RemoteStopTransactionConfirmation ByIdentityAndTransactionId(String identity, Integer transactionId) {
|
||||
var session = Optional.ofNullable(sessionService.getSessionByIdentity(identity))
|
||||
.orElseThrow();
|
||||
|
||||
try {
|
||||
var future = ocppStub.send(session,
|
||||
new RemoteStopTransactionRequest(transactionId));
|
||||
|
||||
var confirmation = future.toCompletableFuture()
|
||||
.get(REMOTE_TIMEOUT_SEC, TimeUnit.SECONDS);
|
||||
|
||||
if (confirmation instanceof RemoteStopTransactionConfirmation stopConsirmation) {
|
||||
log.info("Stop confirmation: {}", stopConsirmation);
|
||||
return stopConsirmation;
|
||||
}
|
||||
|
||||
} catch (OccurenceConstraintException | UnsupportedFeatureException | NotConnectedException |
|
||||
ExecutionException | InterruptedException | TimeoutException e) {
|
||||
log.error("Can't stop transaction by id", e);
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,4 +81,11 @@ public class ResetController {
|
||||
public RemoteStopTransactionConfirmation stopTransaction(@PathVariable("identity") final String identity) {
|
||||
return transactionService.remoteStop(identity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/stopById/{identity}/{transactionId}")
|
||||
public RemoteStopTransactionConfirmation stopTransactionByTransactionId(
|
||||
@PathVariable("identity") final String identity,
|
||||
@PathVariable("transactionId") final Integer transactionId ) {
|
||||
return transactionService.ByIdentityAndTransactionId(identity, transactionId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user