Skip to content

Commit

Permalink
[VIO-2709] UPS emulation (#116)
Browse files Browse the repository at this point in the history
* Initial work to support UPS reading

This is the initial work to support readings from the UPS (duration).

* Change device_type to seconds

Rename the type field in a few spots so that the readings from synse
contain `device_type="seconds"` and `device="duration"`, to match the
output of the snmp plugin.

* Update comment

* Update comment
  • Loading branch information
AdamIsrael authored Dec 8, 2022
1 parent f64f6f8 commit c35abab
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/device/ups.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 3
devices:
- type: seconds
context:
model: emul8-ups
instances:
- info: Seconds on battery power
data:
id: 1
27 changes: 27 additions & 0 deletions pkg/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ var ActionLockValueEmitterSetup = sdk.DeviceAction{
},
}

// ActionUpsDurationValueEmitterSetup initializes a ValueEmitter for each "seconds" type device
var ActionUpsDurationValueEmitterSetup = sdk.DeviceAction{
Name: "UPS Duration emitter setup",
Filter: map[string][]string{
"type": {"seconds"},
},
Action: func(_ *sdk.Plugin, d *sdk.Device) error {
lowerBound, ok := d.Data["min"].(int)
if !ok {
lowerBound = 0
}

upperBound, ok := d.Data["max"].(int)
if !ok {
upperBound = 0
}

step, ok := d.Data["step"].(int)
if !ok {
step = 0
}

emitter := utils.NewValueEmitter(utils.Accumulate).WithLowerBound(lowerBound).WithUpperBound(upperBound).WithStep(step)
return utils.SetEmitter(d.GetID(), emitter)
},
}

// ActionPowerValueEmitterSetup initializes a ValueEmitter for each "power" type device.
var ActionPowerValueEmitterSetup = sdk.DeviceAction{
Name: "power value emitter setup",
Expand Down
27 changes: 27 additions & 0 deletions pkg/devices/ups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package devices

import (
"github.com/vapor-ware/synse-emulator-plugin/pkg/utils"
"github.com/vapor-ware/synse-sdk/v2/sdk"
"github.com/vapor-ware/synse-sdk/v2/sdk/output"
)

// UPS is the handler for the emulated ups device.
var UPS = sdk.DeviceHandler{
Name: "seconds",
Read: secondsRead,
Write: minMaxCurrentWrite,
}

// secondsRead is the read handler for the emulated ups device.
func secondsRead(device *sdk.Device) ([]*output.Reading, error) {
emitter := utils.GetEmitter(device.GetID())
ec, err := output.Seconds.MakeReading(emitter.Next())
if err != nil {
return nil, err
}

return []*output.Reading{
ec,
}, nil
}
2 changes: 2 additions & 0 deletions pkg/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func MakePlugin() *sdk.Plugin {
&devices.Humidity,
&devices.LED,
&devices.Lock,
&devices.UPS,
&devices.Power,
&devices.Pressure,
&devices.Temperature,
Expand All @@ -60,6 +61,7 @@ func MakePlugin() *sdk.Plugin {
&ActionHumidityValueEmitterSetup,
&ActionLEDValueEmitterSetup,
&ActionLockValueEmitterSetup,
&ActionUpsDurationValueEmitterSetup,
&ActionPowerValueEmitterSetup,
&ActionPressureValueEmitterSetup,
&ActionTemperatureValueEmitterSetup,
Expand Down

0 comments on commit c35abab

Please sign in to comment.