Skip to content

Commit 42d927b

Browse files
committed
[central system] Finalize charge point request handler implementation + quick start example
1 parent c0cbf25 commit 42d927b

File tree

6 files changed

+399
-37
lines changed

6 files changed

+399
-37
lines changed

examples/common/DefaultCentralSystemEventsHandler.cpp

Lines changed: 237 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ SOFTWARE.
2525
#include "DefaultCentralSystemEventsHandler.h"
2626

2727
#include <iostream>
28+
#include <thread>
2829

2930
using namespace std;
3031
using namespace ocpp::centralsystem;
32+
using namespace ocpp::types;
3133

3234
/** @brief Constructor */
3335
DefaultCentralSystemEventsHandler::DefaultCentralSystemEventsHandler() : m_chargepoints() { }
@@ -43,19 +45,252 @@ bool DefaultCentralSystemEventsHandler::checkCredentials(const std::string& char
4345
cout << "Check credentials for [" << chargepoint_id << "] : " << password << endl;
4446
return true;
4547
}
46-
#include <thread>
48+
4749
/** @copydoc bool ICentralSystemEventsHandler::chargePointConnected(std::shared_ptr<ICentralSystem::IChargePoint>) */
4850
void DefaultCentralSystemEventsHandler::chargePointConnected(std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint> chargepoint)
4951
{
5052
cout << "Charge point [" << chargepoint->identifier() << "] connected" << endl;
5153
auto iter_chargepoint = m_chargepoints.find(chargepoint->identifier());
5254
if (iter_chargepoint == m_chargepoints.end())
5355
{
54-
m_chargepoints[chargepoint->identifier()] = chargepoint;
56+
m_chargepoints[chargepoint->identifier()] =
57+
std::shared_ptr<ChargePointRequestHandler>(new ChargePointRequestHandler(*this, chargepoint));
5558
}
5659
else
5760
{
5861
cout << "Charge point [" << chargepoint->identifier() << "] already connected" << endl;
5962
chargepoint.reset();
6063
}
6164
}
65+
66+
/** @brief Remove a charge point from the connected charge points */
67+
void DefaultCentralSystemEventsHandler::removeChargePoint(const std::string& identifier)
68+
{
69+
auto iter_chargepoint = m_chargepoints.find(identifier);
70+
if (iter_chargepoint != m_chargepoints.end())
71+
{
72+
m_chargepoints.erase(iter_chargepoint);
73+
}
74+
}
75+
76+
/** @brief Constructor */
77+
DefaultCentralSystemEventsHandler::ChargePointRequestHandler::ChargePointRequestHandler(
78+
DefaultCentralSystemEventsHandler& event_handler, std::shared_ptr<ocpp::centralsystem::ICentralSystem::IChargePoint>& chargepoint)
79+
: m_event_handler(event_handler), m_chargepoint(chargepoint)
80+
{
81+
m_chargepoint->registerHandler(*this);
82+
}
83+
84+
/** @brief Destructor */
85+
DefaultCentralSystemEventsHandler::ChargePointRequestHandler::~ChargePointRequestHandler() { }
86+
87+
// IChargePointRequestHandler interface
88+
89+
/** @copydoc void IChargePointRequestHandler::disconnected() */
90+
void DefaultCentralSystemEventsHandler::ChargePointRequestHandler::disconnected()
91+
{
92+
cout << "[" << m_chargepoint->identifier() << "] - Disconnected" << endl;
93+
std::thread t(
94+
[this]
95+
{
96+
std::this_thread::sleep_for(std::chrono::milliseconds(250));
97+
m_event_handler.removeChargePoint(m_chargepoint->identifier());
98+
});
99+
t.detach();
100+
}
101+
102+
/** @copydoc ocpp::types::IdTagInfo IChargePointRequestHandler::authorize(const std::string&) */
103+
ocpp::types::IdTagInfo DefaultCentralSystemEventsHandler::ChargePointRequestHandler::authorize(const std::string& id_tag)
104+
{
105+
cout << "[" << m_chargepoint->identifier() << "] - Authorize : " << id_tag << endl;
106+
107+
IdTagInfo tag_info;
108+
tag_info.status = AuthorizationStatus::Accepted;
109+
tag_info.expiryDate = DateTime(DateTime::now().timestamp() + 3600);
110+
return tag_info;
111+
}
112+
113+
/** @copydoc ocpp::types::RegistrationStatus IChargePointRequestHandler::bootNotification(const std::string&,
114+
const std::string&,
115+
const std::string&,
116+
const std::string&,
117+
const std::string&,
118+
const std::string&,
119+
const std::string&,
120+
const std::string&) */
121+
ocpp::types::RegistrationStatus DefaultCentralSystemEventsHandler::ChargePointRequestHandler::bootNotification(
122+
const std::string& model,
123+
const std::string& serial_number,
124+
const std::string& vendor,
125+
const std::string& firmware_version,
126+
const std::string& iccid,
127+
const std::string& imsi,
128+
const std::string& meter_serial_number,
129+
const std::string& meter_type)
130+
{
131+
(void)iccid;
132+
(void)imsi;
133+
(void)meter_serial_number;
134+
(void)meter_type;
135+
136+
cout << "[" << m_chargepoint->identifier() << "] - Boot notification : vendor = " << vendor << " - model = " << model
137+
<< " - s/n = " << serial_number << " - firmware = " << firmware_version << endl;
138+
139+
return RegistrationStatus::Accepted;
140+
}
141+
142+
/** @copydoc ocpp::types::DataTransferStatus IChargePointRequestHandler::dataTransfer(const std::string&,
143+
const std::string&,
144+
const std::string&,
145+
std::string&) */
146+
ocpp::types::DataTransferStatus DefaultCentralSystemEventsHandler::ChargePointRequestHandler::dataTransfer(const std::string& vendor_id,
147+
const std::string& message_id,
148+
const std::string& request_data,
149+
std::string& response_data)
150+
{
151+
cout << "[" << m_chargepoint->identifier() << "] - Data transfer : vendor = " << vendor_id << " - message = " << message_id
152+
<< " - data = " << request_data << endl;
153+
154+
response_data = "";
155+
return DataTransferStatus::UnknownVendorId;
156+
}
157+
158+
/** @copydoc void IChargePointRequestHandler::diagnosticStatusNotification(ocpp::types::DiagnosticsStatus) */
159+
void DefaultCentralSystemEventsHandler::ChargePointRequestHandler::diagnosticStatusNotification(ocpp::types::DiagnosticsStatus status)
160+
{
161+
cout << "[" << m_chargepoint->identifier() << "] - Diagnostic status notification : " << DiagnosticsStatusHelper.toString(status)
162+
<< endl;
163+
}
164+
165+
/** @copydoc void IChargePointRequestHandler::firmwareStatusNotification(ocpp::types::FirmwareStatus) */
166+
void DefaultCentralSystemEventsHandler::ChargePointRequestHandler::firmwareStatusNotification(ocpp::types::FirmwareStatus status)
167+
{
168+
cout << "[" << m_chargepoint->identifier() << "] - Firmware status notification : " << FirmwareStatusHelper.toString(status) << endl;
169+
}
170+
171+
/** @copydoc void IChargePointRequestHandler::meterValues(unsigned int,
172+
const ocpp::types::Optional<int>&,
173+
const std::vector<ocpp::types::MeterValue>&) */
174+
void DefaultCentralSystemEventsHandler::ChargePointRequestHandler::meterValues(unsigned int connector_id,
175+
const ocpp::types::Optional<int>& transaction_id,
176+
const std::vector<ocpp::types::MeterValue>& meter_values)
177+
{
178+
cout << "[" << m_chargepoint->identifier() << "] - Meter values : connector = " << connector_id
179+
<< " - transaction = " << (transaction_id.isSet() ? std::to_string(transaction_id) : "not set") << endl;
180+
for (const MeterValue& meter_value : meter_values)
181+
{
182+
cout << " - timestamp : " << meter_value.timestamp.str() << ", sampled values : " << endl;
183+
for (const SampledValue& sampled_value : meter_value.sampledValue)
184+
{
185+
cout << " - value = " << sampled_value.value;
186+
if (sampled_value.unit.isSet())
187+
{
188+
cout << ", unit = " << UnitOfMeasureHelper.toString(sampled_value.unit);
189+
}
190+
if (sampled_value.phase.isSet())
191+
{
192+
cout << ", phase = " << PhaseHelper.toString(sampled_value.phase);
193+
}
194+
if (sampled_value.measurand.isSet())
195+
{
196+
cout << ", measurand = " << MeasurandHelper.toString(sampled_value.measurand);
197+
}
198+
if (sampled_value.context.isSet())
199+
{
200+
cout << ", context = " << ReadingContextHelper.toString(sampled_value.context);
201+
}
202+
if (sampled_value.location.isSet())
203+
{
204+
cout << ", location = " << LocationHelper.toString(sampled_value.location);
205+
}
206+
if (sampled_value.format.isSet())
207+
{
208+
cout << ", format = " << ValueFormatHelper.toString(sampled_value.format);
209+
}
210+
cout << endl;
211+
}
212+
}
213+
}
214+
215+
/** @copydoc ocpp::types::IdTagInfo IChargePointRequestHandler::startTransaction(unsigned int,
216+
const std::string&,
217+
int,
218+
const ocpp::types::Optional<int>&,
219+
const ocpp::types::DateTime&,
220+
int&) */
221+
ocpp::types::IdTagInfo DefaultCentralSystemEventsHandler::ChargePointRequestHandler::startTransaction(
222+
unsigned int connector_id,
223+
const std::string& id_tag,
224+
int meter_start,
225+
const ocpp::types::Optional<int>& reservation_id,
226+
const ocpp::types::DateTime& timestamp,
227+
int& transaction_id)
228+
{
229+
static int current_transaction_id = 1;
230+
231+
cout << "[" << m_chargepoint->identifier() << "] - Start transaction : connector = " << connector_id << " - id tag = " << id_tag
232+
<< " - meter start = " << meter_start
233+
<< " - reservation = " << (reservation_id.isSet() ? std::to_string(reservation_id) : "not set")
234+
<< " - timestamp = " << timestamp.str() << endl;
235+
236+
transaction_id = current_transaction_id;
237+
current_transaction_id++;
238+
239+
IdTagInfo tag_info;
240+
tag_info.status = AuthorizationStatus::Accepted;
241+
tag_info.expiryDate = DateTime(DateTime::now().timestamp() + 3600);
242+
return tag_info;
243+
}
244+
245+
/** @copydoc void IChargePointRequestHandler::statusNotification(unsigned int,
246+
ocpp::types::ChargePointErrorCode,
247+
const std::string&,
248+
ocpp::types::ChargePointStatus,
249+
const ocpp::types::DateTime&,
250+
const std::string&,
251+
const std::string&) */
252+
void DefaultCentralSystemEventsHandler::ChargePointRequestHandler::statusNotification(unsigned int connector_id,
253+
ocpp::types::ChargePointErrorCode error_code,
254+
const std::string& info,
255+
ocpp::types::ChargePointStatus status,
256+
const ocpp::types::DateTime& timestamp,
257+
const std::string& vendor_id,
258+
const std::string& vendor_error)
259+
{
260+
(void)vendor_id;
261+
(void)vendor_error;
262+
263+
cout << "[" << m_chargepoint->identifier() << "] - Status notification : connector = " << connector_id
264+
<< " - status = " << ChargePointStatusHelper.toString(status) << " - error = " << ChargePointErrorCodeHelper.toString(error_code)
265+
<< " - info = " << info << " - timestamp = " << ((timestamp == DateTime(0)) ? "not set" : timestamp.str()) << endl;
266+
}
267+
268+
/** @copydoc ocpp::types::Optional<ocpp::types::IdTagInfo> IChargePointRequestHandler::stopTransaction(
269+
const std::string&,
270+
int,
271+
const ocpp::types::DateTime&,
272+
int,
273+
ocpp::types::Reason,
274+
const std::vector<ocpp::types::MeterValue>&) */
275+
ocpp::types::Optional<ocpp::types::IdTagInfo> DefaultCentralSystemEventsHandler::ChargePointRequestHandler::stopTransaction(
276+
const std::string& id_tag,
277+
int meter_stop,
278+
const ocpp::types::DateTime& timestamp,
279+
int transaction_id,
280+
ocpp::types::Reason reason,
281+
const std::vector<ocpp::types::MeterValue>& transaction_data)
282+
{
283+
(void)transaction_data;
284+
cout << "[" << m_chargepoint->identifier() << "] - Stop transaction : transaction = " << transaction_id
285+
<< " - id tag = " << (id_tag.empty() ? "not set" : id_tag) << " - meter stop = " << meter_stop
286+
<< " - reason = " << ReasonHelper.toString(reason) << " - timestamp = " << timestamp.str() << endl;
287+
288+
ocpp::types::Optional<ocpp::types::IdTagInfo> ret;
289+
if (!id_tag.empty())
290+
{
291+
IdTagInfo& tag_info = ret.value();
292+
tag_info.status = AuthorizationStatus::Accepted;
293+
tag_info.expiryDate = DateTime(DateTime::now().timestamp() + 3600);
294+
}
295+
return ret;
296+
}

0 commit comments

Comments
 (0)