nv_redfish/chassis/power_supply.rs
1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
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
16use crate::schema::redfish::power_supply::PowerSupply as PowerSupplySchema;
17use crate::schema::redfish::power_supply_metrics::PowerSupplyMetrics;
18use crate::Error;
19use crate::NvBmc;
20use crate::Resource;
21use crate::ResourceSchema;
22use nv_redfish_core::Bmc;
23use nv_redfish_core::NavProperty;
24use std::sync::Arc;
25
26#[cfg(feature = "sensors")]
27use crate::extract_sensor_uris;
28#[cfg(feature = "sensors")]
29use crate::sensor::SensorRef;
30
31/// Represents a power supply in a chassis.
32///
33/// Provides access to power supply information and associated metrics/sensors.
34pub struct PowerSupply<B: Bmc> {
35 bmc: NvBmc<B>,
36 data: Arc<PowerSupplySchema>,
37}
38
39impl<B: Bmc> PowerSupply<B> {
40 /// Create a new power supply handle.
41 pub(crate) async fn new(
42 bmc: &NvBmc<B>,
43 nav: &NavProperty<PowerSupplySchema>,
44 ) -> Result<Self, Error<B>> {
45 nav.get(bmc.as_ref())
46 .await
47 .map_err(Error::Bmc)
48 .map(|data| Self {
49 bmc: bmc.clone(),
50 data,
51 })
52 }
53
54 /// Get the raw schema data for this power supply.
55 ///
56 /// Returns an `Arc` to the underlying schema, allowing cheap cloning
57 /// and sharing of the data.
58 #[must_use]
59 pub fn raw(&self) -> Arc<PowerSupplySchema> {
60 self.data.clone()
61 }
62
63 /// Get power supply metrics.
64 ///
65 /// Returns the power supply's performance and state metrics if available.
66 ///
67 /// # Errors
68 ///
69 /// Returns an error if:
70 /// - The power supply does not have metrics
71 /// - Fetching metrics data fails
72 pub async fn metrics(&self) -> Result<Arc<PowerSupplyMetrics>, Error<B>> {
73 let metrics_ref = self
74 .data
75 .metrics
76 .as_ref()
77 .ok_or(Error::MetricsNotAvailable)?;
78
79 metrics_ref.get(self.bmc.as_ref()).await.map_err(Error::Bmc)
80 }
81
82 /// Get the metrics sensors for this power supply.
83 ///
84 /// Returns a vector of `Sensor<B>` obtained from metrics metrics, if available.
85 /// # Errors
86 ///
87 /// Returns an error if get of metrics failed.
88 #[cfg(feature = "sensors")]
89 pub async fn metrics_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
90 let sensor_refs = if let Some(metrics_ref) = &self.data.metrics {
91 metrics_ref
92 .get(self.bmc.as_ref())
93 .await
94 .map_err(Error::Bmc)
95 .map(|m| {
96 extract_sensor_uris!(m,
97 single: input_voltage,
98 single: input_current_amps,
99 single: input_power_watts,
100 single: energyk_wh,
101 single: frequency_hz,
102 single: output_power_watts,
103 single: temperature_celsius,
104 single: fan_speed_percent,
105 vec: rail_voltage,
106 vec: rail_current_amps,
107 vec: rail_power_watts,
108 vec: fan_speeds_percent
109 )
110 })?
111 } else {
112 Vec::new()
113 };
114
115 Ok(sensor_refs
116 .into_iter()
117 .map(|r| SensorRef::new(self.bmc.clone(), r))
118 .collect())
119 }
120}
121
122impl<B: Bmc> Resource for PowerSupply<B> {
123 fn resource_ref(&self) -> &ResourceSchema {
124 &self.data.as_ref().base
125 }
126}