From bc014f7c4612fa175215812585e7f216f66f4e08 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Mon, 31 Mar 2014 15:26:35 -0700 Subject: [PATCH] Add Init function to DriverInterface --- device.go | 6 ++++++ driver.go | 1 + robot.go | 16 +++++++++++++--- test_helper.go | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/device.go b/device.go index 18d4494e..46ca8807 100644 --- a/device.go +++ b/device.go @@ -12,6 +12,7 @@ type device struct { } type Device interface { + Init() bool Start() bool Halt() bool } @@ -27,6 +28,11 @@ func NewDevice(driver DriverInterface, r *Robot) *device { return d } +func (d *device) Init() bool { + log.Println("Device " + d.Name + " initialized") + return d.Driver.Init() +} + func (d *device) Start() bool { log.Println("Device " + d.Name + " started") return d.Driver.Start() diff --git a/driver.go b/driver.go index d007312e..19e8d672 100644 --- a/driver.go +++ b/driver.go @@ -9,6 +9,7 @@ type Driver struct { } type DriverInterface interface { + Init() bool Start() bool Halt() bool } diff --git a/robot.go b/robot.go index aec1da11..c8ab34fb 100644 --- a/robot.go +++ b/robot.go @@ -28,10 +28,12 @@ func (r *Robot) startRobot() { r.initName() r.initCommands() r.initConnections() - r.initDevices() if r.startConnections() != true { panic("Could not start connections") } + if r.initDevices() != true { + panic("Could not initialize devices") + } if r.startDevices() != true { panic("Could not start devices") } @@ -63,13 +65,21 @@ func (r *Robot) initConnections() { } } -func (r *Robot) initDevices() { +func (r *Robot) initDevices() bool { r.devices = make([]*device, len(r.Devices)) log.Println("Initializing devices...") for i, device := range r.Devices { - log.Println("Initializing device ", FieldByNamePtr(device, "Name"), "...") r.devices[i] = NewDevice(device, r) } + success := true + for _, device := range r.devices { + log.Println("Initializing device " + device.Name + "...") + if device.Init() == false { + success = false + break + } + } + return success } func (r *Robot) startConnections() bool { diff --git a/test_helper.go b/test_helper.go index 682dd1c3..d73ed122 100644 --- a/test_helper.go +++ b/test_helper.go @@ -4,6 +4,7 @@ type testDriver struct { Driver } +func (me *testDriver) Init() bool { return true } func (me *testDriver) Start() bool { return true } func (me *testDriver) Halt() bool { return true }