-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn.go
96 lines (83 loc) · 1.96 KB
/
conn.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package server
import (
"bufio"
"fmt"
"net"
"strings"
"github.com/tarm/serial"
)
const (
ConnTypeUSB = "usb"
ConnTypeNetwork = "network"
)
type ScannerConn struct {
Type string
udpAddress *net.UDPAddr
udpConn *net.UDPConn
usbPort string
usbConfig *serial.Config
usbConn *serial.Port
usbReader *bufio.Scanner
}
func NewUDPConn(addr *net.UDPAddr) (*ScannerConn, error) {
return &ScannerConn{
Type: ConnTypeNetwork,
udpAddress: addr,
}, nil
}
func NewUSBConn(path string) (*ScannerConn, error) {
return &ScannerConn{
Type: ConnTypeUSB,
usbPort: path,
usbConfig: &serial.Config{Name: path, Baud: 115200},
}, nil
}
func (c *ScannerConn) Open() error {
var connErr error
if c.Type == ConnTypeNetwork {
c.udpConn, connErr = net.DialUDP("udp", nil, c.udpAddress)
} else if c.Type == ConnTypeUSB {
c.usbConn, connErr = serial.OpenPort(c.usbConfig)
c.usbReader = bufio.NewScanner(c.usbConn)
c.usbReader.Split(ScanLinesWithCR)
}
return connErr
}
func (c ScannerConn) Close() error {
if c.Type == ConnTypeNetwork {
return c.udpConn.Close()
} else if c.Type == ConnTypeUSB {
return c.usbConn.Close()
}
return nil
}
func (c ScannerConn) Write(b []byte) (n int, err error) {
if c.Type == ConnTypeNetwork {
return c.udpConn.Write(b)
} else if c.Type == ConnTypeUSB {
fixed := []byte(strings.Replace(string(b), "\n", "\r", -1))
return c.usbConn.Write(fixed)
}
return 0, nil
}
func (c ScannerConn) Read(b []byte) (n int, err error) {
if c.Type == ConnTypeNetwork {
return c.udpConn.Read(b)
} else if c.Type == ConnTypeUSB {
if !c.usbReader.Scan() {
return 0, nil
}
scanned := c.usbReader.Bytes()
copy(b, scanned)
return len(scanned), nil
}
return 0, nil
}
func (c ScannerConn) String() string {
if c.Type == ConnTypeNetwork {
return fmt.Sprintf("Connected to %s via UDP", c.udpAddress)
} else if c.Type == ConnTypeUSB {
return fmt.Sprintf("Connected to %s via USB", c.usbPort)
}
return "unknown"
}