core: Refactor audio platform for new adaptor/driver signatures
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
56558cd9cb
commit
54f0c16be7
|
@ -10,8 +10,8 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
gbot := gobot.NewGobot()
|
gbot := gobot.NewGobot()
|
||||||
|
|
||||||
e := audio.NewAudioAdaptor("sound")
|
e := audio.NewAdaptor()
|
||||||
laser := audio.NewAudioDriver(e, "laser", "./examples/laser.mp3")
|
laser := audio.NewDriver(e, "./examples/laser.mp3")
|
||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
gobot.Every(2*time.Second, func() {
|
gobot.Every(2*time.Second, func() {
|
||||||
|
|
|
@ -9,23 +9,23 @@ import (
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AudioAdaptor struct {
|
type Adaptor struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAudioAdaptor(name string) *AudioAdaptor {
|
func NewAdaptor() *Adaptor {
|
||||||
return &AudioAdaptor{
|
return &Adaptor{}
|
||||||
name: name,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
var errorsList []error
|
||||||
|
|
||||||
if fileName == "" {
|
if fileName == "" {
|
||||||
|
|
|
@ -9,15 +9,12 @@ import (
|
||||||
"github.com/hybridgroup/gobot/gobottest"
|
"github.com/hybridgroup/gobot/gobottest"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.Adaptor = (*AudioAdaptor)(nil)
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
||||||
|
|
||||||
func TestAudioAdaptor(t *testing.T) {
|
func TestAudioAdaptor(t *testing.T) {
|
||||||
a := NewAudioAdaptor("tester")
|
a := NewAdaptor()
|
||||||
|
|
||||||
gobottest.Assert(t, a.Name(), "tester")
|
|
||||||
|
|
||||||
gobottest.Assert(t, len(a.Connect()), 0)
|
gobottest.Assert(t, len(a.Connect()), 0)
|
||||||
|
|
||||||
gobottest.Assert(t, len(a.Finalize()), 0)
|
gobottest.Assert(t, len(a.Finalize()), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,14 +35,14 @@ func TestAudioAdaptorCommandsUnknown(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAudioAdaptorSoundWithNoFilename(t *testing.T) {
|
func TestAudioAdaptorSoundWithNoFilename(t *testing.T) {
|
||||||
a := NewAudioAdaptor("tester")
|
a := NewAdaptor()
|
||||||
|
|
||||||
errors := a.Sound("")
|
errors := a.Sound("")
|
||||||
gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
|
gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
|
func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
|
||||||
a := NewAudioAdaptor("tester")
|
a := NewAdaptor()
|
||||||
|
|
||||||
errors := a.Sound("doesnotexist.mp3")
|
errors := a.Sound("doesnotexist.mp3")
|
||||||
gobottest.Assert(t, errors[0].Error(), "stat doesnotexist.mp3: no such file or directory")
|
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) {
|
func TestAudioAdaptorSoundWithValidMP3Filename(t *testing.T) {
|
||||||
execCommand = gobottest.ExecCommand
|
execCommand = gobottest.ExecCommand
|
||||||
|
|
||||||
a := NewAudioAdaptor("tester")
|
a := NewAdaptor()
|
||||||
defer func() { execCommand = exec.Command }()
|
defer func() { execCommand = exec.Command }()
|
||||||
|
|
||||||
errors := a.Sound("../../examples/laser.mp3")
|
errors := a.Sound("../../examples/laser.mp3")
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
package audio
|
package audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AudioDriver struct {
|
type Driver struct {
|
||||||
name string
|
name string
|
||||||
connection gobot.Connection
|
connection gobot.Connection
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
|
@ -17,9 +18,8 @@ type AudioDriver struct {
|
||||||
filename string
|
filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAudioDriver(a *AudioAdaptor, name string, filename string) *AudioDriver {
|
func NewDriver(a *Adaptor, filename string) *Driver {
|
||||||
d := &AudioDriver{
|
d := &Driver{
|
||||||
name: name,
|
|
||||||
connection: a,
|
connection: a,
|
||||||
interval: 500 * time.Millisecond,
|
interval: 500 * time.Millisecond,
|
||||||
filename: filename,
|
filename: filename,
|
||||||
|
@ -30,30 +30,32 @@ func NewAudioDriver(a *AudioAdaptor, name string, filename string) *AudioDriver
|
||||||
return d
|
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
|
return d.connection
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AudioDriver) Sound(fileName string) []error {
|
func (d *Driver) Sound(fileName string) []error {
|
||||||
return d.Connection().(*AudioAdaptor).Sound(fileName)
|
return d.Connection().(*Adaptor).Sound(fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AudioDriver) Play() []error {
|
func (d *Driver) Play() []error {
|
||||||
return d.Sound(d.Filename())
|
return d.Sound(d.Filename())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AudioDriver) adaptor() *AudioAdaptor {
|
func (d *Driver) adaptor() *Adaptor {
|
||||||
return d.Connection().(*AudioAdaptor)
|
return d.Connection().(*Adaptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AudioDriver) Start() (err []error) {
|
func (d *Driver) Start() (err []error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AudioDriver) Halt() (err []error) {
|
func (d *Driver) Halt() (err []error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,15 +10,14 @@ import (
|
||||||
"github.com/hybridgroup/gobot/gobottest"
|
"github.com/hybridgroup/gobot/gobottest"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.Driver = (*AudioDriver)(nil)
|
var _ gobot.Driver = (*Driver)(nil)
|
||||||
|
|
||||||
func TestAudioDriver(t *testing.T) {
|
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.Filename(), "../../examples/laser.mp3")
|
||||||
|
|
||||||
gobottest.Assert(t, d.Connection().Name(), "conn")
|
gobottest.Refute(t, d.Connection(), nil)
|
||||||
|
|
||||||
gobottest.Assert(t, len(d.Start()), 0)
|
gobottest.Assert(t, len(d.Start()), 0)
|
||||||
|
|
||||||
|
@ -26,7 +25,7 @@ func TestAudioDriver(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAudioDriverSoundWithNoFilename(t *testing.T) {
|
func TestAudioDriverSoundWithNoFilename(t *testing.T) {
|
||||||
d := NewAudioDriver(NewAudioAdaptor("conn"), "dev", "")
|
d := NewDriver(NewAdaptor(), "")
|
||||||
|
|
||||||
errors := d.Sound("")
|
errors := d.Sound("")
|
||||||
gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
|
gobottest.Assert(t, errors[0].Error(), "Requires filename for audio file.")
|
||||||
|
@ -36,7 +35,7 @@ func TestAudioDriverSoundWithDefaultFilename(t *testing.T) {
|
||||||
execCommand = gobottest.ExecCommand
|
execCommand = gobottest.ExecCommand
|
||||||
defer func() { execCommand = exec.Command }()
|
defer func() { execCommand = exec.Command }()
|
||||||
|
|
||||||
d := NewAudioDriver(NewAudioAdaptor("conn"), "dev", "../../examples/laser.mp3")
|
d := NewDriver(NewAdaptor(), "../../examples/laser.mp3")
|
||||||
|
|
||||||
errors := d.Play()
|
errors := d.Play()
|
||||||
gobottest.Assert(t, len(errors), 0)
|
gobottest.Assert(t, len(errors), 0)
|
||||||
|
|
Loading…
Reference in New Issue