document code

This commit is contained in:
alexisvisco 2019-03-26 10:25:49 +01:00
parent 0208b42e2a
commit 7f4cb89dca
5 changed files with 176 additions and 69 deletions

58
sonic/control.go Normal file
View file

@ -0,0 +1,58 @@
package sonic
import (
"errors"
"fmt"
)
var InvalidActionName = errors.New("invalid action name")
// Controllable is used for administration purposes.
type Controllable interface {
// Trigger an action.
// Command syntax TRIGGER [<action>]?.
Trigger(action Action) (err error)
// Quit refer to the Base interface
Quit() (err error)
// Quit refer to the Base interface
Ping() (err error)
}
type ControlChannel struct {
*Driver
}
func NewControl(host string, port int, password string) (Controllable, error) {
driver := &Driver{
Host: host,
Port: port,
Password: password,
channel: Control,
}
err := driver.Connect()
if err != nil {
return nil, err
}
return ControlChannel{
Driver: driver,
}, nil
}
func (c ControlChannel) Trigger(action Action) (err error) {
if IsActionValid(action) {
return InvalidActionName
}
err = c.write(fmt.Sprintf("TRIGGER %s", action))
if err != nil {
return err
}
// should get OK
_, err = c.read()
if err != nil {
return err
}
return nil
}