Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docker/MQTTManager/include/entity_manager/entity_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ void EntityManager::load_nspanels() {
auto existing_nspanel = EntityManager::get_nspanel_by_id(nspanel_id);
if (existing_nspanel) [[likely]] {
(*existing_nspanel)->reload_config();
(*existing_nspanel)->send_config();
} else {
std::lock_guard<std::mutex> mutex_guard(EntityManager::_nspanels_mutex);
auto panel = std::shared_ptr<NSPanel>(new NSPanel(nspanel_id));
Expand Down Expand Up @@ -368,7 +367,7 @@ void EntityManager::load_media_players() {

void EntityManager::load_covers() {
auto cover_ids = database_manager::database.select(&database_manager::Entity::id, sqlite_orm::from<database_manager::Entity>(),
sqlite_orm::where(sqlite_orm::glob(&database_manager::Entity::entity_type, "cover")));
sqlite_orm::where(sqlite_orm::glob(&database_manager::Entity::entity_type, "cover")));
SPDLOG_INFO("Loading {} covers.", cover_ids.size());

// Check if any existing cover has been removed.
Expand Down Expand Up @@ -489,7 +488,7 @@ void EntityManager::load_scenes() {
SPDLOG_INFO("Scene {}::{} was found in database but not in config. Creating scene.", scene->get_id(), scene->get_name());
std::lock_guard<std::mutex> mutex_guard(EntityManager::_entities_mutex);
EntityManager::_entities.push_back(scene);
} else if (scene_settings.scene_type.compare("nspm_scene") == 0) {
} else if (scene_settings.scene_type.compare("nspm_scene") == 0 || scene_settings.scene_type.compare("nspm") == 0) {
std::shared_ptr<NSPMScene> scene = std::shared_ptr<NSPMScene>(new NSPMScene(scene_settings.id));
SPDLOG_INFO("Scene {}::{} was found in database but not in config. Creating scene.", scene->get_id(), scene->get_name());
std::lock_guard<std::mutex> mutex_guard(EntityManager::_entities_mutex);
Expand Down
371 changes: 149 additions & 222 deletions docker/MQTTManager/include/mqtt_manager_config/mqtt_manager_config.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#ifndef MQTTMANAGER_CONFIG_HPP
#define MQTTMANAGER_CONFIG_HPP
#include "light/light.hpp"
#include "filesystem"
#include <boost/algorithm/string.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/signals2.hpp>
#include <cstdint>
#include <database_manager/database_manager.hpp>
#include <expected>
#include <filesystem>
#include <mutex>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <optional>
#include <protobuf/protobuf_mqttmanager.pb.h>
#include <spdlog/spdlog.h>
#include <string>
Expand Down Expand Up @@ -167,24 +170,24 @@ class MqttManagerConfig {

static inline LightTurnOnBehaviour _light_turn_on_behaviour = LightTurnOnBehaviour::COLOR_TEMPERATURE;

static std::optional<std::string> _get_file_md5_checksum(std::string file_path);
static inline std::mutex _md5_checksum_files_mutex;
static inline std::string _md5_checksum_firmware_sonoff;
static inline std::string _md5_checksum_firmware_custom;
static inline std::string _md5_checksum_littlefs_sonoff;
static inline std::string _md5_checksum_littlefs_custom;
static inline std::string _md5_checksum_eu_tft1;
static inline std::string _md5_checksum_eu_tft2;
static inline std::string _md5_checksum_eu_tft3;
static inline std::string _md5_checksum_eu_tft4;
static inline std::string _md5_checksum_us_tft1;
static inline std::string _md5_checksum_us_tft2;
static inline std::string _md5_checksum_us_tft3;
static inline std::string _md5_checksum_us_tft4;
static inline std::string _md5_checksum_us_horizontal_mirrored_tft1;
static inline std::string _md5_checksum_us_horizontal_mirrored_tft2;
static inline std::string _md5_checksum_us_horizontal_mirrored_tft3;
static inline std::string _md5_checksum_us_horizontal_mirrored_tft4;
// A struct of values needed to cache and hold checksums for files. Stored with an unordered map
// where the key of the map is the path to the file.
struct _CachedChecksum {
std::optional<std::filesystem::file_time_type> mtime;
std::string checksum;
};

// Map of cached checksums. Key = file path.
static inline std::unordered_map<std::string, _CachedChecksum> _checksums;

// Mutex for _checksums map
static inline std::mutex _checksums_mutex;

// Get a checksum for a given file. Return error string in case of failure.
static std::expected<std::string, std::string> _get_file_md5_checksum(std::string file_path);

// Get the mtime (modified time) from a given file. Return error string in case of failure.
static std::expected<std::filesystem::file_time_type, std::string> _get_file_mtime(std::string file_path);

static inline std::unordered_map<MQTT_MANAGER_SETTING, std::string> _settings_values_cache;

Expand Down
18 changes: 10 additions & 8 deletions docker/MQTTManager/include/nspanel/nspanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <exception>
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <google/protobuf/util/message_differencer.h>
#include <iomanip>
#include <ixwebsocket/IXWebSocketSendInfo.h>
#include <light/light.hpp>
Expand Down Expand Up @@ -61,10 +62,6 @@ NSPanel::NSPanel(uint32_t id) {
this->_id = id;
SPDLOG_INFO("Loading new NSPanel with ID {}.", id);
this->reload_config();
if (!this->_mqtt_config_topic.empty() && this->_state != MQTT_MANAGER_NSPANEL_STATE::AWAITING_ACCEPT) {
SPDLOG_INFO("Loaded accepted NSPanel {}::{}.", this->_id, this->_name);
this->_state = MQTT_MANAGER_NSPANEL_STATE::WAITING;
}

CommandManager::attach_callback(boost::bind(&NSPanel::command_callback, this, _1));
WebsocketServer::attach_stomp_callback(fmt::format("nspanel/{}/command", this->_mac), boost::bind(&NSPanel::handle_stomp_command_callback, this, _1));
Expand Down Expand Up @@ -143,7 +140,7 @@ void NSPanel::reload_config() {
} else if (panel_settings.model.compare("web") == 0) {
this->_model = MQTT_MANAGER_NSPANEL_MODEL::WEB;
} else {
SPDLOG_ERROR("Failed to prase panel model for NSPanel {}::{}. Got value '{}'. Will assume sonoff.", panel_settings.id, panel_settings.friendly_name, panel_settings.model);
SPDLOG_ERROR("Failed to parse panel model for NSPanel {}::{}. Got value '{}'. Will assume sonoff.", panel_settings.id, panel_settings.friendly_name, panel_settings.model);
this->_model = MQTT_MANAGER_NSPANEL_MODEL::SONOFF;
}

Expand Down Expand Up @@ -199,7 +196,7 @@ void NSPanel::reload_config() {
if (!panel_settings.denied && !panel_settings.accepted) {
// No decission has been made on wether ot accept or deny panel. It is therefore awaiting_accept
this->_state = MQTT_MANAGER_NSPANEL_STATE::AWAITING_ACCEPT;
} else if (this->_state == MQTT_MANAGER_NSPANEL_STATE::AWAITING_ACCEPT || this->_state == MQTT_MANAGER_NSPANEL_STATE::DENIED) {
} else if (panel_settings.accepted && (this->_state == MQTT_MANAGER_NSPANEL_STATE::AWAITING_ACCEPT || this->_state == MQTT_MANAGER_NSPANEL_STATE::DENIED)) {
// The panel has just been accepted. Wait for it to register again.
this->_state = MQTT_MANAGER_NSPANEL_STATE::WAITING;
}
Expand Down Expand Up @@ -522,8 +519,13 @@ void NSPanel::send_config() {
}
}

SPDLOG_DEBUG("Sending updated NSPanelConfig to panel {}::{} over MQTT.", this->_id, this->_name);
MQTT_Manager::publish_protobuf(this->_mqtt_config_topic, config, true);
google::protobuf::util::MessageDifferencer differencer;
if (!differencer.Equals(this->_last_send_config, config)) {
SPDLOG_DEBUG("Sending updated NSPanelConfig to panel {}::{} over MQTT.", this->_id, this->_name);
MQTT_Manager::publish_protobuf(this->_mqtt_config_topic, config, true);
} else {
SPDLOG_DEBUG("Not sending updated config for NSPanel {}::{} as no config change has occured from last sent config.");
}
}

NSPanel::~NSPanel() {
Expand Down
3 changes: 3 additions & 0 deletions docker/MQTTManager/include/nspanel/nspanel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ class NSPanel {
// A thermostat temperature limit setting, or 0 if it is not a number.
int _get_nspanel_temperature_limit_setting(std::string key);

// The last config sent to the panel that is currently retained in MQTT.
NSPanelConfig _last_send_config;

// Vars:
uint32_t _id;
database_manager::NSPanel _settings; // Settings loaded from database
Expand Down
4 changes: 2 additions & 2 deletions docker/MQTTManager/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

void sigusr1_handler(int signal) {
if (signal == SIGUSR1) {
SPDLOG_INFO("Reloading config from manager. Will wait 50ms for other processing to complete before proceeding with config reload.");
std::this_thread::sleep_for(std::chrono::milliseconds(50));
SPDLOG_INFO("Reloading config from manager.");
// std::this_thread::sleep_for(std::chrono::milliseconds(50));

MqttManagerConfig::load();
MQTT_Manager::reload_config();
Expand Down
6 changes: 3 additions & 3 deletions docker/MQTTManager/tests/nspanel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <algorithm>
#include <command_manager/command_manager.hpp>
#include <functional>
#include <iterator>
#include <gtest/gtest.h>
#include <iterator>
#include <media_player/media_player.hpp>
#include <nspanel/nspanel.hpp>
#include <protobuf_nspanel.pb.h>
Expand Down Expand Up @@ -756,10 +756,10 @@ TEST_F(NSPanelTest, panel_not_yet_accepted_is_awaiting_accept) {
EXPECT_EQ(panel->get_state(), MQTT_MANAGER_NSPANEL_STATE::AWAITING_ACCEPT);
}

TEST_F(NSPanelTest, accepted_panel_is_loaded_as_waiting) {
TEST_F(NSPanelTest, accepted_panel_is_loaded_as_offline) {
load_panel();

EXPECT_EQ(panel->get_state(), MQTT_MANAGER_NSPANEL_STATE::WAITING);
EXPECT_EQ(panel->get_state(), MQTT_MANAGER_NSPANEL_STATE::OFFLINE);
}

// Accepting in the web interface sets accepted and reloads the manager. The panel must leave
Expand Down
Loading
Loading