-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
createResource.go
39 lines (35 loc) · 1009 Bytes
/
createResource.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 pb
import (
"context"
"github.com/google/uuid"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"google.golang.org/grpc/peer"
)
func (req *CreateResourceRequest) ToRACommand(ctx context.Context) (*commands.CreateResourceRequest, error) {
correlationUUID, err := uuid.NewRandom()
if err != nil {
return nil, err
}
connectionID := ""
peer, ok := peer.FromContext(ctx)
if ok {
connectionID = peer.Addr.String()
}
href := req.GetResourceId().GetHref()
if len(href) > 0 && href[0] != '/' {
href = "/" + href
}
return &commands.CreateResourceRequest{
ResourceId: commands.NewResourceID(req.GetResourceId().GetDeviceId(), href),
CorrelationId: correlationUUID.String(),
TimeToLive: req.GetTimeToLive(),
Content: &commands.Content{
Data: req.GetContent().GetData(),
ContentType: req.GetContent().GetContentType(),
CoapContentFormat: -1,
},
CommandMetadata: &commands.CommandMetadata{
ConnectionId: connectionID,
},
}, nil
}