Skip to content

Commit

Permalink
Merge pull request hybridgroup#155 from hybridgroup/errors
Browse files Browse the repository at this point in the history
Add error handling
  • Loading branch information
deadprogram committed Nov 20, 2014
2 parents 56e8496 + 586507a commit 2305c22
Show file tree
Hide file tree
Showing 93 changed files with 1,809 additions and 1,072 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ PACKAGES := gobot gobot/api gobot/platforms/intel-iot/edison gobot/sysfs $(shell

test:
for package in $(PACKAGES) ; do \
go test github.com/hybridgroup/$$package ; \
go test -a github.com/hybridgroup/$$package ; \
done ; \

cover:
echo "mode: count" > profile.cov ; \
for package in $(PACKAGES) ; do \
go test -covermode=count -coverprofile=tmp.cov github.com/hybridgroup/$$package ; \
go test -a -covermode=count -coverprofile=tmp.cov github.com/hybridgroup/$$package ; \
cat tmp.cov | grep -v "mode: count" >> profile.cov ; \
done ; \
rm tmp.cov ; \
Expand Down
4 changes: 2 additions & 2 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

import (
"errors"
"fmt"
"log"
)

Expand All @@ -28,26 +29,33 @@ func (c *connections) Each(f func(Connection)) {
}

// Start initializes all the connections.
func (c *connections) Start() error {
var err error
func (c *connections) Start() (errs []error) {
log.Println("Starting connections...")
for _, connection := range *c {
info := "Starting connection " + connection.Name()
if connection.Port() != "" {
info = info + " on port " + connection.Port()
}
log.Println(info + "...")
if connection.Connect() == false {
err = errors.New("Could not start connection")
break
if errs = connection.Connect(); len(errs) > 0 {
for i, err := range errs {
errs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
}
return
}
}
return err
return
}

// Finalize finishes all the connections.
func (c *connections) Finalize() {
func (c *connections) Finalize() (errs []error) {
for _, connection := range *c {
connection.Finalize()
if cerrs := connection.Finalize(); cerrs != nil {
for i, err := range cerrs {
cerrs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
}
errs = append(errs, cerrs...)
}
}
return errs
}
24 changes: 16 additions & 8 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

import (
"errors"
"fmt"
"log"
)

Expand Down Expand Up @@ -30,26 +31,33 @@ func (d *devices) Each(f func(Device)) {
}

// Start starts all the devices.
func (d *devices) Start() error {
var err error
func (d *devices) Start() (errs []error) {
log.Println("Starting devices...")
for _, device := range *d {
info := "Starting device " + device.Name()
if device.Pin() != "" {
info = info + " on pin " + device.Pin()
}
log.Println(info + "...")
if device.Start() == false {
err = errors.New("Could not start device")
break
if errs = device.Start(); len(errs) > 0 {
for i, err := range errs {
errs[i] = errors.New(fmt.Sprintf("Device %q: %v", device.Name(), err))
}
return
}
}
return err
return
}

// Halt stop all the devices.
func (d *devices) Halt() {
func (d *devices) Halt() (errs []error) {
for _, device := range *d {
device.Halt()
if derrs := device.Halt(); len(derrs) > 0 {
for i, err := range derrs {
derrs[i] = errors.New(fmt.Sprintf("Device %q: %v", device.Name(), err))
}
errs = append(errs, derrs...)
}
}
return
}
4 changes: 2 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/beaglebone_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func main() {
g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b)
fmt.Println("color", blinkm.Color())
color, _ := blinkm.Color()
fmt.Println("color", color)
})
}

Expand Down
3 changes: 2 additions & 1 deletion examples/beaglebone_direct_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func main() {

work := func() {
gobot.Every(500*time.Millisecond, func() {
if button.DigitalRead() == 1 {
val, _ := button.DigitalRead()
if val == 1 {
led.DigitalWrite(1)
} else {
led.DigitalWrite(0)
Expand Down
3 changes: 2 additions & 1 deletion examples/edison_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func main() {
g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b)
fmt.Println("color", blinkm.Color())
color, _ := blinkm.Color()
fmt.Println("color", color)
})
}

Expand Down
3 changes: 2 additions & 1 deletion examples/firmata_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func main() {
g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b)
fmt.Println("color", blinkm.Color())
color, _ := blinkm.Color()
fmt.Println("color", color)
})
}

Expand Down
3 changes: 2 additions & 1 deletion examples/firmata_hmc6352.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func main() {

work := func() {
gobot.Every(100*time.Millisecond, func() {
fmt.Println("Heading", hmc6352.Heading)
heading, _ := hmc6352.Heading()
fmt.Println("Heading", heading)
})
}

Expand Down
3 changes: 2 additions & 1 deletion examples/raspi_blinkm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func main() {
g := byte(gobot.Rand(255))
b := byte(gobot.Rand(255))
blinkm.Rgb(r, g, b)
fmt.Println("color", blinkm.Color())
color, _ := blinkm.Color()
fmt.Println("color", color)
})
}

Expand Down
29 changes: 25 additions & 4 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,40 @@ 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() (errs []error) {
if rerrs := g.robots.Start(); len(rerrs) > 0 {
for _, err := range rerrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}

c := make(chan os.Signal, 1)
g.trap(c)
if len(errs) > 0 {
// there was an error during start, so we immediatly pass the interrupt
// in order to disconnect the initialized robots, connections and devices
c <- os.Interrupt
}

// waiting for interrupt coming on the channel
_ = <-c
g.robots.Each(func(r *Robot) {
log.Println("Stopping Robot", r.Name, "...")
r.Devices().Halt()
r.Connections().Finalize()
if herrs := r.Devices().Halt(); len(herrs) > 0 {
for _, err := range herrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
if cerrs := r.Connections().Finalize(); len(cerrs) > 0 {
for _, err := range cerrs {
log.Println("Error:", err)
errs = append(errs, err)
}
}
})
return errs
}

// Robots fetch all robots associated with this Gobot instance.
Expand Down
21 changes: 13 additions & 8 deletions platforms/ardrone/ardrone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/hybridgroup/gobot"
)

var _ gobot.AdaptorInterface = (*ArdroneAdaptor)(nil)

// drone defines expected drone behaviour
type drone interface {
Takeoff() bool
Expand All @@ -23,7 +25,7 @@ type drone interface {
type ArdroneAdaptor struct {
gobot.Adaptor
drone drone
connect func(*ArdroneAdaptor)
connect func(*ArdroneAdaptor) (err error)
}

// NewArdroneAdaptor creates a new ardrone and connects with default configuration
Expand All @@ -33,27 +35,30 @@ func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
name,
"ArdroneAdaptor",
),
connect: func(a *ArdroneAdaptor) {
connect: func(a *ArdroneAdaptor) (err error) {
config := client.DefaultConfig()
if len(v) > 0 {
config.Ip = v[0]
}
d, err := client.Connect(config)
if err != nil {
panic(err)
return
}
a.drone = d
return
},
}
}

// Connect returns true when connection to ardrone is established correclty
func (a *ArdroneAdaptor) Connect() bool {
a.connect(a)
return true
func (a *ArdroneAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil {
return []error{err}
}
return
}

// Finalize returns true when connection is finalized correctly
func (a *ArdroneAdaptor) Finalize() bool {
return true
func (a *ArdroneAdaptor) Finalize() (errs []error) {
return
}
7 changes: 4 additions & 3 deletions platforms/ardrone/ardrone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (

func initTestArdroneAdaptor() *ArdroneAdaptor {
a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) {
a.connect = func(a *ArdroneAdaptor) error {
a.drone = &testDrone{}
return nil
}
return a
}

func TestConnect(t *testing.T) {
a := initTestArdroneAdaptor()
gobot.Assert(t, a.Connect(), true)
gobot.Assert(t, len(a.Connect()), 0)
}

func TestFinalize(t *testing.T) {
a := initTestArdroneAdaptor()
gobot.Assert(t, a.Finalize(), true)
gobot.Assert(t, len(a.Finalize()), 0)
}
10 changes: 6 additions & 4 deletions platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/hybridgroup/gobot"
)

var _ gobot.DriverInterface = (*ArdroneDriver)(nil)

type ArdroneDriver struct {
gobot.Driver
}
Expand All @@ -30,13 +32,13 @@ func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
}

// Start returns true if driver is started succesfully
func (a *ArdroneDriver) Start() bool {
return true
func (a *ArdroneDriver) Start() (errs []error) {
return
}

// Halt returns true if driver is halted succesfully
func (a *ArdroneDriver) Halt() bool {
return true
func (a *ArdroneDriver) Halt() (errs []error) {
return
}

// TakeOff makes the drone start flying
Expand Down
7 changes: 4 additions & 3 deletions platforms/ardrone/ardrone_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

func initTestArdroneDriver() *ArdroneDriver {
a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) {
a.connect = func(a *ArdroneAdaptor) (err error) {
a.drone = &testDrone{}
return
}
d := NewArdroneDriver(a, "drone")
a.Connect()
Expand All @@ -17,12 +18,12 @@ func initTestArdroneDriver() *ArdroneDriver {

func TestArdroneDriverStart(t *testing.T) {
d := initTestArdroneDriver()
gobot.Assert(t, d.Start(), true)
gobot.Assert(t, len(d.Start()), 0)
}

func TestArdroneDriverHalt(t *testing.T) {
d := initTestArdroneDriver()
gobot.Assert(t, d.Halt(), true)
gobot.Assert(t, len(d.Halt()), 0)
}
func TestArdroneDriverTakeOff(t *testing.T) {
d := initTestArdroneDriver()
Expand Down
Loading

0 comments on commit 2305c22

Please sign in to comment.