1 /**
2 * Copyright 2017 Google Inc.
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
17 #include "failsafeloggers/builder.hpp"
18
19 #include "conf.hpp"
20 #include "failsafeloggers/failsafe_logger.hpp"
21 #include "failsafeloggers/failsafe_logger_utility.hpp"
22
23 #include <algorithm>
24 #include <iostream>
25 #include <memory>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29
30 namespace pid_control
31 {
32
buildFailsafeLoggers(const std::unordered_map<int64_t,std::shared_ptr<ZoneInterface>> & zones,const size_t logMaxCountPerSecond)33 void buildFailsafeLoggers(
34 const std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>>& zones,
35 const size_t logMaxCountPerSecond /* = 20 */)
36 {
37 zoneIdToFailsafeLogger =
38 std::unordered_map<int64_t,
39 std::shared_ptr<pid_control::FailsafeLogger>>();
40 sensorNameToZoneId =
41 std::unordered_map<std::string, std::vector<int64_t>>();
42 for (const auto& zoneIdToZone : zones)
43 {
44 int64_t zoneId = zoneIdToZone.first;
45 // Create a failsafe logger for each zone.
46 zoneIdToFailsafeLogger[zoneId] = std::make_shared<FailsafeLogger>(
47 logMaxCountPerSecond, zoneIdToZone.second->getFailSafeMode());
48 // Build the sensor-zone topology map.
49 std::vector<std::string> sensorNames =
50 zoneIdToZone.second->getSensorNames();
51 for (const std::string& sensorName : sensorNames)
52 {
53 if (std::find(sensorNameToZoneId[sensorName].begin(),
54 sensorNameToZoneId[sensorName].end(), zoneId) ==
55 sensorNameToZoneId[sensorName].end())
56 {
57 sensorNameToZoneId[sensorName].push_back(zoneId);
58 }
59 }
60 std::cerr << "Build failsafe logger for Zone " << zoneId
61 << " with initial "
62 << "failsafe mode: " << zoneIdToZone.second->getFailSafeMode()
63 << "\n";
64 }
65 }
66
67 } // namespace pid_control
68