26 #include "modules/mqtt/MqttConfig_odb.h"
28 #include <boost/property_tree/ptree.hpp>
30 #include <zmqpp/context.hpp>
31 #include <zmqpp/message.hpp>
36 MqttModule::MqttModule(zmqpp::context &ctx, zmqpp::socket *pipe,
37 boost::property_tree::ptree
const &cfg,
55 boost::property_tree::ptree module_config =
config_.get_child(
"module_config");
57 if (module_config.get<
bool>(
"use_database",
false))
67 for (
const auto &server_config :
mqtt_config_->servers())
69 using namespace Colorize;
70 INFO(
"Creating Mqtt Server: "
75 <<
"\n\t Username: " <<
green(
underline(server_config->username())));
78 utils_->config_checker().register_object(server.
name(),
80 servers_.push_back(std::move(server));
100 using namespace odb::core;
101 auto db =
utils_->database();
104 schema_version v = db->schema_version(
"module_mqtt");
105 schema_version cv(schema_catalog::current_version(*db,
"module_mqtt"));
108 transaction t(db->begin());
109 INFO(
"Attempt to create module_mqtt SQL schema.");
110 schema_catalog::create_schema(*db,
"module_mqtt");
115 INFO(
"Mqtt Module performing database migration. Going from version "
116 << v <<
" to version " << cv);
117 transaction t(db->begin());
118 schema_catalog::migrate(*db, cv,
"module_mqtt");
126 odb::transaction t(
utils_->database()->begin());
127 odb::result<MqttServerConfig> sresult(
129 for (
const auto &server : sresult)
133 ASSERT_LOG(server_ptr,
"Loading from database/cache failed");
134 if (server_ptr->enabled())
140 odb::transaction t2(
utils_->database()->begin());
141 odb::result<MqttExternalMessage> tresult(
143 for (
const auto &topic : tresult)
145 std::shared_ptr<MqttExternalMessage> topic_ptr =
147 ASSERT_LOG(topic_ptr ==
nullptr,
"Loading from database/cache failed");
152 INFO(
"Mqtt module using SQL database for configuration.");
156 const boost::property_tree::ptree &module_config)
159 for (
auto &node : module_config.get_child(
"servers"))
161 auto server_config = std::make_shared<MqttServerConfig>();
162 boost::property_tree::ptree xml_server_cfg = node.second;
164 server_config->name(xml_server_cfg.get_child(
"name").data());
165 server_config->host(xml_server_cfg.get<std::string>(
"host",
"localhost"));
166 server_config->port(xml_server_cfg.get<uint16_t>(
"port", 1883));
167 server_config->client_id_ = xml_server_cfg.get<std::string>(
"client_id",
"leosac");
168 server_config->subscribe_prefix_ = xml_server_cfg.get<std::string>(
"subscribe_prefix",
"homeassistant/");
169 server_config->publish_prefix_ = xml_server_cfg.get<std::string>(
"publish_prefix",
"leosac/");
170 server_config->username_ = xml_server_cfg.get<std::string>(
"username",
"");
171 server_config->password_ = xml_server_cfg.get<std::string>(
"password",
"");
172 server_config->ssl_ = xml_server_cfg.get<
bool>(
"ssl",
false);
173 server_config->ssl_ca_certs_ = xml_server_cfg.get<std::string>(
"ssl_ca_certs",
"");
174 server_config->ssl_client_certfile_ = xml_server_cfg.get<std::string>(
"ssl_client_certfile",
"");
175 server_config->ssl_client_keyfile_ = xml_server_cfg.get<std::string>(
"ssl_client_keyfile",
"");
176 server_config->ssl_insecure_ = xml_server_cfg.get<
bool>(
"ssl_insecure",
false);
181 for (
const auto &node : module_config.get_child(
"topics"))
183 auto topic_config = std::make_shared<MqttExternalMessage>();
184 boost::property_tree::ptree xml_topic_cfg = node.second;
186 std::string direction;
187 std::string virtualtype;
189 topic_config->name(xml_topic_cfg.get_child(
"name").data());
190 topic_config->subject(xml_topic_cfg.get_child(
"subject").data());
191 direction = xml_topic_cfg.get_child(
"direction").data();
193 virtualtype = xml_topic_cfg.get_child(
"virtualtype").data();
194 if (virtualtype ==
"rfidreader")
198 else if (virtualtype ==
"led")
202 else if (virtualtype ==
"buzzer")
210 topic_config->payload(xml_topic_cfg.get<std::string>(
"payload",
""));