From 66edab442a83423d4828107a2a0847b491856a17 Mon Sep 17 00:00:00 2001 From: Lucas DREZET Date: Thu, 23 Feb 2023 15:42:56 +0100 Subject: [PATCH 1/2] add new command to Charge Point to send on MQTT its OCPP config --- README.md | 15 +++++++++- src/chargepoint/mqtt/MqttManager.cpp | 42 ++++++++++++++++++++++++++++ src/chargepoint/mqtt/MqttManager.h | 5 ++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a9b8856..a6560b7 100644 --- a/README.md +++ b/README.md @@ -348,4 +348,17 @@ The expected command payload is : { "faulted": false } - ``` \ No newline at end of file + ``` + + +Each simulated Charge Point are listening to the following topic to execute a certain command: **cp_simu/cps/simu_cp_XXX/cmd**. + +The expected command payload is : + ``` + { + "type": "" + } + ``` +So for there are 2 commands: +* close: ask to end the application +* ocpp_config: ask to send on MQTT topic **cp_simu/cps/simu_cp_XXX/ocpp_config** all the OCPP config of the Charge Point \ No newline at end of file diff --git a/src/chargepoint/mqtt/MqttManager.cpp b/src/chargepoint/mqtt/MqttManager.cpp index 018f092..a791d75 100644 --- a/src/chargepoint/mqtt/MqttManager.cpp +++ b/src/chargepoint/mqtt/MqttManager.cpp @@ -99,6 +99,10 @@ void MqttManager::mqttMessageReceived(const char* topic, const std::string& mess std::cout << "Close command received" << std::endl; m_end = true; } + else if (strcmp(type, "ocpp_config") == 0) + { + publishOcppConfig(); + } } else { @@ -205,6 +209,7 @@ void MqttManager::start(unsigned int nb_phases, unsigned int max_charge_point_cu std::string chargepoint_tag_topics = chargepoint_topic + "connectors/+/id_tag"; std::string chargepoint_faulted_topics = chargepoint_topic + "connectors/+/faulted"; m_status_topic = chargepoint_topic + "status"; + m_ocpp_config_topic = chargepoint_topic + "ocpp_config"; m_connectors_topic = chargepoint_topic + "connectors/"; // MQTT client @@ -326,6 +331,43 @@ bool MqttManager::publishStatus(const std::string& status, unsigned int nb_phase return ret; } +/** @brief Publish the ocpp config of the connectors */ +void MqttManager::publishOcppConfig() +{ + // Check connectivity + if (m_mqtt->isConnected()) + { + // Compute topic name + std::stringstream topic; + topic << m_ocpp_config_topic; + + // Get vector of key/value for ocpp config + std::vector> keys; + std::vector values; + std::vector> unknown_values; + m_config.ocppConfig().getConfiguration(keys, values, unknown_values); + + // Create the JSON message + rapidjson::Document msg; + msg.Parse("{}"); + for (const ocpp::types::KeyValue keyValue : values) + { + if (!keyValue.value.value().empty()) + { + rapidjson::Value key(keyValue.key.c_str(), msg.GetAllocator()); + rapidjson::Value value(keyValue.value.value().c_str(), msg.GetAllocator()); + msg.AddMember(key, value, msg.GetAllocator()); + } + } + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + msg.Accept(writer); + + // Publish + m_mqtt->publish(topic.str(), buffer.GetString(), IMqttClient::QoS::QOS_0, true); + } +} + /** @brief Publish the data of the connectors */ void MqttManager::publishData(const std::vector& connectors) { diff --git a/src/chargepoint/mqtt/MqttManager.h b/src/chargepoint/mqtt/MqttManager.h index bb28920..1531795 100644 --- a/src/chargepoint/mqtt/MqttManager.h +++ b/src/chargepoint/mqtt/MqttManager.h @@ -77,6 +77,9 @@ class MqttManager : public IMqttClient::IListener /** @brief Publish the data of the connectors */ void publishData(const std::vector& connectors); + /** @brief Publish the ocpp config of the charge point */ + void publishOcppConfig(); + private: /** @brief Configuration */ SimulatedChargePointConfig& m_config; @@ -92,6 +95,8 @@ class MqttManager : public IMqttClient::IListener IMqttClient* m_mqtt; /** @brief Status topic */ std::string m_status_topic; + /** @brief Config topic */ + std::string m_ocpp_config_topic; /** @brief Connectors topic */ std::string m_connectors_topic; From 5031971355a9274d4bc1777c0735a8a32f20c895 Mon Sep 17 00:00:00 2001 From: LucasDREZET <33202916+LucasDREZET@users.noreply.github.com> Date: Mon, 27 Feb 2023 15:17:22 +0100 Subject: [PATCH 2/2] Fix english mistake in README Co-authored-by: c-jimenez <18682655+c-jimenez@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a6560b7..df8d631 100644 --- a/README.md +++ b/README.md @@ -359,6 +359,6 @@ The expected command payload is : "type": "" } ``` -So for there are 2 commands: +So far there are 2 commands: * close: ask to end the application * ocpp_config: ask to send on MQTT topic **cp_simu/cps/simu_cp_XXX/ocpp_config** all the OCPP config of the Charge Point \ No newline at end of file