Adding EventStream method to spark adaptor
This commit is contained in:
parent
5dd554dbc6
commit
70e89aec05
|
@ -0,0 +1,36 @@
|
||||||
|
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() {
|
||||||
|
stream, err := sparkCore.EventStream("all", "")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
} else {
|
||||||
|
for {
|
||||||
|
ev := <-stream.Events
|
||||||
|
fmt.Println(ev.Event(), ev.Data())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
robot := gobot.NewRobot("spark",
|
||||||
|
[]gobot.Connection{sparkCore},
|
||||||
|
[]gobot.Device{},
|
||||||
|
work,
|
||||||
|
)
|
||||||
|
|
||||||
|
gbot.AddRobot(robot)
|
||||||
|
|
||||||
|
gbot.Start()
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/donovanhide/eventsource"
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||||
)
|
)
|
||||||
|
@ -108,6 +109,44 @@ func (s *SparkCoreAdaptor) DigitalRead(pin string) (val int, err error) {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EventStream returns an event stream based on the following params:
|
||||||
|
//
|
||||||
|
// * source - "all"/"devices"/"device" (More info at: http://docs.spark.io/api/#reading-data-from-a-core-events)
|
||||||
|
// * name - Event name to subscribe for, leave blank to subscribe to all events.
|
||||||
|
//
|
||||||
|
// A stream returned contains an Event chan that can be used to process received
|
||||||
|
// information. Each event has Id(), Data() and Event() methods.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// stream, err := sparkCore.EventStream("all", "")
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err.Error())
|
||||||
|
// } else {
|
||||||
|
// for {
|
||||||
|
// ev := <-stream.Events
|
||||||
|
// fmt.Println(ev.Event(), ev.Data())
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
func (s *SparkCoreAdaptor) EventStream(source string, name string) (stream *eventsource.Stream, err error) {
|
||||||
|
var url string
|
||||||
|
|
||||||
|
switch source {
|
||||||
|
case "all":
|
||||||
|
url = fmt.Sprintf("%s/v1/events/%s?access_token=%s", s.APIServer, name, s.AccessToken)
|
||||||
|
case "devices":
|
||||||
|
url = fmt.Sprintf("%s/v1/devices/events/%s?access_token=%s", s.APIServer, name, s.AccessToken)
|
||||||
|
case "device":
|
||||||
|
url = fmt.Sprintf("%s/events/%s?access_token=%s", s.deviceURL(), name, s.AccessToken)
|
||||||
|
default:
|
||||||
|
err = errors.New("source param should be: all, devices or device")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stream, err = eventsource.Subscribe(url, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// setAPIServer sets spark cloud api server, this can be used to change from default api.spark.io
|
// setAPIServer sets spark cloud api server, this can be used to change from default api.spark.io
|
||||||
func (s *SparkCoreAdaptor) setAPIServer(server string) {
|
func (s *SparkCoreAdaptor) setAPIServer(server string) {
|
||||||
s.APIServer = server
|
s.APIServer = server
|
||||||
|
|
Loading…
Reference in New Issue