Housekeeping

Housekeeping
This commit is contained in:
Theo Arends 2019-07-24 13:09:42 +02:00
parent 398a72d128
commit c2413a2256
3 changed files with 41 additions and 40 deletions

View File

@ -68,6 +68,11 @@ void ExecuteCommand(char *cmnd, int source)
/********************************************************************************************/ /********************************************************************************************/
// topic: /power1 data: toggle = Console command
// topic: cmnd/sonoff/power1 data: toggle = Mqtt command using topic
// topic: cmnd/sonoffs/power1 data: toggle = Mqtt command using a group topic
// topic: cmnd/DVES_83BB10_fb/power1 data: toggle = Mqtt command using fallback topic
void CommandHandler(char* topic, uint8_t* data, unsigned int data_len) void CommandHandler(char* topic, uint8_t* data, unsigned int data_len)
{ {
if (data_len > MQTT_MAX_PACKET_SIZE) { return; } // Do not allow more data than would be feasable within stack space if (data_len > MQTT_MAX_PACKET_SIZE) { return; } // Do not allow more data than would be feasable within stack space

View File

@ -41,10 +41,10 @@ const char S_JSON_DOMOTICZ_COMMAND_INDEX_LVALUE[] PROGMEM = "{\"" D_CMND_DOMOTIC
char domoticz_in_topic[] = DOMOTICZ_IN_TOPIC; char domoticz_in_topic[] = DOMOTICZ_IN_TOPIC;
char domoticz_out_topic[] = DOMOTICZ_OUT_TOPIC; char domoticz_out_topic[] = DOMOTICZ_OUT_TOPIC;
bool domoticz_subscribe = false;
uint8_t domoticz_update_flag = 1;
int domoticz_update_timer = 0; int domoticz_update_timer = 0;
unsigned long fan_debounce = 0; // iFan02 state debounce timer uint32_t domoticz_fan_debounce = 0; // iFan02 state debounce timer
bool domoticz_subscribe = false;
bool domoticz_update_flag = true;
int DomoticzBatteryQuality(void) int DomoticzBatteryQuality(void)
{ {
@ -85,7 +85,7 @@ void MqttPublishDomoticzFanState()
Response_P(DOMOTICZ_MESSAGE, (int)Settings.domoticz_relay_idx[1], (0 == fan_speed) ? 0 : 2, svalue, DomoticzBatteryQuality(), DomoticzRssiQuality()); Response_P(DOMOTICZ_MESSAGE, (int)Settings.domoticz_relay_idx[1], (0 == fan_speed) ? 0 : 2, svalue, DomoticzBatteryQuality(), DomoticzRssiQuality());
MqttPublish(domoticz_in_topic); MqttPublish(domoticz_in_topic);
fan_debounce = millis(); domoticz_fan_debounce = millis();
} }
} }
@ -94,7 +94,7 @@ void DomoticzUpdateFanState()
if (domoticz_update_flag) { if (domoticz_update_flag) {
MqttPublishDomoticzFanState(); MqttPublishDomoticzFanState();
} }
domoticz_update_flag = 1; domoticz_update_flag = true;
} }
#endif // USE_SONOFF_IFAN #endif // USE_SONOFF_IFAN
@ -125,7 +125,7 @@ void DomoticzUpdatePowerState(uint8_t device)
if (domoticz_update_flag) { if (domoticz_update_flag) {
MqttPublishDomoticzPowerState(device); MqttPublishDomoticzPowerState(device);
} }
domoticz_update_flag = 1; domoticz_update_flag = true;
} }
void DomoticzMqttUpdate(void) void DomoticzMqttUpdate(void)
@ -196,20 +196,20 @@ bool DomoticzMqttData(void)
char stemp1[10]; char stemp1[10];
unsigned long idx = 0; unsigned long idx = 0;
int16_t nvalue = -1; int16_t nvalue = -1;
int16_t found = 0; bool found = false;
domoticz_update_flag = 1; domoticz_update_flag = true;
if (!strncmp(XdrvMailbox.topic, domoticz_out_topic, strlen(domoticz_out_topic))) { if (!strncmp(XdrvMailbox.topic, domoticz_out_topic, strlen(domoticz_out_topic))) {
if (XdrvMailbox.data_len < 20) { if (XdrvMailbox.data_len < 20) {
return 1; return true; // No valid data
} }
StaticJsonBuffer<400> jsonBuf; StaticJsonBuffer<400> jsonBuf;
JsonObject& domoticz = jsonBuf.parseObject(XdrvMailbox.data); JsonObject& domoticz = jsonBuf.parseObject(XdrvMailbox.data);
if (!domoticz.success()) { if (!domoticz.success()) {
return 1; return true; // To much or invalid data
} }
// if (strcmp_P(domoticz["dtype"],PSTR("Light/Switch"))) { // if (strcmp_P(domoticz["dtype"],PSTR("Light/Switch"))) {
// return 1; // return true;
// } // }
idx = domoticz["idx"]; idx = domoticz["idx"];
if (domoticz.containsKey("nvalue")) { if (domoticz.containsKey("nvalue")) {
@ -230,18 +230,18 @@ bool DomoticzMqttData(void)
if (domoticz.containsKey("svalue1")) { if (domoticz.containsKey("svalue1")) {
svalue = domoticz["svalue1"]; svalue = domoticz["svalue1"];
} else { } else {
return 1; return true; // Invalid data
} }
svalue = (nvalue == 2) ? svalue / 10 : 0; svalue = (nvalue == 2) ? svalue / 10 : 0;
if (GetFanspeed() == svalue) { if (GetFanspeed() == svalue) {
return 1; // Stop loop as already set return true; // Stop loop as already set
} }
if (TimePassedSince(fan_debounce) < 1000) { if (TimePassedSince(domoticz_fan_debounce) < 1000) {
return 1; // Stop loop if device in limbo return true; // Stop loop if device in limbo
} }
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_FANSPEED)); snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_FANSPEED));
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), svalue); snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), svalue);
found = 1; found = true;
} else } else
#endif // USE_SONOFF_IFAN #endif // USE_SONOFF_IFAN
if (iscolordimmer && 10 == nvalue) { // Color_SetColor if (iscolordimmer && 10 == nvalue) { // Color_SetColor
@ -254,43 +254,41 @@ bool DomoticzMqttData(void)
uint16_t ww = color["ww"]; ww = ww * level / 100; uint16_t ww = color["ww"]; ww = ww * level / 100;
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_COLOR)); snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_COLOR));
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%02x%02x%02x%02x%02x"), r, g, b, cw, ww); snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%02x%02x%02x%02x%02x"), r, g, b, cw, ww);
found = 1; found = true;
} }
else if ((!iscolordimmer && 2 == nvalue) || // gswitch_sSetLevel else if ((!iscolordimmer && 2 == nvalue) || // gswitch_sSetLevel
(iscolordimmer && 15 == nvalue)) { // Color_SetBrightnessLevel (iscolordimmer && 15 == nvalue)) { // Color_SetBrightnessLevel
if (domoticz.containsKey("svalue1")) { if (domoticz.containsKey("svalue1")) {
nvalue = domoticz["svalue1"]; nvalue = domoticz["svalue1"];
} else { } else {
return 1; return true; // Invalid data
} }
if (light_type && (Settings.light_dimmer == nvalue) && ((power >> i) &1)) { if (light_type && (Settings.light_dimmer == nvalue) && ((power >> i) &1)) {
return 1; return true; // State already set
} }
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_DIMMER)); snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_DIMMER));
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue); snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue);
found = 1; found = true;
} }
else if (1 == nvalue || 0 == nvalue) { else if (1 == nvalue || 0 == nvalue) {
if (((power >> i) &1) == (power_t)nvalue) { if (((power >> i) &1) == (power_t)nvalue) {
return 1; // Stop loop return true; // Stop loop
} }
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_POWER "%s"), (devices_present > 1) ? stemp1 : ""); snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_POWER "%s"), (devices_present > 1) ? stemp1 : "");
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue); snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue);
found = 1; found = true;
} }
break; break;
} }
} }
} }
if (!found) { if (!found) { return true; } // No command received
return 1;
}
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_DOMOTICZ D_RECEIVED_TOPIC " %s, " D_DATA " %s"), XdrvMailbox.topic, XdrvMailbox.data); AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_DOMOTICZ D_RECEIVED_TOPIC " %s, " D_DATA " %s"), XdrvMailbox.topic, XdrvMailbox.data);
domoticz_update_flag = 0; domoticz_update_flag = false;
} }
return 0; return false; // Process unchanged or new data
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -348,14 +346,14 @@ bool DomoticzCommand(void)
bool DomoticzSendKey(uint8_t key, uint8_t device, uint8_t state, uint8_t svalflg) bool DomoticzSendKey(uint8_t key, uint8_t device, uint8_t state, uint8_t svalflg)
{ {
bool result = 0; bool result = false;
if (device <= MAX_DOMOTICZ_IDX) { if (device <= MAX_DOMOTICZ_IDX) {
if ((Settings.domoticz_key_idx[device -1] || Settings.domoticz_switch_idx[device -1]) && (svalflg)) { if ((Settings.domoticz_key_idx[device -1] || Settings.domoticz_switch_idx[device -1]) && (svalflg)) {
Response_P(PSTR("{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}"), Response_P(PSTR("{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}"),
(key) ? Settings.domoticz_switch_idx[device -1] : Settings.domoticz_key_idx[device -1], (state) ? (2 == state) ? "Toggle" : "On" : "Off"); (key) ? Settings.domoticz_switch_idx[device -1] : Settings.domoticz_key_idx[device -1], (state) ? (2 == state) ? "Toggle" : "On" : "Off");
MqttPublish(domoticz_in_topic); MqttPublish(domoticz_in_topic);
result = 1; result = true;
} }
} }
return result; return result;
@ -544,6 +542,12 @@ bool Xdrv07(uint8_t function)
if (Settings.flag.mqtt_enabled) { if (Settings.flag.mqtt_enabled) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND:
DomoticzMqttUpdate();
break;
case FUNC_MQTT_DATA:
result = DomoticzMqttData();
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_ADD_BUTTON: case FUNC_WEB_ADD_BUTTON:
WSContentSend_P(HTTP_BTN_MENU_DOMOTICZ); WSContentSend_P(HTTP_BTN_MENU_DOMOTICZ);
@ -561,12 +565,6 @@ bool Xdrv07(uint8_t function)
case FUNC_MQTT_INIT: case FUNC_MQTT_INIT:
domoticz_update_timer = 2; domoticz_update_timer = 2;
break; break;
case FUNC_MQTT_DATA:
result = DomoticzMqttData();
break;
case FUNC_EVERY_SECOND:
DomoticzMqttUpdate();
break;
case FUNC_SHOW_SENSOR: case FUNC_SHOW_SENSOR:
// DomoticzSendSensor(); // DomoticzSendSensor();
break; break;

View File

@ -784,8 +784,6 @@ bool Xsns29(uint8_t function)
if (i2c_flg) { if (i2c_flg) {
switch (function) { switch (function) {
case FUNC_MQTT_DATA:
break;
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
MCP230xx_Detect(); MCP230xx_Detect();
if (mcp230xx_int_counter_en) { if (mcp230xx_int_counter_en) {