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