Skip to content

Commit

Permalink
add start/end transaction details to notification mails (Closes #467)
Browse files Browse the repository at this point in the history
  • Loading branch information
goekay committed Dec 17, 2020
1 parent 8e2d5dd commit 91eca19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
Expand Up @@ -184,7 +184,7 @@ public StartTransactionResponse startTransaction(StartTransactionRequest paramet

int transactionId = ocppServerRepository.insertTransaction(params);

notificationService.ocppTransactionStarted(chargeBoxIdentity, transactionId, parameters.getConnectorId());
notificationService.ocppTransactionStarted(transactionId, params);

return new StartTransactionResponse()
.withIdTagInfo(info)
Expand Down Expand Up @@ -217,7 +217,7 @@ public StopTransactionResponse stopTransaction(StopTransactionRequest parameters

ocppServerRepository.insertMeterValues(chargeBoxIdentity, parameters.getTransactionData(), transactionId);

notificationService.ocppTransactionEnded(chargeBoxIdentity, transactionId);
notificationService.ocppTransactionEnded(params);

return new StopTransactionResponse().withIdTagInfo(idTagInfo);
}
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/de/rwth/idsg/steve/service/NotificationService.java
Expand Up @@ -20,7 +20,9 @@

import com.google.common.base.Strings;
import de.rwth.idsg.steve.NotificationFeature;
import de.rwth.idsg.steve.repository.dto.InsertTransactionParams;
import de.rwth.idsg.steve.repository.dto.MailSettings;
import de.rwth.idsg.steve.repository.dto.UpdateTransactionParams;
import lombok.extern.slf4j.Slf4j;
import ocpp.cs._2015._10.RegistrationStatus;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -94,30 +96,57 @@ public void ocppStationStatusFailure(String chargeBoxId, int connectorId, String
mailService.sendAsync(subject, addTimestamp(body));
}

public void ocppTransactionStarted(String chargeBoxId, int transactionId, int connectorId) {
public void ocppTransactionStarted(int transactionId, InsertTransactionParams params) {
if (isDisabled(OcppTransactionStarted)) {
return;
}

String subject = format("Transaction '%s' has started on charging station '%s' on connector '%s'", transactionId, chargeBoxId, connectorId);
String subject = format("Transaction '%s' has started on charging station '%s' on connector '%s'", transactionId, params.getChargeBoxId(), params.getConnectorId());

mailService.sendAsync(subject, addTimestamp(""));
mailService.sendAsync(subject, addTimestamp(createContent(params)));
}

public void ocppTransactionEnded(String chargeBoxId, int transactionId) {
public void ocppTransactionEnded(UpdateTransactionParams params) {
if (isDisabled(OcppTransactionEnded)) {
return;
}

String subject = format("Transaction '%s' has ended on charging station '%s'", transactionId, chargeBoxId);
String subject = format("Transaction '%s' has ended on charging station '%s'", params.getTransactionId(), params.getChargeBoxId());

mailService.sendAsync(subject, addTimestamp(""));
mailService.sendAsync(subject, addTimestamp(createContent(params)));
}

// -------------------------------------------------------------------------
// Private helpers
// -------------------------------------------------------------------------


private static String createContent(InsertTransactionParams params) {
StringBuilder sb = new StringBuilder("Details:").append(System.lineSeparator())
.append("- chargeBoxId: ").append(params.getChargeBoxId()).append(System.lineSeparator())
.append("- connectorId: ").append(params.getConnectorId()).append(System.lineSeparator())
.append("- idTag: ").append(params.getIdTag()).append(System.lineSeparator())
.append("- startTimestamp: ").append(params.getStartTimestamp()).append(System.lineSeparator())
.append("- startMeterValue: ").append(params.getStartMeterValue());

if (params.isSetReservationId()) {
sb.append(System.lineSeparator()).append("- reservationId: ").append(params.getReservationId());
}

return sb.toString();
}

private static String createContent(UpdateTransactionParams params) {
return new StringBuilder("Details:").append(System.lineSeparator())
.append("- chargeBoxId: ").append(params.getChargeBoxId()).append(System.lineSeparator())
.append("- transactionId: ").append(params.getTransactionId()).append(System.lineSeparator())
.append("- stopTimestamp: ").append(params.getStopTimestamp()).append(System.lineSeparator())
.append("- stopMeterValue: ").append(params.getStopMeterValue()).append(System.lineSeparator())
.append("- stopReason: ").append(params.getStopReason())
.toString();
}


private boolean isDisabled(NotificationFeature f) {
MailSettings settings = mailService.getSettings();

Expand All @@ -130,7 +159,7 @@ private boolean isDisabled(NotificationFeature f) {

private static String addTimestamp(String body) {
String eventTs = "Timestamp of the event: " + DateTime.now().toString();
String newLine = "\r\n\r\n";
String newLine = System.lineSeparator() + System.lineSeparator();

if (Strings.isNullOrEmpty(body)) {
return eventTs;
Expand Down

0 comments on commit 91eca19

Please sign in to comment.