Skip to content

Commit

Permalink
Adding Variable method to spark code 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 944b878 commit c679128
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
34 changes: 34 additions & 0 deletions examples/spark_core_variable.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", "53ff72065067544846101187", "f7e2983869e725addd416270cb055ba04a33fdad")

work := func() {
temp, err := sparkCore.Variable("temperature")

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

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

gbot.AddRobot(robot)

gbot.Start()
}
31 changes: 24 additions & 7 deletions platforms/spark/spark_core_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *SparkCoreAdaptor) AnalogRead(pin string) (val int, err error) {

url := fmt.Sprintf("%v/analogread", s.deviceURL())

resp, err := s.postToSpark(url, params)
resp, err := s.requestToSpark("POST", url, params)
if err == nil {
val = int(resp["return_value"].(float64))
return
Expand All @@ -78,7 +78,7 @@ func (s *SparkCoreAdaptor) AnalogWrite(pin string, level byte) (err error) {
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/analogwrite", s.deviceURL())
_, err = s.postToSpark(url, params)
_, err = s.requestToSpark("POST", url, params)
return
}

Expand All @@ -89,7 +89,7 @@ func (s *SparkCoreAdaptor) DigitalWrite(pin string, level byte) (err error) {
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/digitalwrite", s.deviceURL())
_, err = s.postToSpark(url, params)
_, err = s.requestToSpark("POST", url, params)
return err
}

Expand All @@ -100,14 +100,24 @@ func (s *SparkCoreAdaptor) DigitalRead(pin string) (val int, err error) {
"access_token": {s.AccessToken},
}
url := fmt.Sprintf("%v/digitalread", s.deviceURL())
resp, err := s.postToSpark(url, params)
resp, err := s.requestToSpark("POST", url, params)
if err == nil {
val = int(resp["return_value"].(float64))
return
}
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) {
url := fmt.Sprintf("%v/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
resp, err := s.requestToSpark("GET", url, nil)
val = resp["result"]

return
}

// 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 All @@ -129,10 +139,17 @@ func (s *SparkCoreAdaptor) pinLevel(level byte) string {
return "LOW"
}

// postToSpark makes POST request to spark cloud server, return err != nil if there is
// requestToSpark makes request to spark cloud server, return err != nil if there is
// any issue with the request.
func (s *SparkCoreAdaptor) postToSpark(url string, params url.Values) (m map[string]interface{}, err error) {
resp, err := http.PostForm(url, params)
func (s *SparkCoreAdaptor) requestToSpark(method string, url string, params url.Values) (m map[string]interface{}, err error) {
var resp *http.Response

if method == "POST" {
resp, err = http.PostForm(url, params)
} else if method == "GET" {
resp, err = http.Get(url)
}

if err != nil {
return
}
Expand Down
47 changes: 45 additions & 2 deletions platforms/spark/spark_core_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func TestSparkCoreAdaptorAnalogRead(t *testing.T) {

val, _ = a.AnalogRead("A1")
gobot.Assert(t, val, 0)

}

func TestSparkCoreAdaptorPwmWrite(t *testing.T) {
Expand Down Expand Up @@ -195,8 +194,52 @@ func TestSparkCoreAdaptorDigitalRead(t *testing.T) {
gobot.Assert(t, val, -1)
}

func TestSparkCoreAdaptorSetAPIServer(t *testing.T) {
func TestSparkCoreAdaptorVariable(t *testing.T) {
// When String
response := `{"result": "1"}`

a := initTestSparkCoreAdaptor()
testServer := getDummyResponseForPath("/"+a.DeviceID+"/variable_name", response, t)

a.setAPIServer(testServer.URL)

val, _ := a.Variable("variable_name")
gobot.Assert(t, val.(string), "1")
testServer.Close()

// When float
response = `{"result": 1.1}`
testServer = getDummyResponseForPath("/"+a.DeviceID+"/variable_name", response, t)

a.setAPIServer(testServer.URL)

val, _ = a.Variable("variable_name")
gobot.Assert(t, val.(float64), 1.1)
testServer.Close()

// When int
response = `{"result": 1}`
testServer = getDummyResponseForPath("/"+a.DeviceID+"/variable_name", response, t)

a.setAPIServer(testServer.URL)

val, _ = a.Variable("variable_name")
gobot.Assert(t, val.(float64), 1.0)
testServer.Close()

// When not existent
response = `{"ok": false, "error": "Variable not found"}`
testServer = getDummyResponseForPath("/"+a.DeviceID+"/not_existent", response, t)

a.setAPIServer(testServer.URL)

_, err := a.Variable("not_existent")
gobot.Assert(t, err.Error(), "Variable not found")

testServer.Close()
}

func TestSparkCoreAdaptorSetAPIServer(t *testing.T) {
a := initTestSparkCoreAdaptor()
apiServer := "new_api_server"
gobot.Refute(t, a.APIServer, apiServer)
Expand Down

0 comments on commit c679128

Please sign in to comment.