-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbutton.cpp
More file actions
143 lines (119 loc) · 4.23 KB
/
button.cpp
File metadata and controls
143 lines (119 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "button/button.hpp"
#include "command_manager/command_manager.hpp"
#include "database_manager/database_manager.hpp"
#include "entity_manager/entity_manager.hpp"
#include "mqtt_manager_config/mqtt_manager_config.hpp"
#include "protobuf_general.pb.h"
#include "protobuf_nspanel.pb.h"
#include <boost/bind.hpp>
#include <boost/bind/bind.hpp>
#include <cstdint>
#include <entity/entity.hpp>
#include <entity/entity_icons.hpp>
#include <mutex>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
#include <string>
#include <unistd.h>
ButtonEntity::ButtonEntity(uint32_t button_id)
{
this->_id = button_id;
this->reload_config();
// Build MQTT Topics
std::string mqtt_base_topic = fmt::format("nspanel/entities/button/{}/", this->_id);
this->_current_state = false;
this->_requested_state = false;
CommandManager::attach_callback(boost::bind(&ButtonEntity::command_callback, this, _1));
SPDLOG_DEBUG("Button {}::{} base loaded.", this->_id, this->_name);
}
uint16_t ButtonEntity::get_room_id() {
return this->_room_id;
}
void ButtonEntity::reload_config() {
std::lock_guard<std::mutex> lock_guard(this->_entity_data_mutex);
auto button_entity = database_manager::database.get<database_manager::Entity>(this->_id);
this->_name = button_entity.friendly_name;
SPDLOG_DEBUG("Loading button {}::{}.", this->_id, this->_name);
this->_room_id = button_entity.room_id;
this->_entity_page_id = button_entity.entities_page_id;
this->_entity_page_slot = button_entity.room_view_position;
nlohmann::json entity_data = button_entity.get_entity_data_json();
this->_entity_data = entity_data;
if (entity_data.contains("controller")) {
std::string controller = entity_data["controller"];
if (controller.compare("home_assistant") == 0) {
this->_controller = MQTT_MANAGER_ENTITY_CONTROLLER::HOME_ASSISTANT;
}
else if (controller.compare("homey") == 0)
{
this->_controller = MQTT_MANAGER_ENTITY_CONTROLLER::HOMEY;
}
else if (controller.compare("nspm") == 0)
{
this->_controller = MQTT_MANAGER_ENTITY_CONTROLLER::NSPM;
}
else
{
SPDLOG_ERROR("Got unknown controller ({}) for light {}::{}. Will default to HOME_ASSISTANT.", std::string(controller), this->_id, this->_name);
this->_controller = MQTT_MANAGER_ENTITY_CONTROLLER::HOME_ASSISTANT;
}
} else {
SPDLOG_ERROR("No controller defined for light {}::{}. Will default to HOME_ASSISTANT.", this->_id, this->_name);
this->_controller = MQTT_MANAGER_ENTITY_CONTROLLER::HOME_ASSISTANT;
}
}
MQTT_MANAGER_ENTITY_TYPE ButtonEntity::get_type() {
return MQTT_MANAGER_ENTITY_TYPE::BUTTON;
}
MQTT_MANAGER_ENTITY_CONTROLLER ButtonEntity::get_controller() {
return this->_controller;
}
uint16_t ButtonEntity::get_id() {
return this->_id;
}
std::string ButtonEntity::get_name() {
return this->_name;
}
uint32_t ButtonEntity::get_entity_page_id() {
return this->_entity_page_id;
}
uint8_t ButtonEntity::get_entity_page_slot() {
return this->_entity_page_slot;
}
void ButtonEntity::attach_delete_callback(void (*callback)(ButtonEntity *)) {
this->_button_destroyed_callbacks.connect(callback);
}
void ButtonEntity::detach_delete_callback(void (*callback)(ButtonEntity *)) {
this->_button_destroyed_callbacks.disconnect(callback);
}
ButtonEntity::~ButtonEntity() {
SPDLOG_DEBUG("Destructor for button {}::{} called.", this->_id, this->_name);
this->_button_destroyed_callbacks(this);
this->_signal_entity_destroyed();
CommandManager::detach_callback(boost::bind(&ButtonEntity::command_callback, this, _1));
}
void ButtonEntity::command_callback(NSPanelMQTTManagerCommand &command) {
}
bool ButtonEntity::can_toggle() {
return true;
}
void ButtonEntity::toggle() {
this->_requested_state = !this->_requested_state;
this->send_state_update_to_controller();
if (MqttManagerConfig::get_setting_with_default<bool>(MQTT_MANAGER_SETTING::OPTIMISTIC_MODE))
{
this->_current_state = !this->_current_state;
}
}
std::string_view ButtonEntity::get_icon() {
return EntityIcons::entity_icon_button;
}
uint16_t ButtonEntity::get_icon_color() {
return GUI_Colors::icon_color_off;
}
uint16_t ButtonEntity::get_icon_active_color() {
return GUI_Colors::icon_color_off;
}
std::string ButtonEntity::get_mqtt_state_topic() {
return "";
}