Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConfirmationCompletedHandler #210

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ocpp-common/src/main/java/eu/chargetime/ocpp/Communicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/*
ChargeTime.eu - Java-OCA-OCPP
Copyright (C) 2015-2016 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

MIT License

Expand Down Expand Up @@ -203,6 +204,16 @@ public synchronized void sendCall(String uniqueId, String action, Request reques
public void sendCallResult(String uniqueId, String action, Confirmation confirmation) {
try {
radio.send(makeCallResult(uniqueId, action, packPayload(confirmation)));

ConfirmationCompletedHandler completedHandler = confirmation.getCompletedHandler();

if (completedHandler != null) {
try {
completedHandler.onConfirmationCompleted();
} catch (Throwable e) {
events.onError(uniqueId, "ConfirmationCompletedHandlerFailed", "The confirmation completed callback handler failed with exception " + e.toString(), confirmation);
}
}
} catch (NotConnectedException ex) {
logger.warn("sendCallResult() failed", ex);
events.onError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
MIT License

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,4 +29,14 @@ of this software and associated documentation files (the "Software"), to deal
*/

/** Interface used to flag a Model as a Confirmation payload. */
public interface Confirmation extends Validatable {}
public abstract class Confirmation implements Validatable {
private transient ConfirmationCompletedHandler completedHandler;

public ConfirmationCompletedHandler getCompletedHandler() {
return completedHandler;
}
public void setCompletedHandler(ConfirmationCompletedHandler completedHandler) {
this.completedHandler = completedHandler;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package eu.chargetime.ocpp.model;

/*
ChargeTime.eu - Java-OCA-OCPP

MIT License

Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


/**
* Callback that can perform actions after the confirmation is sent back to the Charge Point
*/
public interface ConfirmationCompletedHandler {
void onConfirmationCompleted();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
MIT License

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
*/

/** Test implementation of the Confirmation interface. Used for tests. */
public class TestConfirmation implements Confirmation {
public class TestConfirmation extends Confirmation {
@Override
public boolean validate() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.mockito.Mockito.*;

import eu.chargetime.ocpp.*;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.ConfirmationCompletedHandler;
import eu.chargetime.ocpp.model.Message;
import eu.chargetime.ocpp.model.Request;
import org.junit.Before;
Expand All @@ -18,6 +20,7 @@
MIT License

Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -195,4 +198,43 @@ public void connected_failedOnceBefore_sameRequestIsRetried() throws Exception {
// Then
verify(receiver, times(3)).send(eq(uniqueId));
}

@Test
public void confirmationCallback_Handler() {
// Given
Confirmation conf = new Confirmation() {
@Override
public boolean validate() {
return true;
}
};

ConfirmationCompletedHandler handler = mock(ConfirmationCompletedHandler.class);
conf.setCompletedHandler(handler);

String uniqueId = "some id";
String action = "some action";

// When
communicator.sendCallResult(uniqueId, action, conf);

// Then
verify(handler, times(1)).onConfirmationCompleted();
}

@Test
public void confirmationCallback_noHandler() {
// Make sure it's not crashing because it has no handler set

Confirmation conf = new Confirmation() {
@Override
public boolean validate() {
return true;
}
};

String uniqueId = "some id";
String action = "some action";
communicator.sendCallResult(uniqueId, action, conf);
}
}
16 changes: 14 additions & 2 deletions ocpp-common/src/test/java/eu/chargetime/ocpp/test/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
MIT License

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -119,7 +120,12 @@ public void sendRequest_someUniqueId_sendsUniqueIdToCommunicator() {
@Test
public void sendConfirmation_sendsConfirmationToCommunicator() {
// Given
Confirmation conf = () -> false;
Confirmation conf = new Confirmation() {
@Override
public boolean validate() {
return false;
}
};
String someUniqueId = "Some id";
String action = "Some action";

Expand Down Expand Up @@ -162,7 +168,13 @@ public void onCall_unhandledCallback_callSendCallError() throws Exception {
public void onCall_handledCallback_callSendCallResult() throws Exception {
// Given
String someId = "Some id";
Confirmation aConfirmation = () -> true;
Confirmation aConfirmation = new Confirmation() {
@Override
public boolean validate() {
return false;
}
};

doAnswer(
invocation ->
invocation.getArgumentAt(0, CompletableFuture.class).complete(aConfirmation))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ChargeTime.eu - Java-OCA-OCPP
Copyright (C) 2015-2016 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

MIT License

Expand Down Expand Up @@ -37,7 +38,7 @@ of this software and associated documentation files (the "Software"), to deal

/** Sent by the Central System to the Charge Point in response to a {@link AuthorizeRequest}. */
@XmlRootElement(name = "authorizeResponse")
public class AuthorizeConfirmation implements Confirmation {
public class AuthorizeConfirmation extends Confirmation {

private IdTagInfo idTagInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ChargeTime.eu - Java-OCA-OCPP
Copyright (C) 2015-2016 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

MIT License

Expand Down Expand Up @@ -45,7 +46,7 @@ of this software and associated documentation files (the "Software"), to deal
*/
@XmlRootElement(name = "bootNotificationResponse")
@XmlType(propOrder = {"status", "currentTime", "interval"})
public class BootNotificationConfirmation implements Confirmation {
public class BootNotificationConfirmation extends Confirmation {

private ZonedDateTime currentTime;
private Integer interval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,7 +38,7 @@ of this software and associated documentation files (the "Software"), to deal

/** return by Charge Point to Central System. */
@XmlRootElement(name = "changeAvailabilityResponse")
public class ChangeAvailabilityConfirmation implements Confirmation {
public class ChangeAvailabilityConfirmation extends Confirmation {

private AvailabilityStatus status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,7 +38,7 @@ of this software and associated documentation files (the "Software"), to deal

/** Returned from Charge Point to Central System */
@XmlRootElement(name = "changeConfigurationResponse")
public class ChangeConfigurationConfirmation implements Confirmation {
public class ChangeConfigurationConfirmation extends Confirmation {

private ConfigurationStatus status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,7 +38,7 @@ of this software and associated documentation files (the "Software"), to deal

/** Sent by the Charge Point to the Central System in response to a {@link ClearCacheRequest}. */
@XmlRootElement(name = "clearCacheResponse")
public class ClearCacheConfirmation implements Confirmation {
public class ClearCacheConfirmation extends Confirmation {

private ClearCacheStatus status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
* Copyright (C) 2022 Emil Melar <emil@iconsultable.no>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -40,7 +41,7 @@
*/
@XmlRootElement(name = "dataTransferResponse")
@XmlType(propOrder = {"status", "data"})
public class DataTransferConfirmation implements Confirmation {
public class DataTransferConfirmation extends Confirmation {

private DataTransferStatus status;
private String data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
MIT License

Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2022 Emil Melar <emil@iconsultable.no>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -42,7 +43,7 @@ of this software and associated documentation files (the "Software"), to deal
*/
@XmlRootElement(name = "getConfigurationResponse")
@XmlType(propOrder = {"configurationKey", "unknownKey"})
public class GetConfigurationConfirmation implements Confirmation {
public class GetConfigurationConfirmation extends Confirmation {

private static final String ERROR_MESSAGE = "Exceeds limit of %s chars";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
* Copyright (C) 2022 Emil Melar <emil@iconsultable.no>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,7 +37,7 @@

/** Sent by the Central System to the Charge Point in response to a {@link HeartbeatRequest}. */
@XmlRootElement(name = "heartbeatResponse")
public class HeartbeatConfirmation implements Confirmation {
public class HeartbeatConfirmation extends Confirmation {
private ZonedDateTime currentTime;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* MIT License
*
* Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2022 Emil Melar <emil@iconsultable.no>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,7 +34,7 @@

/** */
@XmlRootElement(name = "meterValuesResponse")
public class MeterValuesConfirmation implements Confirmation {
public class MeterValuesConfirmation extends Confirmation {
@Override
public boolean validate() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
* Copyright (C) 2022 Emil Melar <emil@iconsultable.no>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,7 +36,7 @@

/** Sent from Charge Point to Central System. */
@XmlRootElement(name = "remoteStartTransactionResponse")
public class RemoteStartTransactionConfirmation implements Confirmation {
public class RemoteStartTransactionConfirmation extends Confirmation {
private RemoteStartStopStatus status;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
* Copyright (C) 2022 Emil Melar <emil@iconsultable.no>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,7 +36,7 @@

/** sent from Charge Point to Central System. */
@XmlRootElement(name = "remoteStopTransactionResponse")
public class RemoteStopTransactionConfirmation implements Confirmation {
public class RemoteStopTransactionConfirmation extends Confirmation {

private RemoteStartStopStatus status;

Expand Down
Loading