|
|
|
@ -15,8 +15,11 @@ import eu.chargetime.ocpp.model.core.StartTransactionConfirmation; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
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; |
|
|
|
@ -26,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<>(); |
|
|
|
|
|
|
|
|
@ -55,33 +59,49 @@ public class TransactionService { |
|
|
|
|
var identity = sessionService.getIdentityBySessionId(sessionIndex); |
|
|
|
|
log.info("Try to stop transaction for {}, with id {}", identity, transactionId); |
|
|
|
|
|
|
|
|
|
if (Objects.nonNull(identity)) { |
|
|
|
|
transactionMap.remove(identity); |
|
|
|
|
} |
|
|
|
|
Optional.ofNullable(identity) |
|
|
|
|
.map(transactionMap::remove) |
|
|
|
|
.filter(Objects::nonNull) |
|
|
|
|
.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) { |
|
|
|
|
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)) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
var future = ocppStub.send(session, |
|
|
|
|
new RemoteStartTransactionRequest(UUID.randomUUID().toString())); |
|
|
|
|
Confirmation confirmation = future.toCompletableFuture().get(); |
|
|
|
|
//20 char limit
|
|
|
|
|
String idTag = String.format("%f.0", Math.random() * 1_000_000); |
|
|
|
|
|
|
|
|
|
var request = new RemoteStartTransactionRequest(idTag); |
|
|
|
|
if (Objects.nonNull(connectorId)) { |
|
|
|
|
request.setConnectorId(connectorId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.info("Send start transaction request {}", request); |
|
|
|
|
var future = ocppStub.send(session, request); |
|
|
|
|
Confirmation confirmation = future.toCompletableFuture() |
|
|
|
|
.get(REMOTE_TIMEOUT_SEC, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
|
|
if (confirmation instanceof RemoteStartTransactionConfirmation startConfirmation) { |
|
|
|
|
RemoteStartStopStatus status = startConfirmation.getStatus(); |
|
|
|
|
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"); |
|
|
|
@ -119,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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|