diff --git a/api.go b/api.go index d3590a7cc..6ba42321e 100644 --- a/api.go +++ b/api.go @@ -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) }) diff --git a/master.go b/master.go index 2c3f54b94..8f9cc395f 100644 --- a/master.go +++ b/master.go @@ -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 diff --git a/robot.go b/robot.go index 1523d53b4..da626744d 100644 --- a/robot.go +++ b/robot.go @@ -18,16 +18,12 @@ type Robot struct { devices []*device `json:"-"` } -var start = func(r *Robot) { +func (r *Robot) Start() { m := GobotMaster() - m.Robots = []Robot{*r} + m.Robots = []*Robot{r} m.Start() } -func (r *Robot) Start() { - start(r) -} - func (r *Robot) startRobot() { r.initName() r.initCommands() @@ -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) } }