Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
battery_collector.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
20#include <chrono>
21#include <cstdint>
22#include <memory>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
29
30namespace kcenon {
31namespace monitoring {
32
37enum class battery_status {
38 unknown,
39 charging,
42 full
43};
44
48inline std::string battery_status_to_string(battery_status status) {
49 switch (status) {
51 return "charging";
53 return "discharging";
55 return "not_charging";
57 return "full";
58 default:
59 return "unknown";
60 }
61}
62
68 std::string id;
69 std::string name;
70 std::string path;
71 std::string manufacturer;
72 std::string model;
73 std::string serial;
74 std::string technology;
75};
76
83
84 // Basic metrics
85 double level_percent{0.0};
87 bool is_charging{false};
88 bool ac_connected{false};
89
90 // Time estimates
93
94 // Capacity metrics
95 double design_capacity_wh{0.0};
97 double current_capacity_wh{0.0};
98 double health_percent{0.0};
99
100 // Electrical metrics
101 double voltage_volts{0.0};
102 double current_amps{0.0};
103 double power_watts{0.0};
104
105 // Thermal
108
109 // Cycle count
110 int64_t cycle_count{-1};
111
112 // Availability flags
113 bool battery_present{false};
114 bool metrics_available{false};
115
116 std::chrono::system_clock::time_point timestamp;
117};
118
119// Forward declaration
120namespace platform {
121class metrics_provider;
122} // namespace platform
123
132 public:
135
136 // Non-copyable, non-moveable due to internal state
141
147
152 std::vector<battery_info> enumerate_batteries();
153
158 std::vector<battery_reading> read_all_batteries();
159
160 private:
161 std::unique_ptr<platform::metrics_provider> provider_;
162};
163
173 public:
175 ~battery_collector() override = default;
176
177 // Non-copyable, non-moveable due to internal state
182
183 // collector_plugin implementation
184 auto name() const -> std::string_view override { return "battery"; }
185 auto collect() -> std::vector<metric> override;
186 auto interval() const -> std::chrono::milliseconds override { return std::chrono::seconds(30); }
187 auto is_available() const -> bool override;
188 auto get_metric_types() const -> std::vector<std::string> override;
189
190 auto get_metadata() const -> plugin_metadata override {
191 return plugin_metadata{
192 .name = name(),
193 .description = "Battery status metrics (charge, health, temperature)",
194 .category = plugin_category::hardware,
195 .version = "1.0.0",
196 .dependencies = {},
197 .requires_platform_support = true
198 };
199 }
200
201 auto initialize(const config_map& config) -> bool override;
202 void shutdown() override {}
203 auto get_statistics() const -> stats_map override;
204
209 std::vector<battery_reading> get_last_readings() const;
210
216
217 private:
219
220 // Configuration
221 bool collect_health_{true};
223
224 // Statistics
225 mutable std::mutex stats_mutex_;
226 std::atomic<size_t> batteries_found_{0};
227 std::atomic<size_t> collection_count_{0};
228 std::atomic<size_t> collection_errors_{0};
229 std::vector<battery_reading> last_readings_;
230
231 // Helper methods
232 metric create_battery_metric(const std::string& name, double value,
233 const battery_reading& reading,
234 const std::string& unit = "") const;
235 void add_battery_metrics(std::vector<metric>& metrics,
236 const battery_reading& reading);
237};
238
239} // namespace monitoring
240} // namespace kcenon
Battery status monitoring collector implementing collector_plugin interface.
std::vector< battery_reading > get_last_readings() const
auto interval() const -> std::chrono::milliseconds override
Get the collection interval for this plugin.
battery_collector & operator=(battery_collector &&)=delete
battery_collector(battery_collector &&)=delete
std::unique_ptr< battery_info_collector > collector_
void shutdown() override
Shutdown plugin and release resources.
auto get_statistics() const -> stats_map override
Get plugin statistics.
auto initialize(const config_map &config) -> bool override
Initialize plugin with configuration.
auto name() const -> std::string_view override
Get the unique name of this plugin.
auto collect() -> std::vector< metric > override
Collect current metrics from this plugin.
auto get_metric_types() const -> std::vector< std::string > override
Get supported metric types.
battery_collector & operator=(const battery_collector &)=delete
metric create_battery_metric(const std::string &name, double value, const battery_reading &reading, const std::string &unit="") const
auto get_metadata() const -> plugin_metadata override
Get plugin metadata.
void add_battery_metrics(std::vector< metric > &metrics, const battery_reading &reading)
std::vector< battery_reading > last_readings_
auto is_available() const -> bool override
Check if this plugin is available on the current system.
battery_collector(const battery_collector &)=delete
Battery data collector using platform abstraction layer.
std::unique_ptr< platform::metrics_provider > provider_
battery_info_collector & operator=(const battery_info_collector &)=delete
battery_info_collector & operator=(battery_info_collector &&)=delete
battery_info_collector(const battery_info_collector &)=delete
std::vector< battery_reading > read_all_batteries()
std::vector< battery_info > enumerate_batteries()
battery_info_collector(battery_info_collector &&)=delete
Pure virtual interface for metric collector plugins.
Abstract interface for platform-specific metrics collection.
Plugin interface for metric collectors.
Adapter for metric types to support interface definitions.
std::unordered_map< std::string, double > stats_map
Type alias for statistics map.
battery_status
Current battery charging status.
@ charging
Battery is charging.
@ not_charging
Battery is not charging (plugged in but not charging)
@ discharging
Battery is discharging.
@ full
Battery is fully charged.
@ hardware
Hardware sensors (GPU, temperature, battery, power)
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
@ platform
Platform/system power domain.
std::string battery_status_to_string(battery_status status)
Convert battery_status to string representation.
Information about a battery source.
std::string serial
Battery serial number.
std::string manufacturer
Battery manufacturer.
std::string model
Battery model name.
std::string technology
Battery technology (e.g., Li-ion, Li-poly)
std::string path
Platform-specific path (e.g., /sys/class/power_supply/BAT0)
std::string name
Human-readable battery name.
std::string id
Unique battery identifier (e.g., "BAT0")
std::chrono::system_clock::time_point timestamp
Reading timestamp.
double current_amps
Current in Amps (positive=charging)
bool ac_connected
True if AC power is connected.
double temperature_celsius
Battery temperature in Celsius.
double design_capacity_wh
Original design capacity in Wh.
battery_status status
Current charging status.
int64_t cycle_count
Battery charge cycles (-1 if unavailable)
int64_t time_to_empty_seconds
Estimated time to empty (-1 if unavailable)
double full_charge_capacity_wh
Current full charge capacity in Wh.
int64_t time_to_full_seconds
Estimated time to full (-1 if unavailable)
battery_info info
Battery information.
double level_percent
Current charge percentage (0-100)
bool battery_present
Whether battery is present.
bool is_charging
True if battery is charging.
bool temperature_available
Whether temperature is available.
double current_capacity_wh
Current energy stored in Wh.
double power_watts
Current power in Watts.
double health_percent
Battery health (full_charge/design * 100)
double voltage_volts
Current voltage in Volts.
bool metrics_available
Whether metrics are available.
Basic metric structure for interface compatibility.
Metadata describing a collector plugin.