-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
getDevice.go
39 lines (34 loc) · 904 Bytes
/
getDevice.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
package client
import (
"context"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// GetDevice retrieves device details from the client.
func (c *Client) GetDevice(
ctx context.Context,
deviceID string,
) (*DeviceDetails, error) {
var deviceDetails *DeviceDetails
err := c.GetDevicesViaCallback(ctx, []string{deviceID}, nil, func(v *pb.Device) {
deviceDetails = &DeviceDetails{
ID: v.GetId(),
Device: v,
}
})
if err != nil {
return nil, err
}
if deviceDetails == nil {
return nil, status.Errorf(codes.NotFound, "not found")
}
err = c.GetResourceLinksViaCallback(ctx, []string{deviceID}, nil, func(v *events.ResourceLinksPublished) {
deviceDetails.Resources = v.GetResources()
})
if err != nil {
return nil, err
}
return deviceDetails, nil
}