MQTT Callback for REST API

* SetBrightness fix from REST API callback from Master
* Added twinkleRandom to REST API (missing?)
* MQTT callback
This commit is contained in:
debsahu 2018-03-26 16:45:00 -04:00
parent 2bc1a68fb3
commit e3060fa7ee

View file

@ -510,11 +510,19 @@ void setup() {
brightness = 0; brightness = 0;
} }
strip.setBrightness(brightness); strip.setBrightness(brightness);
#ifdef ENABLE_MQTT
if (mode == HOLD) { mqtt_client.publish(mqtt_outtopic, String(String("OK %") + String(brightness)).c_str());
mode = ALL; #endif
} #ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK %") + String(brightness)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
getStatusJSON(); getStatusJSON();
}); });
@ -530,6 +538,12 @@ void setup() {
ws2812fx_speed = server.arg("d").toInt(); ws2812fx_speed = server.arg("d").toInt();
ws2812fx_speed = constrain(ws2812fx_speed, 0, 255); ws2812fx_speed = constrain(ws2812fx_speed, 0, 255);
strip.setSpeed(convertSpeed(ws2812fx_speed)); strip.setSpeed(convertSpeed(ws2812fx_speed));
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ?") + String(ws2812fx_speed)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ?") + String(ws2812fx_speed)).c_str());
#endif
} }
getStatusJSON(); getStatusJSON();
@ -563,6 +577,18 @@ void setup() {
mode = OFF; mode = OFF;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =off").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = false;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/all", []() { server.on("/all", []() {
@ -570,6 +596,18 @@ void setup() {
mode = ALL; mode = ALL;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =all").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =all").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/wipe", []() { server.on("/wipe", []() {
@ -577,6 +615,18 @@ void setup() {
mode = WIPE; mode = WIPE;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =wipe").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =wipe").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/rainbow", []() { server.on("/rainbow", []() {
@ -584,6 +634,18 @@ void setup() {
mode = RAINBOW; mode = RAINBOW;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =rainbow").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =rainbow").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/rainbowCycle", []() { server.on("/rainbowCycle", []() {
@ -591,6 +653,18 @@ void setup() {
mode = RAINBOWCYCLE; mode = RAINBOWCYCLE;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =rainbowCycle").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =rainbowCycle").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/theaterchase", []() { server.on("/theaterchase", []() {
@ -598,6 +672,37 @@ void setup() {
mode = THEATERCHASE; mode = THEATERCHASE;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =theaterchase").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =theaterchase").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
});
server.on("/twinkleRandom", []() {
exit_func = true;
mode = TWINKLERANDOM;
getArgs();
getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =twinkleRandom").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =twinkleRandom").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/theaterchaseRainbow", []() { server.on("/theaterchaseRainbow", []() {
@ -605,6 +710,18 @@ void setup() {
mode = THEATERCHASERAINBOW; mode = THEATERCHASERAINBOW;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =theaterchaseRainbow").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =theaterchaseRainbow").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/tv", []() { server.on("/tv", []() {
@ -612,6 +729,18 @@ void setup() {
mode = TV; mode = TV;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =tv").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =tv").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
server.on("/get_modes", []() { server.on("/get_modes", []() {
@ -622,6 +751,20 @@ void setup() {
getArgs(); getArgs();
mode = SET_MODE; mode = SET_MODE;
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK /") + String(ws2812fx_mode).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK /") + String(ws2812fx_mode)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}); });
#ifdef HTTP_OTA #ifdef HTTP_OTA