Skip to content

Commit

Permalink
Review comments. Some changes in functions to make it more readable.
Browse files Browse the repository at this point in the history
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
  • Loading branch information
maaikez committed Nov 13, 2024
1 parent ae1d21a commit d7c7792
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 43 deletions.
20 changes: 11 additions & 9 deletions include/ocpp/v201/charge_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,18 +582,20 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
void set_evse_connectors_unavailable(EvseInterface& evse, bool persist);

///
/// \brief Get connector status.
/// \brief Check if there is a connector available with the given connector type.
/// \param evse_id The evse to check for.
/// \param connector_type The connector type.
/// \return True when a connector is available and the evse id exists.
///
/// This will search if there is a connector on this evse with status 'Available'. It will search through all
/// connectors, optionally filtering by connector type, and return on the first connector that is 'Available'. If
/// there is no 'Available' connector, it will return the status of one of the connectors.
bool is_connector_available(const uint32_t evse_id, std::optional<ConnectorEnum> connector_type);

///
/// \param evse_id The evse id.
/// \param connector_type The connector type to filter on (optional).
/// \return Connector status. If connector type is given and does not exist, std::nullopt.
/// \brief Check if the connector exists on the given evse id.
/// \param evse_id The evse id to check for.
/// \param connector_type The connector type.
/// \return False if evse id does not exist or evse does not have the given connector type.
///
std::optional<ConnectorStatusEnum> get_connector_status(const uint32_t evse_id,
std::optional<ConnectorEnum> connector_type);
bool does_connector_exist(const uint32_t evse_id, std::optional<ConnectorEnum> connector_type);

/// \brief Get the value optional offline flag
/// \return true if the charge point is offline. std::nullopt if it is online;
Expand Down
18 changes: 11 additions & 7 deletions include/ocpp/v201/evse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ class EvseInterface {
/// \return
virtual uint32_t get_number_of_connectors() const = 0;

///
/// \brief Check if the given connector type exists on this evse.
/// \param connector_type The connector type to check.
/// \return True if connector type is unknown or this evse has the given connector type.
///
virtual bool does_connector_exist(ConnectorEnum connector_type) = 0;

///
/// \brief Get connector status.
///
/// This will search if there is a connector on this evse with status 'Available'. It will search through all
/// connectors, optionally filtering by connector type, and return on the first connector that is 'Available'. If
/// there is no 'Available' connector, it will return the status of one of the connectors.
///
/// \param evse_id The evse id.
/// \param connector_type The connector type to filter on (optional).
/// \return Connector status. If connector type is given and does not exist, std::nullopt.
///
virtual std::optional<ConnectorStatusEnum> get_connector_status(const uint32_t evse_id,
std::optional<ConnectorEnum> connector_type) = 0;
virtual std::optional<ConnectorStatusEnum> get_connector_status(std::optional<ConnectorEnum> connector_type) = 0;

/// \brief Opens a new transaction
/// \param transaction_id id of the transaction
Expand Down Expand Up @@ -214,11 +219,10 @@ class Evse : public EvseInterface {

///
/// \brief Get connector type of Connector
/// \param evse_id EVSE id
/// \param connector_id Connector id
/// \return The connector type. If evse or connector id is not correct: std::nullopt.
///
std::optional<ConnectorEnum> get_evse_connector_type(const uint32_t evse_id, const uint32_t connector_id);
std::optional<ConnectorEnum> get_evse_connector_type(const uint32_t connector_id);

public:
/// \brief Construct a new Evse object
Expand All @@ -242,8 +246,8 @@ class Evse : public EvseInterface {
int32_t get_id() const;

uint32_t get_number_of_connectors() const;
std::optional<ConnectorStatusEnum> get_connector_status(const uint32_t evse_id,
std::optional<ConnectorEnum> connector_type);
bool does_connector_exist(const ConnectorEnum connector_type) override;
std::optional<ConnectorStatusEnum> get_connector_status(std::optional<ConnectorEnum> connector_type) override;

void open_transaction(const std::string& transaction_id, const int32_t connector_id, const DateTime& timestamp,
const MeterValue& meter_start, const std::optional<IdToken>& id_token,
Expand Down
48 changes: 31 additions & 17 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,17 +1997,34 @@ void ChargePoint::set_evse_connectors_unavailable(EvseInterface& evse, bool pers
}
}

std::optional<ConnectorStatusEnum> ChargePoint::get_connector_status(const uint32_t evse_id,
std::optional<ConnectorEnum> connector_type) {
bool ChargePoint::is_connector_available(const uint32_t evse_id, std::optional<ConnectorEnum> connector_type) {
EvseInterface* evse;
try {
evse = &evse_manager->get_evse(static_cast<int32_t>(evse_id));
} catch (const EvseOutOfRangeException&) {
EVLOG_error << "Evse id " << evse_id << " is not a valid evse id.";
return std::nullopt;
return false;
}

std::optional<ConnectorStatusEnum> status =
evse->get_connector_status(connector_type.value_or(ConnectorEnum::Unknown));
if (!status.has_value()) {
return false;
}

return evse->get_connector_status(evse_id, connector_type);
return status.value() == ConnectorStatusEnum::Available;
}

bool ChargePoint::does_connector_exist(const uint32_t evse_id, std::optional<ConnectorEnum> connector_type) {
EvseInterface* evse;
try {
evse = &evse_manager->get_evse(static_cast<int32_t>(evse_id));
} catch (const EvseOutOfRangeException&) {
EVLOG_error << "Evse id " << evse_id << " is not a valid evse id.";
return false;
}

return evse->does_connector_exist(connector_type.value_or(ConnectorEnum::Unknown));
}

bool ChargePoint::is_offline() {
Expand Down Expand Up @@ -3375,10 +3392,7 @@ void ChargePoint::handle_reserve_now_request(Call<ReserveNowRequest> call) {
}

// Check if there is a connector available for this evse id.
std::optional<ConnectorStatusEnum> status =
get_connector_status(static_cast<uint32_t>(evse_id.value()), request.connectorType);

if (!status.has_value()) {
if (!does_connector_exist(static_cast<uint32_t>(evse_id.value()), request.connectorType)) {
EVLOG_info << "Trying to make a reservation for connector type "
<< conversions::connector_enum_to_string(request.connectorType.value_or(ConnectorEnum::Unknown))
<< " for evse " << evse_id.value() << ", but this connector type does not exist.";
Expand All @@ -3394,19 +3408,19 @@ void ChargePoint::handle_reserve_now_request(Call<ReserveNowRequest> call) {
return;
}

bool status_found = false;
bool connector_exists = false;
for (uint64_t i = 1; i <= number_of_evses; i++) {
std::optional<ConnectorStatusEnum> status = get_connector_status(i, request.connectorType);
if (status.has_value()) {
status_found = true;
if (status.value() == ConnectorStatusEnum::Available) {
// There is at least one connector available!
break;
}
if (this->does_connector_exist(i, request.connectorType)) {
connector_exists = true;
}

if (this->is_connector_available(i, request.connectorType)) {
// There is at least one connector available!
break;
}
}

if (!status_found) {
if (!connector_exists) {
send_reserve_now_rejected_response(call.uniqueId, "Could not get status info from connector");
return;
}
Expand Down
52 changes: 42 additions & 10 deletions lib/ocpp/v201/evse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,40 @@ uint32_t Evse::get_number_of_connectors() const {
return static_cast<uint32_t>(this->id_connector_map.size());
}

std::optional<ConnectorStatusEnum> Evse::get_connector_status(const uint32_t evse_id,
std::optional<ConnectorEnum> connector_type) {
bool Evse::does_connector_exist(const ConnectorEnum connector_type) {
const uint32_t number_of_connectors = this->get_number_of_connectors();
if (number_of_connectors == 0) {
return false;
}

if (connector_type == ConnectorEnum::Unknown) {
return true;
}

for (uint32_t i = 1; i <= number_of_connectors; ++i) {
Connector* connector;
try {
connector = this->get_connector(static_cast<int32_t>(i));
} catch (const std::logic_error&) {
EVLOG_error << "Connector with id " << i << " does not exist";
continue;
}

if (connector == nullptr) {
EVLOG_error << "Connector with id " << i << " does not exist";
continue;
}

ConnectorEnum type = this->get_evse_connector_type(i).value_or(ConnectorEnum::Unknown);
if (type == ConnectorEnum::Unknown || type == connector_type) {
return true;
}
}

return false;
}

std::optional<ConnectorStatusEnum> Evse::get_connector_status(std::optional<ConnectorEnum> connector_type) {
bool type_found = false;
ConnectorStatusEnum found_status = ConnectorStatusEnum::Unavailable;
const uint32_t number_of_connectors = this->get_number_of_connectors();
Expand All @@ -111,8 +143,7 @@ std::optional<ConnectorStatusEnum> Evse::get_connector_status(const uint32_t evs

const ConnectorStatusEnum connector_status = connector->get_effective_connector_status();

const std::optional<ConnectorEnum> evse_connector_type =
this->get_evse_connector_type(static_cast<uint32_t>(evse_id), i);
const std::optional<ConnectorEnum> evse_connector_type = this->get_evse_connector_type(i);
if (!connector_type.has_value() ||
(!evse_connector_type.has_value() || evse_connector_type.value() == connector_type.value())) {
type_found = true;
Expand Down Expand Up @@ -171,15 +202,15 @@ void Evse::delete_database_transaction() {
}
}

std::optional<ConnectorEnum> Evse::get_evse_connector_type(const uint32_t evse_id, const uint32_t connector_id) {
std::optional<ConnectorEnum> Evse::get_evse_connector_type(const uint32_t connector_id) {

auto connector = this->get_connector(static_cast<int32_t>(connector_id));
if (connector == nullptr) {
return std::nullopt;
}

ComponentVariable connector_cv =
ConnectorComponentVariables::get_component_variable(evse_id, connector_id, ConnectorComponentVariables::Type);
ComponentVariable connector_cv = ConnectorComponentVariables::get_component_variable(
this->evse_id, connector_id, ConnectorComponentVariables::Type);

const std::optional<std::string> connector_type =
this->device_model.get_optional_value<std::string>(connector_cv, AttributeEnum::Actual);
Expand Down Expand Up @@ -226,7 +257,8 @@ void Evse::open_transaction(const std::string& transaction_id, const int32_t con
try {
this->database_handler->transaction_insert(*this->transaction.get(), this->evse_id);
} catch (const QueryExecutionException& e) {
// Delete previous transactions that should not exist anyway and try again. Otherwise throw to higher level
// Delete previous transactions that should not exist anyway and try again. Otherwise throw to higher
// level
this->delete_database_transaction();
this->database_handler->transaction_insert(*this->transaction.get(), this->evse_id);
}
Expand Down Expand Up @@ -561,8 +593,8 @@ void Evse::send_meter_value_on_pricing_trigger(const MeterValue& meter_value) {
}
}

// Check if there is a power kw trigger and if that is triggered. For the power kw trigger, we added hysterisis to
// prevent constant triggering.
// Check if there is a power kw trigger and if that is triggered. For the power kw trigger, we added hysterisis
// to prevent constant triggering.
const std::optional<float> active_power_meter_value = utils::get_total_power_active_import(meter_value);

if (!this->trigger_metervalue_on_power_kw.has_value() or !active_power_meter_value.has_value()) {
Expand Down

0 comments on commit d7c7792

Please sign in to comment.