Skip to content

Commit

Permalink
add ability to force-hide certain channels from publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Oct 9, 2023
1 parent 6fdcfb3 commit 356f8c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/cmnds/cmd_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
int g_hiddenChannels = 0;
static char *g_channelLabels[CHANNEL_MAX] = { 0 };
static int g_bHideTogglePrefix = 0;
// same, for hiding from MQTT
int g_doNotPublishChannels = 0;

void CHANNEL_SetLabel(int ch, const char *s, int bHideTogglePrefix) {
if (ch < 0)
Expand All @@ -34,6 +36,16 @@ bool CHANNEL_ShouldAddTogglePrefixToUI(int ch) {
return false;
return true;
}
bool CHANNEL_HasNeverPublishFlag(int ch) {
if (ch < 0)
return false;
if (ch >= 32)
return false;
if (BIT_CHECK(g_doNotPublishChannels, ch))
return true;
return false;
}

const char *CHANNEL_GetLabel(int ch) {
if (ch >= 0 && ch < CHANNEL_MAX) {
if (g_channelLabels[ch])
Expand Down Expand Up @@ -356,6 +368,32 @@ static commandResult_t CMD_SetChannelVisible(const void *context, const char *cm

return CMD_RES_OK;
}
// hide/show channel from MQTT
static commandResult_t CMD_SetChannelPrivate(const void *context, const char *cmd, const char *args, int cmdFlags) {
int targetCH;
int bOn;

Tokenizer_TokenizeString(args, 0);
// following check must be done after 'Tokenizer_TokenizeString',
// so we know arguments count in Tokenizer. 'cmd' argument is
// only for warning display
if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 2)) {
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}

targetCH = Tokenizer_GetArgInteger(0);
bOn = Tokenizer_GetArgInteger(1);

if (bOn) {
// private means "do not publish"
BIT_SET(g_doNotPublishChannels, targetCH);
}
else {
BIT_CLEAR(g_doNotPublishChannels, targetCH);
}

return CMD_RES_OK;
}
static commandResult_t CMD_GetReadings(const void *context, const char *cmd, const char *args, int cmdFlags){
#ifndef OBK_DISABLE_ALL_DRIVERS
char tmp[96];
Expand Down Expand Up @@ -521,6 +559,11 @@ void CMD_InitChannelCommands(){
//cmddetail:"fn":"CMD_SetChannelVisible","file":"cmnds/cmd_channels.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("SetChannelVisible", CMD_SetChannelVisible, NULL);
//cmddetail:{"name":"SetChannelPrivate","args":"[ChannelIndex][bPrivate]",
//cmddetail:"descr":"Channels marked as private are NEVER published via MQTT.",
//cmddetail:"fn":"CMD_SetChannelPrivate","file":"cmnds/cmd_channels.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("CMD_SetChannelPrivate", CMD_SetChannelPrivate, NULL);
//cmddetail:{"name":"Ch","args":"[InputValue]",
//cmddetail:"descr":"An alternate command to access channels. It returns all used channels in JSON format. The syntax is ChINDEX value, there is no space between Ch and channel index. It can be sent without value to poll channel values.",
//cmddetail:"fn":"CMD_Ch","file":"cmnds/cmd_channels.c","requires":"",
Expand Down
5 changes: 5 additions & 0 deletions src/mqtt/new_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,11 @@ OBK_Publish_Result MQTT_ChannelPublish(int channel, int flags)
char channelNameStr[8];
char valueStr[16];

// allow users to force-hide some channels (those channels are NEVER published)
if (CHANNEL_HasNeverPublishFlag(channel)) {
return OBK_PUBLISH_OK;
}

if (CFG_HasFlag(OBK_FLAG_PUBLISH_MULTIPLIED_VALUES)) {
float dVal = CHANNEL_GetFinalValue(channel);
// Float value
Expand Down
1 change: 1 addition & 0 deletions src/new_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ int CHANNEL_FindMaxValueForChannel(int ch);
// cmd_channels.c
const char* CHANNEL_GetLabel(int ch);
bool CHANNEL_ShouldAddTogglePrefixToUI(int ch);
bool CHANNEL_HasNeverPublishFlag(int ch);
//ledRemap_t *CFG_GetLEDRemap();

void PIN_get_Relay_PWM_Count(int* relayCount, int* pwmCount, int* dInputCount);
Expand Down

0 comments on commit 356f8c9

Please sign in to comment.