gobot.io/x/gobot/v2@v2.1.0/platforms/ble/helpers_test.go (about)

     1  package ble
     2  
     3  import "sync"
     4  
     5  var _ BLEConnector = (*bleTestClientAdaptor)(nil)
     6  
     7  type bleTestClientAdaptor struct {
     8  	name             string
     9  	address          string
    10  	mtx              sync.Mutex
    11  	withoutResponses bool
    12  
    13  	testReadCharacteristic  func(string) ([]byte, error)
    14  	testWriteCharacteristic func(string, []byte) error
    15  }
    16  
    17  func (t *bleTestClientAdaptor) Connect() (err error)      { return }
    18  func (t *bleTestClientAdaptor) Reconnect() (err error)    { return }
    19  func (t *bleTestClientAdaptor) Disconnect() (err error)   { return }
    20  func (t *bleTestClientAdaptor) Finalize() (err error)     { return }
    21  func (t *bleTestClientAdaptor) Name() string              { return t.name }
    22  func (t *bleTestClientAdaptor) SetName(n string)          { t.name = n }
    23  func (t *bleTestClientAdaptor) Address() string           { return t.address }
    24  func (t *bleTestClientAdaptor) WithoutResponses(use bool) { t.withoutResponses = use }
    25  
    26  func (t *bleTestClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) {
    27  	t.mtx.Lock()
    28  	defer t.mtx.Unlock()
    29  	return t.testReadCharacteristic(cUUID)
    30  }
    31  
    32  func (t *bleTestClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err error) {
    33  	t.mtx.Lock()
    34  	defer t.mtx.Unlock()
    35  	return t.testWriteCharacteristic(cUUID, data)
    36  }
    37  
    38  func (t *bleTestClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err error) {
    39  	// TODO: implement this...
    40  	return
    41  }
    42  
    43  func (t *bleTestClientAdaptor) TestReadCharacteristic(f func(cUUID string) (data []byte, err error)) {
    44  	t.mtx.Lock()
    45  	defer t.mtx.Unlock()
    46  	t.testReadCharacteristic = f
    47  }
    48  
    49  func (t *bleTestClientAdaptor) TestWriteCharacteristic(f func(cUUID string, data []byte) (err error)) {
    50  	t.mtx.Lock()
    51  	defer t.mtx.Unlock()
    52  	t.testWriteCharacteristic = f
    53  }
    54  
    55  func NewBleTestAdaptor() *bleTestClientAdaptor {
    56  	return &bleTestClientAdaptor{
    57  		address: "01:02:03:04:05:06",
    58  		testReadCharacteristic: func(cUUID string) (data []byte, e error) {
    59  			return
    60  		},
    61  		testWriteCharacteristic: func(cUUID string, data []byte) (e error) {
    62  			return
    63  		},
    64  	}
    65  }