-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
91 lines (77 loc) · 2.17 KB
/
controller.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
package visca
import (
"context"
"encoding/binary"
"github.com/Zeryoshka/visca/connpool"
"net"
)
type Controller struct {
conns map[string]*connpool.Connpool
seq uint32
cameras map[viscaOverIpAddr]*Camera
}
type request struct {
header []byte
payloadHeader byte
payload []byte
}
func (r *request) toBytes() []byte {
reqLen := len(r.header) + 1 + len(r.payload) + 1
reqBytes := make([]byte, reqLen)
copy(reqBytes[:HeaderLen], r.header)
reqBytes[HeaderLen] = r.payloadHeader
copy(reqBytes[HeaderLen+1:], r.payload)
reqBytes[reqLen-1] = 0xFF
return reqBytes
}
func NewController() (*Controller, error) {
return &Controller{
conns: make(map[string]*connpool.Connpool),
cameras: make(map[viscaOverIpAddr]*Camera),
seq: 0,
}, nil
}
func (c *Controller) Close() error {
for _, conn := range c.conns {
err := conn.Close()
if err != nil {
return err
}
}
return nil
}
func (c *Controller) buildHeader(rtype requestType, payloadLen int) []byte {
header := make([]byte, HeaderLen)
// bytes 0, 1 - request type bytes from spec
switch rtype {
case commandRequest:
copy(header[:2], CommandHeaderPrefix)
}
// bytes 2, 3 - len of payload (2 = 0x0 cause max len 16)
header[2] = 0x00
header[3] = byte(payloadLen) + 2 // basepayload(3-14) + payloadHeader(1) + 0xFF
// bytes 4...8 - seq
binary.LittleEndian.PutUint32(header[4:], c.seq)
c.seq++ // with overflow (needs for devices)
return header
}
func (c *Controller) getPayloadHeader(index byte) byte {
return 0x80 | (index & 0x0F)
}
func (c *Controller) buildRequestFromCommand(index byte, payload []byte) *request {
return &request{
header: c.buildHeader(commandRequest, len(payload)),
payloadHeader: c.getPayloadHeader(index),
payload: payload,
}
}
func (c *Controller) sendCommand(ctx context.Context, viscaAddr viscaOverIpAddr, cmd Command) error {
req := c.buildRequestFromCommand(viscaAddr.index, cmd)
return c.sendRequest(ctx, viscaAddr.addr, req)
}
func (c *Controller) sendRequest(ctx context.Context, addr string, req *request) error {
return c.conns[addr].Do(ctx, func(ctx context.Context, conn net.Conn) error {
_, err := conn.Write(req.toBytes())
return err
})
}