-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
177 lines (141 loc) · 4.28 KB
/
generic.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
package device
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/byuoitav/connpool"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func (dm *DeviceManager) HandlerGetGeneric(ctx *gin.Context) {
addr := ctx.Param("address")
name := ctx.Param("name")
dsp := dm.CreateDSP(addr)
dm.Log.Debug("getting control value", zap.String("address", addr), zap.String("name", name))
c, cancel := context.WithTimeout(ctx.Request.Context(), 5*time.Second)
defer cancel()
val, err := dsp.Control(c, name)
if err != nil {
dm.Log.Error("unable to get control", zap.Error(err))
ctx.String(http.StatusInternalServerError, err.Error())
return
}
dm.Log.Debug("Got control", zap.String("address", addr), zap.String("value", fmt.Sprintf("%+v", val)))
ctx.JSON(http.StatusOK, map[string]float64{
name: val,
})
}
func (dm *DeviceManager) HandlerSetGeneric(ctx *gin.Context) {
addr := ctx.Param("address")
name := ctx.Param("name")
val, err := strconv.ParseFloat(ctx.Param("value"), 64)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
dsp := dm.CreateDSP(addr)
dm.Log.Debug("setting control value", zap.String("address", addr), zap.String("name", name), zap.Float64("value", val))
c, cancel := context.WithTimeout(ctx.Request.Context(), 5*time.Second)
defer cancel()
err = dsp.SetControl(c, name, val)
if err != nil {
dm.Log.Error("unable to set control", zap.Error(err))
ctx.String(http.StatusInternalServerError, err.Error())
return
}
dm.Log.Debug("Set control", zap.String("address", addr))
ctx.JSON(http.StatusOK, map[string]float64{
name: val,
})
}
func (d *DSP) SetControl(ctx context.Context, name string, value float64) error {
req := d.GetGenericSetStatusRequest(ctx)
req.Params.Name = name
req.Params.Value = value
toSend, err := json.Marshal(req)
if err != nil {
return err
}
toSend = append(toSend, 0x00)
var resp []byte
err = d.pool.Do(ctx, func(conn connpool.Conn) error {
d.log.Info("Setting control", zap.String("name", name), zap.Float64("value", value))
deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(3 * time.Second)
}
conn.SetWriteDeadline(deadline)
n, err := conn.Write(toSend)
switch {
case err != nil:
return fmt.Errorf("unable to write command to set volume: %v", err)
case n != len(toSend):
return fmt.Errorf("unable to write command to set volume: wrote %v/%v bytes", n, len(toSend))
}
resp, err = conn.ReadUntil('\x00', deadline)
if err != nil {
return fmt.Errorf("unable to read response: %w", err)
}
d.log.Debug("Got response", zap.ByteString("response", resp))
return nil
})
if err != nil {
return err
}
resp = bytes.Trim(resp, "\x00")
qscResp := QSCSetStatusResponse{}
if err := json.Unmarshal(resp, &qscResp); err != nil {
return fmt.Errorf("unable to parse response: %w", err)
}
if qscResp.Result.Name != name {
return fmt.Errorf("response name (%s) does not match the name sent (%s)", qscResp.Result.Name, name)
}
return nil
}
func (d *DSP) Control(ctx context.Context, name string) (float64, error) {
req := d.GetGenericGetStatusRequest(ctx)
req.Params = append(req.Params, name)
toSend, err := json.Marshal(req)
if err != nil {
return 0, err
}
toSend = append(toSend, 0x00)
var resp []byte
err = d.pool.Do(ctx, func(conn connpool.Conn) error {
d.log.Info("Getting control", zap.String("name", name))
deadline, ok := ctx.Deadline()
if !ok {
deadline = time.Now().Add(3 * time.Second)
}
conn.SetWriteDeadline(deadline)
n, err := conn.Write(toSend)
switch {
case err != nil:
return fmt.Errorf("unable to write command: %w", err)
case n != len(toSend):
return fmt.Errorf("unable to write command: wrote %v/%v bytes", n, len(toSend))
}
resp, err = conn.ReadUntil('\x00', deadline)
if err != nil {
return fmt.Errorf("unable to read response: %w", err)
}
d.log.Debug("Got response", zap.ByteString("response", resp))
return nil
})
if err != nil {
return 0, err
}
resp = bytes.Trim(resp, "\x00")
qscResp := QSCGetStatusResponse{}
if err := json.Unmarshal(resp, &qscResp); err != nil {
return 0, fmt.Errorf("unable to parse response: %w", err)
}
if len(qscResp.Result) == 0 {
return 0, fmt.Errorf("no results in response: '%s'", resp)
}
return qscResp.Result[0].Value, nil
}