| 34 | { } |
| 35 | |
| 36 | bool StateTransaction::commit(bool trimRequest) |
| 37 | { |
| 38 | const nlohmann::json& stateJson = (state != nullptr) ? *state : nlohmann::json::object(); |
| 39 | // Check this before request is trimmed |
| 40 | if (!request.count("on")) |
| 41 | { |
| 42 | if (!stateJson.value("on", false) && request.value("bri", 254) != 0 |
| 43 | && (request.count("bri") || request.count("effect") || request.count("hue") || request.count("sat") |
| 44 | || request.count("xy") || request.count("ct"))) |
| 45 | { |
| 46 | // Turn on if it was turned off |
| 47 | request["on"] = true; |
| 48 | } |
| 49 | else if (request.value("bri", 254) == 0 && stateJson.value("on", true)) |
| 50 | { |
| 51 | // Turn off if brightness is 0 |
| 52 | request["on"] = false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (trimRequest) |
| 57 | { |
| 58 | this->trimRequest(); |
| 59 | } |
| 60 | // Empty request or request with only transition makes no sense |
| 61 | if (!request.empty() && !(request.size() == 1 && request.count("transitiontime"))) |
| 62 | { |
| 63 | nlohmann::json reply = commands.PUTRequest(path, request, CURRENT_FILE_INFO); |
| 64 | if (utils::validatePUTReply(path, request, reply)) |
| 65 | { |
| 66 | if (state != nullptr) |
| 67 | { |
| 68 | // Apply changes to state |
| 69 | for (auto it = request.begin(); it != request.end(); ++it) |
| 70 | { |
| 71 | if (it.key() == "transitiontime") |
| 72 | { |
| 73 | continue; |
| 74 | } |
| 75 | (*state)[it.key()] = it.value(); |
| 76 | } |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | Action StateTransaction::toAction() |
| 86 | { |