Skip to content
Snippets Groups Projects
Commit 1e52527a authored by Matthias Stefan Bodenbenner's avatar Matthias Stefan Bodenbenner
Browse files

Merge branch 'mqtt-error-handling' into 'main'

handle unconnected client cleanup, empty root config & use json::parse

See merge request !3
parents dde42d5f d9a0fe39
Branches main
No related tags found
1 merge request!3handle unconnected client cleanup, empty root config & use json::parse
Pipeline #472564 passed
......@@ -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");
......
......@@ -80,9 +80,15 @@ MQTT::Publisher::Publisher(std::string id, unsigned int buffer) : id(id), buffer
MQTT::Publisher::~Publisher()
{
if (this->is_connected())
{
this->disconnect();
}
if (worker != nullptr)
{
worker->detach();
}
instances--;
if (instances == 0)
{
......@@ -169,12 +175,15 @@ void MQTT::Publisher::disconnect(unsigned int timeout)
void MQTT::Publisher::set_root_topic(std::string root_topic)
{
configuration.root = root_topic;
if (configuration.root.length() > 0)
{
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 (configuration.root.length() > 0)
{
std::string::iterator last_character = configuration.root.end() - 1;
if ((*last_character) != '/')
{
this->configuration.root += "/";
configuration.root += "/";
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment