#include "state.h" #include "tools.h" #include "mqtt.h" #include 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) { if (mode.equals("nodes")) { set_mode(AM_NODES); } else if (mode.equals("first_node")) { set_mode(AM_FIRST_NODE); } else if (mode.equals("corners")) { set_mode(AM_CORNERS); } else if (mode.equals("first_corner")) { set_mode(AM_FIRST_CORNER); } else if (mode.equals("off")) { set_mode(AM_OFF); } else if (mode.equals("flash")) { set_mode(AM_FLASH); } else if (mode.equals("static")) { set_mode(AM_STATIC); } else { LOGln("parse_mode: Unknown mode '%s'.", mode.c_str()); } } 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"] = mode; JsonObject rgb = json.createNestedObject("rgb"); rgb["r"] = color.r; rgb["g"] = color.g; rgb["b"] = color.b; String result = ""; serializeJson(json, result); mqtt_publish_current_state(result); }