/
info.go
104 lines (83 loc) · 2.29 KB
/
info.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
package state
import (
"context"
"fmt"
avcontrol "github.com/byuoitav/av-control-api"
"go.uber.org/zap"
)
type getDeviceInfoRequest struct {
id avcontrol.DeviceID
device avcontrol.DeviceConfig
driver avcontrol.Driver
log *zap.Logger
}
type getDeviceInfoResponse struct {
id avcontrol.DeviceID
info avcontrol.DeviceInfo
}
// GetInfo goes through the devices in the RoomConfig and reports on their status.
// Takes RoomConfig as an input and returns RoomInfo.
func (gs *GetSetter) GetInfo(ctx context.Context, room avcontrol.RoomConfig) (avcontrol.RoomInfo, error) {
if len(room.Devices) == 0 {
return avcontrol.RoomInfo{}, nil
}
// make sure the driver for every device in the room exists
for _, dev := range room.Devices {
if gs.DriverRegistry.Get(dev.Driver) == nil {
return avcontrol.RoomInfo{}, fmt.Errorf("%s: %w", dev.Driver, ErrDriverNotRegistered)
}
}
id := avcontrol.CtxRequestID(ctx)
log := gs.Logger
if len(id) > 0 {
log = gs.Logger.With(zap.String("requestID", id))
}
resps := make(chan getDeviceInfoResponse)
defer close(resps)
roomHealth := avcontrol.RoomInfo{
Devices: make(map[avcontrol.DeviceID]avcontrol.DeviceInfo),
}
for id, dev := range room.Devices {
req := getDeviceInfoRequest{
id: id,
device: dev,
driver: gs.DriverRegistry.Get(dev.Driver),
log: log.With(zap.String("deviceID", string(id))),
}
go func() {
resps <- req.do(ctx)
}()
}
for i := 0; i < len(room.Devices); i++ {
resp := <-resps
roomHealth.Devices[resp.id] = resp.info
}
return roomHealth, nil
}
func (req *getDeviceInfoRequest) do(ctx context.Context) getDeviceInfoResponse {
resp := getDeviceInfoResponse{
id: req.id,
}
req.log.Info("Getting info")
req.log.Debug("Getting device")
dev, err := req.driver.CreateDevice(ctx, req.device.Address)
if err != nil {
req.log.Warn("unable to get device", zap.Error(err))
str := fmt.Sprintf("unable to get device: %s", err)
resp.info.Error = &str
return resp
}
req.log.Debug("Got device")
if dev, ok := dev.(avcontrol.DeviceWithInfo); ok {
info, err := dev.Info(ctx)
if err != nil {
req.log.Warn("unable to get info", zap.Error(err))
str := err.Error()
resp.info.Error = &str
return resp
}
resp.info.Info = &info
}
req.log.Info("Finished getting info")
return resp
}