Skip to content

Commit

Permalink
Initial GETs for api
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 23, 2013
1 parent e6ef542 commit e8ee0c9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 8 deletions.
25 changes: 25 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Connection struct {
Name string
Adaptor interface{}
Port string
Robot *Robot
Robot *Robot `json:"-"`
Params map[string]string
}

Expand Down
2 changes: 1 addition & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Device struct {
Name string
Interval string
Robot *Robot
Robot *Robot `json:"-"`
Driver interface{}
Params map[string]string
}
Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions examples/sphero_api.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion examples/sphero_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
},
})
Expand Down
12 changes: 8 additions & 4 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gobot

import (
"encoding/json"
"math/rand"
"net"
"reflect"
Expand Down Expand Up @@ -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)
}

0 comments on commit e8ee0c9

Please sign in to comment.