Skip to content

Commit

Permalink
Implemented more MQTT topic
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Sep 6, 2024
1 parent 2de8a27 commit 2c4dab7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ Application::Application(Storage<Config> &config_storage, Storage<PresetNames> &
event_property_changed.subscribe(this, [this](auto sender, auto type, auto arg) {
if (sender == this) return;

if (type == NotificationProperty::COLOR) {
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 All @@ -53,6 +55,10 @@ void Application::load() {
set_palette(current_palette, palette->value.data, palette->value.size);
}

if (config.preset_id >= preset_names.count) {
config.preset_id = preset_names.count - 1;
}

#if GAMMA_CORRECTION_RT == DISABLED
if (config.gamma != 0) {
napplyGamma_video(current_palette.entries, 16, gamma_value(config.gamma));
Expand Down
10 changes: 5 additions & 5 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
#include "sys_constants.h"

#define WIFI_MODE (WIFI_AP_MODE)
#define WIFI_SSID CREDENTIAL_WIFI_SSID
#define WIFI_PASSWORD CREDENTIAL_WIFI_PASSWORD

#define WIFI_CONNECTION_CHECK_INTERVAL (5000u) // Interval (ms) between Wi-Fi connection check
#define WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL (0u) // Max time (ms) to wait for Wi-Fi connection before switch to AP mode
// 0 - Newer switch to AP mode

#define WEB_AUTH
#define AUTH_USER CREDENTIAL_AUTH_USER
#define AUTH_PASSWORD CREDENTIAL_AUTH_PASSWORD

#define TIME_ZONE (5.f) // GMT +5:00

Expand Down Expand Up @@ -49,7 +45,7 @@
#define AUDIO_WINDOW_DURATION (5000u)


#define MQTT (0u) // MQTT protocol Enabled
#define MQTT (1u) // MQTT protocol Enabled

#define MQTT_CONNECTION_TIMEOUT (15000u) // Connection attempt timeout to MQTT server
#define MQTT_RECONNECT_TIMEOUT (5000u) // Time before new reconnection attempt to MQTT server
Expand All @@ -58,10 +54,14 @@
#define MQTT_TOPIC_BRIGHTNESS MQTT_PREFIX "/brightness"
#define MQTT_TOPIC_POWER MQTT_PREFIX "/power"
#define MQTT_TOPIC_COLOR MQTT_PREFIX "/color"
#define MQTT_TOPIC_PRESET MQTT_PREFIX "/preset"
#define MQTT_TOPIC_PALETTE MQTT_PREFIX "/palette"
#define MQTT_TOPIC_NIGHT_MODE MQTT_PREFIX "/night_mode"

#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_COLOR MQTT_OUT_PREFIX "/color"
#define MQTT_OUT_TOPIC_PRESET MQTT_OUT_PREFIX "/preset"
#define MQTT_OUT_TOPIC_PALETTE MQTT_OUT_PREFIX "/palette"
#define MQTT_OUT_TOPIC_NIGHT_MODE MQTT_OUT_PREFIX "/night_mode"
8 changes: 8 additions & 0 deletions src/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ std::map<PacketType, PropertyMetadata> PacketTypeMetadataMap = {
MQTT_TOPIC_BRIGHTNESS, MQTT_OUT_TOPIC_BRIGHTNESS,
}
},
{
PacketType::PRESET_ID,
{
NotificationProperty::PRESET, PacketType::PRESET_ID,
offsetof(Config, preset_id), sizeof(Config::preset_id),
MQTT_TOPIC_PRESET, MQTT_OUT_TOPIC_PRESET,
}
},
{
PacketType::NIGHT_MODE_ENABLED,
{
Expand Down
8 changes: 5 additions & 3 deletions src/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ MAKE_ENUM(NotificationProperty, uint8_t,
POWER, 0,
BRIGHTNESS, 1,
COLOR, 2,
NIGHT_MODE_ENABLED, 3,
PRESET, 3,
PALETTE, 4,
NIGHT_MODE_ENABLED, 5,
)

struct PropertyMetadata {
NotificationProperty property;
PacketType packet_type;
NotificationProperty property{};
PacketType packet_type{};

uint8_t value_offset{};
uint8_t value_size{};
Expand Down
2 changes: 2 additions & 0 deletions src/network/protocol/server/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ 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
8 changes: 8 additions & 0 deletions src/network/protocol/server/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ 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 @@ -144,6 +145,9 @@ void MqttServer::_on_message(char *topic, char *payload, AsyncMqttClientMessageP
if (topic_str == MQTT_TOPIC_COLOR) {
uint32_t color = payload_str.toInt();
_app.event_property_changed.publish(this, NotificationProperty::COLOR, &color);
} else if (topic_str == MQTT_TOPIC_PALETTE) {
auto palette_id = (PaletteEnum) payload_str.toInt();
_app.event_property_changed.publish(this, NotificationProperty::PALETTE, &palette_id);
} else {
_process_message(topic_str, payload_str);
}
Expand Down Expand Up @@ -212,6 +216,10 @@ void MqttServer::_process_notification(NotificationProperty prop) {
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);
_publish(MQTT_OUT_TOPIC_PALETTE, 1, str.c_str(), str.length());
return;
}

auto iter_meta = PropertyMetadataMap.find(prop);
Expand Down

0 comments on commit 2c4dab7

Please sign in to comment.