Update sphero package error handling

This commit is contained in:
Adrian Zankich 2014-11-19 16:17:14 -08:00
parent b2097b2b02
commit 15852db617
3 changed files with 26 additions and 20 deletions

View File

@ -13,7 +13,7 @@ var _ gobot.AdaptorInterface = (*SpheroAdaptor)(nil)
type SpheroAdaptor struct {
gobot.Adaptor
sp io.ReadWriteCloser
connect func(*SpheroAdaptor)
connect func(*SpheroAdaptor) (err error)
}
// NewSpheroAdaptor returns a new SpheroAdaptor given a name and port
@ -24,28 +24,31 @@ func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
"SpheroAdaptor",
port,
),
connect: func(a *SpheroAdaptor) {
connect: func(a *SpheroAdaptor) (err error) {
c := &serial.Config{Name: a.Port(), Baud: 115200}
s, err := serial.OpenPort(c)
if err != nil {
panic(err)
return err
}
a.sp = s
return
},
}
}
// Connect initiates a connection to the Sphero. Returns true on successful connection.
func (a *SpheroAdaptor) Connect() error {
a.connect(a)
func (a *SpheroAdaptor) Connect() (err error) {
if err = a.connect(a); err != nil {
return
}
a.SetConnected(true)
return nil
return
}
// Reconnect attempts to reconnect to the Sphero. If the Sphero has an active connection
// it will first close that connection and then establish a new connection.
// Returns true on Successful reconnection
func (a *SpheroAdaptor) Reconnect() error {
func (a *SpheroAdaptor) Reconnect() (err error) {
if a.Connected() == true {
a.Disconnect()
}
@ -53,8 +56,10 @@ func (a *SpheroAdaptor) Reconnect() error {
}
// Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.
func (a *SpheroAdaptor) Disconnect() error {
a.sp.Close()
func (a *SpheroAdaptor) Disconnect() (err error) {
if err = a.sp.Close(); err != nil {
return
}
a.SetConnected(false)
return nil
}

View File

@ -8,7 +8,7 @@ import (
func initTestSpheroAdaptor() *SpheroAdaptor {
a := NewSpheroAdaptor("bot", "/dev/null")
a.sp = gobot.NullReadWriteCloser{}
a.connect = func(a *SpheroAdaptor) {}
a.connect = func(a *SpheroAdaptor) (err error) { return nil }
return a
}

View File

@ -3,7 +3,7 @@ package sphero
import (
"bytes"
"encoding/binary"
"fmt"
"errors"
"time"
"github.com/hybridgroup/gobot"
@ -60,6 +60,7 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
responseChannel: make(chan []uint8, 1024),
}
s.AddEvent("error")
s.AddEvent("collision")
s.AddCommand("SetRGB", func(params map[string]interface{}) interface{} {
r := uint8(params["r"].(float64))
@ -114,11 +115,14 @@ func (s *SpheroDriver) adaptor() *SpheroAdaptor {
//
// Emits the Events:
// "collision" SpheroDriver.Collision - On Collision Detected
func (s *SpheroDriver) Start() error {
func (s *SpheroDriver) Start() (err error) {
go func() {
for {
packet := <-s.packetChannel
s.write(packet)
err = s.write(packet)
if err != nil {
gobot.Publish(s.Event("error"), err)
}
}
}()
@ -265,20 +269,17 @@ func (s *SpheroDriver) craftPacket(body []uint8, did byte, cid byte) *packet {
return packet
}
func (s *SpheroDriver) write(packet *packet) {
func (s *SpheroDriver) write(packet *packet) (err error) {
buf := append(packet.header, packet.body...)
buf = append(buf, packet.checksum)
length, err := s.adaptor().sp.Write(buf)
if err != nil {
fmt.Println(s.Name, err)
s.adaptor().Disconnect()
fmt.Println("Reconnecting to SpheroDriver...")
s.adaptor().Connect()
return
return err
} else if length != len(buf) {
fmt.Println("Not enough bytes written", s.Name)
return errors.New("Not enough bytes written")
}
s.seq++
return
}
func (s *SpheroDriver) calculateChecksum(packet *packet) uint8 {