@@ -78,7 +78,7 @@ void MqttManager::mqttMessageReceived(const char* topic, const std::string& mess
7878 }
7979 else
8080 {
81- std::cout << " topic: " << topic << " \n payload : " << message << std::endl;
81+ std::cout << " topic: " << topic << std::endl << " payload : " << message << std::endl;
8282 // Split topic name
8383 std::filesystem::path topic_path (topic);
8484
@@ -322,7 +322,6 @@ void MqttManager::publishData(const std::vector<ConnectorData>& connectors)
322322 std::stringstream topic;
323323 topic << m_connectors_topic << connector.id << " /status" ;
324324
325-
326325 // Create the JSON message
327326 rapidjson::Document msg;
328327 msg.Parse (" {}" );
@@ -341,25 +340,12 @@ void MqttManager::publishData(const std::vector<ConnectorData>& connectors)
341340 msg.AddMember (rapidjson::StringRef (" car_cable_capacity" ), rapidjson::Value (connector.car_cable_capacity ), msg.GetAllocator ());
342341 msg.AddMember (rapidjson::StringRef (" car_ready" ), rapidjson::Value (connector.car_ready ), msg.GetAllocator ());
343342
343+ static const char * consumption_str[] = {" consumption_l1" , " consumption_l2" , " consumption_l3" };
344344 std::vector<float > currents = connector.meter ->getCurrents ();
345345 unsigned int nb_phases = connector.meter ->getNumberOfPhases ();
346- if (nb_phases == 1 )
347- {
348- msg.AddMember (rapidjson::StringRef (" consumption_l1" ), rapidjson::Value (currents[0 ]), msg.GetAllocator ());
349- msg.AddMember (rapidjson::StringRef (" consumption_l2" ), rapidjson::Value (0 ), msg.GetAllocator ());
350- msg.AddMember (rapidjson::StringRef (" consumption_l3" ), rapidjson::Value (0 ), msg.GetAllocator ());
351- }
352- else if (nb_phases == 2 )
353- {
354- msg.AddMember (rapidjson::StringRef (" consumption_l1" ), rapidjson::Value (currents[0 ]), msg.GetAllocator ());
355- msg.AddMember (rapidjson::StringRef (" consumption_l2" ), rapidjson::Value (currents[1 ]), msg.GetAllocator ());
356- msg.AddMember (rapidjson::StringRef (" consumption_l3" ), rapidjson::Value (0 ), msg.GetAllocator ());
357- }
358- else if (nb_phases == 3 )
346+ for (unsigned int i = 0 ; i < nb_phases ; i++)
359347 {
360- msg.AddMember (rapidjson::StringRef (" consumption_l1" ), rapidjson::Value (currents[0 ]), msg.GetAllocator ());
361- msg.AddMember (rapidjson::StringRef (" consumption_l2" ), rapidjson::Value (currents[1 ]), msg.GetAllocator ());
362- msg.AddMember (rapidjson::StringRef (" consumption_l3" ), rapidjson::Value (currents[2 ]), msg.GetAllocator ());
348+ msg.AddMember (rapidjson::StringRef (consumption_str[i]), rapidjson::Value (currents[i]), msg.GetAllocator ());
363349 }
364350
365351 rapidjson::StringBuffer buffer;
0 commit comments