-
Notifications
You must be signed in to change notification settings - Fork 1
/
live.go
232 lines (191 loc) · 5.85 KB
/
live.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package pubsubapi
import (
"context"
"encoding/base64"
"encoding/json"
"strings"
"time"
"github.com/jake-scott/smartthings-nest/internal/pkg/logging"
"github.com/jake-scott/smartthings-nest/internal/pkg/sdmapi"
"github.com/pkg/errors"
apioption "google.golang.org/api/option"
pubsubv1 "google.golang.org/api/pubsub/v1"
)
type Live struct {
sdmProjectID string
gcpProjectID string
subscriptionID string
credsFile string
timeout time.Duration
maxMessageAge time.Duration
logMessages bool
ctx context.Context
}
func NewLiveClient(sdmProjectID string, gcpProjectID string, subscriptionID string) *Live {
return &Live{
sdmProjectID: sdmProjectID,
gcpProjectID: gcpProjectID,
subscriptionID: subscriptionID,
maxMessageAge: time.Second * 120,
ctx: context.Background(),
}
}
func (c *Live) WithServiceAccountCreds(credsFile string) PubSub {
nc := *c
nc.credsFile = credsFile
return &nc
}
func (c *Live) WithTimeout(d time.Duration) PubSub {
nc := *c
nc.timeout = d
return &nc
}
func (c *Live) WithContext(ctx context.Context) PubSub {
nc := *c
nc.ctx = ctx
return &nc
}
func (c *Live) WithMaxMessageAge(d time.Duration) *Live {
nc := *c
nc.maxMessageAge = d
return &nc
}
func (c *Live) WithLogMessages() *Live {
nc := *c
nc.logMessages = true
return &nc
}
func (c *Live) api() (*pubsubv1.Service, error) {
pubsub, err := pubsubv1.NewService(context.TODO(), apioption.WithCredentialsFile(c.credsFile))
if err != nil {
return nil, err
}
return pubsub, nil
}
func (c *Live) MakeContext() (context.Context, context.CancelFunc) {
var ctx = c.ctx
var cancel context.CancelFunc = func() {}
if c.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, c.timeout)
}
return ctx, cancel
}
/*
Message format for resource updates:
{
"eventId" : "0120ecc7-3b57-4eb4-9941-91609f189fb4",
"timestamp" : "2019-01-01T00:00:01Z",
"resourceUpdate" : {
"name" : "enterprises/project-id/devices/device-id",
"traits" : {
"sdm.devices.traits.ThermostatMode" : {
"mode" : "COOL"
}
}
},
"userId": "AVPHwEuBfnPOnTqzVFT4IONX2Qqhu9EJ4ubO-bNnQ-yi"
}
*/
type sdmResourceUpdate struct {
Name string `json:"name"`
Traits json.RawMessage `json:"traits"`
}
type sdmEvent struct {
EventID string `json:"eventID"`
Timestamp time.Time `json:"timestamp"`
ResourceUpdate *sdmResourceUpdate `json:"resourceUpdate,omitempty"`
UserID string `json:"userId"`
}
func (c *Live) AckMessages(ackIDs []string) error {
s, err := c.api()
if err != nil {
return errors.Wrap(err, "initialising the api")
}
ctx, cancel := c.MakeContext()
defer cancel()
ackRequest := pubsubv1.AcknowledgeRequest{
AckIds: ackIDs,
}
subsID := "projects/" + c.gcpProjectID + "/subscriptions/" + c.subscriptionID
_, err = s.Projects.Subscriptions.Acknowledge(subsID, &ackRequest).Context(ctx).Do()
if err != nil {
return errors.Wrap(err, "executing acknowledge call")
}
logging.Logger(nil).Debugf("sent ACK %v", ackIDs)
return nil
}
func (c *Live) parseReceivedMessages(messages []*pubsubv1.ReceivedMessage) (toAck []string, events []SdmEvent, err error) {
for _, message := range messages {
logging.Logger(nil).Infof("pubsub message: ID %s, Devliery attempt %d", message.Message.MessageId, message.DeliveryAttempt)
// event data is base64 encoded
data, err := base64.StdEncoding.DecodeString(message.Message.Data)
if err != nil {
logging.Logger(nil).WithError(err).Error("decoding base64-encoded data field")
continue
}
if c.logMessages {
logging.Logger(nil).Debugf("message data (ID %s): %s", message.Message.MessageId, data)
}
// retreieve the message publish time
publishTime, err := time.Parse(time.RFC3339Nano, message.Message.PublishTime)
if err != nil {
logging.Logger(nil).WithError(err).Warnf("parsing message publish time (`%s`)", message.Message.PublishTime)
} else {
if time.Now().After(publishTime.Add(c.maxMessageAge)) {
logging.Logger(nil).Warnf("ignoring message ID %s, older than %s (%s)", message.Message.MessageId, c.maxMessageAge, publishTime)
toAck = append(toAck, message.AckId)
continue
}
}
event := sdmEvent{}
if err := json.Unmarshal(data, &event); err != nil {
logging.Logger(nil).WithError(err).Error("parsing SDM event")
continue
}
if event.ResourceUpdate == nil {
logging.Logger(nil).Warnf("ignoring message ID %s, not a resource update (%s)", message.Message.MessageId, message.Message.Data)
toAck = append(toAck, message.AckId)
continue
}
t := sdmapi.NewTraits()
if err := t.Parse(event.ResourceUpdate.Traits); err != nil {
logging.Logger(nil).WithError(err).Error("parsing device traits")
continue
}
parsedEvent := SdmEvent{
AckID: message.AckId,
Timestamp: event.Timestamp,
DeviceID: c.shortDeviceName(event.ResourceUpdate.Name),
Traits: t,
}
events = append(events, parsedEvent)
}
return
}
func (c *Live) shortDeviceName(longName string) string {
return strings.TrimPrefix(longName, "enterprises/"+c.sdmProjectID+"/devices/")
}
func (c *Live) Pull() ([]SdmEvent, error) {
s, err := c.api()
if err != nil {
return nil, errors.Wrap(err, "initialising the api")
}
ctx, cancel := c.MakeContext()
defer cancel()
pullRequest := pubsubv1.PullRequest{
MaxMessages: 10,
}
subsID := "projects/" + c.gcpProjectID + "/subscriptions/" + c.subscriptionID
response, err := s.Projects.Subscriptions.Pull(subsID, &pullRequest).Context(ctx).Do()
if err != nil {
return nil, errors.Wrap(err, "pulling messages from topic subscription")
}
messagesToAck, events, err := c.parseReceivedMessages(response.ReceivedMessages)
// Ack messages we declined to process
if len(messagesToAck) > 0 {
if err := c.AckMessages(messagesToAck); err != nil {
logging.Logger(nil).WithError(err).Warnf("acknowledging %d old messages", len(messagesToAck))
}
}
return events, err
}