Browse Source

Moved domoticz code from relay.ino to domoticz.ino

fastled
Xose Pérez 7 years ago
parent
commit
7f41967e7a
3 changed files with 66 additions and 71 deletions
  1. +63
    -0
      code/espurna/domoticz.ino
  2. +3
    -0
      code/espurna/espurna.ino
  3. +0
    -71
      code/espurna/relay.ino

+ 63
- 0
code/espurna/domoticz.ino View File

@ -8,6 +8,8 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#if ENABLE_DOMOTICZ
#include <ArduinoJson.h>
template<typename T> void domoticzSend(const char * key, T nvalue, const char * svalue) {
unsigned int idx = getSetting(key).toInt();
if (idx > 0) {
@ -21,4 +23,65 @@ template<typename T> void domoticzSend(const char * key, T nvalue) {
domoticzSend(key, nvalue, "");
}
void relayDomoticzSend(unsigned int relayID) {
char buffer[15];
sprintf(buffer, "dczRelayIdx%d", relayID);
domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
}
int relayFromIdx(unsigned int idx) {
for (int relayID=0; relayID<relayCount(); relayID++) {
if (relayToIdx(relayID) == idx) {
return relayID;
}
}
return -1;
}
int relayToIdx(unsigned int relayID) {
char buffer[15];
sprintf(buffer, "dczRelayIdx%d", relayID);
return getSetting(buffer).toInt();
}
void domoticzSetup() {
mqttRegister([](unsigned int type, const char * topic, const char * payload) {
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribeRaw(dczTopicOut.c_str());
}
if (type == MQTT_MESSAGE_EVENT) {
// Check topic
if (dczTopicOut.equals(topic)) {
// Parse response
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char *) payload);
if (!root.success()) {
DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
return;
}
// IDX
unsigned long idx = root["idx"];
int relayID = relayFromIdx(idx);
if (relayID >= 0) {
unsigned long value = root["nvalue"];
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
relayStatus(relayID, value == 1);
}
}
}
});
}
#endif

+ 3
- 0
code/espurna/espurna.ino View File

@ -244,6 +244,9 @@ void setup() {
#if ENABLE_EMON
powerMonitorSetup();
#endif
#if ENABLE_DOMOTICZ
domoticzSetup();
#endif
// Prepare configuration for version 2.0
hwUpwardsCompatibility();


+ 0
- 71
code/espurna/relay.ino View File

@ -306,74 +306,6 @@ void relayWS() {
wsSend(output.c_str());
}
//------------------------------------------------------------------------------
// Domoticz
//------------------------------------------------------------------------------
#if ENABLE_DOMOTICZ
void relayDomoticzSend(unsigned int relayID) {
char buffer[15];
sprintf(buffer, "dczRelayIdx%d", relayID);
domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
}
int relayFromIdx(unsigned int idx) {
for (int relayID=0; relayID<relayCount(); relayID++) {
if (relayToIdx(relayID) == idx) {
return relayID;
}
}
return -1;
}
int relayToIdx(unsigned int relayID) {
char buffer[15];
sprintf(buffer, "dczRelayIdx%d", relayID);
return getSetting(buffer).toInt();
}
void relayDomoticzSetup() {
mqttRegister([](unsigned int type, const char * topic, const char * payload) {
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribeRaw(dczTopicOut.c_str());
}
if (type == MQTT_MESSAGE_EVENT) {
// Check topic
if (dczTopicOut.equals(topic)) {
// Parse response
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char *) payload);
if (!root.success()) {
DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
return;
}
// IDX
unsigned long idx = root["idx"];
int relayID = relayFromIdx(idx);
if (relayID >= 0) {
unsigned long value = root["nvalue"];
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
relayStatus(relayID, value == 1);
}
}
}
});
}
#endif
//------------------------------------------------------------------------------
// MQTT
//------------------------------------------------------------------------------
@ -495,9 +427,6 @@ void relaySetup() {
relaySetupAPI();
relaySetupMQTT();
#if ENABLE_DOMOTICZ
relayDomoticzSetup();
#endif
DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size());


Loading…
Cancel
Save