Skip to main content

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<Option<Arc<PowerSupplyMetrics>>, Error<B>> {
73        if let Some(metrics_ref) = &self.data.metrics {
74            metrics_ref
75                .get(self.bmc.as_ref())
76                .await
77                .map_err(Error::Bmc)
78                .map(Some)
79        } else {
80            Ok(None)
81        }
82    }
83
84    /// Get the metrics sensors for this power supply.
85    ///
86    /// Returns a vector of `Sensor<B>` obtained from metrics metrics, if available.
87    /// # Errors
88    ///
89    /// Returns an error if get of metrics failed.
90    #[cfg(feature = "sensors")]
91    pub async fn metrics_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
92        let sensor_refs = if let Some(metrics_ref) = &self.data.metrics {
93            metrics_ref
94                .get(self.bmc.as_ref())
95                .await
96                .map_err(Error::Bmc)
97                .map(|m| {
98                    extract_sensor_uris!(m,
99                        single: input_voltage,
100                        single: input_current_amps,
101                        single: input_power_watts,
102                        single: energyk_wh,
103                        single: frequency_hz,
104                        single: output_power_watts,
105                        single: temperature_celsius,
106                        single: fan_speed_percent,
107                        vec: rail_voltage,
108                        vec: rail_current_amps,
109                        vec: rail_power_watts,
110                        vec: fan_speeds_percent
111                    )
112                })?
113        } else {
114            Vec::new()
115        };
116
117        Ok(sensor_refs
118            .into_iter()
119            .map(|r| SensorRef::new(self.bmc.clone(), r))
120            .collect())
121    }
122}
123
124impl<B: Bmc> Resource for PowerSupply<B> {
125    fn resource_ref(&self) -> &ResourceSchema {
126        &self.data.as_ref().base
127    }
128}