2013-12-31 08:51:21 +08:00
|
|
|
package gobot
|
|
|
|
|
2016-10-19 00:23:59 +08:00
|
|
|
import "os"
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
type NullReadWriteCloser struct{}
|
2014-04-11 21:02:21 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func (NullReadWriteCloser) Write(p []byte) (int, error) {
|
2014-04-11 21:02:21 +08:00
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func (NullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
}
|
2014-07-10 00:38:43 +08:00
|
|
|
|
2014-06-14 07:01:39 +08:00
|
|
|
func (NullReadWriteCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-31 08:51:21 +08:00
|
|
|
type testDriver struct {
|
2014-11-21 10:00:32 +08:00
|
|
|
name string
|
|
|
|
pin string
|
|
|
|
connection Connection
|
|
|
|
Commander
|
2013-12-31 08:51:21 +08:00
|
|
|
}
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
var (
|
2023-11-16 03:51:52 +08:00
|
|
|
testDriverStart = func() error { return nil }
|
|
|
|
testDriverHalt = func() error { return nil }
|
2023-10-21 02:50:42 +08:00
|
|
|
)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func (t *testDriver) Start() error { return testDriverStart() }
|
|
|
|
func (t *testDriver) Halt() error { return testDriverHalt() }
|
2014-11-21 10:00:32 +08:00
|
|
|
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-21 10:00:32 +08:00
|
|
|
func (t *testDriver) Pin() string { return t.pin }
|
|
|
|
func (t *testDriver) Connection() Connection { return t.connection }
|
2013-12-31 08:51:21 +08:00
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
|
2014-06-12 08:41:04 +08:00
|
|
|
t := &testDriver{
|
2014-11-21 10:00:32 +08:00
|
|
|
name: name,
|
|
|
|
connection: adaptor,
|
2014-11-30 16:19:53 +08:00
|
|
|
pin: pin,
|
2014-11-21 10:00:32 +08:00
|
|
|
Commander: NewCommander(),
|
2013-12-31 09:22:16 +08:00
|
|
|
}
|
2014-06-12 08:41:04 +08:00
|
|
|
|
2015-03-17 10:49:36 +08:00
|
|
|
t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} { return nil })
|
2014-06-12 08:41:04 +08:00
|
|
|
|
|
|
|
return t
|
2013-12-31 08:51:21 +08:00
|
|
|
}
|
2014-04-15 14:16:35 +08:00
|
|
|
|
2014-06-14 05:46:07 +08:00
|
|
|
type testAdaptor struct {
|
2014-11-21 10:00:32 +08:00
|
|
|
name string
|
|
|
|
port string
|
2014-06-14 05:46:07 +08:00
|
|
|
}
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
var (
|
2023-11-16 03:51:52 +08:00
|
|
|
testAdaptorConnect = func() error { return nil }
|
|
|
|
testAdaptorFinalize = func() error { return nil }
|
2023-10-21 02:50:42 +08:00
|
|
|
)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func (t *testAdaptor) Finalize() error { return testAdaptorFinalize() }
|
|
|
|
func (t *testAdaptor) Connect() error { return testAdaptorConnect() }
|
|
|
|
func (t *testAdaptor) Name() string { return t.name }
|
|
|
|
func (t *testAdaptor) SetName(n string) { t.name = n }
|
|
|
|
func (t *testAdaptor) Port() string { return t.port }
|
2014-06-14 05:46:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
func newTestAdaptor(name string, port string) *testAdaptor { //nolint:unparam // keep for tests
|
2014-06-11 06:16:11 +08:00
|
|
|
return &testAdaptor{
|
2014-11-21 10:00:32 +08:00
|
|
|
name: name,
|
2014-11-30 16:19:53 +08:00
|
|
|
port: port,
|
2014-04-15 14:16:35 +08:00
|
|
|
}
|
2013-12-31 08:51:21 +08:00
|
|
|
}
|
2014-01-01 05:16:25 +08:00
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
func newTestRobot(name string) *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")
|
2014-05-03 18:22:22 +08:00
|
|
|
work := func() {}
|
2014-07-11 02:14:08 +08:00
|
|
|
r := NewRobot(name,
|
|
|
|
[]Connection{adaptor1, adaptor2, adaptor3},
|
|
|
|
[]Device{driver1, driver2, driver3},
|
|
|
|
work,
|
|
|
|
)
|
2014-11-30 16:19:53 +08:00
|
|
|
r.AddCommand("RobotCommand", func(params map[string]interface{}) interface{} { return nil })
|
2016-10-19 00:23:59 +08:00
|
|
|
r.trap = func(c chan os.Signal) {
|
|
|
|
c <- os.Interrupt
|
|
|
|
}
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2014-06-12 08:41:04 +08:00
|
|
|
return r
|
2014-01-01 05:16:25 +08:00
|
|
|
}
|