diff --git a/src/MQTT/Configuration.cpp b/src/MQTT/Configuration.cpp index c67f0230af7af5df73d80b2b081e3b673d05de86..658edd19bd0ff14f77518d878d93804e35bd8466 100644 --- a/src/MQTT/Configuration.cpp +++ b/src/MQTT/Configuration.cpp @@ -27,8 +27,7 @@ MQTT::Configuration::Configuration() MQTT::Configuration::Configuration(std::string filename) { std::ifstream infile(filename); - json j; - infile >> j; + json j = json::parse(infile); host = j.value("host", "127.0.0.1"); port = j.value("port", 1883); username = j.value("username", "guest"); diff --git a/src/MQTT/Publisher.cpp b/src/MQTT/Publisher.cpp index cc2eb0a9813e0a6060b47acdc7060e00f0eb7fc7..2333434c7c09178cc457f8e15a8152c0cb06bebc 100644 --- a/src/MQTT/Publisher.cpp +++ b/src/MQTT/Publisher.cpp @@ -81,8 +81,14 @@ MQTT::Publisher::Publisher(std::string id, unsigned int buffer) : id(id), buffer MQTT::Publisher::~Publisher() { - this->disconnect(); - worker->detach(); + if (this->is_connected()) + { + this->disconnect(); + } + if (worker != nullptr) + { + worker->detach(); + } instances--; if (instances == 0) { @@ -169,11 +175,14 @@ void MQTT::Publisher::disconnect(unsigned int timeout) void MQTT::Publisher::set_root_topic(std::string root_topic) { configuration.root = root_topic; - std::string::iterator last_character = configuration.root.end() - 1; - if ((*last_character) != '/') + if (configuration.root.length() > 0) { - configuration.root += "/"; - } + std::string::iterator last_character = configuration.root.end() - 1; + if ((*last_character) != '/') + { + configuration.root += "/"; + } + } } void MQTT::Publisher::set_buffer(unsigned int buffer) @@ -183,6 +192,10 @@ void MQTT::Publisher::set_buffer(unsigned int buffer) bool MQTT::Publisher::is_connected(void) { + if (this->client == nullptr) + { + return false; + } try { return client->is_connected(); @@ -196,10 +209,13 @@ bool MQTT::Publisher::is_connected(void) void MQTT::Publisher::configure(MQTT::Configuration configuration) { this->configuration = configuration; - std::string::iterator last_character = this->configuration.root.end() - 1; - if ((*last_character) != '/') + if (configuration.root.length() > 0) { - this->configuration.root += "/"; + std::string::iterator last_character = configuration.root.end() - 1; + if ((*last_character) != '/') + { + configuration.root += "/"; + } } }