-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
resource.go
59 lines (52 loc) · 1.41 KB
/
resource.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
package test
import (
"time"
"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
)
func SortResources(s commands.Resources) commands.Resources {
s.Sort()
return s
}
func ResourceLinksToResources(deviceID string, s []schema.ResourceLink) []*commands.Resource {
r := make([]*commands.Resource, 0, len(s))
for _, l := range s {
l.DeviceID = deviceID
r = append(r, commands.SchemaResourceLinkToResource(l, time.Time{}))
}
CleanUpResourcesArray(r)
return r
}
func ResourceLinksToResourceIds(deviceID string, s []schema.ResourceLink) []*commands.ResourceId {
r := make([]*commands.ResourceId, 0, len(s))
for _, l := range s {
l.DeviceID = deviceID
r = append(r, commands.SchemaResourceLinkToResourceId(l))
}
return r
}
func ResourceLinksToResources2(deviceID string, s []schema.ResourceLink) []*pb.Resource {
r := make([]*pb.Resource, 0, len(s))
for _, l := range s {
res := pb.Resource{
Types: l.ResourceTypes,
Data: &events.ResourceChanged{
ResourceId: &commands.ResourceId{
DeviceId: deviceID,
Href: l.Href,
},
},
}
r = append(r, &res)
}
return r
}
func CleanUpResourcesArray(resources []*commands.Resource) []*commands.Resource {
for _, r := range resources {
r.ValidUntil = 0
}
SortResources(resources)
return resources
}