Skip to content

Commit

Permalink
Add CancelReservation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sumlin committed Apr 17, 2018
1 parent c7f2e8f commit eb29a99
Show file tree
Hide file tree
Showing 16 changed files with 692 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.model.firmware.*;
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageRequest;
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageRequestType;
import eu.chargetime.ocpp.model.reservation.CancelReservationConfirmation;
import eu.chargetime.ocpp.model.reservation.CancelReservationRequest;
import eu.chargetime.ocpp.model.reservation.ReserveNowConfirmation;
import eu.chargetime.ocpp.model.reservation.ReserveNowRequest;
import eu.chargetime.ocpp.test.FakeCentral.serverType;
Expand Down Expand Up @@ -137,6 +139,10 @@ public boolean hasReceivedReserveNowConfirmation() {
return dummyHandlers.wasLatestConfirmation(ReserveNowConfirmation.class);
}

public boolean hasReceivedCancelReservationConfirmation() {
return dummyHandlers.wasLatestConfirmation(CancelReservationConfirmation.class);
}

public boolean hasReceivedUpdateFirmwareConfirmation() {
return dummyHandlers.wasLatestConfirmation(UpdateFirmwareConfirmation.class);
}
Expand Down Expand Up @@ -264,6 +270,11 @@ public void sendReserveNowRequest(Integer connectorId, Calendar expiryDate, Stri
send(request);
}

public void sendCancelReservationRequest(Integer reservationId) throws Exception {
CancelReservationRequest request = new CancelReservationRequest(reservationId);
send(request);
}

public boolean hasReceivedResetConfirmation(String status) {
boolean result = false;
ResetConfirmation confirmation = dummyHandlers.getReceivedConfirmation(new ResetConfirmation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageConfirmation;
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageRequest;
import eu.chargetime.ocpp.model.remotetrigger.TriggerMessageStatus;
import eu.chargetime.ocpp.model.reservation.ReservationStatus;
import eu.chargetime.ocpp.model.reservation.ReserveNowConfirmation;
import eu.chargetime.ocpp.model.reservation.ReserveNowRequest;
import eu.chargetime.ocpp.model.reservation.*;
import eu.chargetime.ocpp.model.smartcharging.ChargingProfileStatus;
import eu.chargetime.ocpp.model.smartcharging.SetChargingProfileConfirmation;
import eu.chargetime.ocpp.model.smartcharging.SetChargingProfileRequest;
Expand Down Expand Up @@ -178,6 +176,12 @@ public ReserveNowConfirmation handleReserveNowRequest(ReserveNowRequest request)
receivedRequest = request;
return new ReserveNowConfirmation(ReservationStatus.Accepted);
}

@Override
public CancelReservationConfirmation handleCancelReservationRequest(CancelReservationRequest request) {
receivedRequest = request;
return new CancelReservationConfirmation(CancelReservationStatus.Accepted);
}
});

switch (type) {
Expand Down Expand Up @@ -360,6 +364,10 @@ public boolean hasHandledReserveNowRequest() {
return receivedRequest instanceof ReserveNowRequest;
}

public boolean hasHandledCancelReservationRequest() {
return receivedRequest instanceof CancelReservationRequest;
}

public boolean hasHandledChangeConfigurationRequest() {
return receivedRequest instanceof ChangeConfigurationRequest;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package eu.chargetime.ocpp.test.reservation.json

import eu.chargetime.ocpp.test.FakeCentral
import eu.chargetime.ocpp.test.FakeCentralSystem
import eu.chargetime.ocpp.test.FakeChargePoint
import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

/*
ChargeTime.eu - Java-OCA-OCPP
MIT License
Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2018 Mikhail Kladkevich <kladmv@ecp-share.com>
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.
*/

class JSONCancelReservationSpec extends Specification {
@Shared
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
@Shared
FakeChargePoint chargePoint = new FakeChargePoint()

def setupSpec() {
// When a Central System is running
centralSystem.started()
}

def setup() {
chargePoint.connect()
}

def cleanup() {
chargePoint.disconnect()
}

def "Central System sends a CancelReservation request and receives a response"() {
def conditions = new PollingConditions(timeout: 1)
given:
conditions.eventually {
assert centralSystem.connected()
}

when:
centralSystem.sendCancelReservationRequest(1)

then:
conditions.eventually {
assert chargePoint.hasHandledCancelReservationRequest()
assert centralSystem.hasReceivedCancelReservationConfirmation()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package eu.chargetime.ocpp.feature;

import eu.chargetime.ocpp.feature.profile.Profile;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;
import eu.chargetime.ocpp.model.reservation.CancelReservationConfirmation;
import eu.chargetime.ocpp.model.reservation.CancelReservationRequest;

/*
ChargeTime.eu - Java-OCA-OCPP
MIT License
Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2018 Mikhail Kladkevich <kladmv@ecp-share.com>
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.
*/

public class CancelReservationFeature extends Feature {

public CancelReservationFeature(Profile ownerProfile) {
super(ownerProfile);
}

@Override
public Class<? extends Request> getRequestType() {
return CancelReservationRequest.class;
}

@Override
public Class<? extends Confirmation> getConfirmationType() {
return CancelReservationConfirmation.class;
}

@Override
public String getAction() {
return "CancelReservation";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/

import eu.chargetime.ocpp.model.reservation.CancelReservationConfirmation;
import eu.chargetime.ocpp.model.reservation.CancelReservationRequest;
import eu.chargetime.ocpp.model.reservation.ReserveNowConfirmation;
import eu.chargetime.ocpp.model.reservation.ReserveNowRequest;

public interface ClientReservationEventHandler {
ReserveNowConfirmation handleReserveNowRequest(ReserveNowRequest request);

CancelReservationConfirmation handleCancelReservationRequest(CancelReservationRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.feature.*;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;
import eu.chargetime.ocpp.model.reservation.CancelReservationRequest;
import eu.chargetime.ocpp.model.reservation.ReserveNowRequest;

import java.util.HashSet;
Expand All @@ -42,6 +43,7 @@ public ClientReservationProfile(ClientReservationEventHandler eventHandler) {
this.eventHandler = eventHandler;
features = new HashSet<>();
features.add(new ReserveNowFeature(this));
features.add(new CancelReservationFeature(this));
}

@Override
Expand All @@ -55,6 +57,8 @@ public Confirmation handleRequest(UUID sessionIndex, Request request) {

if (request instanceof ReserveNowRequest) {
result = eventHandler.handleReserveNowRequest((ReserveNowRequest) request);
} else if (request instanceof CancelReservationRequest) {
result = eventHandler.handleCancelReservationRequest((CancelReservationRequest) request);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ServerReservationProfile implements Profile {
public ServerReservationProfile() {
features = new HashSet<>();
features.add(new ReserveNowFeature(this));
features.add(new CancelReservationFeature(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package eu.chargetime.ocpp.model.reservation;

/*
* ChargeTime.eu - Java-OCA-OCPP
*
* MIT License
*
* Copyright (C) 2016 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2018 Mikhail Kladkevich <kladmv@ecp-share.com>
*
* 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.
*/

import eu.chargetime.ocpp.model.Confirmation;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Objects;

/**
* Sent by the Charge Point to the Central System in response to an {@link CancelReservationRequest}.
*/
@XmlRootElement(name = "cancelReservationResponse")
public class CancelReservationConfirmation implements Confirmation {
private CancelReservationStatus status;

public CancelReservationConfirmation() { }

public CancelReservationConfirmation(CancelReservationStatus status) {
setStatus(status);
}

@Override
public boolean validate() {
return status != null;
}

/**
* This indicates the success or failure of the cancelling of a reservation by Central System.
*
* @return CancelReservationStatus, status of the request.
*/
public CancelReservationStatus getStatus() {
return status;
}

/**
* Required.
* This indicates the success or failure of the cancelling of a reservation by Central System.
*
* @param status CancelReservationStatus, status of the request.
*/
@XmlElement
public void setStatus(CancelReservationStatus status) {
this.status = status;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CancelReservationConfirmation that = (CancelReservationConfirmation) o;
return status == that.status;
}

@Override
public int hashCode() {
return Objects.hash(status);
}

@Override
public String toString() {
return "CancelReservationConfirmation{" +
"status=" + status +
", isValid=" + String.valueOf(validate()) +
'}';
}
}
Loading

0 comments on commit eb29a99

Please sign in to comment.