2014-11-21 10:00:32 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
type commander struct {
|
|
|
|
commands map[string]func(map[string]interface{}) interface{}
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Commander is the interface which describes the behaviour for a Driver or Adaptor
|
|
|
|
// which exposes API commands.
|
2014-11-21 10:00:32 +08:00
|
|
|
type Commander interface {
|
2014-12-31 21:15:52 +08:00
|
|
|
// Command returns a command given a name. Returns nil if the command is not found.
|
2014-11-22 03:57:26 +08:00
|
|
|
Command(string) (command func(map[string]interface{}) interface{})
|
2014-12-31 21:15:52 +08:00
|
|
|
// Commands returns a map of commands.
|
2014-11-21 10:00:32 +08:00
|
|
|
Commands() (commands map[string]func(map[string]interface{}) interface{})
|
2014-12-31 21:15:52 +08:00
|
|
|
// AddCommand adds a command given a name.
|
2014-11-21 10:00:32 +08:00
|
|
|
AddCommand(name string, command func(map[string]interface{}) interface{})
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// NewCommander returns a new Commander.
|
2014-11-21 10:00:32 +08:00
|
|
|
func NewCommander() Commander {
|
|
|
|
return &commander{
|
|
|
|
commands: make(map[string]func(map[string]interface{}) interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 20:16:22 +08:00
|
|
|
// Command returns the command interface when passed a valid command name
|
|
|
|
func (c *commander) Command(name string) func(map[string]interface{}) interface{} {
|
|
|
|
return c.commands[name]
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 03:40:58 +08:00
|
|
|
// Commands returns the entire map of valid commands
|
2014-11-21 10:00:32 +08:00
|
|
|
func (c *commander) Commands() map[string]func(map[string]interface{}) interface{} {
|
|
|
|
return c.commands
|
|
|
|
}
|
|
|
|
|
2016-12-02 03:40:58 +08:00
|
|
|
// AddCommand adds a new command, when passed a command name and the command interface.
|
2014-11-21 10:00:32 +08:00
|
|
|
func (c *commander) AddCommand(name string, command func(map[string]interface{}) interface{}) {
|
|
|
|
c.commands[name] = command
|
|
|
|
}
|