blob: 0e139193200f20bbc1900dacac9158e5a89813a2 [file] [log] [blame]
James Zheng6df8bb52024-11-27 23:38:47 +00001#pragma once
2
3#include <deque>
4#include <memory>
5#include <string>
6#include <unordered_map>
7#include <unordered_set>
8
9namespace pid_control
10{
11
12/**
13 * Log the reason for a zone to enter and leave the failsafe mode.
14 *
15 * Particularly, for entering the failsafe mode:
16 * 1. A sensor is specified in thermal config as an input but missed in DBus
17 * 2. A sensor has null readings in DBus
18 * 3. A sensor is abnormal in DBus (not functional, not enabled, etc)
19 * 4. A sensor's reading is above upper critical (UC) limit
20 *
21 * Among the above reasons:
22 * 1 excludes 2, 3, 4.
23 * 2 excludes 1, 4.
24 * 3 excludes 1.
25 * 4 excludes 1, 2.
26 *
27 * Note that this log is at the zone level, not the sensor level.
28 */
29class FailsafeLogger
30{
31 public:
32 FailsafeLogger(size_t logMaxCountPerSecond = 20,
33 bool currentFailsafeState = false) :
34 _logMaxCountPerSecond(logMaxCountPerSecond),
35 _currentFailsafeState(currentFailsafeState)
36 {}
37 ~FailsafeLogger() = default;
38
39 /** Attempt to output an entering/leaving-failsafe-mode log.
40 */
41 void outputFailsafeLog(int64_t zoneId, bool newFailsafeState,
42 const std::string location,
43 const std::string reason);
44
45 private:
46 // The maximum number of log entries to be output within 1 second.
47 size_t _logMaxCountPerSecond;
48 // Whether the zone is currently in the failsafe mode.
49 bool _currentFailsafeState;
50 // The timestamps of the log entries.
51 std::deque<size_t> _logTimestamps;
52 // The logs already encountered in the current state.
53 std::unordered_set<std::string> _logsInCurrentState;
54};
55
56} // namespace pid_control