Robot is now a pointer
This commit is contained in:
parent
48b2eed23b
commit
afa6cf11f2
2
api.go
2
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)
|
||||
})
|
||||
|
|
18
master.go
18
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
|
||||
|
|
12
robot.go
12
robot.go
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue