-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
232 lines (173 loc) · 6.03 KB
/
device.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
package device
import (
"encoding/json"
"net"
"net/http"
"strconv"
"strings"
"github.com/byuoitav/common/status"
"github.com/byuoitav/common/structs"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type DeviceManager struct {
Log *zap.Logger
Lvl *zap.AtomicLevel
ReqQueue chan VSRequest
}
func (dm *DeviceManager) RunHTTPServer(router *gin.Engine, port string) error {
dm.Log.Info("registering http endpoints")
dev := router.Group("")
dev.GET("/input/:encoder/:decoder", dm.SetStreamHostHandler)
dev.POST("/:address/videowall", dm.SetVideoWallHandler)
dev.GET("input/get/:address", dm.GetConnectedHostHandler)
dev.GET("/:address/hardware", dm.GetDeviceInfoHandler)
dev.GET("/:address/signal", dm.GetStreamSignalHandler)
dev.PUT("/configure/:encoder", dm.ConfigureDeviceHandler)
server := http.Server{
Addr: port,
MaxHeaderBytes: 1024 * 10,
}
return router.Run(server.Addr)
}
func (dm *DeviceManager) SetStreamHostHandler(c *gin.Context) {
dm.Log.Debug("setting stream host")
encoder := c.Param("encoder")
decoder := c.Param("decoder")
eIP := resolveIPAddress(encoder)
dIP := resolveIPAddress(decoder)
// check to see if the encoder is up?
cmdStr := getCommandString(SWITCH_HOST)
tokens := strings.Split(cmdStr, "temp")
cmdStr = tokens[0] + eIP.IP.String() + tokens[1]
respChan := make(chan VSResponse)
defer close(respChan)
req := VSRequest{
Address: dIP.IP.String(),
Command: cmdStr,
RespChannel: respChan,
}
dm.ReqQueue <- req
resp := <-respChan
if resp.Error != nil {
dm.Log.Error("failed to make request for setting stream host", zap.Error(resp.Error))
c.JSON(http.StatusInternalServerError, "failed to make request to decoder")
return
}
dm.Log.Debug("set stream host successfully", zap.String("encoder", encoder), zap.String("decoder", decoder))
c.JSON(http.StatusOK, status.Input{Input: encoder})
}
func (dm *DeviceManager) SetVideoWallHandler(c *gin.Context) {
dm.Log.Debug("setting video wall parameters")
address := c.Param("address")
var wallParams videoWallParams
err := c.Bind(&wallParams)
if err != nil {
dm.Log.Error("failed to bind request body for setting video wall parameters", zap.Error(err))
c.JSON(http.StatusBadRequest, "invalid request body")
return
}
ip := resolveIPAddress(address)
cmdStr := getCommandString(VIDEO_WALL)
tokens := strings.Split(cmdStr, "temp")
cmdStr = tokens[0] + strconv.Itoa(wallParams.TotalRows) + tokens[1] + strconv.Itoa(wallParams.TotalColumns) + tokens[2] + strconv.Itoa(wallParams.RowPosition) + tokens[3] + strconv.Itoa(wallParams.ColumnPosition) + tokens[4]
respChan := make(chan VSResponse)
defer close(respChan)
req := VSRequest{
Address: ip.IP.String(),
Command: cmdStr,
RespChannel: respChan,
}
dm.ReqQueue <- req
resp := <-respChan
if resp.Error != nil {
dm.Log.Error("failed to make request for setting video wall parameters", zap.Error(resp.Error))
c.JSON(http.StatusInternalServerError, "failed to make request to decoder")
return
}
dm.Log.Debug("set video wall parameters successfully", zap.String("decoder", address))
c.JSON(http.StatusOK, "ok")
}
func (dm *DeviceManager) GetConnectedHostHandler(c *gin.Context) {
dm.Log.Debug("getting the current stream host")
address := c.Param("address")
ip := resolveIPAddress(address)
dm.Log.Debug("found the current stream host", zap.String("decoder", address), zap.String("encoder", ip.IP.String()))
c.JSON(http.StatusOK, status.Input{Input: ip.IP.String()})
}
func (dm *DeviceManager) GetDeviceInfoHandler(c *gin.Context) {
dm.Log.Debug("getting device details")
address := c.Param("address")
ip := resolveIPAddress(address)
cmdStr := getCommandString(GET_INFO)
respChan := make(chan VSResponse)
defer close(respChan)
req := VSRequest{
Address: ip.IP.String(),
Command: cmdStr,
RespChannel: respChan,
}
dm.ReqQueue <- req
resp := <-respChan
if resp.Error != nil {
dm.Log.Error("failed to make request for getting device details", zap.Error(resp.Error))
c.JSON(http.StatusInternalServerError, "failed to make request to decoder")
return
}
var info structs.HardwareInfo
info.ModelName = resp.Response["UNIT.MODEL"]
info.FirmwareVersion = resp.Response["UNIT.FIRMWARE"]
info.BuildDate = resp.Response["UNIT.FIRMWARE_DATE"]
info.PowerStatus = "" // uptime; not found with vs devices
info.NetworkInfo.IPAddress = resp.Response["IP.ADDRESS"]
info.NetworkInfo.MACAddress = resp.Response["UNIT.MAC_ADDRESS"]
details, _ := json.Marshal(info)
dm.Log.Debug("got device details", zap.String("device info", string(details)))
c.JSON(http.StatusOK, info)
}
func (dm *DeviceManager) GetStreamSignalHandler(c *gin.Context) {
dm.Log.Debug("getting signal data")
address := c.Param("address")
ip := resolveIPAddress(address)
cmdStr := getCommandString(GET_SIGNAL)
respChan := make(chan VSResponse)
defer close(respChan)
req := VSRequest{
Address: ip.IP.String(),
Command: cmdStr,
RespChannel: respChan,
}
dm.ReqQueue <- req
resp := <-respChan
if resp.Error != nil {
dm.Log.Error("failed to make request for getting device details", zap.Error(resp.Error))
c.JSON(http.StatusInternalServerError, "failed to make request to device")
return
}
timing := resp.Response["VIDEO.TIMING"]
var signalStatus structs.ActiveSignal
if timing == "Not Available" {
dm.Log.Debug("no signal", zap.String("address", address))
signalStatus.Active = false
} else {
dm.Log.Debug("active signal", zap.String("address", address))
signalStatus.Active = true
}
dm.Log.Debug("returning signal status", zap.Bool("status", signalStatus.Active))
c.JSON(http.StatusOK, signalStatus)
}
func (dm *DeviceManager) ConfigureDeviceHandler(c *gin.Context) {
dm.Log.Debug("configuring encoder")
encoder := c.Param("encoder")
ip := resolveIPAddress(encoder)
dm.Log.Debug("needed for av-api to work")
c.JSON(http.StatusOK, status.Input{Input: ip.IP.String()})
}
func resolveIPAddress(host string) *net.IPAddr {
ipAddr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil
}
ipAddr.IP = ipAddr.IP.To4()
return ipAddr
}