From 77181854a7bfd33897809cbc4a7efebdafa64071 Mon Sep 17 00:00:00 2001 From: Adrian Scillato <35405447+ascillato@users.noreply.github.com> Date: Mon, 23 Jul 2018 01:01:52 -0300 Subject: [PATCH] Added KNX commands * KNX_ENABLED 0/1 * KNX_ENHANCED 0/1 --- sonoff/xdrv_11_knx.ino | 55 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/sonoff/xdrv_11_knx.ino b/sonoff/xdrv_11_knx.ino index 5aadbc6c0..35fa559ad 100644 --- a/sonoff/xdrv_11_knx.ino +++ b/sonoff/xdrv_11_knx.ino @@ -196,8 +196,10 @@ const char *device_param_cb[] = { // Commands #define D_CMND_KNXTXCMND "KnxTx_Cmnd" #define D_CMND_KNXTXVAL "KnxTx_Val" -enum KnxCommands { CMND_KNXTXCMND, CMND_KNXTXVAL }; -const char kKnxCommands[] PROGMEM = D_CMND_KNXTXCMND "|" D_CMND_KNXTXVAL ; +#define D_CMND_KNX_ENABLED "Knx_Enabled" +#define D_CMND_KNX_ENHANCED "Knx_Enhanced" +enum KnxCommands { CMND_KNXTXCMND, CMND_KNXTXVAL, CMND_KNX_ENABLED, CMND_KNX_ENHANCED } ; +const char kKnxCommands[] PROGMEM = D_CMND_KNXTXCMND "|" D_CMND_KNXTXVAL "|" D_CMND_KNX_ENABLED "|" D_CMND_KNX_ENHANCED ; byte KNX_GA_Search( byte param, byte start = 0 ) @@ -1031,14 +1033,12 @@ boolean KnxCommand() uint8_t index = XdrvMailbox.index; int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kKnxCommands); - if (!(Settings.flag.knx_enabled)) { return false; } - if (-1 == command_code) { return false; } // Unknown command else if ((CMND_KNXTXCMND == command_code) && (index > 0) && (index <= MAX_KNXTX_CMNDS) && (XdrvMailbox.data_len > 0)) { // index <- KNX SLOT to use // XdrvMailbox.payload <- data to send - + if (!(Settings.flag.knx_enabled)) { return false; } // Search all the registered GA that has that output (variable: KNX SLOTx) as parameter byte i = KNX_GA_Search(index + KNX_SLOT1 -1); while ( i != KNX_Empty ) { @@ -1056,12 +1056,14 @@ boolean KnxCommand() i = KNX_GA_Search(index + KNX_SLOT1 -1, i + 1); } + snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s\"}"), + command, index, XdrvMailbox.data ); } else if ((CMND_KNXTXVAL == command_code) && (index > 0) && (index <= MAX_KNXTX_CMNDS) && (XdrvMailbox.data_len > 0)) { // index <- KNX SLOT to use // XdrvMailbox.payload <- data to send - + if (!(Settings.flag.knx_enabled)) { return false; } // Search all the registered GA that has that output (variable: KNX SLOTx) as parameter byte i = KNX_GA_Search(index + KNX_SLOT1 -1); while ( i != KNX_Empty ) { @@ -1083,13 +1085,48 @@ boolean KnxCommand() i = KNX_GA_Search(index + KNX_SLOT1 -1, i + 1); } + snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s\"}"), + command, index, XdrvMailbox.data ); + } + + else if (CMND_KNX_ENABLED == command_code) { + if (!XdrvMailbox.data_len) { + if (Settings.flag.knx_enabled) { + snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("1")); + } else { + snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("0")); + } + } else { + if (XdrvMailbox.payload == 1) { + Settings.flag.knx_enabled = 1; + } else if (XdrvMailbox.payload == 0) { + Settings.flag.knx_enabled = 0; + } else { return false; } // Incomplete command + } + snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"), + command, XdrvMailbox.data ); + } + + else if (CMND_KNX_ENHANCED == command_code) { + if (!XdrvMailbox.data_len) { + if (Settings.flag.knx_enable_enhancement) { + snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("1")); + } else { + snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("0")); + } + } else { + if (XdrvMailbox.payload == 1) { + Settings.flag.knx_enable_enhancement = 1; + } else if (XdrvMailbox.payload == 0) { + Settings.flag.knx_enable_enhancement = 0; + } else { return false; } // Incomplete command + } + snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"), + command, XdrvMailbox.data ); } else { return false; } // Incomplete command - snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s\"}"), - command, index, XdrvMailbox.data ); - return true; }