Refactor robot and master

This commit is contained in:
Adrian Zankich 2014-01-02 15:12:41 -08:00
parent 50db5504b8
commit c5c16d441e
3 changed files with 42 additions and 39 deletions

View File

@ -21,7 +21,7 @@ func (m *Master) Start() {
runtime.GOMAXPROCS(m.NumCPU) runtime.GOMAXPROCS(m.NumCPU)
for s := range m.Robots { for s := range m.Robots {
go m.Robots[s].startRobot() m.Robots[s].startRobot()
} }
c := make(chan os.Signal) c := make(chan os.Signal)
@ -36,36 +36,26 @@ func (m *Master) Start() {
} }
func (m *Master) FindRobot(name string) *Robot { func (m *Master) FindRobot(name string) *Robot {
for s := range m.Robots { for _, robot := range m.Robots {
if m.Robots[s].Name == name { if robot.Name == name {
return &m.Robots[s] return &robot
} }
} }
return nil return nil
} }
func (m *Master) FindRobotDevice(name string, device string) *device { func (m *Master) FindRobotDevice(name string, device string) *device {
for r := range m.Robots { robot := m.FindRobot(name)
if m.Robots[r].Name == name { if robot != nil {
for d := range m.Robots[r].devices { return robot.GetDevice(device)
if m.Robots[r].devices[d].Name == device {
return m.Robots[r].devices[d]
}
}
}
} }
return nil return nil
} }
func (m *Master) FindRobotConnection(name string, connection string) *connection { func (m *Master) FindRobotConnection(name string, connection string) *connection {
for r := range m.Robots { robot := m.FindRobot(name)
if m.Robots[r].Name == name { if robot != nil {
for c := range m.Robots[r].connections { return robot.GetConnection(connection)
if m.Robots[r].connections[c].Name == connection {
return m.Robots[r].connections[c]
}
}
}
} }
return nil return nil
} }

View File

@ -28,10 +28,10 @@ var _ = Describe("Master", func() {
Expect(myMaster.FindRobot("Robot 1").Name).To(Equal("Robot 1")) Expect(myMaster.FindRobot("Robot 1").Name).To(Equal("Robot 1"))
}) })
It("should Find the specific robot device", func() { It("should Find the specific robot device", func() {
Expect(myMaster.FindRobotDevice("Robot 1", "Device 1").Name).To(Equal("Device 1")) Expect(myMaster.FindRobotDevice("Robot 2", "Device 2").Name).To(Equal("Device 2"))
}) })
It("should Find the specific robot connection", func() { It("should Find the specific robot connection", func() {
Expect(myMaster.FindRobotConnection("Robot 1", "Connection 1").Name).To(Equal("Connection 1")) Expect(myMaster.FindRobotConnection("Robot 3", "Connection 1").Name).To(Equal("Connection 1"))
}) })
}) })
}) })

View File

@ -57,27 +57,27 @@ func (r *Robot) initCommands() {
func (r *Robot) initConnections() { func (r *Robot) initConnections() {
r.connections = make([]*connection, len(r.Connections)) r.connections = make([]*connection, len(r.Connections))
log.Println("Initializing connections...") log.Println("Initializing connections...")
for i := range r.Connections { for i, connection := range r.Connections {
log.Println("Initializing connection ", FieldByNamePtr(r.Connections[i], "Name"), "...") log.Println("Initializing connection ", FieldByNamePtr(connection, "Name"), "...")
r.connections[i] = NewConnection(r.Connections[i], r) r.connections[i] = NewConnection(connection, r)
} }
} }
func (r *Robot) initDevices() { func (r *Robot) initDevices() {
r.devices = make([]*device, len(r.Devices)) r.devices = make([]*device, len(r.Devices))
log.Println("Initializing devices...") log.Println("Initializing devices...")
for i := range r.Devices { for i, device := range r.Devices {
log.Println("Initializing device ", FieldByNamePtr(r.Devices[i], "Name"), "...") log.Println("Initializing device ", FieldByNamePtr(device, "Name"), "...")
r.devices[i] = NewDevice(r.Devices[i], r) r.devices[i] = NewDevice(device, r)
} }
} }
func (r *Robot) startConnections() bool { func (r *Robot) startConnections() bool {
log.Println("Starting connections...") log.Println("Starting connections...")
success := true success := true
for i := range r.connections { for _, connection := range r.connections {
log.Println("Starting connection " + r.connections[i].Name + "...") log.Println("Starting connection " + connection.Name + "...")
if r.connections[i].Connect() == false { if connection.Connect() == false {
success = false success = false
break break
} }
@ -88,9 +88,9 @@ func (r *Robot) startConnections() bool {
func (r *Robot) startDevices() bool { func (r *Robot) startDevices() bool {
log.Println("Starting devices...") log.Println("Starting devices...")
success := true success := true
for i := range r.devices { for _, device := range r.devices {
log.Println("Starting device " + r.devices[i].Name + "...") log.Println("Starting device " + device.Name + "...")
if r.devices[i].Start() == false { if device.Start() == false {
success = false success = false
break break
} }
@ -99,8 +99,8 @@ func (r *Robot) startDevices() bool {
} }
func (r *Robot) finalizeConnections() { func (r *Robot) finalizeConnections() {
for i := range r.connections { for _, connection := range r.connections {
r.connections[i].Finalize() connection.Finalize()
} }
} }
@ -109,9 +109,22 @@ func (r *Robot) GetDevices() []*device {
} }
func (r *Robot) GetDevice(name string) *device { func (r *Robot) GetDevice(name string) *device {
for i := range r.devices { for _, device := range r.devices {
if r.devices[i].Name == name { if device.Name == name {
return r.devices[i] return device
}
}
return nil
}
func (r *Robot) GetConnections() []*connection {
return r.connections
}
func (r *Robot) GetConnection(name string) *connection {
for _, connection := range r.connections {
if connection.Name == name {
return connection
} }
} }
return nil return nil