-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.go
117 lines (100 loc) · 4.2 KB
/
utils.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
105
106
107
108
109
110
111
112
113
114
115
116
117
package utils
import (
"fmt"
"time"
"github.com/golang/snappy"
"github.com/google/uuid"
isEvents "github.com/plgd-dev/hub/v2/identity-store/events"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
)
const (
DeviceIDKey = "deviceId"
HrefIDKey = "hrefId"
LeadResourceTypeKey = "leadResourceType"
LeadResourcePrefix = "leadrt"
)
const (
Devices = "devices"
PlgdOwnersOwnerDevices = isEvents.PlgdOwnersOwner + "." + Devices
PlgdOwnersOwnerDevicesDevice = PlgdOwnersOwnerDevices + ".{" + DeviceIDKey + "}"
PlgdOwnersOwnerDevicesDeviceResourceLinks = PlgdOwnersOwnerDevicesDevice + ".resource-links"
PlgdOwnersOwnerDevicesDeviceResourceLinksEvent = PlgdOwnersOwnerDevicesDeviceResourceLinks + ".{" + isEvents.EventTypeKey + "}"
PlgdOwnersOwnerDevicesDeviceMetadata = PlgdOwnersOwnerDevicesDevice + ".metadata"
PlgdOwnersOwnerDevicesDeviceMetadataEvent = PlgdOwnersOwnerDevicesDeviceMetadata + ".{" + isEvents.EventTypeKey + "}"
PlgdOwnersOwnerDevicesDeviceResources = PlgdOwnersOwnerDevicesDevice + ".resources"
PlgdOwnersOwnerDevicesDeviceResourcesResource = PlgdOwnersOwnerDevicesDeviceResources + ".{" + HrefIDKey + "}"
PlgdOwnersOwnerDevicesDeviceResourcesResourceEvent = PlgdOwnersOwnerDevicesDeviceResourcesResource + ".{" + isEvents.EventTypeKey + "}"
PlgdOwnersOwnerDevicesDeviceResourcesResourceEventLeadResourceType = PlgdOwnersOwnerDevicesDeviceResourcesResourceEvent + "." + LeadResourcePrefix + ".{" + LeadResourceTypeKey + "}"
)
func HrefToID(href string) uuid.UUID {
return uuid.NewSHA1(uuid.NameSpaceURL, []byte(href))
}
func WithHrefId(hrefId string) func(values map[string]string) {
return func(values map[string]string) {
values[HrefIDKey] = hrefId
}
}
func WithDeviceID(deviceID string) func(values map[string]string) {
return func(values map[string]string) {
values[DeviceIDKey] = deviceID
}
}
func WithLeadResourceType(leadResourceType string) func(values map[string]string) {
return func(values map[string]string) {
values[LeadResourceTypeKey] = leadResourceType
}
}
func GetDeviceSubject(owner, deviceID string) []string {
return []string{isEvents.ToSubject(PlgdOwnersOwnerDevicesDevice, isEvents.WithOwner(owner), WithDeviceID(deviceID)) + ".>"}
}
func GetDeviceMetadataEventSubject(owner, deviceID, eventType string) []string {
return []string{isEvents.ToSubject(PlgdOwnersOwnerDevicesDeviceMetadataEvent, isEvents.WithOwner(owner), WithDeviceID(deviceID), isEvents.WithEventType(eventType))}
}
func GetResourceEventSubjects(owner string, resourceID *commands.ResourceId, eventType string, leadResourceTypeEnabled bool) []string {
subject := isEvents.ToSubject(PlgdOwnersOwnerDevicesDeviceResourcesResourceEvent, isEvents.WithOwner(owner), WithDeviceID(resourceID.GetDeviceId()), isEvents.WithEventType(eventType), WithHrefId(GetSubjectHrefID(resourceID.GetHref())))
if !leadResourceTypeEnabled {
return []string{subject}
}
return []string{subject + ".>"}
}
func GetSubjectHrefID(href string) string {
switch href {
case "", "*":
return "*"
default:
return HrefToID(href).String()
}
}
func TimeNowMs() uint64 {
now := time.Now()
unix := now.UnixNano()
return uint64(unix / int64(time.Millisecond))
}
type ProtobufMarshaler interface {
Marshal() ([]byte, error)
}
type ProtobufUnmarshaler interface {
Unmarshal([]byte) error
}
func Marshal(v interface{}) ([]byte, error) {
if p, ok := v.(ProtobufMarshaler); ok {
src, err := p.Marshal()
if err != nil {
return nil, fmt.Errorf("cannot marshal event: %w", err)
}
dst := make([]byte, 1024)
return snappy.Encode(dst, src), nil
}
return nil, fmt.Errorf("marshal is not supported by %T", v)
}
func Unmarshal(b []byte, v interface{}) error {
if p, ok := v.(ProtobufUnmarshaler); ok {
dst := make([]byte, 1024)
dst, err := snappy.Decode(dst, b)
if err != nil {
return fmt.Errorf("cannot decode buffer: %w", err)
}
return p.Unmarshal(dst)
}
return fmt.Errorf("unmarshal is not supported by %T", v)
}