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

Allow to disable Reservation module #302

Merged
merged 3 commits into from
May 18, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- File index ([#270](https://github.com/matth-x/MicroOcpp/pull/270))
- Config `Cst_TxStartOnPowerPathClosed` to put back TxStartPoint ([#271](https://github.com/matth-x/MicroOcpp/pull/271))
- Build flag `MO_ENABLE_V16_RESERVATION=0` disables Reservation module ([#302](https://github.com/matth-x/MicroOcpp/pull/302))
- Function `bool isConnected()` in `Connection` interface ([#282](https://github.com/matth-x/MicroOcpp/pull/282))
- Build flags for customizing memory limits of SmartCharging ([#260](https://github.com/matth-x/MicroOcpp/pull/260))
- SConscript ([#287](https://github.com/matth-x/MicroOcpp/pull/287))
Expand Down
3 changes: 3 additions & 0 deletions src/MicroOcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,11 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden
new HeartbeatService(*context)));
model.setAuthorizationService(std::unique_ptr<AuthorizationService>(
new AuthorizationService(*context, filesystem)));

#if MO_ENABLE_V16_RESERVATION
model.setReservationService(std::unique_ptr<ReservationService>(
new ReservationService(*context, MO_NUMCONNECTORS)));
#endif

#if MO_ENABLE_V201
model.setVariableService(std::unique_ptr<VariableService>(
Expand Down
35 changes: 23 additions & 12 deletions src/MicroOcpp/Model/ConnectorBase/Connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ ChargePointStatus Connector::getStatus() {
} else {
res = ChargePointStatus::Charging;
}
} else if (model.getReservationService() && model.getReservationService()->getReservation(connectorId)) {
}
#if MO_ENABLE_V16_RESERVATION
else if (model.getReservationService() && model.getReservationService()->getReservation(connectorId)) {
res = ChargePointStatus::Reserved;
} else if ((!transaction || !transaction->isActive()) && //no transaction preparation
}
#endif
else if ((!transaction || !transaction->isActive()) && //no transaction preparation
(!connectorPluggedInput || !connectorPluggedInput()) && //no vehicle plugged
(!occupiedInput || !occupiedInput())) { //occupied override clear
res = ChargePointStatus::Available;
Expand Down Expand Up @@ -609,17 +613,22 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {
}
}

Reservation *reservation = nullptr;
int reservationId = -1;
bool offlineBlockedResv = false; //if offline authorization will be blocked by reservation

#if MO_ENABLE_V16_RESERVATION
//check if blocked by reservation
if (model.getReservationService()) {
const char *parentIdTag = localAuth ? localAuth->getParentIdTag() : nullptr;

reservation = model.getReservationService()->getReservation(
auto reservation = model.getReservationService()->getReservation(
connectorId,
idTag,
parentIdTag);

if (reservation) {
reservationId = reservation->getReservationId();
}

if (reservation && !reservation->matches(
idTag,
Expand All @@ -640,6 +649,7 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {
}
}
}
#endif //MO_ENABLE_V16_RESERVATION

transaction = allocateTransaction();

Expand All @@ -661,8 +671,8 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {
if (localAuth && localPreAuthorizeBool && localPreAuthorizeBool->getBool()) {
MO_DBG_DEBUG("Begin transaction process (%s), preauthorized locally", idTag != nullptr ? idTag : "");

if (reservation) {
transaction->setReservationId(reservation->getReservationId());
if (reservationId >= 0) {
transaction->setReservationId(reservationId);
}
transaction->setAuthorized();

Expand Down Expand Up @@ -692,6 +702,7 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {
return;
}

#if MO_ENABLE_V16_RESERVATION
if (model.getReservationService()) {
auto reservation = model.getReservationService()->getReservation(
connectorId,
Expand All @@ -714,6 +725,7 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {
}
}
}
#endif //MO_ENABLE_V16_RESERVATION

MO_DBG_DEBUG("Authorized transaction process (%s)", tx->getIdTag());
tx->setAuthorized();
Expand All @@ -724,13 +736,10 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {

//capture local auth and reservation check in for timeout handler
bool localAuthFound = localAuth;
bool reservationFound = reservation;
int reservationId = reservation ? reservation->getReservationId() : -1;
authorize->setOnTimeoutListener([this, tx,
offlineBlockedAuth,
offlineBlockedResv,
localAuthFound,
reservationFound,
reservationId] () {

if (offlineBlockedAuth) {
Expand All @@ -753,7 +762,7 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {

if (localAuthFound && localAuthorizeOfflineBool && localAuthorizeOfflineBool->getBool()) {
MO_DBG_DEBUG("Offline transaction process (%s), locally authorized", tx->getIdTag());
if (reservationFound) {
if (reservationId >= 0) {
tx->setReservationId(reservationId);
}
tx->setAuthorized();
Expand All @@ -765,7 +774,7 @@ std::shared_ptr<Transaction> Connector::beginTransaction(const char *idTag) {

if (allowOfflineTxForUnknownIdBool && allowOfflineTxForUnknownIdBool->getBool()) {
MO_DBG_DEBUG("Offline transaction process (%s), allow unknown ID", tx->getIdTag());
if (reservationFound) {
if (reservationId >= 0) {
tx->setReservationId(reservationId);
}
tx->setAuthorized();
Expand Down Expand Up @@ -811,14 +820,16 @@ std::shared_ptr<Transaction> Connector::beginTransaction_authorized(const char *
MO_DBG_DEBUG("Begin transaction process (%s), already authorized", idTag != nullptr ? idTag : "");

transaction->setAuthorized();


#if MO_ENABLE_V16_RESERVATION
if (model.getReservationService()) {
if (auto reservation = model.getReservationService()->getReservation(connectorId, idTag, parentIdTag)) {
if (reservation->matches(idTag, parentIdTag)) {
transaction->setReservationId(reservation->getReservationId());
}
}
}
#endif //MO_ENABLE_V16_RESERVATION

transaction->commit();

Expand Down
20 changes: 13 additions & 7 deletions src/MicroOcpp/Model/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,27 @@ void Model::loop() {

if (chargeControlCommon)
chargeControlCommon->loop();

if (smartChargingService)
smartChargingService->loop();

if (heartbeatService)
heartbeatService->loop();

if (meteringService)
meteringService->loop();

if (diagnosticsService)
diagnosticsService->loop();

if (firmwareService)
firmwareService->loop();


#if MO_ENABLE_V16_RESERVATION
if (reservationService)
reservationService->loop();

#endif //MO_ENABLE_V16_RESERVATION

if (resetService)
resetService->loop();

Expand Down Expand Up @@ -171,6 +173,7 @@ AuthorizationService *Model::getAuthorizationService() {
return authorizationService.get();
}

#if MO_ENABLE_V16_RESERVATION
void Model::setReservationService(std::unique_ptr<ReservationService> rs) {
reservationService = std::move(rs);
capabilitiesUpdated = true;
Expand All @@ -179,6 +182,7 @@ void Model::setReservationService(std::unique_ptr<ReservationService> rs) {
ReservationService *Model::getReservationService() {
return reservationService.get();
}
#endif //MO_ENABLE_V16_RESERVATION

void Model::setBootService(std::unique_ptr<BootService> bs){
bootService = std::move(bs);
Expand Down Expand Up @@ -286,12 +290,14 @@ void Model::updateSupportedStandardProfiles() {
}
}

#if MO_ENABLE_V16_RESERVATION
if (reservationService) {
if (!strstr(supportedFeatureProfilesString->getString(), "Reservation")) {
if (!buf.empty()) buf += ',';
buf += "Reservation";
}
}
#endif //MO_ENABLE_V16_RESERVATION

if (smartChargingService) {
if (!strstr(supportedFeatureProfilesString->getString(), "SmartCharging")) {
Expand Down
12 changes: 10 additions & 2 deletions src/MicroOcpp/Model/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class FirmwareService;
class DiagnosticsService;
class HeartbeatService;
class AuthorizationService;
class ReservationService;
class BootService;
class ResetService;

#if MO_ENABLE_V16_RESERVATION
class ReservationService;
#endif //MO_ENABLE_V16_RESERVATION

#if MO_ENABLE_CERT_MGMT
class CertificateService;
#endif //MO_ENABLE_CERT_MGMT
Expand All @@ -49,10 +52,13 @@ class Model {
std::unique_ptr<DiagnosticsService> diagnosticsService;
std::unique_ptr<HeartbeatService> heartbeatService;
std::unique_ptr<AuthorizationService> authorizationService;
std::unique_ptr<ReservationService> reservationService;
std::unique_ptr<BootService> bootService;
std::unique_ptr<ResetService> resetService;

#if MO_ENABLE_V16_RESERVATION
std::unique_ptr<ReservationService> reservationService;
#endif //MO_ENABLE_V16_RESERVATION

#if MO_ENABLE_CERT_MGMT
std::unique_ptr<CertificateService> certService;
#endif //MO_ENABLE_CERT_MGMT
Expand Down Expand Up @@ -110,8 +116,10 @@ class Model {
void setAuthorizationService(std::unique_ptr<AuthorizationService> authorizationService);
AuthorizationService *getAuthorizationService();

#if MO_ENABLE_V16_RESERVATION
void setReservationService(std::unique_ptr<ReservationService> reservationService);
ReservationService *getReservationService();
#endif //MO_ENABLE_V16_RESERVATION

void setBootService(std::unique_ptr<BootService> bs);
BootService *getBootService() const;
Expand Down
8 changes: 7 additions & 1 deletion src/MicroOcpp/Model/Reservation/Reservation.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// Copyright Matthias Akstaller 2019 - 2024
// MIT License

#include <MicroOcpp/Version.h>

#if MO_ENABLE_V16_RESERVATION

#include <MicroOcpp/Model/Reservation/Reservation.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Debug.h>
Expand Down Expand Up @@ -130,3 +134,5 @@ void Reservation::clear() {

configuration_save();
}

#endif //MO_ENABLE_V16_RESERVATION
11 changes: 8 additions & 3 deletions src/MicroOcpp/Model/Reservation/Reservation.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// Copyright Matthias Akstaller 2019 - 2024
// MIT License

#ifndef RESERVATION_H
#define RESERVATION_H
#ifndef MO_RESERVATION_H
#define MO_RESERVATION_H

#include <MicroOcpp/Version.h>

#if MO_ENABLE_V16_RESERVATION

#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Core/Time.h>
Expand Down Expand Up @@ -65,4 +69,5 @@ class Reservation {

}

#endif //MO_ENABLE_V16_RESERVATION
#endif
12 changes: 9 additions & 3 deletions src/MicroOcpp/Model/Reservation/ReservationService.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// Copyright Matthias Akstaller 2019 - 2024
// MIT License

#include <MicroOcpp/Version.h>

#if MO_ENABLE_V16_RESERVATION

#include <MicroOcpp/Model/Reservation/ReservationService.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Model/Model.h>
Expand All @@ -24,8 +28,8 @@ ReservationService::ReservationService(Context& context, unsigned int numConnect

reserveConnectorZeroSupportedBool = declareConfiguration<bool>("ReserveConnectorZeroSupported", true, CONFIGURATION_VOLATILE, true);

context.getOperationRegistry().registerOperation("CancelReservation", [&context] () {
return new Ocpp16::CancelReservation(context.getModel());});
context.getOperationRegistry().registerOperation("CancelReservation", [this] () {
return new Ocpp16::CancelReservation(*this);});
context.getOperationRegistry().registerOperation("ReserveNow", [&context] () {
return new Ocpp16::ReserveNow(context.getModel());});
}
Expand Down Expand Up @@ -210,3 +214,5 @@ bool ReservationService::updateReservation(int reservationId, unsigned int conne
MO_DBG_ERR("error finding blocking reservation");
return false;
}

#endif //MO_ENABLE_V16_RESERVATION
11 changes: 8 additions & 3 deletions src/MicroOcpp/Model/Reservation/ReservationService.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// Copyright Matthias Akstaller 2019 - 2024
// MIT License

#ifndef RESERVATIONSERVICE_H
#define RESERVATIONSERVICE_H
#ifndef MO_RESERVATIONSERVICE_H
#define MO_RESERVATIONSERVICE_H

#include <MicroOcpp/Version.h>

#if MO_ENABLE_V16_RESERVATION

#include <MicroOcpp/Model/Reservation/Reservation.h>

Expand Down Expand Up @@ -44,4 +48,5 @@ class ReservationService {

}

#endif //MO_ENABLE_V16_RESERVATION
#endif
21 changes: 11 additions & 10 deletions src/MicroOcpp/Operations/CancelReservation.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// Copyright Matthias Akstaller 2019 - 2024
// MIT License

#include <MicroOcpp/Version.h>

#if MO_ENABLE_V16_RESERVATION

#include <MicroOcpp/Operations/CancelReservation.h>
#include <MicroOcpp/Model/Model.h>
#include <MicroOcpp/Model/Reservation/ReservationService.h>
#include <MicroOcpp/Debug.h>

using MicroOcpp::Ocpp16::CancelReservation;

CancelReservation::CancelReservation(Model& model) : model(model) {
CancelReservation::CancelReservation(ReservationService& reservationService) : reservationService(reservationService) {

}

Expand All @@ -23,13 +26,9 @@ void CancelReservation::processReq(JsonObject payload) {
return;
}

if (model.getReservationService()) {
if (auto reservation = model.getReservationService()->getReservationById(payload["reservationId"])) {
found = true;
reservation->clear();
}
} else {
errorCode = "InternalError";
if (auto reservation = reservationService.getReservationById(payload["reservationId"])) {
found = true;
reservation->clear();
}
}

Expand All @@ -43,3 +42,5 @@ std::unique_ptr<DynamicJsonDocument> CancelReservation::createConf(){
}
return doc;
}

#endif //MO_ENABLE_V16_RESERVATION
Loading
Loading