Initial GETs for api
This commit is contained in:
parent
e6ef542a98
commit
e8ee0c9d57
|
@ -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()
|
||||
}
|
|
@ -9,7 +9,7 @@ type Connection struct {
|
|||
Name string
|
||||
Adaptor interface{}
|
||||
Port string
|
||||
Robot *Robot
|
||||
Robot *Robot `json:"-"`
|
||||
Params map[string]string
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
type Device struct {
|
||||
Name string
|
||||
Interval string
|
||||
Robot *Robot
|
||||
Robot *Robot `json:"-"`
|
||||
Driver interface{}
|
||||
Params map[string]string
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
}
|
|
@ -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)))
|
||||
})
|
||||
},
|
||||
})
|
||||
|
|
12
robot.go
12
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]
|
||||
|
|
6
utils.go
6
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue