-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add transform fns to allow generalized plugins to manipulate reading …
…values (#399)
- Loading branch information
1 parent
73e9dc0
commit ac64e26
Showing
10 changed files
with
548 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Synse SDK | ||
// Copyright (c) 2019 Vapor IO | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package funcs | ||
|
||
import "github.com/vapor-ware/synse-sdk/sdk/utils" | ||
|
||
// GetBuiltins returns all of the built-in Funcs supplied by the SDK. | ||
func GetBuiltins() []*Func { | ||
return []*Func{ | ||
&FtoC, | ||
} | ||
} | ||
|
||
// FtoC is a Func which converts a value from degrees Fahrenheit to | ||
// degrees Celsius. | ||
var FtoC = Func{ | ||
Name: "FtoC", | ||
Fn: func(value interface{}) (interface{}, error) { | ||
f, err := utils.ConvertToFloat64(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := float64((f - 32.0) * 5.0 / 9.0) | ||
return c, nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Synse SDK | ||
// Copyright (c) 2019 Vapor IO | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package funcs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetBuiltins(t *testing.T) { | ||
fns := GetBuiltins() | ||
assert.NotEmpty(t, fns) | ||
} | ||
|
||
func TestFtoC_Fn(t *testing.T) { | ||
cases := []struct { | ||
f float64 | ||
c float64 | ||
}{ | ||
{f: -459.67, c: -273.15}, | ||
{f: -50, c: -45.56}, | ||
{f: -40, c: -40.00}, | ||
{f: -30, c: -34.44}, | ||
{f: -20, c: -28.89}, | ||
{f: -10, c: -23.33}, | ||
{f: 0, c: -17.78}, | ||
{f: 10, c: -12.22}, | ||
{f: 20, c: -6.67}, | ||
{f: 30, c: -1.11}, | ||
{f: 32, c: 0}, | ||
{f: 40, c: 4.44}, | ||
{f: 50, c: 10.00}, | ||
{f: 60, c: 15.56}, | ||
{f: 70, c: 21.11}, | ||
{f: 80, c: 26.67}, | ||
{f: 90, c: 32.22}, | ||
{f: 100, c: 37.78}, | ||
{f: 110, c: 43.33}, | ||
{f: 120, c: 48.89}, | ||
{f: 130, c: 54.44}, | ||
{f: 140, c: 60.00}, | ||
{f: 150, c: 65.56}, | ||
{f: 160, c: 71.11}, | ||
{f: 170, c: 76.67}, | ||
{f: 180, c: 82.22}, | ||
{f: 190, c: 87.78}, | ||
{f: 200, c: 93.33}, | ||
{f: 212, c: 100}, | ||
{f: 300, c: 148.89}, | ||
{f: 400, c: 204.44}, | ||
{f: 500, c: 260.00}, | ||
{f: 600, c: 315.56}, | ||
{f: 700, c: 371.11}, | ||
{f: 800, c: 426.67}, | ||
{f: 900, c: 482.22}, | ||
{f: 1000, c: 537.78}, | ||
} | ||
|
||
for _, c := range cases { | ||
val, err := FtoC.Fn(c.f) | ||
assert.NoError(t, err) | ||
assert.InDelta(t, c.c, val, 0.01) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Synse SDK | ||
// Copyright (c) 2019 Vapor IO | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package funcs | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vapor-ware/synse-sdk/sdk/errors" | ||
) | ||
|
||
var registeredFuncs map[string]*Func | ||
|
||
func init() { | ||
registeredFuncs = make(map[string]*Func) | ||
for _, f := range GetBuiltins() { | ||
registeredFuncs[f.Name] = f | ||
} | ||
} | ||
|
||
// Get gets a Func by its name. If a func with the specified name | ||
// is not found, nil is returned. | ||
func Get(name string) *Func { | ||
return registeredFuncs[name] | ||
} | ||
|
||
// Register registers new funcs to the tracked funcs. | ||
func Register(funcs ...*Func) error { | ||
multiErr := errors.NewMultiError("func registration") | ||
|
||
for _, f := range funcs { | ||
if _, exists := registeredFuncs[f.Name]; exists { | ||
multiErr.Add(fmt.Errorf("conflict: Func with name '%s' already exists", f.Name)) | ||
continue | ||
} | ||
registeredFuncs[f.Name] = f | ||
} | ||
return multiErr.Err() | ||
} | ||
|
||
// Func is a function that can be applied to a device reading. | ||
type Func struct { | ||
// Name is the name of the function. This is how it is identified | ||
// and referenced. | ||
Name string | ||
|
||
// Fn is the function which will be called on the reading value. | ||
Fn func(value interface{}) (interface{}, error) | ||
} | ||
|
||
// Call calls the function defined for the Func. | ||
func (fn *Func) Call(value interface{}) (interface{}, error) { | ||
return fn.Fn(value) | ||
} |
Oops, something went wrong.