/
batterymetrics.go
133 lines (119 loc) · 5.17 KB
/
batterymetrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"github.com/stmcginnis/gofish/common"
)
// BatteryMetrics shall be used to represent the metrics of a battery unit for a Redfish implementation.
type BatteryMetrics struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataEtag is the odata etag.
ODataEtag string `json:"@odata.etag"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// CellVoltages shall contain the cell voltages, in volt units, for this battery. The value of the DataSourceUri
// property, if present, shall reference a resource of type Sensor with the ReadingType property containing the
// value 'Voltage'.
CellVoltages []SensorVoltageExcerpt
// CellVoltages@odata.count
CellVoltagesCount int `json:"CellVoltages@odata.count"`
// ChargePercent shall contain the amount of charge available, in percent units, typically '0' to '100', in this
// battery. The value of the DataSourceUri property, if present, shall reference a resource of type Sensor with the
// ReadingType property containing the value 'Percent'.
ChargePercent SensorExcerpt
// Description provides a description of this resource.
Description string
// DischargeCycles shall contain the number of discharges this battery has sustained.
DischargeCycles float64
// InputCurrentAmps shall contain the input current, in ampere units, for this battery. The value of the
// DataSourceUri property, if present, shall reference a resource of type Sensor with the ReadingType property
// containing the value 'Current'.
InputCurrentAmps SensorCurrentExcerpt
// InputVoltage shall contain the input voltage, in volt units, for this battery. The value of the DataSourceUri
// property, if present, shall reference a resource of type Sensor with the ReadingType property containing the
// value 'Voltage'.
InputVoltage SensorVoltageExcerpt
// OutputCurrentAmps shall contain the output currents, in ampere units, for this battery. The value of the
// DataSourceUri property, if present, shall reference a resource of type Sensor with the ReadingType property
// containing the value 'Current'. The sensors shall appear in the same array order as the OutputVoltages property.
OutputCurrentAmps []SensorCurrentExcerpt
// OutputCurrentAmps@odata.count
OutputCurrentAmpsCount int `json:"OutputCurrentAmps@odata.count"`
// OutputVoltages shall contain the output voltages, in volt units, for this battery. The value of the
// DataSourceUri property, if present, shall reference a resource of type Sensor with the ReadingType property
// containing the value 'Voltage'. The sensors shall appear in the same array order as the OutputCurrentAmps
// property.
OutputVoltages []SensorVoltageExcerpt
// OutputVoltages@odata.count
OutputVoltagesCount int `json:"OutputVoltages@odata.count"`
// Status shall contain any status or health properties of the resource.
Status common.Status
// StoredChargeAmpHours shall contain the stored charge, in ampere-hour units, for this battery. The value of the
// DataSourceUri property, if present, shall reference a resource of type Sensor with the ReadingType property
// containing the value 'ChargeAh'.
StoredChargeAmpHours SensorExcerpt
// StoredEnergyWattHours shall contain the stored energy, in watt-hour units, for this battery. The value of the
// DataSourceUri property, if present, shall reference a resource of type Sensor with the ReadingType property
// containing the value 'EnergyWh'.
StoredEnergyWattHours SensorExcerpt
// TemperatureCelsius shall contain the temperature, in degree Celsius units, for this battery. The value of the
// DataSourceUri property, if present, shall reference a resource of type Sensor with the ReadingType property
// containing the value 'Temperature'.
TemperatureCelsius SensorExcerpt
}
// GetBatteryMetrics will get a BatteryMetrics instance from the service.
func GetBatteryMetrics(c common.Client, uri string) (*BatteryMetrics, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var batterymetrics BatteryMetrics
err = json.NewDecoder(resp.Body).Decode(&batterymetrics)
if err != nil {
return nil, err
}
batterymetrics.SetClient(c)
return &batterymetrics, nil
}
// ListReferencedBatteryMetricss gets the collection of BatteryMetrics from
// a provided reference.
func ListReferencedBatteryMetricss(c common.Client, link string) ([]*BatteryMetrics, error) {
var result []*BatteryMetrics
if link == "" {
return result, nil
}
type GetResult struct {
Item *BatteryMetrics
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
batterymetrics, err := GetBatteryMetrics(c, link)
ch <- GetResult{Item: batterymetrics, Link: link, Error: err}
}
go func() {
err := common.CollectList(get, c, link)
if err != nil {
collectionError.Failures[link] = err
}
close(ch)
}()
for r := range ch {
if r.Error != nil {
collectionError.Failures[r.Link] = r.Error
} else {
result = append(result, r.Item)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}