Robot is now a pointer

This commit is contained in:
Adrian Zankich 2014-04-14 23:15:54 -07:00
parent 48b2eed23b
commit afa6cf11f2
3 changed files with 14 additions and 18 deletions

2
api.go
View File

@ -38,7 +38,7 @@ func Api(bot *Master) {
m.Get("/robots", func() string {
jsonRobots := make([]*jsonRobot, 0)
for _, robot := range bot.Robots {
jsonRobots = append(jsonRobots, a.formatJsonRobot(&robot))
jsonRobots = append(jsonRobots, a.formatJsonRobot(robot))
}
return toJson(jsonRobots)
})

View File

@ -7,7 +7,7 @@ import (
)
type Master struct {
Robots []Robot
Robots []*Robot
NumCPU int
}
@ -17,15 +17,19 @@ func GobotMaster() *Master {
return m
}
var startRobots = func(m *Master) {
var trap = func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
}
func (m *Master) Start() {
runtime.GOMAXPROCS(m.NumCPU)
for s := range m.Robots {
m.Robots[s].startRobot()
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
var c = make(chan os.Signal, 1)
trap(c)
for _ = range c {
for r := range m.Robots {
@ -36,14 +40,10 @@ var startRobots = func(m *Master) {
}
}
func (m *Master) Start() {
startRobots(m)
}
func (m *Master) FindRobot(name string) *Robot {
for _, robot := range m.Robots {
if robot.Name == name {
return &robot
return robot
}
}
return nil

View File

@ -18,14 +18,10 @@ type Robot struct {
devices []*device `json:"-"`
}
var start = func(r *Robot) {
m := GobotMaster()
m.Robots = []Robot{*r}
m.Start()
}
func (r *Robot) Start() {
start(r)
m := GobotMaster()
m.Robots = []*Robot{r}
m.Start()
}
func (r *Robot) startRobot() {
@ -50,7 +46,7 @@ func (r *Robot) initName() {
if r.Name == "" {
rand.Seed(time.Now().UTC().UnixNano())
i := rand.Int()
r.Name = fmt.Sprintf("Robot %v", i)
r.Name = fmt.Sprintf("Robot%v", i)
}
}