1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00

Pass error instead of panic

This commit is contained in:
Adrian Zankich 2014-11-12 11:21:50 -08:00
parent c564e99d2d
commit 17cbf2c5f3
7 changed files with 34 additions and 21 deletions

View File

@ -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

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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{