From c5c16d441e1d4277af4838c97e64c4bd027766cd Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Thu, 2 Jan 2014 15:12:41 -0800 Subject: [PATCH] Refactor robot and master --- master.go | 30 ++++++++++-------------------- master_test.go | 4 ++-- robot.go | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/master.go b/master.go index f1c6ecdf..2d8eb5fe 100644 --- a/master.go +++ b/master.go @@ -21,7 +21,7 @@ func (m *Master) Start() { runtime.GOMAXPROCS(m.NumCPU) for s := range m.Robots { - go m.Robots[s].startRobot() + m.Robots[s].startRobot() } c := make(chan os.Signal) @@ -36,36 +36,26 @@ func (m *Master) Start() { } func (m *Master) FindRobot(name string) *Robot { - for s := range m.Robots { - if m.Robots[s].Name == name { - return &m.Robots[s] + for _, robot := range m.Robots { + if robot.Name == name { + return &robot } } return nil } func (m *Master) FindRobotDevice(name string, device string) *device { - for r := range m.Robots { - if m.Robots[r].Name == name { - for d := range m.Robots[r].devices { - if m.Robots[r].devices[d].Name == device { - return m.Robots[r].devices[d] - } - } - } + robot := m.FindRobot(name) + if robot != nil { + return robot.GetDevice(device) } return nil } func (m *Master) FindRobotConnection(name string, connection string) *connection { - for r := range m.Robots { - if m.Robots[r].Name == name { - for c := range m.Robots[r].connections { - if m.Robots[r].connections[c].Name == connection { - return m.Robots[r].connections[c] - } - } - } + robot := m.FindRobot(name) + if robot != nil { + return robot.GetConnection(connection) } return nil } diff --git a/master_test.go b/master_test.go index 914889dc..2be92452 100644 --- a/master_test.go +++ b/master_test.go @@ -28,10 +28,10 @@ var _ = Describe("Master", func() { Expect(myMaster.FindRobot("Robot 1").Name).To(Equal("Robot 1")) }) 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() { - Expect(myMaster.FindRobotConnection("Robot 1", "Connection 1").Name).To(Equal("Connection 1")) + Expect(myMaster.FindRobotConnection("Robot 3", "Connection 1").Name).To(Equal("Connection 1")) }) }) }) diff --git a/robot.go b/robot.go index c7d53a41..1237e12c 100644 --- a/robot.go +++ b/robot.go @@ -57,27 +57,27 @@ func (r *Robot) initCommands() { func (r *Robot) initConnections() { r.connections = make([]*connection, len(r.Connections)) log.Println("Initializing connections...") - for i := range r.Connections { - log.Println("Initializing connection ", FieldByNamePtr(r.Connections[i], "Name"), "...") - r.connections[i] = NewConnection(r.Connections[i], r) + for i, connection := range r.Connections { + log.Println("Initializing connection ", FieldByNamePtr(connection, "Name"), "...") + r.connections[i] = NewConnection(connection, r) } } func (r *Robot) initDevices() { r.devices = make([]*device, len(r.Devices)) log.Println("Initializing devices...") - for i := range r.Devices { - log.Println("Initializing device ", FieldByNamePtr(r.Devices[i], "Name"), "...") - r.devices[i] = NewDevice(r.Devices[i], r) + for i, device := range r.Devices { + log.Println("Initializing device ", FieldByNamePtr(device, "Name"), "...") + r.devices[i] = NewDevice(device, r) } } func (r *Robot) startConnections() bool { log.Println("Starting connections...") success := true - for i := range r.connections { - log.Println("Starting connection " + r.connections[i].Name + "...") - if r.connections[i].Connect() == false { + for _, connection := range r.connections { + log.Println("Starting connection " + connection.Name + "...") + if connection.Connect() == false { success = false break } @@ -88,9 +88,9 @@ func (r *Robot) startConnections() bool { func (r *Robot) startDevices() bool { log.Println("Starting devices...") success := true - for i := range r.devices { - log.Println("Starting device " + r.devices[i].Name + "...") - if r.devices[i].Start() == false { + for _, device := range r.devices { + log.Println("Starting device " + device.Name + "...") + if device.Start() == false { success = false break } @@ -99,8 +99,8 @@ func (r *Robot) startDevices() bool { } func (r *Robot) finalizeConnections() { - for i := range r.connections { - r.connections[i].Finalize() + for _, connection := range r.connections { + connection.Finalize() } } @@ -109,9 +109,22 @@ func (r *Robot) GetDevices() []*device { } func (r *Robot) GetDevice(name string) *device { - for i := range r.devices { - if r.devices[i].Name == name { - return r.devices[i] + for _, device := range r.devices { + if device.Name == name { + 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