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