/
txqr.go
71 lines (61 loc) · 1.5 KB
/
txqr.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package txqrtester
import (
"encoding/json"
"fmt"
"github.com/divan/txqr/cmd/txqr-tester/ws"
"github.com/divan/txqr/mobile"
"github.com/gorilla/websocket"
)
// Connector represents a connection to the txqr-tester app
// via websockets.
type Connector struct {
conn *websocket.Conn
*txqr.Decoder
}
func NewConnector() *Connector {
return &Connector{
Decoder: txqr.NewDecoder(),
}
}
// Connect attempts to establish connection to the WS server.
func (c *Connector) Connect(address string) error {
conn, _, err := websocket.DefaultDialer.Dial(address, nil)
if err != nil {
return fmt.Errorf("dial: %v", err)
}
c.conn = conn
return c.connect()
}
// StartNext notifies txqr-tester that app is ready to scan next animated QR.
func (c *Connector) StartNext() error {
return c.sendCommand(ws.UIRequest{
Cmd: ws.CmdStartNext,
})
}
// SendResult sends scanning result in milliseconds.
func (c *Connector) SendResult(duration int64) error {
return c.sendCommand(ws.UIRequest{
Cmd: ws.CmdResult,
Duration: duration,
})
}
// Close closes underlying WebSocket connection.
func (c *Connector) Close() error {
if c.conn == nil {
return nil
}
return c.conn.Close()
}
func (c *Connector) connect() error {
return c.sendCommand(ws.UIRequest{
Cmd: ws.CmdConnect,
Device: "N/A",
})
}
func (c *Connector) sendCommand(req ws.UIRequest) error {
data, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("json marshal: %v", err)
}
return c.conn.WriteMessage(websocket.TextMessage, data)
}