| 271 | } |
| 272 | |
| 273 | void Room::command_callback(NSPanelMQTTManagerCommand &command) { |
| 274 | if (command.has_first_page_turn_on() && command.first_page_turn_on().selected_room() == this->_id && !command.first_page_turn_on().global()) { |
| 275 | SPDLOG_DEBUG("Room {}:{} got command to turn lights on from first page.", this->_id, this->_name); |
| 276 | if (MqttManagerConfig::get_settings().optimistic_mode) { |
| 277 | SPDLOG_DEBUG("Optimistic mode enabled, will not send state updates via callbacks in room."); |
| 278 | this->_send_status_updates = false; |
| 279 | } |
| 280 | |
| 281 | std::vector<std::shared_ptr<Light>> all_room_lights = this->get_all_entities_by_type<Light>(MQTT_MANAGER_ENTITY_TYPE::LIGHT); |
| 282 | // Remove any light entities that are not controlled by first page. |
| 283 | all_room_lights.erase(std::remove_if(all_room_lights.begin(), all_room_lights.end(), [](std::shared_ptr<Light> light) { |
| 284 | return !light->get_controlled_from_main_page(); |
| 285 | }), |
| 286 | all_room_lights.end()); |
| 287 | |
| 288 | std::vector<std::shared_ptr<Light>> lights_list; |
| 289 | // Get all lights that are on |
| 290 | for (auto light : all_room_lights) { |
| 291 | if (light->get_state() && ((command.first_page_turn_on().affect_lights() == NSPanelMQTTManagerCommand_AffectLightsOptions::NSPanelMQTTManagerCommand_AffectLightsOptions_ALL || |
| 292 | (command.first_page_turn_on().affect_lights() == NSPanelMQTTManagerCommand_AffectLightsOptions::NSPanelMQTTManagerCommand_AffectLightsOptions_CEILING_LIGHTS && light->get_light_type() == MQTT_MANAGER_LIGHT_TYPE::CEILING) || |
| 293 | (command.first_page_turn_on().affect_lights() == NSPanelMQTTManagerCommand_AffectLightsOptions::NSPanelMQTTManagerCommand_AffectLightsOptions_TABLE_LIGHTS && light->get_light_type() == MQTT_MANAGER_LIGHT_TYPE::TABLE)))) { |
| 294 | lights_list.push_back(light); |
| 295 | } |
| 296 | } |
| 297 | if (lights_list.size() == 0) { |
| 298 | // No lights that were on were found, get all lights in room. |
| 299 | SPDLOG_DEBUG("No lights were on in room {}::{}. Will affect all lights in room instead.", this->_id, this->_name); |
| 300 | for (auto light : all_room_lights) { |
| 301 | if (command.first_page_turn_on().affect_lights() == NSPanelMQTTManagerCommand_AffectLightsOptions::NSPanelMQTTManagerCommand_AffectLightsOptions_ALL || |
| 302 | (command.first_page_turn_on().affect_lights() == NSPanelMQTTManagerCommand_AffectLightsOptions::NSPanelMQTTManagerCommand_AffectLightsOptions_CEILING_LIGHTS && light->get_light_type() == MQTT_MANAGER_LIGHT_TYPE::CEILING) || |
| 303 | (command.first_page_turn_on().affect_lights() == NSPanelMQTTManagerCommand_AffectLightsOptions::NSPanelMQTTManagerCommand_AffectLightsOptions_TABLE_LIGHTS && light->get_light_type() == MQTT_MANAGER_LIGHT_TYPE::TABLE)) { |
| 304 | lights_list.push_back(light); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Build the individual commands that are to be sent to each light. |
| 310 | NSPanelMQTTManagerCommand cmd; |
| 311 | NSPanelMQTTManagerCommand_LightCommand *light_cmd = cmd.mutable_light_command(); |
| 312 | light_cmd->set_has_brightness(command.first_page_turn_on().has_brightness_value()); |
| 313 | light_cmd->set_brightness(command.first_page_turn_on().brightness_slider_value()); |
| 314 | light_cmd->set_has_color_temperature(command.first_page_turn_on().has_kelvin_value()); |
| 315 | light_cmd->set_color_temperature(command.first_page_turn_on().kelvin_slider_value()); |
| 316 | light_cmd->set_has_hue(false); |
| 317 | light_cmd->set_has_saturation(false); |
| 318 | |
| 319 | for (int i = 0; i < lights_list.size(); i++) { |
| 320 | light_cmd->clear_light_ids(); |
| 321 | light_cmd->add_light_ids(lights_list[i]->get_id()); |
| 322 | lights_list[i]->command_callback(cmd); |
| 323 | } |
| 324 | |
| 325 | if (MqttManagerConfig::get_settings().optimistic_mode) { |
| 326 | this->_send_room_state_update(); |
| 327 | this->_send_status_updates = true; |
| 328 | SPDLOG_DEBUG("Optimistic mode enabled, reenabled send state updates via callbacks in room."); |
| 329 | } |
| 330 | } else if (command.has_first_page_turn_off()) { |
no test coverage detected