core: Refactor audio platform for new adaptor/driver signatures

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-09-25 12:17:01 +02:00
parent 56558cd9cb
commit 54f0c16be7
5 changed files with 38 additions and 40 deletions

View File

@ -10,8 +10,8 @@ import (
func main() {
gbot := gobot.NewGobot()
e := audio.NewAudioAdaptor("sound")
laser := audio.NewAudioDriver(e, "laser", "./examples/laser.mp3")
e := audio.NewAdaptor()
laser := audio.NewDriver(e, "./examples/laser.mp3")
work := func() {
gobot.Every(2*time.Second, func() {

View File

@ -9,23 +9,23 @@ import (
"path"
)
type AudioAdaptor struct {
type Adaptor struct {
name string
}
func NewAudioAdaptor(name string) *AudioAdaptor {
return &AudioAdaptor{
name: name,
}
func NewAdaptor() *Adaptor {
return &Adaptor{}
}
func (a *AudioAdaptor) Name() string { return a.name }
func (a *Adaptor) Name() string { return a.name }
func (a *AudioAdaptor) Connect() []error { return nil }
func (a *Adaptor) SetName(n string) { a.name = n }
func (a *AudioAdaptor) Finalize() []error { return nil }
func (a *Adaptor) Connect() []error { return nil }
func (a *AudioAdaptor) Sound(fileName string) []error {
func (a *Adaptor) Finalize() []error { return nil }
func (a *Adaptor) Sound(fileName string) []error {
var errorsList []error
if fileName == "" {

View File

@ -9,15 +9,12 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Adaptor = (*AudioAdaptor)(nil)
var _ gobot.Adaptor = (*Adaptor)(nil)
func TestAudioAdaptor(t *testing.T) {
a := NewAudioAdaptor("tester")
gobottest.Assert(t, a.Name(), "tester")
a := NewAdaptor()
gobottest.Assert(t, len(a.Connect()), 0)
gobottest.Assert(t, len(a.Finalize()), 0)
}
@ -38,14 +35,14 @@ func TestAudioAdaptorCommandsUnknown(t *testing.T) {
}
func TestAudioAdaptorSoundWithNoFilename(t *testing.T) {
a := NewAudioAdaptor("tester")
a := NewAdaptor()
errors := a.Sound("")
gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
}
func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
a := NewAudioAdaptor("tester")
a := NewAdaptor()
errors := a.Sound("doesnotexist.mp3")
gobottest.Assert(t, errors[0].Error(), "stat doesnotexist.mp3: no such file or directory")
@ -54,7 +51,7 @@ func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
func TestAudioAdaptorSoundWithValidMP3Filename(t *testing.T) {
execCommand = gobottest.ExecCommand
a := NewAudioAdaptor("tester")
a := NewAdaptor()
defer func() { execCommand = exec.Command }()
errors := a.Sound("../../examples/laser.mp3")

View File

@ -3,11 +3,12 @@
package audio
import (
"github.com/hybridgroup/gobot"
"time"
"github.com/hybridgroup/gobot"
)
type AudioDriver struct {
type Driver struct {
name string
connection gobot.Connection
interval time.Duration
@ -17,9 +18,8 @@ type AudioDriver struct {
filename string
}
func NewAudioDriver(a *AudioAdaptor, name string, filename string) *AudioDriver {
d := &AudioDriver{
name: name,
func NewDriver(a *Adaptor, filename string) *Driver {
d := &Driver{
connection: a,
interval: 500 * time.Millisecond,
filename: filename,
@ -30,30 +30,32 @@ func NewAudioDriver(a *AudioAdaptor, name string, filename string) *AudioDriver
return d
}
func (d *AudioDriver) Name() string { return d.name }
func (d *Driver) Name() string { return d.name }
func (d *AudioDriver) Filename() string { return d.filename }
func (d *Driver) SetName(n string) { d.name = n }
func (d *AudioDriver) Connection() gobot.Connection {
func (d *Driver) Filename() string { return d.filename }
func (d *Driver) Connection() gobot.Connection {
return d.connection
}
func (d *AudioDriver) Sound(fileName string) []error {
return d.Connection().(*AudioAdaptor).Sound(fileName)
func (d *Driver) Sound(fileName string) []error {
return d.Connection().(*Adaptor).Sound(fileName)
}
func (d *AudioDriver) Play() []error {
func (d *Driver) Play() []error {
return d.Sound(d.Filename())
}
func (d *AudioDriver) adaptor() *AudioAdaptor {
return d.Connection().(*AudioAdaptor)
func (d *Driver) adaptor() *Adaptor {
return d.Connection().(*Adaptor)
}
func (d *AudioDriver) Start() (err []error) {
func (d *Driver) Start() (err []error) {
return
}
func (d *AudioDriver) Halt() (err []error) {
func (d *Driver) Halt() (err []error) {
return
}

View File

@ -10,15 +10,14 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Driver = (*AudioDriver)(nil)
var _ gobot.Driver = (*Driver)(nil)
func TestAudioDriver(t *testing.T) {
d := NewAudioDriver(NewAudioAdaptor("conn"), "dev", "../../examples/laser.mp3")
d := NewDriver(NewAdaptor(), "../../examples/laser.mp3")
gobottest.Assert(t, d.Name(), "dev")
gobottest.Assert(t, d.Filename(), "../../examples/laser.mp3")
gobottest.Assert(t, d.Connection().Name(), "conn")
gobottest.Refute(t, d.Connection(), nil)
gobottest.Assert(t, len(d.Start()), 0)
@ -26,7 +25,7 @@ func TestAudioDriver(t *testing.T) {
}
func TestAudioDriverSoundWithNoFilename(t *testing.T) {
d := NewAudioDriver(NewAudioAdaptor("conn"), "dev", "")
d := NewDriver(NewAdaptor(), "")
errors := d.Sound("")
gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
@ -36,7 +35,7 @@ func TestAudioDriverSoundWithDefaultFilename(t *testing.T) {
execCommand = gobottest.ExecCommand
defer func() { execCommand = exec.Command }()
d := NewAudioDriver(NewAudioAdaptor("conn"), "dev", "../../examples/laser.mp3")
d := NewDriver(NewAdaptor(), "../../examples/laser.mp3")
errors := d.Play()
gobottest.Assert(t, len(errors), 0)