/
volume.go
240 lines (189 loc) · 5.52 KB
/
volume.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package atuhdsw52ed
import (
"context"
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
"github.com/byuoitav/connpool"
"go.uber.org/zap"
)
var (
ErrorVolumeOR = errors.New("Volume is out of range")
ErrorMute = errors.New("Mute State is invalid")
regGetVolume = regexp.MustCompile("VOUT1 (-?\\d+)")
regGetMute = regexp.MustCompile("VOUTMute1 (...)")
)
//Volumes .
func (vs *AtlonaVideoSwitcher5x1) Volumes(ctx context.Context, blocks []string) (map[string]int, error) {
vs.log.Info("Getting Volume")
volumeOut := make(map[string]int)
var volumeStr string
err := vs.pool.Do(ctx, func(conn connpool.Conn) error {
deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(10 * time.Second)
}
if err := conn.SetDeadline(deadline); err != nil {
return fmt.Errorf("unable to set connection deadline: %w", err)
}
cmd := []byte("VOUT1 sta\r\n")
n, err := conn.Write(cmd)
switch {
case err != nil:
return fmt.Errorf("unable to write to connection: %w", err)
case n != len(cmd):
return fmt.Errorf("unable to write to connection: wrote %v/%v bytes", n, len(cmd))
}
var match [][]string
for len(match) == 0 {
buf, err := conn.ReadUntil(asciiCarriageReturn, deadline)
if err != nil {
return fmt.Errorf("unable to read from connection: %w", err)
}
match = regGetVolume.FindAllStringSubmatch(string(buf), -1)
}
volumeStr = strings.TrimPrefix(match[0][1], "0")
return nil
})
if err != nil {
return volumeOut, err
}
volumeLevel, err := strconv.Atoi(volumeStr)
if err != nil {
return volumeOut, fmt.Errorf("failed to convert volume to int: %s", err.Error())
}
if volumeLevel < -35 {
volumeOut[""] = 0
} else {
volume := ((volumeLevel + 35) * 2)
if volume%2 != 0 {
volume = volume + 1
}
volumeOut[""] = volume
}
vs.log.Info("Got Volume", zap.Any("Vol", volumeOut))
return volumeOut, nil
}
//SetVolume .
func (vs *AtlonaVideoSwitcher5x1) SetVolume(ctx context.Context, block string, level int) error {
if level == 0 {
level = -80
} else {
convertedVolume := -35 + math.Round(float64(level/2))
level = int(convertedVolume)
}
levelStr := strconv.Itoa(level)
vs.log.Info("Setting Volume", zap.String("to", levelStr))
cmd := []byte(fmt.Sprintf("VOUT1 %s\r\n", levelStr))
return vs.pool.Do(ctx, func(conn connpool.Conn) error {
deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(10 * time.Second)
}
if err := conn.SetDeadline(deadline); err != nil {
return fmt.Errorf("unable to set connection deadline: %w", err)
}
n, err := conn.Write(cmd)
switch {
case err != nil:
return fmt.Errorf("Unable to write to connection: %w", err)
case n != len(cmd):
return fmt.Errorf("uanble to write to cvonnection: wrote %v%v bytes", n, len(cmd))
}
buf, err := conn.ReadUntil(asciiCarriageReturn, deadline)
if err != nil {
return fmt.Errorf("failed to read from connection %w", err)
}
if strings.Contains(string(buf), "ERROR") {
return ErrorVolumeOR
}
vs.log.Info("Successfully set volume", zap.String("to", levelStr))
return nil
})
}
//Mutes
func (vs *AtlonaVideoSwitcher5x1) Mutes(ctx context.Context, blocks []string) (map[string]bool, error) {
vs.log.Info("Getting Muted State")
mute := make(map[string]bool)
var muteState string
err := vs.pool.Do(ctx, func(conn connpool.Conn) error {
deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(10 * time.Second)
}
if err := conn.SetDeadline(deadline); err != nil {
return fmt.Errorf("unable to set connection deadline: %w", err)
}
cmd := []byte("VOUTMute1 sta\r\n")
n, err := conn.Write(cmd)
switch {
case err != nil:
return fmt.Errorf("unable to write to connection: %w", err)
case n != len(cmd):
return fmt.Errorf("unable to write to connection: wrote %v/%v bytes", n, len(cmd))
}
var match [][]string
for len(match) == 0 {
buf, err := conn.ReadUntil(asciiCarriageReturn, deadline)
if err != nil {
return fmt.Errorf("unable to read from connection: %w", err)
}
match = regGetMute.FindAllStringSubmatch(string(buf), -1)
}
muteState = strings.TrimPrefix(match[0][1], "0")
if muteState[len(muteState)-1:] == string('\r') {
muteState = strings.TrimSuffix(muteState, string('\r'))
}
return nil
})
if err != nil {
return mute, err
}
if muteState == "on" {
mute[""] = true
} else {
mute[""] = false
}
vs.log.Info("Got Mute State", zap.Any("State", mute))
return mute, nil
}
//SetMute .
func (vs *AtlonaVideoSwitcher5x1) SetMute(ctx context.Context, output string, muted bool) error {
var state string
if muted {
state = "on"
} else {
state = "off"
}
vs.log.Info("Muting System", zap.String("state", state))
cmd := []byte(fmt.Sprintf("VOUTMute1 %s\r\n", state))
return vs.pool.Do(ctx, func(conn connpool.Conn) error {
deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(10 * time.Second)
}
if err := conn.SetDeadline(deadline); err != nil {
return fmt.Errorf("uanble to write to connection deadline: %w", err)
}
n, err := conn.Write(cmd)
switch {
case err != nil:
return fmt.Errorf("unable to write to connection: %w", err)
case n != len(cmd):
return fmt.Errorf("unable to write to connection: wrote %v%v bytes", n, len(cmd))
}
buf, err := conn.ReadUntil(asciiCarriageReturn, deadline)
if err != nil {
return fmt.Errorf("failed to read form connection %w", err)
}
if strings.Contains(string(buf), "failed") {
return ErrorMute
}
vs.log.Info("Successfully set MUTE state", zap.String("state", state))
return nil
})
}