-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
64 lines (57 loc) · 1.31 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
package sense
import (
"context"
"github.com/dnesting/sense/internal/client"
)
type Device struct {
ID string
Name string
Type string
Make string
Model string
Location string
}
// I'm not entirely sure what the relationship between these fields is, so
// just pick one that seems reasonable.
func getType(d client.Device) string {
tags := deref(d.Tags)
for _, tag := range []string{
"UserDeviceType",
"Type",
"DefaultUserDeviceType",
} {
if s := stringOrEmpty(tags[tag]); s != "" {
return s
}
}
return ""
}
func stringOrEmpty(v interface{}) string {
if s, ok := v.(string); ok {
return s
}
return ""
}
// GetDevices returns a list of devices known to the given monitor.
func (s *Client) GetDevices(ctx context.Context, monitorID int, includeMerged bool) (devs []Device, err error) {
res, err1 := s.client.GetDevicesWithResponse(
ctx,
monitorID,
&client.GetDevicesParams{
IncludeMerged: &includeMerged,
})
if err := client.Ensure(err1, "GetDevices", res, 200); err != nil {
return nil, err
}
for _, d := range deref(res.JSON200.Devices) {
devs = append(devs, Device{
ID: deref(d.Id),
Name: deref(d.Name),
Type: getType(d),
Make: deref(d.Make),
Model: deref(d.Model),
Location: deref(d.Location),
})
}
return devs, nil
}