diff --git a/adaptor.go b/adaptor.go index 6a6d072a..613f0be0 100644 --- a/adaptor.go +++ b/adaptor.go @@ -11,8 +11,8 @@ type Adaptor struct { // AdaptorInterface defines behaviour expected for a Gobot Adaptor type AdaptorInterface interface { - Finalize() bool - Connect() bool + Finalize() error + Connect() error Port() string Name() string Type() string diff --git a/connection.go b/connection.go index 75d5689c..1d1193e3 100644 --- a/connection.go +++ b/connection.go @@ -2,6 +2,7 @@ package gobot import ( "errors" + "fmt" "log" ) @@ -37,8 +38,9 @@ func (c *connections) Start() error { info = info + " on port " + connection.Port() } log.Println(info + "...") - if connection.Connect() == false { - err = errors.New("Could not start connection") + err = connection.Connect() + if err != nil { + err = errors.New(fmt.Sprintf("Could not start connection: %v", err)) break } } diff --git a/device.go b/device.go index 15f925bf..fdc7698a 100644 --- a/device.go +++ b/device.go @@ -2,6 +2,7 @@ package gobot import ( "errors" + "fmt" "log" ) @@ -39,8 +40,9 @@ func (d *devices) Start() error { info = info + " on pin " + device.Pin() } log.Println(info + "...") - if device.Start() == false { - err = errors.New("Could not start device") + err = device.Start() + if err != nil { + err = errors.New(fmt.Sprintf("Could not start device: %v", err)) break } } diff --git a/driver.go b/driver.go index 36002753..53fb6219 100644 --- a/driver.go +++ b/driver.go @@ -7,8 +7,8 @@ import ( // DriverInterface defines Driver expected behaviour type DriverInterface interface { - Start() bool - Halt() bool + Start() error + Halt() error Adaptor() AdaptorInterface SetInterval(time.Duration) Interval() time.Duration diff --git a/gobot.go b/gobot.go index 7d246332..b5dd0264 100644 --- a/gobot.go +++ b/gobot.go @@ -56,8 +56,11 @@ func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} { } // Start runs the main Gobot event loop -func (g *Gobot) Start() { - g.robots.Start() +func (g *Gobot) Start() error { + err := g.robots.Start() + if err != nil { + return err + } c := make(chan os.Signal, 1) g.trap(c) @@ -69,6 +72,7 @@ func (g *Gobot) Start() { r.Devices().Halt() r.Connections().Finalize() }) + return nil } // Robots fetch all robots associated with this Gobot instance. diff --git a/robot.go b/robot.go index b1b27761..06ce0512 100644 --- a/robot.go +++ b/robot.go @@ -1,6 +1,7 @@ package gobot import ( + "errors" "fmt" "log" ) @@ -33,13 +34,17 @@ func (r *robots) Len() int { // Start initialises the event loop. All robots that were added will // be automtically started as a result of this call. -func (r *robots) Start() { +func (r *robots) Start() error { for _, robot := range *r { - robot.Start() + err := robot.Start() + if err != nil { + return err + } } + return nil } -// Each enumerates thru the robts and calls specified function +// Each enumerates thru the robots and calls specified function func (r *robots) Each(f func(*Robot)) { for _, robot := range *r { f(robot) @@ -105,18 +110,19 @@ func (r *Robot) Command(name string) func(map[string]interface{}) interface{} { // Start a robot instance and runs it's work function if any. You should not // need to manually start a robot if already part of a Gobot application as the // robot will be automatically started for you. -func (r *Robot) Start() { +func (r *Robot) Start() error { log.Println("Starting Robot", r.Name, "...") if err := r.Connections().Start(); err != nil { - panic("Could not start connections") + return errors.New(fmt.Sprintf("Could not start connections: %v", err)) } if err := r.Devices().Start(); err != nil { - panic("Could not start devices") + return errors.New(fmt.Sprintf("Could not start devices %v", err)) } if r.Work != nil { log.Println("Starting work...") r.Work() } + return nil } // Devices retrieves all devices associated with this robot. diff --git a/test_helper.go b/test_helper.go index 4624be24..93fd6ca7 100644 --- a/test_helper.go +++ b/test_helper.go @@ -62,9 +62,8 @@ type testDriver struct { Driver } -func (t *testDriver) Init() bool { return true } -func (t *testDriver) Start() bool { return true } -func (t *testDriver) Halt() bool { return true } +func (t *testDriver) Start() error { return nil } +func (t *testDriver) Halt() error { return nil } func NewTestDriver(name string, adaptor *testAdaptor) *testDriver { t := &testDriver{ @@ -94,8 +93,8 @@ type testAdaptor struct { Adaptor } -func (t *testAdaptor) Finalize() bool { return true } -func (t *testAdaptor) Connect() bool { return true } +func (t *testAdaptor) Finalize() error { return nil } +func (t *testAdaptor) Connect() error { return nil } func NewTestAdaptor(name string) *testAdaptor { return &testAdaptor{