espleaf/src/state.cpp

139 lines
4.1 KiB
C++

#include "state.h"
#include "tools.h"
#include "mqtt.h"
#include <ArduinoJson.h>
void State::parse_json(char* str) {
StaticJsonDocument<512> json;
deserializeJson(json, str);
if(json.containsKey("state")) parse_state(json["state"]);
if(json.containsKey("effect")) parse_mode(json["effect"]);
if(json.containsKey("duration")) set_duration(json["duration"]);
if(json.containsKey("brightness")) set_brightness(json["brightness"]);
if(json.containsKey("rgb")) set_color(json["rgb"]);
if(json.containsKey("speedup")) set_speedup(json["speedup"]);
}
void State::parse_state(String state) {
if (state.equals("ON")) {
set_mode(AM_CORNERS);
} else if (state.equals("OFF")) {
set_mode(AM_OFF);
} else {
LOGln("parse_state: Unknown state %s", state.c_str());
}
}
void State::parse_mode(String mode) {
AnimationMode m = AM_NONE;
for (std::pair<AnimationMode, const char*> pair : animation_mode_names) {
if (mode.equals(pair.second)) {
m = pair.first;
break;
}
}
if (m == AM_NONE) {
LOGln("parse_mode: Unknown mode '%s'.", mode.c_str());
} else {
set_mode(m);
}
}
void State::set_mode(AnimationMode m) {
_mode = m;
}
void State::set_duration(uint16_t d) {
_duration = d;
}
void State::set_brightness(uint8_t b) {
_brightness = b;
}
void State::set_color(JsonObject rgb) {
if (!rgb.containsKey("r") || !rgb.containsKey("g") || !rgb.containsKey("b")) {
LOGln("set_color: Invalid rgb data.");
} else {
_color = CRGB(rgb["r"], rgb["g"], rgb["b"]);
}
}
void State::set_speedup(uint8_t s) {
_speedup = s;
}
void State::set(String key, String value) {
if (key.equals("state")) { parse_state(value); }
else if (key.equals("effect") || key.equals("mode")) { parse_mode(value); }
else if (key.equals("duration")) { set_duration(value.toInt()); }
else if (key.equals("brightness")) { set_brightness(value.toInt()); }
else if (key.equals("color")) {
if (value.equals("red")) { _color = CRGB::Red; }
else if (value.equals("green")) { _color = CRGB::Green; }
else if (value.equals("blue")) { _color = CRGB::Blue; }
else if (value.equals("pink")) { _color = CRGB::Pink; }
else if (value.equals("yellow")) { _color = CRGB::Yellow; }
else if (value.equals("orange")) { _color = CRGB::Orange; }
else { LOGln("Unknown color name %s.", value.c_str());}
} else if (key.equals("speedup")) { set_speedup(value.toInt()); }
else {
LOGln("Unknown key '%s'. (For reference: Value is '%s'.)", key.c_str(), value.c_str());
}
}
void State::commit() {
if (_brightness >= 0) {
if (temp_mode_until == 0) {
return_to_brightness = FastLED.getBrightness();
}
FastLED.setBrightness(_brightness);
}
if (_mode != AM_NONE) {
if (_duration > 0) {
temp_mode = _mode;
temp_mode_until = millis() + _duration*1000;
} else {
mode = _mode;
}
}
if (_color != CRGB(0, 0, 0)) {
color = _color;
}
publish_current_state();
}
void State::publish_current_state() {
StaticJsonDocument<512> json;
json["state"] = (mode==AM_OFF) ? "OFF" : "ON";
json["brightness"] = FastLED.getBrightness();
json["effect"] = animation_mode_names[get_active_mode()];
JsonObject rgb = json.createNestedObject("rgb");
rgb["r"] = color.r;
rgb["g"] = color.g;
rgb["b"] = color.b;
String result = "";
serializeJson(json, result);
LOGln("Reporting current state: %s", result.c_str());
mqtt_publish_current_state(result);
}
AnimationMode State::get_active_mode() {
AnimationMode active_mode = mode;
if (temp_mode_until > 0) {
if (temp_mode_until>millis()) {
active_mode = temp_mode;
} else {
temp_mode_until = 0;
if (return_to_brightness != -1) {
FastLED.setBrightness(return_to_brightness);
return_to_brightness = -1;
}
State::publish_current_state();
}
}
return active_mode;
}