1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +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 // AdaptorInterface defines behaviour expected for a Gobot Adaptor
type AdaptorInterface interface { type AdaptorInterface interface {
Finalize() bool Finalize() error
Connect() bool Connect() error
Port() string Port() string
Name() string Name() string
Type() string Type() string

View File

@ -2,6 +2,7 @@ package gobot
import ( import (
"errors" "errors"
"fmt"
"log" "log"
) )
@ -37,8 +38,9 @@ func (c *connections) Start() error {
info = info + " on port " + connection.Port() info = info + " on port " + connection.Port()
} }
log.Println(info + "...") log.Println(info + "...")
if connection.Connect() == false { err = connection.Connect()
err = errors.New("Could not start connection") if err != nil {
err = errors.New(fmt.Sprintf("Could not start connection: %v", err))
break break
} }
} }

View File

@ -2,6 +2,7 @@ package gobot
import ( import (
"errors" "errors"
"fmt"
"log" "log"
) )
@ -39,8 +40,9 @@ func (d *devices) Start() error {
info = info + " on pin " + device.Pin() info = info + " on pin " + device.Pin()
} }
log.Println(info + "...") log.Println(info + "...")
if device.Start() == false { err = device.Start()
err = errors.New("Could not start device") if err != nil {
err = errors.New(fmt.Sprintf("Could not start device: %v", err))
break break
} }
} }

View File

@ -7,8 +7,8 @@ import (
// DriverInterface defines Driver expected behaviour // DriverInterface defines Driver expected behaviour
type DriverInterface interface { type DriverInterface interface {
Start() bool Start() error
Halt() bool Halt() error
Adaptor() AdaptorInterface Adaptor() AdaptorInterface
SetInterval(time.Duration) SetInterval(time.Duration)
Interval() 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 // Start runs the main Gobot event loop
func (g *Gobot) Start() { func (g *Gobot) Start() error {
g.robots.Start() err := g.robots.Start()
if err != nil {
return err
}
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
g.trap(c) g.trap(c)
@ -69,6 +72,7 @@ func (g *Gobot) Start() {
r.Devices().Halt() r.Devices().Halt()
r.Connections().Finalize() r.Connections().Finalize()
}) })
return nil
} }
// Robots fetch all robots associated with this Gobot instance. // Robots fetch all robots associated with this Gobot instance.

View File

@ -1,6 +1,7 @@
package gobot package gobot
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
) )
@ -33,13 +34,17 @@ func (r *robots) Len() int {
// Start initialises the event loop. All robots that were added will // Start initialises the event loop. All robots that were added will
// be automtically started as a result of this call. // be automtically started as a result of this call.
func (r *robots) Start() { func (r *robots) Start() error {
for _, robot := range *r { 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)) { func (r *robots) Each(f func(*Robot)) {
for _, robot := range *r { for _, robot := range *r {
f(robot) 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 // 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 // need to manually start a robot if already part of a Gobot application as the
// robot will be automatically started for you. // robot will be automatically started for you.
func (r *Robot) Start() { func (r *Robot) Start() error {
log.Println("Starting Robot", r.Name, "...") log.Println("Starting Robot", r.Name, "...")
if err := r.Connections().Start(); err != nil { 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 { 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 { if r.Work != nil {
log.Println("Starting work...") log.Println("Starting work...")
r.Work() r.Work()
} }
return nil
} }
// Devices retrieves all devices associated with this robot. // Devices retrieves all devices associated with this robot.

View File

@ -62,9 +62,8 @@ type testDriver struct {
Driver Driver
} }
func (t *testDriver) Init() bool { return true } func (t *testDriver) Start() error { return nil }
func (t *testDriver) Start() bool { return true } func (t *testDriver) Halt() error { return nil }
func (t *testDriver) Halt() bool { return true }
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver { func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
t := &testDriver{ t := &testDriver{
@ -94,8 +93,8 @@ type testAdaptor struct {
Adaptor Adaptor
} }
func (t *testAdaptor) Finalize() bool { return true } func (t *testAdaptor) Finalize() error { return nil }
func (t *testAdaptor) Connect() bool { return true } func (t *testAdaptor) Connect() error { return nil }
func NewTestAdaptor(name string) *testAdaptor { func NewTestAdaptor(name string) *testAdaptor {
return &testAdaptor{ return &testAdaptor{