forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.go
244 lines (218 loc) · 10.1 KB
/
battery.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// ChargeState is the current state of the batter charge.
type ChargeState string
const (
// IdleChargeState shall indicate the battery is idle and energy is not entering or leaving the battery. Small
// amounts of energy may enter or leave the battery while in this state if the battery is regulating itself.
IdleChargeState ChargeState = "Idle"
// ChargingChargeState shall indicate the battery is charging and energy is entering the battery.
ChargingChargeState ChargeState = "Charging"
// DischargingChargeState shall indicate the battery is discharging and energy is leaving the battery.
DischargingChargeState ChargeState = "Discharging"
)
// Battery shall represent a battery for a Redfish implementation. It may also represent a location, such as a
// slot, socket, or bay, where a unit may be installed if the State property within the Status property contains
// 'Absent'.
type Battery 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"`
// Assembly shall contain a link to a resource of type Assembly.
assembly string
// CapacityActualAmpHours shall contain the actual maximum capacity of this battery in amp-hour units.
CapacityActualAmpHours float64
// CapacityActualWattHours shall contain the actual maximum capacity of this battery in watt-hour units.
CapacityActualWattHours float64
// CapacityRatedAmpHours shall contain the rated maximum capacity of this battery in amp-hour units.
CapacityRatedAmpHours float64
// CapacityRatedWattHours shall contain the rated maximum capacity of this battery in watt-hour units.
CapacityRatedWattHours float64
// ChargeState shall contain the charge state of this battery.
ChargeState ChargeState
// Description provides a description of this resource.
Description string
// FirmwareVersion shall contain the firmware version as defined by the manufacturer for this battery.
FirmwareVersion string
// HotPluggable shall indicate whether the device can be inserted or removed while the underlying equipment
// otherwise remains in its current operational state. Devices indicated as hot-pluggable shall allow the device to
// become operable without altering the operational state of the underlying equipment. Devices that cannot be
// inserted or removed from equipment in operation, or devices that cannot become operable without affecting the
// operational state of that equipment, shall be indicated as not hot-pluggable.
HotPluggable bool
// Location shall contain the location information of this battery.
Location common.Location
// LocationIndicatorActive shall contain the state of the indicator used to physically identify or locate this
// resource.
LocationIndicatorActive bool
// Manufacturer shall contain the name of the organization responsible for producing the battery. This organization
// may be the entity from whom the battery is purchased, but this is not necessarily true.
Manufacturer string
// MaxChargeRateAmps shall contain the maximum charge rate of this battery in amp units.
MaxChargeRateAmps float64
// MaxChargeVoltage shall contain the maximum charge voltage of this battery.
MaxChargeVoltage float64
// MaxDischargeRateAmps shall contain the maximum discharge rate of this battery in amp units.
MaxDischargeRateAmps float64
// Metrics shall contain a link to a resource of type BatteryMetrics.
metrics string
// Model shall contain the model information as defined by the manufacturer for this battery.
Model string
// PartNumber shall contain the part number as defined by the manufacturer for this battery.
PartNumber string
// ProductionDate shall contain the date of production or manufacture for this battery.
ProductionDate string
// Replaceable shall indicate whether this component can be independently replaced as allowed by the vendor's
// replacement policy. A value of 'false' indicates the component needs to be replaced by policy as part of another
// component. If the 'LocationType' property of this component contains 'Embedded', this property shall contain
// 'false'.
Replaceable bool
// SerialNumber shall contain the serial number as defined by the manufacturer for this battery.
SerialNumber string
// SparePartNumber shall contain the spare or replacement part number as defined by the manufacturer for this
// battery.
SparePartNumber string
// StateOfHealthPercent shall contain the state of health, in percent units, of 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'.
StateOfHealthPercent SensorExcerpt
// Status shall contain any status or health properties of the resource.
Status common.Status
// Version shall contain the hardware version of this battery as determined by the vendor or supplier.
Version string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
// memory is an array of links to Memory devices.
memory []string
// MemoryCount is the number of associated Memory devices.
MemoryCount int
// storageControllers is an array of links to associated StorageControllers.
storageControllers []string
// StorageControllersCount is the number of associated StorageControllers.
StorageControllersCount int
calibrateTarget string
resetTarget string
selfTestTarget string
}
// UnmarshalJSON unmarshals a Battery object from the raw JSON.
func (battery *Battery) UnmarshalJSON(b []byte) error {
type temp Battery
type Links struct {
// Memory shall contain an array of links to resources of type Memory that represent the memory devices to which
// this battery provides power during a power-loss event, such as battery-backed NVDIMMs. This property shall not
// be present if the battery powers the containing chassis as a whole rather than individual components in a
// chassis.
Memory []string
MemoryCount int `json:"[email protected]"`
// StorageControllers shall contain an array of links to resources of type StorageController that represent the
// storage controllers to which this battery provides power during a power-loss event, such as battery-backed RAID
// controllers. This property shall not be present if the battery powers the containing chassis as a whole rather
// than individual components in a chassis.
StorageControllers []string
StorageControllersCount int `json:"[email protected]"`
}
type Actions struct {
BatteryCalibrate common.ActionTarget `json:"#Battery.Calibrate"`
BatteryReset common.ActionTarget `json:"#Battery.Reset"`
BatterySelfTest common.ActionTarget `json:"#Battery.SelfTest"`
}
var t struct {
temp
Assembly common.Link
Metrics common.Link
Links Links
Actions Actions
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*battery = Battery(t.temp)
// Extract the links to other entities for later
battery.assembly = t.Assembly.String()
battery.metrics = t.Metrics.String()
battery.memory = t.Links.Memory
battery.MemoryCount = t.Links.MemoryCount
battery.storageControllers = t.Links.StorageControllers
battery.StorageControllersCount = t.Links.StorageControllersCount
battery.calibrateTarget = t.Actions.BatteryCalibrate.Target
battery.resetTarget = t.Actions.BatteryReset.Target
battery.selfTestTarget = t.Actions.BatterySelfTest.Target
// This is a read/write object, so we need to save the raw object data for later
battery.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (battery *Battery) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(Battery)
_ = original.UnmarshalJSON(battery.rawData)
readWriteFields := []string{
"LocationIndicatorActive",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(battery).Elem()
return battery.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetBattery will get a Battery instance from the service.
func GetBattery(c common.Client, uri string) (*Battery, error) {
return common.GetObject[Battery](c, uri)
}
// ListReferencedBatterys gets the collection of Battery from
// a provided reference.
func ListReferencedBatterys(c common.Client, link string) ([]*Battery, error) {
return common.GetCollectionObjects[Battery](c, link)
}
// Assembly get the containing assembly of this battery.
func (battery *Battery) Assembly() (*Assembly, error) {
if battery.assembly == "" {
return nil, nil
}
return GetAssembly(battery.GetClient(), battery.assembly)
}
// BatteryMetrics get the metrics for this battery.
func (battery *Battery) BatteryMetrics() (*BatteryMetrics, error) {
if battery.metrics == "" {
return nil, nil
}
return GetBatteryMetrics(battery.GetClient(), battery.metrics)
}
// Memory returns a collection of Memory devices associated with this Battery.
func (battery *Battery) Memory() ([]*Memory, error) {
return common.GetObjects[Memory](battery.GetClient(), battery.memory)
}
// StorageControllers returns a collection of StorageControllers associated with this Battery.
func (battery *Battery) StorageControllers() ([]*StorageController, error) {
return common.GetObjects[StorageController](battery.GetClient(), battery.storageControllers)
}
// Calibrate performs a self-calibration, or learn cycle, of the battery.
func (battery *Battery) Calibrate() error {
payload := struct{}{}
return battery.Post(battery.calibrateTarget, payload)
}
// Reset resets the battery.
func (battery *Battery) Reset(resetType ResetType) error {
t := struct {
ResetType ResetType
}{ResetType: resetType}
return battery.Post(battery.resetTarget, t)
}
// SelfTest performs a self-test of the battery.
func (battery *Battery) SelfTest() error {
payload := struct{}{}
return battery.Post(battery.selfTestTarget, payload)
}