gobot.io/x/gobot/v2@v2.1.0/platforms/leap/leap_motion_adaptor.go (about)

     1  package leap
     2  
     3  import (
     4  	"io"
     5  
     6  	"gobot.io/x/gobot/v2"
     7  
     8  	"golang.org/x/net/websocket"
     9  )
    10  
    11  // Adaptor is the Gobot Adaptor connection to the Leap Motion
    12  type Adaptor struct {
    13  	name    string
    14  	port    string
    15  	ws      io.ReadWriteCloser
    16  	connect func(string) (io.ReadWriteCloser, error)
    17  }
    18  
    19  // NewAdaptor creates a new leap motion adaptor using specified port,
    20  // which is this case is the host IP or name of the Leap Motion daemon
    21  func NewAdaptor(port string) *Adaptor {
    22  	return &Adaptor{
    23  		name: gobot.DefaultName("LeapMotion"),
    24  		port: port,
    25  		connect: func(host string) (io.ReadWriteCloser, error) {
    26  			return websocket.Dial("ws://"+host+"/v6.json", "", "http://"+host)
    27  		},
    28  	}
    29  }
    30  
    31  // Name returns the Adaptor Name
    32  func (l *Adaptor) Name() string { return l.name }
    33  
    34  // SetName sets the Adaptor Name
    35  func (l *Adaptor) SetName(n string) { l.name = n }
    36  
    37  // Port returns the Adaptor Port which is this case is the host IP or name
    38  func (l *Adaptor) Port() string { return l.port }
    39  
    40  // Connect returns true if connection to leap motion is established successfully
    41  func (l *Adaptor) Connect() (err error) {
    42  	ws, e := l.connect(l.Port())
    43  	if e != nil {
    44  		return e
    45  	}
    46  
    47  	l.ws = ws
    48  	return
    49  }
    50  
    51  // Finalize ends connection to leap motion
    52  func (l *Adaptor) Finalize() (err error) { return }