/* DOMOTICZ MODULE Copyright (C) 2016-2019 by Xose PĂ©rez */ #if DOMOTICZ_SUPPORT #include "broker.h" #include "domoticz.h" #include "mqtt.h" #include "relay.h" bool _dcz_enabled = false; std::bitset _dcz_relay_state; //------------------------------------------------------------------------------ // Private methods //------------------------------------------------------------------------------ unsigned char _domoticzIdx(unsigned char relayID, unsigned char defaultValue = 0) { return getSetting({"dczRelayIdx", relayID}, defaultValue); } int _domoticzRelay(unsigned int idx) { for (unsigned char relayID=0; relayID(), color["g"].as(), color["b"].as(), color["ww"].as(), color["cw"].as(), color["t"].as(), color["Level"].as(), idx ); // m field contains information about color mode (enum ColorMode from domoticz ColorSwitch.h): unsigned int cmode = color["m"]; if (cmode == 2) { // ColorModeWhite - WW,CW,temperature (t unused for now) if (lightChannels() < 2) return; lightChannel(0, color["ww"]); lightChannel(1, color["cw"]); } else if (cmode == 3 || cmode == 4) { // ColorModeRGB or ColorModeCustom if (lightChannels() < 3) return; 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::BRIGHTNESS_MAX (unsigned char) lightBrightness((root["Level"].as() / 100.0) * Light::BRIGHTNESS_MAX); lightUpdate(true, mqttForward()); } #endif void _domoticzMqtt(unsigned int type, const char * topic, char * payload) { if (!_dcz_enabled) return; const String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); if (type == MQTT_CONNECT_EVENT) { // Subscribe to domoticz action topics mqttSubscribeRaw(dczTopicOut.c_str()); // Send relays state on connection domoticzSendRelays(); } if (type == MQTT_MESSAGE_EVENT) { // Check topic if (dczTopicOut.equals(topic)) { // Parse response DynamicJsonBuffer jsonBuffer(1024); JsonObject& root = jsonBuffer.parseObject(payload); if (!root.success()) { DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n")); return; } // IDX unsigned int idx = root["idx"]; String stype = root["stype"]; #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE if (stype.startsWith("RGB") && (_domoticzIdx(0) == idx)) { _domoticzLight(idx, root); } #endif int 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 BROKER_SUPPORT void _domoticzConfigCallback(const String& key, const String& value) { if (key.equals("relayDummy")) { _domoticzRelayConfigure(value.toInt()); return; } } void _domoticzBrokerCallback(const String& topic, unsigned char id, unsigned int value) { // Only process status messages for switches if (!topic.equals(MQTT_TOPIC_RELAY)) { return; } if (_domoticzStatus(id) == value) return; _dcz_relay_state[id] = value; domoticzSendRelay(id, value); } #endif // BROKER_SUPPORT #if WEB_SUPPORT bool _domoticzWebSocketOnKeyCheck(const char * key, JsonVariant& value) { return (strncmp(key, "dcz", 3) == 0); } void _domoticzWebSocketOnVisible(JsonObject& root) { root["dczVisible"] = static_cast(haveRelaysOrSensors()); } void _domoticzWebSocketOnConnected(JsonObject& root) { root["dczEnabled"] = getSetting("dczEnabled", 1 == DOMOTICZ_ENABLED); root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC); root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); JsonArray& relays = root.createNestedArray("dczRelays"); for (unsigned char i=0; i void domoticzSend(const char * key, T nvalue, const char * svalue) { if (!_dcz_enabled) return; const auto idx = getSetting(key, 0); if (idx > 0) { char payload[128]; snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); } } template void domoticzSend(const char * key, T nvalue) { domoticzSend(key, nvalue, ""); } void domoticzSendRelay(unsigned char relayID, bool status) { if (!_dcz_enabled) return; char buffer[15]; snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); domoticzSend(buffer, status ? "1" : "0"); } void domoticzSendRelays() { for (uint8_t relayID=0; relayID < relayCount(); relayID++) { domoticzSendRelay(relayID, relayStatus(relayID)); } } void domoticzSetup() { _domoticzConfigure(); #if WEB_SUPPORT wsRegister() .onVisible(_domoticzWebSocketOnVisible) .onConnected(_domoticzWebSocketOnConnected) .onKeyCheck(_domoticzWebSocketOnKeyCheck); #endif #if BROKER_SUPPORT StatusBroker::Register(_domoticzBrokerCallback); ConfigBroker::Register(_domoticzConfigCallback); #endif // Callbacks mqttRegister(_domoticzMqtt); espurnaRegisterReload(_domoticzConfigure); } bool domoticzEnabled() { return _dcz_enabled; } #endif