Update examples, slight refactor and more tests
This commit is contained in:
parent
5002287a78
commit
51feaa9985
|
@ -13,18 +13,15 @@ func main() {
|
|||
sparkCore := spark.NewSparkCoreAdaptor("spark", "DEVICE_ID", "ACCESS_TOKEN")
|
||||
|
||||
work := func() {
|
||||
result, err := sparkCore.Function("brew", "hello")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
if result, err := sparkCore.Function("brew", "202,230"); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Printf("result from executing function is: %v", result)
|
||||
fmt.Println("result from \"brew\":", result)
|
||||
}
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("spark",
|
||||
[]gobot.Connection{sparkCore},
|
||||
[]gobot.Device{},
|
||||
work,
|
||||
)
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/spark"
|
||||
|
@ -13,18 +14,17 @@ func main() {
|
|||
sparkCore := spark.NewSparkCoreAdaptor("spark", "DEVICE_ID", "ACCESS_TOKEN")
|
||||
|
||||
work := func() {
|
||||
temp, err := sparkCore.Variable("temperature")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Printf("temp from variable is: %v", temp)
|
||||
}
|
||||
gobot.Every(1*time.Second, func() {
|
||||
if temp, err := sparkCore.Variable("temperature"); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("result from \"temperature\" is:", temp)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("spark",
|
||||
[]gobot.Connection{sparkCore},
|
||||
[]gobot.Device{},
|
||||
work,
|
||||
)
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||
|
@ -108,12 +109,24 @@ func (s *SparkCoreAdaptor) DigitalRead(pin string) (val int, err error) {
|
|||
return -1, err
|
||||
}
|
||||
|
||||
// Variable returns a core variable value,
|
||||
// returned value can be a float64 or a string
|
||||
func (s *SparkCoreAdaptor) Variable(name string) (val interface{}, err error) {
|
||||
// Variable returns a core variable value as a string
|
||||
func (s *SparkCoreAdaptor) Variable(name string) (result string, err error) {
|
||||
url := fmt.Sprintf("%v/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
|
||||
resp, err := s.requestToSpark("GET", url, nil)
|
||||
val = resp["result"]
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
val := resp["result"]
|
||||
switch val.(type) {
|
||||
case bool:
|
||||
result = strconv.FormatBool(val.(bool))
|
||||
case float64:
|
||||
result = strconv.FormatFloat(val.(float64), 'f', -1, 64)
|
||||
case string:
|
||||
result = val.(string)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -122,21 +135,21 @@ func (s *SparkCoreAdaptor) Variable(name string) (val interface{}, err error) {
|
|||
// returns value from request.
|
||||
// Takes a String as the only argument and returns an Int.
|
||||
// If function is not defined in core, it will time out
|
||||
func (s *SparkCoreAdaptor) Function(name string, paramString string) (val int, err error) {
|
||||
func (s *SparkCoreAdaptor) Function(name string, args string) (val int, err error) {
|
||||
params := url.Values{
|
||||
"args": {paramString},
|
||||
"args": {args},
|
||||
"access_token": {s.AccessToken},
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s", s.deviceURL(), name)
|
||||
resp, err := s.requestToSpark("POST", url, params)
|
||||
|
||||
if err == nil {
|
||||
val = int(resp["return_value"].(float64))
|
||||
return
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return -1, err
|
||||
val = int(resp["return_value"].(float64))
|
||||
return
|
||||
}
|
||||
|
||||
// setAPIServer sets spark cloud api server, this can be used to change from default api.spark.io
|
||||
|
|
|
@ -72,6 +72,7 @@ func TestNewSparkCoreAdaptor(t *testing.T) {
|
|||
}
|
||||
|
||||
gobot.Assert(t, spark.APIServer, "https://api.spark.io")
|
||||
gobot.Assert(t, spark.Name(), "bot")
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorConnect(t *testing.T) {
|
||||
|
@ -194,6 +195,30 @@ func TestSparkCoreAdaptorDigitalRead(t *testing.T) {
|
|||
gobot.Assert(t, val, -1)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorFunction(t *testing.T) {
|
||||
response := `{"return_value": 1}`
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
testServer := getDummyResponseForPath("/"+a.DeviceID+"/hello", response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
|
||||
val, _ := a.Function("hello", "100,200")
|
||||
gobot.Assert(t, val, 1)
|
||||
testServer.Close()
|
||||
|
||||
// When not existent
|
||||
response = `{"ok": false, "error": "timeout"}`
|
||||
testServer = getDummyResponseForPath("/"+a.DeviceID+"/hello", response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
|
||||
_, err := a.Function("hello", "")
|
||||
gobot.Assert(t, err.Error(), "timeout")
|
||||
|
||||
testServer.Close()
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorVariable(t *testing.T) {
|
||||
// When String
|
||||
response := `{"result": "1"}`
|
||||
|
@ -204,7 +229,7 @@ func TestSparkCoreAdaptorVariable(t *testing.T) {
|
|||
a.setAPIServer(testServer.URL)
|
||||
|
||||
val, _ := a.Variable("variable_name")
|
||||
gobot.Assert(t, val.(string), "1")
|
||||
gobot.Assert(t, val, "1")
|
||||
testServer.Close()
|
||||
|
||||
// When float
|
||||
|
@ -214,7 +239,7 @@ func TestSparkCoreAdaptorVariable(t *testing.T) {
|
|||
a.setAPIServer(testServer.URL)
|
||||
|
||||
val, _ = a.Variable("variable_name")
|
||||
gobot.Assert(t, val.(float64), 1.1)
|
||||
gobot.Assert(t, val, "1.1")
|
||||
testServer.Close()
|
||||
|
||||
// When int
|
||||
|
@ -224,7 +249,17 @@ func TestSparkCoreAdaptorVariable(t *testing.T) {
|
|||
a.setAPIServer(testServer.URL)
|
||||
|
||||
val, _ = a.Variable("variable_name")
|
||||
gobot.Assert(t, val.(float64), 1.0)
|
||||
gobot.Assert(t, val, "1")
|
||||
testServer.Close()
|
||||
|
||||
// When bool
|
||||
response = `{"result": true}`
|
||||
testServer = getDummyResponseForPath("/"+a.DeviceID+"/variable_name", response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
|
||||
val, _ = a.Variable("variable_name")
|
||||
gobot.Assert(t, val, "true")
|
||||
testServer.Close()
|
||||
|
||||
// When not existent
|
||||
|
|
Loading…
Reference in New Issue