gobot.io/x/gobot@v1.16.0/api/helpers_test.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gobot.io/x/gobot"
     7  )
     8  
     9  type NullReadWriteCloser struct{}
    10  
    11  func (NullReadWriteCloser) Write(p []byte) (int, error) {
    12  	return len(p), nil
    13  }
    14  
    15  func (NullReadWriteCloser) Read(b []byte) (int, error) {
    16  	return len(b), nil
    17  }
    18  
    19  func (NullReadWriteCloser) Close() error {
    20  	return nil
    21  }
    22  
    23  type testDriver struct {
    24  	name       string
    25  	pin        string
    26  	connection gobot.Connection
    27  	gobot.Commander
    28  	gobot.Eventer
    29  }
    30  
    31  func (t *testDriver) Start() (err error)           { return }
    32  func (t *testDriver) Halt() (err error)            { return }
    33  func (t *testDriver) Name() string                 { return t.name }
    34  func (t *testDriver) SetName(n string)             { t.name = n }
    35  func (t *testDriver) Pin() string                  { return t.pin }
    36  func (t *testDriver) Connection() gobot.Connection { return t.connection }
    37  
    38  func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
    39  	t := &testDriver{
    40  		name:       name,
    41  		connection: adaptor,
    42  		pin:        pin,
    43  		Eventer:    gobot.NewEventer(),
    44  		Commander:  gobot.NewCommander(),
    45  	}
    46  
    47  	t.AddEvent("TestEvent")
    48  
    49  	t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
    50  		name := params["name"].(string)
    51  		return fmt.Sprintf("hello %v", name)
    52  	})
    53  
    54  	t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
    55  		name := params["name"].(string)
    56  		return fmt.Sprintf("hello %v", name)
    57  	})
    58  
    59  	return t
    60  }
    61  
    62  type testAdaptor struct {
    63  	name string
    64  	port string
    65  }
    66  
    67  var testAdaptorConnect = func() (err error) { return }
    68  var testAdaptorFinalize = func() (err error) { return }
    69  
    70  func (t *testAdaptor) Finalize() (err error) { return testAdaptorFinalize() }
    71  func (t *testAdaptor) Connect() (err error)  { return testAdaptorConnect() }
    72  func (t *testAdaptor) Name() string          { return t.name }
    73  func (t *testAdaptor) SetName(n string)      { t.name = n }
    74  func (t *testAdaptor) Port() string          { return t.port }
    75  
    76  func newTestAdaptor(name string, port string) *testAdaptor {
    77  	return &testAdaptor{
    78  		name: name,
    79  		port: port,
    80  	}
    81  }
    82  
    83  func newTestRobot(name string) *gobot.Robot {
    84  	adaptor1 := newTestAdaptor("Connection1", "/dev/null")
    85  	adaptor2 := newTestAdaptor("Connection2", "/dev/null")
    86  	adaptor3 := newTestAdaptor("", "/dev/null")
    87  	driver1 := newTestDriver(adaptor1, "Device1", "0")
    88  	driver2 := newTestDriver(adaptor2, "Device2", "2")
    89  	driver3 := newTestDriver(adaptor3, "", "1")
    90  	work := func() {}
    91  	r := gobot.NewRobot(name,
    92  		[]gobot.Connection{adaptor1, adaptor2, adaptor3},
    93  		[]gobot.Device{driver1, driver2, driver3},
    94  		work,
    95  	)
    96  	r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
    97  		message := params["message"].(string)
    98  		robot := params["robot"].(string)
    99  		return fmt.Sprintf("hey %v, %v", robot, message)
   100  	})
   101  	return r
   102  }