mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
Merge branch 'master' into ginkgo
Conflicts: robot.go
This commit is contained in:
commit
9eda99b74c
28
api.go
28
api.go
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/codegangsta/martini"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type api struct{}
|
||||
@ -20,6 +21,23 @@ func Api(bot *Master) {
|
||||
return toJson(bot.FindRobot(params["robotname"]))
|
||||
})
|
||||
|
||||
m.Get("/robots/:robotname/commands", func(params martini.Params) string {
|
||||
return toJson(bot.FindRobot(params["robotname"]).RobotCommands)
|
||||
})
|
||||
|
||||
robot_command_route := "/robots/:robotname/commands/:command"
|
||||
|
||||
m.Get(robot_command_route, func(params martini.Params, res http.ResponseWriter, req *http.Request) string {
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
var body map[string]interface{}
|
||||
decoder.Decode(&body)
|
||||
if len(body) == 0 {
|
||||
body = map[string]interface{}{}
|
||||
}
|
||||
body["robotname"] = params["robotname"]
|
||||
return a.executeRobotCommand(bot, params, body)
|
||||
})
|
||||
|
||||
m.Get("/robots/:robotname/devices", func(params martini.Params) string {
|
||||
return toJson(bot.FindRobot(params["robotname"]).GetDevices())
|
||||
})
|
||||
@ -58,3 +76,13 @@ func (a *api) executeCommand(bot *Master, params martini.Params, res http.Respon
|
||||
}
|
||||
return toJson(map[string]interface{}{"results": "Unknown Command"})
|
||||
}
|
||||
|
||||
func (a *api) executeRobotCommand(bot *Master, m_params martini.Params, params ...interface{}) string {
|
||||
robot := bot.FindRobot(m_params["robotname"])
|
||||
in := make([]reflect.Value, len(params))
|
||||
for k, param := range params {
|
||||
in[k] = reflect.ValueOf(param)
|
||||
}
|
||||
ret := reflect.ValueOf(robot.Commands[m_params["command"]]).Call(in)
|
||||
return toJson(map[string]interface{}{"results": ret[0].Interface()})
|
||||
}
|
||||
|
@ -2,15 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
. "gobot"
|
||||
"time"
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
robot := Robot{
|
||||
robot := gobot.Robot{
|
||||
Work: func() {
|
||||
Every(300*time.Millisecond, func() { fmt.Println("Greetings human") })
|
||||
gobot.Every("0.5s", func() { fmt.Println("Greetings human") })
|
||||
},
|
||||
}
|
||||
|
||||
|
25
examples/hello_api.go
Normal file
25
examples/hello_api.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
func Hello(params map[string]interface{}) string {
|
||||
name := params["name"].(string)
|
||||
return fmt.Sprintf("hi %v", name)
|
||||
}
|
||||
|
||||
func main() {
|
||||
master := gobot.GobotMaster()
|
||||
gobot.Api(master)
|
||||
|
||||
hello := new(gobot.Robot)
|
||||
hello.Name = "hello"
|
||||
hello.Work = func() {}
|
||||
hello.Commands = map[string]interface{}{"Hello": Hello}
|
||||
|
||||
master.Robots = append(master.Robots, *hello)
|
||||
|
||||
master.Start()
|
||||
}
|
@ -5,9 +5,16 @@ import (
|
||||
"github.com/hybridgroup/gobot-sphero"
|
||||
)
|
||||
|
||||
var Master *gobot.Master = gobot.GobotMaster()
|
||||
|
||||
func TurnBlue(params map[string]interface{}) bool {
|
||||
sphero := Master.FindRobotDevice(params["robotname"].(string), "sphero")
|
||||
gobot.Call(sphero.Driver, "SetRGB", uint8(0), uint8(0), uint8(255))
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
master := gobot.GobotMaster()
|
||||
gobot.Api(master)
|
||||
gobot.Api(Master)
|
||||
|
||||
spheros := map[string]string{
|
||||
"Sphero-BPO": "127.0.0.1:4560",
|
||||
@ -26,13 +33,14 @@ func main() {
|
||||
sphero.SetRGB(uint8(255), uint8(0), uint8(0))
|
||||
}
|
||||
|
||||
master.Robots = append(master.Robots, gobot.Robot{
|
||||
Master.Robots = append(Master.Robots, gobot.Robot{
|
||||
Name: name,
|
||||
Connections: []interface{}{spheroAdaptor},
|
||||
Devices: []interface{}{sphero},
|
||||
Work: work,
|
||||
Commands: map[string]interface{}{"TurnBlue": TurnBlue},
|
||||
})
|
||||
}
|
||||
|
||||
master.Start()
|
||||
Master.Start()
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package gobot
|
||||
|
||||
import "time"
|
||||
|
||||
type Master struct {
|
||||
Robots []Robot
|
||||
}
|
||||
@ -15,10 +13,7 @@ func (m *Master) Start() {
|
||||
for s := range m.Robots {
|
||||
go m.Robots[s].Start()
|
||||
}
|
||||
|
||||
for {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
select {}
|
||||
}
|
||||
|
||||
func (m *Master) FindRobot(name string) *Robot {
|
||||
@ -29,6 +24,7 @@ func (m *Master) FindRobot(name string) *Robot {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Master) FindRobotDevice(name string, device string) *Device {
|
||||
for r := range m.Robots {
|
||||
if m.Robots[r].Name == name {
|
||||
@ -41,6 +37,7 @@ func (m *Master) FindRobotDevice(name string, device string) *Device {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Master) FindRobotConnection(name string, connection string) *Connection {
|
||||
for r := range m.Robots {
|
||||
if m.Robots[r].Name == name {
|
||||
|
21
robot.go
21
robot.go
@ -8,24 +8,27 @@ import (
|
||||
)
|
||||
|
||||
type Robot struct {
|
||||
Connections []interface{}
|
||||
Devices []interface{}
|
||||
Name string
|
||||
Work func() `json:"-"`
|
||||
connections []*Connection `json:"-"`
|
||||
devices []*Device `json:"-"`
|
||||
Connections []interface{}
|
||||
Devices []interface{}
|
||||
Name string
|
||||
Commands map[string]interface{} `json:"-"`
|
||||
RobotCommands []string `json:"Commands"`
|
||||
Work func() `json:"-"`
|
||||
connections []*Connection `json:"-"`
|
||||
devices []*Device `json:"-"`
|
||||
}
|
||||
|
||||
func (r *Robot) Start() {
|
||||
r.initName()
|
||||
for k, _ := range r.Commands {
|
||||
r.RobotCommands = append(r.RobotCommands, k)
|
||||
}
|
||||
r.initConnections()
|
||||
r.initDevices()
|
||||
r.startConnections()
|
||||
r.startDevices()
|
||||
r.Work()
|
||||
for {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
select {}
|
||||
}
|
||||
|
||||
func (r *Robot) initName() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user