diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 0f081b7c5..57e009245 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2415,12 +2415,12 @@ const char* g_obk_flagNames[] = { "[LED] Smooth transitions for LED (EXPERIMENTAL)", "[MQTT] Always publish channels used by TuyaMCU", "[LED] Force RGB mode (3 PWMs for LEDs) and ignore futher PWMs if they are set", + "[MQTT] Retain power channels (Relay channels, etc)", "error", "error", "error", "error", - "error", -}; +}; int http_fn_cfg_generic(http_request_t* request) { int i; diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c index 95c447ce6..42d4f5a99 100644 --- a/src/mqtt/new_mqtt.c +++ b/src/mqtt/new_mqtt.c @@ -1059,13 +1059,22 @@ OBK_Publish_Result MQTT_ChannelChangeCallback(int channel, int iVal) { char channelNameStr[8]; char valueStr[16]; - + int flags; + + flags = 0; addLogAdv(LOG_INFO, LOG_FEATURE_MAIN, "Channel has changed! Publishing change %i with %i \n", channel, iVal); sprintf(channelNameStr, "%i", channel); sprintf(valueStr, "%i", iVal); - return MQTT_PublishMain(mqtt_client, channelNameStr, valueStr, 0, true); + // This will set RETAIN flag for all channels that are used for RELAY + if (CFG_HasFlag(OBK_FLAG_MQTT_RETAIN_POWER_CHANNELS)) { + if (CHANNEL_IsPowerRelayChannel(channel)) { + flags |= OBK_PUBLISH_FLAG_RETAIN; + } + } + + return MQTT_PublishMain(mqtt_client, channelNameStr, valueStr, flags, true); } OBK_Publish_Result MQTT_ChannelPublish(int channel, int flags) { @@ -1080,6 +1089,13 @@ OBK_Publish_Result MQTT_ChannelPublish(int channel, int flags) sprintf(channelNameStr, "%i", channel); sprintf(valueStr, "%i", iValue); + // This will set RETAIN flag for all channels that are used for RELAY + if (CFG_HasFlag(OBK_FLAG_MQTT_RETAIN_POWER_CHANNELS)) { + if (CHANNEL_IsPowerRelayChannel(channel)) { + flags |= OBK_PUBLISH_FLAG_RETAIN; + } + } + return MQTT_PublishMain(mqtt_client, channelNameStr, valueStr, flags, true); } // This console command will trigger a publish of all used variables (channels and extra stuff) diff --git a/src/new_pins.c b/src/new_pins.c index b7b0d0fea..1c18c8a15 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -849,6 +849,17 @@ bool CHANNEL_IsInUse(int ch) { } +bool CHANNEL_IsPowerRelayChannel(int ch) { + int i; + for (i = 0; i < PLATFORM_GPIO_MAX; i++) { + if (g_cfg.pins.channels[i] == ch) { + int role = g_cfg.pins.roles[i]; if (role == IOR_Relay || role == IOR_Relay_n) { + return true; + } + } + } + return false; +} bool CHANNEL_HasRoleThatShouldBePublished(int ch) { int i; for (i = 0; i < PLATFORM_GPIO_MAX; i++) { diff --git a/src/new_pins.h b/src/new_pins.h index 34a18feac..e6a1e2fcc 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -164,9 +164,10 @@ typedef struct pinsState_s { #define OBK_FLAG_LED_SMOOTH_TRANSITIONS 18 #define OBK_FLAG_TUYAMCU_ALWAYSPUBLISHCHANNELS 19 #define OBK_FLAG_LED_FORCE_MODE_RGB 20 +#define OBK_FLAG_MQTT_RETAIN_POWER_CHANNELS 21 -#define OBK_TOTAL_FLAGS 20 +#define OBK_TOTAL_FLAGS 22 #define CGF_MQTT_CLIENT_ID_SIZE 64 @@ -307,6 +308,7 @@ void CHANNEL_AddClamped(int ch, int iVal, int min, int max); int CHANNEL_Get(int ch); int CHANNEL_GetRoleForOutputChannel(int ch); bool CHANNEL_HasRoleThatShouldBePublished(int ch); +bool CHANNEL_IsPowerRelayChannel(int ch); // See: enum ChannelType void CHANNEL_SetType(int ch, int type); int CHANNEL_GetType(int ch);