From 3ec2c536b1f59a566e58410f362260f75571dac9 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 7 Sep 2024 00:11:43 +0500 Subject: [PATCH] More MQTT topics --- src/application.cpp | 2 -- src/constants.h | 6 +++++ src/metadata.cpp | 12 ++++++++++ src/metadata.h | 11 +++++---- src/network/protocol/server/base.cpp | 2 -- src/network/protocol/server/mqtt.h | 34 +++++++++++++++++++++------- 6 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 90e716d..0ea7833 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -27,8 +27,6 @@ Application::Application(Storage &config_storage, Storage & if (type == NotificationProperty::COLOR && arg) { this->change_color(*(uint32_t *) arg); - } else if (type == NotificationProperty::PALETTE && arg) { - this->preset().palette = *(PaletteEnum *) arg; } else if (type == NotificationProperty::NIGHT_MODE_ENABLED) { this->night_mode_manager.reset(); } diff --git a/src/constants.h b/src/constants.h index 2b8464c..db18429 100644 --- a/src/constants.h +++ b/src/constants.h @@ -53,6 +53,9 @@ #define MQTT_PREFIX MDNS_NAME #define MQTT_TOPIC_BRIGHTNESS MQTT_PREFIX "/brightness" #define MQTT_TOPIC_POWER MQTT_PREFIX "/power" +#define MQTT_TOPIC_SPEED MQTT_PREFIX "/speed" +#define MQTT_TOPIC_SCALE MQTT_PREFIX "/scale" +#define MQTT_TOPIC_LIGHT MQTT_PREFIX "/light" #define MQTT_TOPIC_COLOR MQTT_PREFIX "/color" #define MQTT_TOPIC_PRESET MQTT_PREFIX "/preset" #define MQTT_TOPIC_PALETTE MQTT_PREFIX "/palette" @@ -61,6 +64,9 @@ #define MQTT_OUT_PREFIX MQTT_PREFIX "/out" #define MQTT_OUT_TOPIC_BRIGHTNESS MQTT_OUT_PREFIX "/brightness" #define MQTT_OUT_TOPIC_POWER MQTT_OUT_PREFIX "/power" +#define MQTT_OUT_TOPIC_SPEED MQTT_OUT_PREFIX "/speed" +#define MQTT_OUT_TOPIC_SCALE MQTT_OUT_PREFIX "/scale" +#define MQTT_OUT_TOPIC_LIGHT MQTT_OUT_PREFIX "/light" #define MQTT_OUT_TOPIC_COLOR MQTT_OUT_PREFIX "/color" #define MQTT_OUT_TOPIC_PRESET MQTT_OUT_PREFIX "/preset" #define MQTT_OUT_TOPIC_PALETTE MQTT_OUT_PREFIX "/palette" diff --git a/src/metadata.cpp b/src/metadata.cpp index ca04735..5d50e97 100644 --- a/src/metadata.cpp +++ b/src/metadata.cpp @@ -75,5 +75,17 @@ std::map _build_topic_property_metadata_map( result[metadata.mqtt_in_topic] = metadata; } + result[MQTT_TOPIC_SPEED] = {NotificationProperty::SPEED, PacketType::SPEED, 0, 0, + MQTT_TOPIC_SPEED, MQTT_OUT_TOPIC_SPEED}; + + result[MQTT_TOPIC_SCALE] = {NotificationProperty::SCALE, PacketType::SCALE, 0, 0, + MQTT_TOPIC_SCALE, MQTT_OUT_TOPIC_SCALE}; + + result[MQTT_TOPIC_LIGHT] = {NotificationProperty::LIGHT, PacketType::LIGHT, 0, 0, + MQTT_TOPIC_LIGHT, MQTT_OUT_TOPIC_LIGHT}; + + result[MQTT_TOPIC_PALETTE] = {NotificationProperty::PALETTE, PacketType::PALETTE, 0, 0, + MQTT_TOPIC_PALETTE, MQTT_OUT_TOPIC_PALETTE}; + return result; } \ No newline at end of file diff --git a/src/metadata.h b/src/metadata.h index 4dd9777..cea48ab 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -16,10 +16,13 @@ class Application; MAKE_ENUM(NotificationProperty, uint8_t, POWER, 0, BRIGHTNESS, 1, - COLOR, 2, - PRESET, 3, - PALETTE, 4, - NIGHT_MODE_ENABLED, 5, + SPEED, 2, + SCALE, 3, + LIGHT, 4, + COLOR, 5, + PRESET, 6, + PALETTE, 7, + NIGHT_MODE_ENABLED, 8, ) struct PropertyMetadata { diff --git a/src/network/protocol/server/base.cpp b/src/network/protocol/server/base.cpp index 605cc00..858bcd9 100644 --- a/src/network/protocol/server/base.cpp +++ b/src/network/protocol/server/base.cpp @@ -46,8 +46,6 @@ Response ServerBase::handle_packet_data(const uint8_t *buffer, uint16_t length) auto iter = PacketTypeMetadataMap.find(header->type); if (iter != PacketTypeMetadataMap.end()) { _app.event_property_changed.publish(this, iter->second.property); - } else if (header->type == PacketType::PALETTE) { - _app.event_property_changed.publish(this, NotificationProperty::PALETTE); } } diff --git a/src/network/protocol/server/mqtt.h b/src/network/protocol/server/mqtt.h index a8320ef..0e4dd17 100644 --- a/src/network/protocol/server/mqtt.h +++ b/src/network/protocol/server/mqtt.h @@ -118,7 +118,6 @@ void MqttServer::_on_connect(bool) { } _subscribe(MQTT_TOPIC_COLOR, 1); - _subscribe(MQTT_TOPIC_PALETTE, 1); _last_connection_attempt_time = millis(); _change_state(MqttServerState::CONNECTED); @@ -142,12 +141,22 @@ void MqttServer::_on_message(char *topic, char *payload, AsyncMqttClientMessageP _transform_topic_payload(topic_str, payload_str); - if (topic_str == MQTT_TOPIC_COLOR) { - uint32_t color = payload_str.toInt(); - _app.event_property_changed.publish(this, NotificationProperty::COLOR, &color); + //TODO: Refactor + if (topic_str == MQTT_TOPIC_SPEED) { + app().preset().speed = payload_str.toInt(); + _app.event_property_changed.publish(this, NotificationProperty::SPEED); + } else if (topic_str == MQTT_TOPIC_SCALE) { + app().preset().scale = payload_str.toInt(); + _app.event_property_changed.publish(this, NotificationProperty::SCALE); + } else if (topic_str == MQTT_TOPIC_LIGHT) { + app().preset().light = payload_str.toInt(); + _app.event_property_changed.publish(this, NotificationProperty::LIGHT); } else if (topic_str == MQTT_TOPIC_PALETTE) { - auto palette_id = (PaletteEnum) payload_str.toInt(); - _app.event_property_changed.publish(this, NotificationProperty::PALETTE, &palette_id); + app().preset().palette = (PaletteEnum) payload_str.toInt(); + _app.event_property_changed.publish(this, NotificationProperty::PALETTE); + } else if (topic_str == MQTT_TOPIC_COLOR) { + app().change_color(payload_str.toInt()); + _app.event_property_changed.publish(this, NotificationProperty::COLOR); } else { _process_message(topic_str, payload_str); } @@ -212,12 +221,21 @@ void MqttServer::_after_message_process(const PropertyMetadata &meta) { void MqttServer::_process_notification(NotificationProperty prop) { - if (prop == NotificationProperty::COLOR) { + if (prop == NotificationProperty::SPEED) { + String str(_app.preset().speed); + _publish(MQTT_OUT_TOPIC_SPEED, 1, str.c_str(), str.length()); + } else if (prop == NotificationProperty::SCALE) { + String str(_app.preset().scale); + _publish(MQTT_OUT_TOPIC_SCALE, 1, str.c_str(), str.length()); + } else if (prop == NotificationProperty::LIGHT) { + String str(_app.preset().light); + _publish(MQTT_OUT_TOPIC_LIGHT, 1, str.c_str(), str.length()); + } else if (prop == NotificationProperty::COLOR) { String str(_app.current_color()); _publish(MQTT_OUT_TOPIC_COLOR, 1, str.c_str(), str.length()); return; } else if (prop == NotificationProperty::PALETTE) { - String str((int) _app.palette->code); + String str((int) _app.preset().palette); _publish(MQTT_OUT_TOPIC_PALETTE, 1, str.c_str(), str.length()); return; }