blob: fe35db00668a5dd4e963ee120c1dc5d4859aeafb [file] [log] [blame]
Matt Spinlerd7abf362018-01-18 12:40:02 -06001#pragma once
2
3#include <deque>
4#include <tuple>
5#include <vector>
6
7namespace witherspoon
8{
9namespace power
10{
11namespace history
12{
13
14static constexpr auto recIDPos = 0;
15static constexpr auto recTimePos = 1;
16static constexpr auto recAvgPos = 2;
17static constexpr auto recMaxPos = 3;
18using Record = std::tuple<size_t, int64_t, int64_t, int64_t>;
19
20/**
21 * @class RecordManager
22 *
23 * This class manages the records for the input power history of
24 * a power supply.
25 *
26 * The history is the average and maximum power values across 30s
27 * intervals. Every 30s, a new record will be available from the
28 * PS. This class takes that raw PS data and converts it into
29 * something useable by D-Bus. It ensures the readings are always
30 * sorted newest to oldest, and prunes out the oldest entries when
31 * necessary. If there is a problem with the ordering IDs coming
32 * from the PS, it will clear out the old records and start over.
33 */
34class RecordManager
35{
36 public:
37
38 static constexpr auto LAST_SEQUENCE_ID = 0xFF;
39
40 using DBusRecord = std::tuple<uint64_t, int64_t>;
41 using DBusRecordList = std::vector<DBusRecord>;
42
43 RecordManager() = delete;
44 ~RecordManager() = default;
45 RecordManager(const RecordManager&) = default;
46 RecordManager& operator=(const RecordManager&) = default;
47 RecordManager(RecordManager&&) = default;
48 RecordManager& operator=(RecordManager&&) = default;
49
50 /**
51 * @brief Constructor
52 *
53 * @param[in] maxRec - the maximum number of history
54 * records to keep at a time
55 */
56 RecordManager(size_t maxRec) :
57 RecordManager(maxRec, LAST_SEQUENCE_ID)
58 {
59 }
60
61 /**
62 * @brief Constructor
63 *
64 * @param[in] maxRec - the maximum number of history
65 * records to keep at a time
66 * @param[in] lastSequenceID - the last sequence ID the power supply
67 * will use before starting over
68 */
69 RecordManager(size_t maxRec, size_t lastSequenceID) :
70 maxRecords(maxRec),
71 lastSequenceID(lastSequenceID)
72 {
73 }
74
75 /**
76 * @brief Returns the number of records
77 *
78 * @return size_t - the number of records
79 *
80 */
81 inline size_t getNumRecords() const
82 {
83 return records.size();
84 }
85
86 /**
87 * @brief Deletes all records
88 */
89 inline void clear()
90 {
91 records.clear();
92 }
93
94 private:
95
96 /**
97 * @brief The maximum number of entries to keep in the history.
98 *
99 * When a new record is added, the oldest one will be removed.
100 */
101 const size_t maxRecords;
102
103 /**
104 * @brief The last ID the power supply returns before rolling over
105 * back to the first ID of 0.
106 */
107 const size_t lastSequenceID;
108
109 /**
110 * @brief The list of timestamp/average/maximum records.
111 * Newer records are added to the front, and older ones
112 * removed from the back.
113 */
114 std::deque<Record> records;
115};
116
117}
118}
119}