Update spark package

This commit is contained in:
Adrian Zankich 2014-04-27 11:56:23 -07:00
parent 82b8fd5224
commit 9ac54feaed
9 changed files with 162 additions and 172 deletions

View File

@ -2,22 +2,22 @@ package main
import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-gpio"
"github.com/hybridgroup/gobot-spark"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/spark"
)
func main() {
master := gobot.GobotMaster()
master := gobot.NewMaster()
gobot.Api(master)
spark := new(gobotSpark.SparkAdaptor)
spark.Name = "spark"
spark.Params = map[string]interface{}{
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
led := gobotGPIO.NewLed(spark)
led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "D7"
@ -29,7 +29,7 @@ func main() {
master.Robots = append(master.Robots, &gobot.Robot{
Name: "spark",
Connections: []gobot.Connection{spark},
Connections: []gobot.Connection{sparkCore},
Devices: []gobot.Device{led},
Work: work,
})

View File

@ -2,20 +2,19 @@ package main
import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-gpio"
"github.com/hybridgroup/gobot-spark"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/spark"
)
func main() {
spark := new(gobotSpark.SparkAdaptor)
spark.Name = "spark"
spark.Params = map[string]interface{}{
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
led := gobotGPIO.NewLed(spark)
led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "D7"
@ -26,7 +25,7 @@ func main() {
}
robot := gobot.Robot{
Connections: []gobot.Connection{spark},
Connections: []gobot.Connection{sparkCore},
Devices: []gobot.Device{led},
Work: work,
}

View File

@ -3,25 +3,24 @@ package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-gpio"
"github.com/hybridgroup/gobot-spark"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/spark"
)
func main() {
spark := new(gobotSpark.SparkAdaptor)
spark.Name = "spark"
spark.Params = map[string]interface{}{
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
button := gobotGPIO.NewButton(spark)
button := gpio.NewButton(sparkCore)
button.Name = "button"
button.Pin = "D5"
button.Interval = "2s"
led := gobotGPIO.NewLed(spark)
led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "D7"
@ -36,7 +35,7 @@ func main() {
}
robot := gobot.Robot{
Connections: []gobot.Connection{spark},
Connections: []gobot.Connection{sparkCore},
Devices: []gobot.Device{button, led},
Work: work,
}

View File

@ -2,20 +2,19 @@ package main
import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-gpio"
"github.com/hybridgroup/gobot-spark"
"github.com/hybridgroup/gobot/gpio"
"github.com/hybridgroup/gobot/spark"
)
func main() {
spark := new(gobotSpark.SparkAdaptor)
spark.Name = "spark"
spark.Params = map[string]interface{}{
sparkCore := spark.NewSparkCoreAdaptor()
sparkCore.Name = "spark"
sparkCore.Params = map[string]interface{}{
"device_id": "",
"access_token": "",
}
led := gobotGPIO.NewLed(spark)
led := gpio.NewLed(sparkCore)
led.Name = "led"
led.Pin = "A1"
@ -33,7 +32,7 @@ func main() {
}
robot := gobot.Robot{
Connections: []Connection{spark},
Connections: []Connection{sparkCore},
Devices: []Device{led},
Work: work,
}

View File

@ -1,4 +1,4 @@
package gobotSpark
package spark
import (
. "github.com/onsi/ginkgo"

View File

@ -1,109 +0,0 @@
package gobotSpark
import (
"encoding/json"
"fmt"
"github.com/hybridgroup/gobot"
"io/ioutil"
"net/http"
"net/url"
)
type SparkAdaptor struct {
gobot.Adaptor
}
func (me *SparkAdaptor) Connect() bool {
me.Connected = true
return true
}
func (me *SparkAdaptor) Reconnect() bool {
return me.Connect()
}
func (me *SparkAdaptor) Disconnect() bool {
me.Connected = false
return true
}
func (me *SparkAdaptor) Finalize() bool {
me.Disconnect()
return true
}
func (me *SparkAdaptor) AnalogRead(pin string) float64 {
params := url.Values{
"params": {pin},
"access_token": {me.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/analogread", me.deviceUrl())
resp := me.postToSpark(url, params)
if resp != nil {
return resp["return_value"].(float64)
}
return 0
}
func (me *SparkAdaptor) PwmWrite(pin string, level byte) {
me.AnalogWrite(pin, level)
}
func (me *SparkAdaptor) AnalogWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, level)},
"access_token": {me.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/analogwrite", me.deviceUrl())
me.postToSpark(url, params)
}
func (me *SparkAdaptor) DigitalWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, me.pinLevel(level))},
"access_token": {me.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/digitalwrite", me.deviceUrl())
me.postToSpark(url, params)
}
func (me *SparkAdaptor) DigitalRead(pin string) int {
params := url.Values{
"params": {pin},
"access_token": {me.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/digitalread", me.deviceUrl())
resp := me.postToSpark(url, params)
if resp != nil {
return int(resp["return_value"].(float64))
}
return -1
}
func (me *SparkAdaptor) deviceUrl() string {
return fmt.Sprintf("https://api.spark.io/v1/devices/%v", me.Params["device_id"])
}
func (me *SparkAdaptor) pinLevel(level byte) string {
if level == 1 {
return "HIGH"
} else {
return "LOW"
}
}
func (me *SparkAdaptor) postToSpark(url string, params url.Values) map[string]interface{} {
resp, err := http.PostForm(url, params)
if err != nil {
fmt.Println(me.Name, "Error writing to spark device", err)
return nil
}
m := make(map[string]interface{})
buf, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(buf, &m)
if resp.Status != "200 OK" {
fmt.Println(me.Name, "Error: ", m["error"])
return nil
}
return m
}

View File

@ -1,29 +0,0 @@
package gobotSpark
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Spark", func() {
var (
adaptor *SparkAdaptor
)
BeforeEach(func() {
adaptor = new(SparkAdaptor)
})
PIt("Must be able to Finalize", func() {
Expect(adaptor.Finalize()).To(Equal(true))
})
PIt("Must be able to Connect", func() {
Expect(adaptor.Connect()).To(Equal(true))
})
PIt("Must be able to Disconnect", func() {
Expect(adaptor.Disconnect()).To(Equal(true))
})
PIt("Must be able to Reconnect", func() {
Expect(adaptor.Reconnect()).To(Equal(true))
})
})

108
spark/spark_core_adaptor.go Normal file
View File

@ -0,0 +1,108 @@
package spark
import (
"encoding/json"
"fmt"
"github.com/hybridgroup/gobot"
"io/ioutil"
"net/http"
"net/url"
)
type SparkCoreAdaptor struct {
gobot.Adaptor
}
func NewSparkCoreAdaptor() *SparkCoreAdaptor {
return &SparkCoreAdaptor{}
}
func (s *SparkCoreAdaptor) Connect() bool {
s.Connected = true
return true
}
func (s *SparkCoreAdaptor) Finalize() bool {
s.Connected = false
return true
}
func (s *SparkCoreAdaptor) AnalogRead(pin string) float64 {
params := url.Values{
"params": {pin},
"access_token": {s.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/analogread", s.deviceUrl())
resp := s.postToSpark(url, params)
if resp != nil {
return resp["return_value"].(float64)
}
return 0
}
func (s *SparkCoreAdaptor) PwmWrite(pin string, level byte) {
s.AnalogWrite(pin, level)
}
func (s *SparkCoreAdaptor) AnalogWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, level)},
"access_token": {s.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/analogwrite", s.deviceUrl())
s.postToSpark(url, params)
}
func (s *SparkCoreAdaptor) DigitalWrite(pin string, level byte) {
params := url.Values{
"params": {fmt.Sprintf("%v,%v", pin, s.pinLevel(level))},
"access_token": {s.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/digitalwrite", s.deviceUrl())
s.postToSpark(url, params)
}
func (s *SparkCoreAdaptor) DigitalRead(pin string) int {
params := url.Values{
"params": {pin},
"access_token": {s.Params["access_token"].(string)},
}
url := fmt.Sprintf("%v/digitalread", s.deviceUrl())
resp := s.postToSpark(url, params)
if resp != nil {
return int(resp["return_value"].(float64))
}
return -1
}
func (s *SparkCoreAdaptor) deviceUrl() string {
return fmt.Sprintf("https://api.spark.io/v1/devices/%v", s.Params["device_id"])
}
func (s *SparkCoreAdaptor) pinLevel(level byte) string {
if level == 1 {
return "HIGH"
} else {
return "LOW"
}
}
func (s *SparkCoreAdaptor) postToSpark(url string, params url.Values) map[string]interface{} {
resp, err := http.PostForm(url, params)
if err != nil {
fmt.Println(s.Name, "Error writing to spark device", err)
return nil
}
m := make(map[string]interface{})
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(s.Name, "Error reading response body", err)
return nil
}
json.Unmarshal(buf, &m)
if resp.Status != "200 OK" {
fmt.Println(s.Name, "Error: ", m["error"])
return nil
}
return m
}

View File

@ -0,0 +1,23 @@
package spark
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Spark", func() {
var (
s *SparkCoreAdaptor
)
BeforeEach(func() {
s = NewSparkCoreAdaptor()
})
It("Must be able to Finalize", func() {
Expect(s.Finalize()).To(Equal(true))
})
It("Must be able to Connect", func() {
Expect(s.Connect()).To(Equal(true))
})
})