Skip to content
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
38 changes: 32 additions & 6 deletions examples/common/DefaultCentralSystemEventsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ using namespace ocpp::x509;
DefaultCentralSystemEventsHandler::DefaultCentralSystemEventsHandler(std::filesystem::path iso_v2g_root_ca,
std::filesystem::path iso_mo_root_ca,
bool set_pending_status)
: m_iso_v2g_root_ca(iso_v2g_root_ca),
: m_chargepoints_mutex(),
m_iso_v2g_root_ca(iso_v2g_root_ca),
m_iso_mo_root_ca(iso_mo_root_ca),
m_set_pending_status(set_pending_status),
m_chargepoints(),
Expand Down Expand Up @@ -83,6 +84,9 @@ bool DefaultCentralSystemEventsHandler::checkCredentials(const std::string& char
void DefaultCentralSystemEventsHandler::chargePointConnected(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
{
cout << "Charge point [" << chargepoint->identifier() << "] connected" << endl;

std::lock_guard<std::mutex> lock(m_chargepoints_mutex);

auto iter_chargepoint = m_chargepoints.find(chargepoint->identifier());
if (iter_chargepoint == m_chargepoints.end())
{
Expand All @@ -103,13 +107,38 @@ void DefaultCentralSystemEventsHandler::removeChargePoint(const std::string& ide
[this, identifier = identifier]
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));

std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
m_chargepoints.erase(identifier);
m_pending_chargepoints.erase(identifier);
m_accepted_chargepoints.erase(identifier);
});
t.detach();
}

/** @brief Indicate if a charge point must be accepted */
bool DefaultCentralSystemEventsHandler::isAcceptedChargePoint(const std::string& identifier)
{
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
return (m_accepted_chargepoints.find(identifier) != m_accepted_chargepoints.end());
}

/** @brief Add a charge point to the pending list */
void DefaultCentralSystemEventsHandler::addPendingChargePoint(
std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
{
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
m_pending_chargepoints[chargepoint->identifier()] = chargepoint;
}

/** @brief Add a charge point to the accepted list */
void DefaultCentralSystemEventsHandler::addAcceptedChargePoint(
std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
{
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
m_accepted_chargepoints[chargepoint->identifier()] = chargepoint;
}

/** @brief Constructor */
DefaultCentralSystemEventsHandler::ChargePointRequestHandler::ChargePointRequestHandler(
DefaultCentralSystemEventsHandler& event_handler, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>& chargepoint)
Expand Down Expand Up @@ -170,12 +199,9 @@ ocpp::types::RegistrationStatus DefaultCentralSystemEventsHandler::ChargePointRe
ocpp::types::RegistrationStatus ret = RegistrationStatus::Accepted;
if (m_event_handler.setPendingEnabled())
{
auto accepted_chargepoint = m_event_handler.acceptedChargePoints();
auto iter_accepted = accepted_chargepoint.find(m_chargepoint->identifier());
if (iter_accepted == accepted_chargepoint.end())
if (!m_event_handler.isAcceptedChargePoint(m_chargepoint->identifier()))
{
m_event_handler.pendingChargePoints()[m_chargepoint->identifier()] = m_chargepoint;

m_event_handler.addPendingChargePoint(m_chargepoint);
ret = RegistrationStatus::Pending;
}
}
Expand Down
34 changes: 26 additions & 8 deletions examples/common/DefaultCentralSystemEventsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SOFTWARE.

#include <filesystem>
#include <map>
#include <mutex>

/** @brief Default central system event handlers implementation for the examples */
class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSystemEventsHandler
Expand Down Expand Up @@ -237,19 +238,25 @@ class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSy
std::string m_generated_certificate;
};

/** @brief Get the list of the connected charge points */
std::map<std::string, std::shared_ptr<ChargePointRequestHandler>>& chargePoints() { return m_chargepoints; }
/** @brief Get the number connected charge points */
size_t chargePointsCount()
{
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
return m_chargepoints.size();
}

/** @brief Get the list of the pending charge points */
std::map<std::string, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>>& pendingChargePoints()
/** @brief Get the list of the connected charge points */
std::map<std::string, std::shared_ptr<ChargePointRequestHandler>> chargePoints()
{
return m_pending_chargepoints;
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
return m_chargepoints;
}

/** @brief Get the list of the accepted charge points */
std::map<std::string, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>>& acceptedChargePoints()
/** @brief Get the list of the pending charge points */
std::map<std::string, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>> pendingChargePoints()
{
return m_accepted_chargepoints;
std::lock_guard<std::mutex> lock(m_chargepoints_mutex);
return m_pending_chargepoints;
}

/** @brief Path to the V2G root CA */
Expand All @@ -263,7 +270,18 @@ class DefaultCentralSystemEventsHandler : public ocpp::centralsystem::ICentralSy
/** @brief Remove a charge point from the connected charge points */
void removeChargePoint(const std::string& identifier);

/** @brief Indicate if a charge point must be accepted */
bool isAcceptedChargePoint(const std::string& identifier);

/** @brief Add a charge point to the pending list */
void addPendingChargePoint(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint);

/** @brief Add a charge point to the accepted list */
void addAcceptedChargePoint(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint);

protected:
/** @brief Mutex for charge point list */
std::mutex m_chargepoints_mutex;
/** @brief Path to the V2G root CA */
std::filesystem::path m_iso_v2g_root_ca;
/** @brief Path to the MO root CA */
Expand Down
10 changes: 5 additions & 5 deletions examples/iso15118_centralsystem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ int main(int argc, char* argv[])
while (true)
{
// For each pending charge point
for (auto& iter_chargepoint : event_handler.pendingChargePoints())
auto pending_chargepoints = event_handler.pendingChargePoints();
for (auto& iter_chargepoint : pending_chargepoints)
{
auto chargepoint = iter_chargepoint.second;
auto iter_accepted = event_handler.acceptedChargePoints().find(chargepoint->identifier());
if (iter_accepted == event_handler.acceptedChargePoints().end())
auto chargepoint = iter_chargepoint.second;
if (!event_handler.isAcceptedChargePoint(chargepoint->identifier()))
{
std::cout << "---------------------------------------------" << std::endl;
std::cout << "Pending Charge point : " << chargepoint->identifier() << std::endl;
Expand Down Expand Up @@ -239,7 +239,7 @@ int main(int argc, char* argv[])
}

// Accept charge point
event_handler.acceptedChargePoints()[chargepoint->identifier()] = chargepoint;
event_handler.addAcceptedChargePoint(chargepoint);

// Trigger a boot notification to force it to update its registration status
chargepoint->triggerMessage(MessageTrigger::BootNotification, Optional<unsigned int>());
Expand Down
3 changes: 2 additions & 1 deletion examples/quick_start_centralsystem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ int main(int argc, char* argv[])
std::this_thread::sleep_for(std::chrono::seconds(1));

// For each connected charge point
for (auto& iter_chargepoint : event_handler.chargePoints())
auto connected_chargepoints = event_handler.chargePoints();
for (auto& iter_chargepoint : connected_chargepoints)
{
{
auto chargepoint = iter_chargepoint.second->proxy();
Expand Down