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.
52 lines
1.8 KiB
52 lines
1.8 KiB
package com.yablochkov.ocppstub;
|
|
|
|
import eu.chargetime.ocpp.model.core.*;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.ZonedDateTime;
|
|
import java.util.Base64;
|
|
import java.util.UUID;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class BootService {
|
|
private final static int PASSWD = 1;
|
|
public final static int INTERVAL_SEC = 10;
|
|
|
|
private final SessionService sessionService;
|
|
private final TriggerService triggerService;
|
|
|
|
public BootNotificationConfirmation handle(UUID sessionIndex, BootNotificationRequest request) {
|
|
String auth = sessionService.getAuthBySessionId(sessionIndex);
|
|
String passwd = getPasswd(auth);
|
|
|
|
if (passwd != null) {
|
|
log.info("Accept boot request {}", sessionIndex);
|
|
triggerService.bootCompleted(sessionIndex);
|
|
return createResponse(RegistrationStatus.Accepted);
|
|
}
|
|
|
|
log.info("Pending boot request {}", sessionIndex);
|
|
return createResponse(RegistrationStatus.Pending);
|
|
}
|
|
|
|
private String getPasswd(String basic) {
|
|
if (basic != null && !basic.isEmpty()) {
|
|
byte[] credDecoded = Base64.getDecoder().decode(basic.substring("Basic ".length()));
|
|
String credentials = new String(credDecoded, StandardCharsets.UTF_8);
|
|
if (credentials.contains(":")) {
|
|
String[] parsedCredential = credentials.split(":", 2);
|
|
return parsedCredential[PASSWD];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private BootNotificationConfirmation createResponse(RegistrationStatus status) {
|
|
return new BootNotificationConfirmation(ZonedDateTime.now(), INTERVAL_SEC, status);
|
|
}
|
|
}
|
|
|