Skip to content

Commit

Permalink
Adding Function methods to spark core adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Cervantes authored and zankich committed Dec 28, 2014
1 parent c679128 commit 5002287
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
34 changes: 34 additions & 0 deletions examples/spark_core_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/spark"
)

func main() {
gbot := gobot.NewGobot()

sparkCore := spark.NewSparkCoreAdaptor("spark", "DEVICE_ID", "ACCESS_TOKEN")

work := func() {
result, err := sparkCore.Function("brew", "hello")

if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("result from executing function is: %v", result)
}
}

robot := gobot.NewRobot("spark",
[]gobot.Connection{sparkCore},
[]gobot.Device{},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
2 changes: 1 addition & 1 deletion examples/spark_core_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func main() {
gbot := gobot.NewGobot()

sparkCore := spark.NewSparkCoreAdaptor("spark", "53ff72065067544846101187", "f7e2983869e725addd416270cb055ba04a33fdad")
sparkCore := spark.NewSparkCoreAdaptor("spark", "DEVICE_ID", "ACCESS_TOKEN")

work := func() {
temp, err := sparkCore.Variable("temperature")
Expand Down
30 changes: 24 additions & 6 deletions platforms/spark/spark_core_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ func (s *SparkCoreAdaptor) Variable(name string) (val interface{}, err error) {
return
}

// Function executes a core function and
// 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) {
params := url.Values{
"args": {paramString},
"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
}

return -1, err
}

// setAPIServer sets spark cloud api server, this can be used to change from default api.spark.io
func (s *SparkCoreAdaptor) setAPIServer(server string) {
s.APIServer = server
Expand Down Expand Up @@ -163,12 +184,9 @@ func (s *SparkCoreAdaptor) requestToSpark(method string, url string, params url.
json.Unmarshal(buf, &m)

if resp.Status != "200 OK" {
if _, ok := m["error"]; ok {
err = errors.New(m["error"].(string))
} else {
err = errors.New(fmt.Sprintf("&v: error communicating to the spark cloud", resp.Status))
}
return
err = errors.New(fmt.Sprintf("&v: error communicating to the spark cloud", resp.Status))
} else if _, ok := m["error"]; ok {
err = errors.New(m["error"].(string))
}

return
Expand Down
8 changes: 4 additions & 4 deletions platforms/spark/spark_core_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ func TestSparkCoreAdaptorPostToSpark(t *testing.T) {
// When error on request
vals := url.Values{}
vals.Add("error", "error")
resp, err := a.postToSpark("http://invalid%20host.com", vals)
resp, err := a.requestToSpark("POST", "http://invalid%20host.com", vals)
if err == nil {
t.Errorf("postToSpark() should return an error when request was unsuccessful but returned", resp)
t.Errorf("requestToSpark() should return an error when request was unsuccessful but returned", resp)
}

// When error reading body
Expand All @@ -291,9 +291,9 @@ func TestSparkCoreAdaptorPostToSpark(t *testing.T) {
})
defer testServer.Close()

resp, err = a.postToSpark(testServer.URL+"/existent", vals)
resp, err = a.requestToSpark("POST", testServer.URL+"/existent", vals)
if err == nil {
t.Errorf("postToSpark() should return an error when status is not 200 but returned", resp)
t.Errorf("requestToSpark() should return an error when status is not 200 but returned", resp)
}

}

0 comments on commit 5002287

Please sign in to comment.