Skip to content

Commit

Permalink
[messages-rpc] Use rapidjson to set decimal precision to 1 digit + co…
Browse files Browse the repository at this point in the history
…de format
  • Loading branch information
c-jimenez committed Mar 10, 2024
1 parent 208562b commit 90409e6
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 40 deletions.
6 changes: 3 additions & 3 deletions examples/common/DefaultChargePointEventsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ bool DefaultChargePointEventsHandler::remoteStartTransactionRequested(unsigned i
bool ret = false;
cout << "Remote start transaction : " << connector_id << " - " << id_tag << endl;

if(connector_id > m_config.ocppConfig().numberOfConnectors() || connector_id == 0)
if (connector_id > m_config.ocppConfig().numberOfConnectors() || connector_id == 0)
{
ret=false;
ret = false;
}
else
{
m_remote_start_pending[connector_id - 1u] = true;
m_remote_start_id_tag[connector_id - 1u] = id_tag;
ret=true;
ret = true;
}
return ret;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/common/config/OcppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ ocpp::types::ConfigurationStatus OcppConfig::setConfiguration(const std::string&
}
}

if (ret != ConfigurationStatus::Rejected)
{
if (ret != ConfigurationStatus::Rejected)
{
if ((it->second & PARAM_OCPP) != 0)
{
m_config.set(OCPP_PARAMS, key, value);
Expand All @@ -225,7 +225,7 @@ ocpp::types::ConfigurationStatus OcppConfig::setConfiguration(const std::string&
{
ret = ConfigurationStatus::Accepted;
}
}
}
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/chargepoint/smartcharging/ProfileDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ std::string ProfileDatabase::serialize(const ocpp::types::ChargingProfile& profi

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.SetMaxDecimalPlaces(1); // OCPP decimals have 1 digit precision
profile_json.Accept(writer);
profile_str = buffer.GetString();
return profile_str;
Expand Down
2 changes: 1 addition & 1 deletion src/chargepoint/smartcharging/SmartChargingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ bool SmartChargingManager::handleMessage(const ocpp::messages::SetChargingProfil
}
else
{
ret = false;
ret = false;
error_message = "Recurring profiles must have a start schedule and a duration";
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/messages/StatusNotification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,12 @@ bool StatusNotificationReqConverter::toJson(const StatusNotificationReq& data, r
fill(json, "errorCode", ChargePointErrorCodeHelper.toString(data.errorCode));
fill(json, "status", ChargePointStatusHelper.toString(data.status));
fill(json, "timestamp", data.timestamp);
//if (data.errorCode != ChargePointErrorCode::NoError)
//{
if(data.info.isSet())
fill(json, "info", data.info);
if(data.vendorId.isSet())
fill(json, "vendorId", data.vendorId);
if(data.vendorErrorCode.isSet())
fill(json, "vendorErrorCode", data.vendorErrorCode);
// }
if (data.info.isSet())
fill(json, "info", data.info);
if (data.vendorId.isSet())
fill(json, "vendorId", data.vendorId);
if (data.vendorErrorCode.isSet())
fill(json, "vendorErrorCode", data.vendorErrorCode);
return true;
}

Expand Down
25 changes: 2 additions & 23 deletions src/messages/types/ChargingScheduleConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,22 @@ bool ChargingScheduleConverter::fromJson(const rapidjson::Value& json,
return ret;
}


double getDecimal(float data)
{
std::stringstream m_stringstream;
std::string m_string;

m_stringstream.str("");
m_stringstream.clear();
m_stringstream << std::fixed << std::setprecision(1) << data;
m_string = m_stringstream.str();

double res = std::stod(m_string);

return res;
}

/** @copydoc bool IMessageConverter<ocpp::types::ChargingSchedule>::toJson(const ocpp::types::ChargingSchedule&,
* rapidjson::Document&) */
bool ChargingScheduleConverter::toJson(const ocpp::types::ChargingSchedule& data, rapidjson::Document& json)
{
std::stringstream m_stringstream;
std::string m_string;


fill(json, "startSchedule", data.startSchedule);
fill(json, "duration", data.duration);
fill(json, "chargingRateUnit", ChargingRateUnitTypeHelper.toString(data.chargingRateUnit));
fill(json, "minChargingRate", getDecimal(data.minChargingRate));

fill(json, "minChargingRate", data.minChargingRate);

rapidjson::Value chargingSchedulePeriod(rapidjson::kArrayType);
for (const ChargingSchedulePeriod& schedule_period : data.chargingSchedulePeriod)
{
rapidjson::Document value;
value.Parse("{}");
fill(value, "startPeriod", schedule_period.startPeriod);
fill(value, "limit", getDecimal(schedule_period.limit));
fill(value, "limit", schedule_period.limit);
fill(value, "numberPhases", schedule_period.numberPhases);
chargingSchedulePeriod.PushBack(value.Move(), *allocator);
}
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/RpcBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ bool RpcBase::call(const std::string& action,
// Serialize message
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.SetMaxDecimalPlaces(1); // OCPP decimals have 1 digit precision
payload.Accept(writer);

std::stringstream serialized_message;
Expand Down Expand Up @@ -186,6 +187,7 @@ void RpcBase::processIncomingRequest(std::shared_ptr<RpcMessage>& rpc_message)
// Serialize message
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.SetMaxDecimalPlaces(1); // OCPP decimals have 1 digit precision
response.Accept(writer);

std::stringstream serialized_message;
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/libwebsockets/LibWebsocketClientPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void LibWebsocketClientPool::process()

// Dummy vhost to handle context related events
struct lws_protocols protocols[] = {{"LibWebsocketClientPool", &LibWebsocketClientPool::eventCallback, 0, 0, 0, this, 0},
LWS_PROTOCOL_LIST_TERM};
LWS_PROTOCOL_LIST_TERM};
struct lws_context_creation_info vhost_info;
memset(&vhost_info, 0, sizeof(vhost_info));
vhost_info.protocols = protocols;
Expand Down

0 comments on commit 90409e6

Please sign in to comment.