Skip to content

Commit

Permalink
More MQTT topics
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Sep 6, 2024
1 parent 2c4dab7 commit 3ec2c53
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Application::Application(Storage<Config> &config_storage, Storage<PresetNames> &

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();
}
Expand Down
6 changes: 6 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions src/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,17 @@ std::map<String, PropertyMetadata> _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;
}
11 changes: 7 additions & 4 deletions src/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/network/protocol/server/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
34 changes: 26 additions & 8 deletions src/network/protocol/server/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 3ec2c53

Please sign in to comment.