From e8ee0c9d57247f03eae76e79bfa34e586ca5072f Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Sat, 23 Nov 2013 10:36:08 -0800 Subject: [PATCH] Initial GETs for api --- api.go | 25 +++++++++++++++++++++++++ connection.go | 2 +- device.go | 2 +- driver.go | 2 +- examples/sphero_api.go | 38 ++++++++++++++++++++++++++++++++++++++ examples/sphero_master.go | 2 +- robot.go | 12 ++++++++---- utils.go | 6 ++++++ 8 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 api.go create mode 100644 examples/sphero_api.go diff --git a/api.go b/api.go new file mode 100644 index 00000000..b4a37421 --- /dev/null +++ b/api.go @@ -0,0 +1,25 @@ +package gobot + +import "github.com/codegangsta/martini" + +func Api(bot *Gobot) { + m := martini.Classic() + + m.Get("/robots", func() string { + return toJson(bot.Robots) + }) + + m.Get("/robots/:robotname", func(params martini.Params) string { + return toJson(bot.FindRobot(params["robotname"])) + }) + + m.Get("/robots/:robotname/devices", func(params martini.Params) string { + return toJson(bot.FindRobot(params["robotname"]).GetDevices()) + }) + + m.Get("/robots/:robotname/devices/:devicename", func(params martini.Params) string { + return toJson(bot.FindRobotDevice(params["robotname"], params["devicename"])) + }) + + go m.Run() +} diff --git a/connection.go b/connection.go index eaaad42b..8f9b3380 100644 --- a/connection.go +++ b/connection.go @@ -9,7 +9,7 @@ type Connection struct { Name string Adaptor interface{} Port string - Robot *Robot + Robot *Robot `json:"-"` Params map[string]string } diff --git a/device.go b/device.go index 6c0babbe..6b268ac5 100644 --- a/device.go +++ b/device.go @@ -8,7 +8,7 @@ import ( type Device struct { Name string Interval string - Robot *Robot + Robot *Robot `json:"-"` Driver interface{} Params map[string]string } diff --git a/driver.go b/driver.go index 4ab5039a..aca82e67 100644 --- a/driver.go +++ b/driver.go @@ -7,7 +7,7 @@ type Driver struct { Pin string Name string Params map[string]string - Events map[string]chan interface{} + Events map[string]chan interface{} `json:"-"` } func NewDriver(d Driver) Driver { diff --git a/examples/sphero_api.go b/examples/sphero_api.go new file mode 100644 index 00000000..7faaf3fd --- /dev/null +++ b/examples/sphero_api.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/hybridgroup/gobot" + "github.com/hybridgroup/gobot-sphero" +) + +func main() { + bot := gobot.NewGobot() + gobot.Api(bot) + + spheros := map[string]string{ + "Sphero-BPO": "127.0.0.1:4560", + } + + for name, port := range spheros { + spheroAdaptor := new(gobotSphero.SpheroAdaptor) + spheroAdaptor.Name = "sphero" + spheroAdaptor.Port = port + + sphero := gobotSphero.NewSphero(spheroAdaptor) + sphero.Name = "sphero" + sphero.Interval = "0.5s" + + work := func(){ + sphero.SetRGB(uint8(255), uint8(0), uint8(0)) + } + + bot.Robots = append(bot.Robots, gobot.Robot { + Name: name, + Connections: []interface{} { spheroAdaptor }, + Devices: []interface{} { sphero }, + Work: work, + }) + } + + bot.Start() +} diff --git a/examples/sphero_master.go b/examples/sphero_master.go index 160d6c95..df8882d3 100644 --- a/examples/sphero_master.go +++ b/examples/sphero_master.go @@ -37,7 +37,7 @@ func main() { Work: func(){ sphero := bot.FindRobot("Sphero-BPO") gobot.Every("1s", func () { - gobot.Call(sphero.Device("sphero").Driver, "SetRGB", uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255))) + gobot.Call(sphero.GetDevice("sphero").Driver, "SetRGB", uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255))) }) }, }) diff --git a/robot.go b/robot.go index 1650f62c..97e16674 100644 --- a/robot.go +++ b/robot.go @@ -11,9 +11,9 @@ type Robot struct { Connections []interface{} Devices []interface{} Name string - Work func() - connections []*Connection - devices []*Device + Work func() `json:"-"` + connections []*Connection `json:"-"` + devices []*Device `json:"-"` } func (r *Robot) Start() { @@ -66,7 +66,11 @@ func (r *Robot) startDevices() { } } -func (r *Robot) Device(name string) *Device { +func (r *Robot) GetDevices() []*Device { + return r.devices +} + +func (r *Robot) GetDevice(name string) *Device { for i := range r.devices { if r.devices[i].Name == name { return r.devices[i] diff --git a/utils.go b/utils.go index 6953e4d9..f251d418 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,7 @@ package gobot import ( + "encoding/json" "math/rand" "net" "reflect" @@ -61,3 +62,8 @@ func Call(thing interface{}, method string, params ...interface{}) (result []ref result = reflect.ValueOf(thing).MethodByName(method).Call(in) return } + +func toJson(obj interface{}) string { + b, _ := json.Marshal(obj) + return string(b) +}