/* DOMOTICZ MODULE Copyright (C) 2016-2018 by Xose PĂ©rez */ #if DOMOTICZ_SUPPORT #include bool _dcz_enabled = false; //------------------------------------------------------------------------------ // Private methods //------------------------------------------------------------------------------ unsigned char _domoticzRelay(unsigned int idx) { for (unsigned char relayID=0; relayID= 0) { unsigned char value = root["nvalue"]; DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %u for IDX %u\n"), value, idx); relayStatus(relayID, value == 1); } } } }; #if BROKER_SUPPORT void _domoticzBrokerCallback(const char * topic, unsigned char id, const char * payload) { if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { unsigned char value = atoi(payload); domoticzSendRelay(id, value == 1); } } #endif // BROKER_SUPPORT #if WEB_SUPPORT bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) { return (strncmp(key, "dcz", 3) == 0); } void _domoticzWebSocketOnSend(JsonObject& root) { root["dczVisible"] = 1; root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; 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; unsigned int idx = getSetting(key).toInt(); if (idx > 0) { char payload[128]; snprintf(payload, sizeof(payload), "{\"idx\": %u, \"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)); } } unsigned int domoticzIdx(unsigned char relayID) { char buffer[15]; snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); return getSetting(buffer).toInt(); } void domoticzSetup() { _domoticzConfigure(); #if WEB_SUPPORT wsOnSendRegister(_domoticzWebSocketOnSend); wsOnReceiveRegister(_domoticzWebSocketOnReceive); #endif #if BROKER_SUPPORT brokerRegister(_domoticzBrokerCallback); #endif // Callbacks mqttRegister(_domoticzMqtt); espurnaRegisterReload(_domoticzConfigure); } bool domoticzEnabled() { return _dcz_enabled; } #endif