Skip to content

Commit

Permalink
update simple example plugin for v3 (#352)
Browse files Browse the repository at this point in the history
* update simple example to use new sdk

* update device configs
  • Loading branch information
edaniszewski committed Mar 30, 2020
1 parent 6e758cc commit 7823e36
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 69 deletions.
29 changes: 12 additions & 17 deletions examples/simple_plugin/config/device/led.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
version: 3
locations:
- name: unknown
rack:
name: unknown
board:
name: unknown
devices:
- name: example.led
- type: led
handler: state
metadata:
author: vaporio
outputs:
- type: simple.led
model: sample-led
tags:
- foobar/xyz:led
instances:
- info: Chamber LED 1
location: unknown
- info: LED 1
tags:
- zone:1
data:
id: 5
- info: Chamber LED 2
location: unknown
data:
id: 6
id: 1
- info: LED 2
tags:
- zone:2
30 changes: 14 additions & 16 deletions examples/simple_plugin/config/device/test.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
version: 3
locations:
- name: unknown
rack:
name: unknown
board:
name: unknown
devices:
- name: example.temperature
- type: temperature
handler: temperature
metadata:
author: vaporio
outputs:
- type: simple.temperature
- model: sample-temperature
tags:
- foobar/xyz:temp
instances:
- info: CEC Temperature 1
location: unknown
- info: Temperature 1
tags:
- zone:1
data:
id: 1
- info: CEC Temperature 2
location: unknown
- info: Temperature 2
tags:
- zone:2
data:
id: 2
- info: CEC Temperature 3
location: unknown
- info: Temperature 3
tags:
- zone:3
data:
id: 3
85 changes: 49 additions & 36 deletions examples/simple_plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

"github.com/vapor-ware/synse-sdk/sdk"
"github.com/vapor-ware/synse-sdk/sdk/output"
)

// The Plugin metadata. At a minimum, all plugins need a name. This information
Expand All @@ -23,18 +24,26 @@ var (
// A single device could support multiple outputs, but at a minimum requires one.
var (
// The output for temperature devices.
temperatureOutput = sdk.OutputType{
Name: "simple.temperature",
temperatureOutput = output.Output{
Name: "temperature",
Precision: 2,
Unit: sdk.Unit{
Name: "celsius",
Symbol: "C",
Type: "temperature",
Units: map[output.SystemOfMeasure]*output.Unit{
// fixme: use built-in once it exists
output.NONE: {Name: "Fahrenheit", Symbol: "F", System: string(output.NONE)},
},
Converters: map[output.SystemOfMeasure]func(value interface{}, to output.SystemOfMeasure) (interface{}, error){
// Define a converter that just returns the same value.
// fixme: once we have built-in outputs, we can just use that here instead.
output.NONE: func(value interface{}, to output.SystemOfMeasure) (i interface{}, e error) {
return value, nil
},
},
}

// The output for LED devices.
ledOutput = sdk.OutputType{
Name: "simple.led",
// The output for on/off state devices.
stateOutput = output.Output{
Name: "state",
}
)

Expand All @@ -45,17 +54,15 @@ var (
ledHandler = sdk.DeviceHandler{
Name: "example.led",

Read: func(device *sdk.Device) ([]*sdk.Reading, error) {
reading, err := device.GetOutput("simple.led").MakeReading(strconv.Itoa(rand.Int())) // nolint: gas, gosec
if err != nil {
return nil, err
}
return []*sdk.Reading{
Read: func(device *sdk.Device) ([]*output.Reading, error) {
reading := stateOutput.From(strconv.Itoa(rand.Int()))

return []*output.Reading{
reading,
}, nil
},
Write: func(device *sdk.Device, data *sdk.WriteData) error {
fmt.Printf("[led handler]: WRITE (%v)\n", device.ID())
fmt.Printf("[led handler]: WRITE (%v)\n", device.GetID())
fmt.Printf("Data -> %v\n", data.Data)
fmt.Printf("Action -> %v\n", data.Action)
return nil
Expand All @@ -66,15 +73,15 @@ var (
temperatureHandler = sdk.DeviceHandler{
Name: "example.temperature",

Read: func(device *sdk.Device) ([]*sdk.Reading, error) {
reading, err := device.GetOutput("simple.temperature").MakeReading(strconv.Itoa(rand.Int())) // nolint: gas, gosec
if err != nil {
return nil, err
}
return []*sdk.Reading{reading}, nil
Read: func(device *sdk.Device) ([]*output.Reading, error) {
reading := temperatureOutput.From(strconv.Itoa(rand.Int())) // nolint: gas, gosec

return []*output.Reading{
reading,
}, nil
},
Write: func(device *sdk.Device, data *sdk.WriteData) error {
fmt.Printf("[temperature handler]: WRITE (%v)\n", device.ID())
fmt.Printf("[temperature handler]: WRITE (%v)\n", device.GetID())
fmt.Printf("Data -> %v\n", data.Data)
fmt.Printf("Action -> %v\n", data.Action)
return nil
Expand All @@ -83,31 +90,37 @@ var (
)

func main() {
// Set the metadata for the plugin.
sdk.SetPluginMeta(
pluginName,
pluginMaintainer,
pluginDesc,
"",
)

// Create a new Plugin instance.
plugin := sdk.NewPlugin()
plugin, err := sdk.NewPlugin()
if err != nil {
log.Fatal(err)
}

// Set plugin metadata.
plugin.SetInfo(&sdk.PluginMetadata{
Name: pluginName,
Maintainer: pluginMaintainer,
Description: pluginDesc,
})

// Register our output types with the Plugin.
err := plugin.RegisterOutputTypes(
// Register custom outputs
// fixme: won't need to do this once there are built-ins
err = plugin.RegisterOutputs(
&temperatureOutput,
&ledOutput,
&stateOutput,
)
if err != nil {
log.Fatal(err)
}

// Register our device handlers with the Plugin.
plugin.RegisterDeviceHandlers(
// Register device handlers for the plugin.
err = plugin.RegisterDeviceHandlers(
&temperatureHandler,
&ledHandler,
)
if err != nil {
log.Fatal(err)
}

// Run the plugin.
if err := plugin.Run(); err != nil {
Expand Down

0 comments on commit 7823e36

Please sign in to comment.