1 /** 2 * Copyright © 2017 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "anyof.hpp" 17 18 #include "fan.hpp" 19 #include "psensor.hpp" 20 21 #include <phosphor-logging/log.hpp> 22 23 #include <algorithm> 24 25 namespace phosphor 26 { 27 namespace fan 28 { 29 namespace presence 30 { 31 AnyOf::AnyOf(const Fan& fan, 32 const std::vector<std::reference_wrapper<PresenceSensor>>& s) : 33 RedundancyPolicy(fan), 34 state() 35 { 36 for (auto& sensor : s) 37 { 38 state.emplace_back(sensor, false); 39 } 40 } 41 42 void AnyOf::stateChanged(bool present, PresenceSensor& sensor) 43 { 44 // Find the sensor that changed state. 45 auto sit = 46 std::find_if(state.begin(), state.end(), [&sensor](const auto& s) { 47 return std::get<0>(s).get() == sensor; 48 }); 49 if (sit != state.end()) 50 { 51 // Update our cache of the sensors state and re-evaluate. 52 std::get<bool>(*sit) = present; 53 auto newState = 54 std::any_of(state.begin(), state.end(), 55 [](const auto& s) { return std::get<bool>(s); }); 56 setPresence(fan, newState); 57 } 58 } 59 60 void AnyOf::monitor() 61 { 62 // Start all sensors in the anyof redundancy set. 63 64 for (auto& s : state) 65 { 66 auto& sensor = std::get<0>(s); 67 std::get<bool>(s) = sensor.get().start(); 68 } 69 70 auto present = std::any_of(state.begin(), state.end(), 71 [](const auto& s) { return std::get<bool>(s); }); 72 setPresence(fan, present); 73 } 74 75 } // namespace presence 76 } // namespace fan 77 } // namespace phosphor 78