2014-11-30 16:19:53 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
2014-12-19 06:07:48 +08:00
|
|
|
type NullReadWriteCloser struct{}
|
|
|
|
|
|
|
|
func (NullReadWriteCloser) Write(p []byte) (int, error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (NullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (NullReadWriteCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
type testDriver struct {
|
|
|
|
name string
|
|
|
|
pin string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Commander
|
2015-03-17 10:49:36 +08:00
|
|
|
gobot.Eventer
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testDriver) Start() (errs []error) { return }
|
|
|
|
func (t *testDriver) Halt() (errs []error) { return }
|
|
|
|
func (t *testDriver) Name() string { return t.name }
|
2016-09-25 17:46:55 +08:00
|
|
|
func (t *testDriver) SetName(n string) { t.name = n }
|
2014-11-30 16:19:53 +08:00
|
|
|
func (t *testDriver) Pin() string { return t.pin }
|
|
|
|
func (t *testDriver) Connection() gobot.Connection { return t.connection }
|
|
|
|
|
|
|
|
func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
|
|
|
|
t := &testDriver{
|
|
|
|
name: name,
|
|
|
|
connection: adaptor,
|
|
|
|
pin: pin,
|
2015-03-17 10:49:36 +08:00
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-30 16:19:53 +08:00
|
|
|
Commander: gobot.NewCommander(),
|
|
|
|
}
|
|
|
|
|
2015-03-17 10:49:36 +08:00
|
|
|
t.AddEvent("TestEvent")
|
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
|
|
|
|
name := params["name"].(string)
|
|
|
|
return fmt.Sprintf("hello %v", name)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
|
|
|
|
name := params["name"].(string)
|
|
|
|
return fmt.Sprintf("hello %v", name)
|
|
|
|
})
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type testAdaptor struct {
|
|
|
|
name string
|
|
|
|
port string
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAdaptorConnect = func() (errs []error) { return }
|
|
|
|
var testAdaptorFinalize = func() (errs []error) { return }
|
|
|
|
|
|
|
|
func (t *testAdaptor) Finalize() (errs []error) { return testAdaptorFinalize() }
|
|
|
|
func (t *testAdaptor) Connect() (errs []error) { return testAdaptorConnect() }
|
|
|
|
func (t *testAdaptor) Name() string { return t.name }
|
2016-09-25 17:46:55 +08:00
|
|
|
func (t *testAdaptor) SetName(n string) { t.name = n }
|
2014-11-30 16:19:53 +08:00
|
|
|
func (t *testAdaptor) Port() string { return t.port }
|
|
|
|
|
|
|
|
func newTestAdaptor(name string, port string) *testAdaptor {
|
|
|
|
return &testAdaptor{
|
|
|
|
name: name,
|
|
|
|
port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestRobot(name string) *gobot.Robot {
|
|
|
|
adaptor1 := newTestAdaptor("Connection1", "/dev/null")
|
|
|
|
adaptor2 := newTestAdaptor("Connection2", "/dev/null")
|
|
|
|
adaptor3 := newTestAdaptor("", "/dev/null")
|
|
|
|
driver1 := newTestDriver(adaptor1, "Device1", "0")
|
|
|
|
driver2 := newTestDriver(adaptor2, "Device2", "2")
|
|
|
|
driver3 := newTestDriver(adaptor3, "", "1")
|
|
|
|
work := func() {}
|
|
|
|
r := gobot.NewRobot(name,
|
|
|
|
[]gobot.Connection{adaptor1, adaptor2, adaptor3},
|
|
|
|
[]gobot.Device{driver1, driver2, driver3},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
|
|
|
|
message := params["message"].(string)
|
|
|
|
robot := params["robot"].(string)
|
|
|
|
return fmt.Sprintf("hey %v, %v", robot, message)
|
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|