Skip to content

Commit

Permalink
Update examples, slight refactor and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 28, 2014
1 parent 5002287 commit 51feaa9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
9 changes: 3 additions & 6 deletions examples/spark_core_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
16 changes: 8 additions & 8 deletions examples/spark_core_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"time"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/spark"
Expand All @@ -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,
)

Expand Down
33 changes: 23 additions & 10 deletions platforms/spark/spark_core_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
41 changes: 38 additions & 3 deletions platforms/spark/spark_core_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"}`
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 51feaa9

Please sign in to comment.