diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index cd8ca46b..423c90ee 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -46,6 +46,56 @@ void _domoticzStatus(unsigned char id, bool status) { relayStatus(id, status); } +#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE + +void _domoticzLight(unsigned int idx, const JsonObject& root) { + + if (!lightHasColor()) return; + + JsonObject& color = root["Color"]; + if (!color.success()) return; + + // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h): + unsigned int cmode = color["m"]; + + if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h + + lightChannel(0, color["r"]); + lightChannel(1, color["g"]); + lightChannel(2, color["b"]); + + // WARM WHITE (or MONOCHROME WHITE) and COLD WHITE are always sent. + // Apply only when supported. + if (lightChannels() > 3) { + lightChannel(3, color["ww"]); + } + + if (lightChannels() > 4) { + lightChannel(4, color["cw"]); + } + + // domoticz uses 100 as maximum value while we're using LIGHT_MAX_BRIGHTNESS + unsigned int brightness = (root["Level"].as() / 100.0) * LIGHT_MAX_BRIGHTNESS; + lightBrightness(brightness); + + DEBUG_MSG_P(PSTR("[DOMOTICZ] Received rgb:%u,%u,%u ww:%u,cw:%u brightness:%u for IDX %u\n"), + color["r"].as(), + color["g"].as(), + color["b"].as(), + color["ww"].as(), + color["cw"].as(), + brightness, + idx + ); + + lightUpdate(true, mqttForward()); + + } + +} + +#endif + void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) { if (!_dcz_enabled) return; @@ -78,79 +128,18 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) // IDX unsigned int idx = root["idx"]; String stype = root["stype"]; - if ( - (stype.equals("RGB") || stype.equals("RGBW") || stype.equals("RGBWW")) - && domoticzIdx(0) == idx - ) { -#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE - if (lightHasColor()) { - - // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h): - unsigned int cmode = root["Color"]["m"]; - unsigned int cval; - - if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom - see domoticz ColorSwitch.h - - // RED - cval = root["Color"]["r"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received RED value %u for IDX %u\n"), cval, idx); - lightChannel(0, cval); - - // GREEN - cval = root["Color"]["g"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received GREEN value %u for IDX %u\n"), cval, idx); - lightChannel(1, cval); - - // BLUE - cval = root["Color"]["b"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received BLUE value %u for IDX %u\n"), cval, idx); - lightChannel(2, cval); - - // WARM WHITE or MONOCHROME WHITE if supported - if (lightChannels() > 3) { - cval = root["Color"]["ww"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received WARM WHITE value %u for IDX %u\n"), cval, idx); - lightChannel(3, cval); - } - - // COLD WHITE if supported - if (lightChannels() > 4) { - cval = root["Color"]["cw"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received COLD WHITE value %u for IDX %u\n"), cval, idx); - lightChannel(4, cval); - } - - - unsigned int brightness = root["Level"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received BRIGHTNESS value %u for IDX %u\n"), brightness, idx); - unsigned int br = (brightness / 100.0) * LIGHT_MAX_BRIGHTNESS; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Calculated BRIGHTNESS value %u for IDX %u\n"), br, idx); - lightBrightness(br); // domoticz uses 100 as maximum value while we're using LIGHT_MAX_BRIGHTNESS - - // update lights - lightUpdate(true, mqttForward()); - - } - - - - unsigned char relayID = _domoticzRelay(idx); - if (relayID >= 0) { - unsigned char value = root["nvalue"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); - _domoticzStatus(relayID, value > 0); - } - } -#else - DEBUG_MSG_P(PSTR("[DOMOTICZ] ESPurna compiled without LIGHT_PROVIDER")); -#endif - } else { - unsigned char relayID = _domoticzRelay(idx); - if (relayID >= 0) { - unsigned char value = root["nvalue"]; - DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); - _domoticzStatus(relayID, value == 1); + + #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE + if (stype.startsWith("RGB") && (domoticzIdx(0) == idx)) { + _domoticzLight(idx, root); } + #endif + + unsigned char relayID = _domoticzRelay(idx); + if (relayID >= 0) { + unsigned char value = root["nvalue"]; + DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); + _domoticzStatus(relayID, value > 1); } } @@ -171,7 +160,7 @@ void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsig _dcz_relay_state[id] = status; domoticzSendRelay(id, status); } - + } #endif // BROKER_SUPPORT