-
Notifications
You must be signed in to change notification settings - Fork 481
/
client.go
428 lines (341 loc) · 12 KB
/
client.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//
// Copyright (C) 2020 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0
package redis
import (
"fmt"
"sync"
"github.com/edgexfoundry/edgex-go/internal/pkg/db"
redisClient "github.com/edgexfoundry/edgex-go/internal/pkg/db/redis"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/errors"
model "github.com/edgexfoundry/go-mod-core-contracts/v2/models"
"github.com/google/uuid"
)
var currClient *Client // a singleton so Readings can be de-referenced
var once sync.Once
type Client struct {
*redisClient.Client
loggingClient logger.LoggingClient
}
func NewClient(config db.Configuration, logger logger.LoggingClient) (*Client, errors.EdgeX) {
var err error
dc := &Client{}
dc.Client, err = redisClient.NewClient(config, logger)
dc.loggingClient = logger
if err != nil {
return nil, errors.NewCommonEdgeX(errors.KindDatabaseError, "redis client creation failed", err)
}
return dc, nil
}
// CloseSession closes the connections to Redis
func (c *Client) CloseSession() {
c.Pool.Close()
currClient = nil
once = sync.Once{}
}
// AddEvent adds a new event
func (c *Client) AddEvent(e model.Event) (model.Event, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
if e.Id != "" {
_, err := uuid.Parse(e.Id)
if err != nil {
return model.Event{}, errors.NewCommonEdgeX(errors.KindInvalidId, "uuid parsing failed", err)
}
}
return addEvent(conn, e)
}
// EventById gets an event by id
func (c *Client) EventById(id string) (event model.Event, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
event, edgeXerr = eventById(conn, id)
if edgeXerr != nil {
return event, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return
}
// DeleteEventById removes an event by id
func (c *Client) DeleteEventById(id string) (edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr = deleteEventById(conn, id)
if edgeXerr != nil {
return errors.NewCommonEdgeXWrapper(edgeXerr)
}
return
}
// Add a new device profle
func (c *Client) AddDeviceProfile(dp model.DeviceProfile) (model.DeviceProfile, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
if dp.Id != "" {
_, err := uuid.Parse(dp.Id)
if err != nil {
return model.DeviceProfile{}, errors.NewCommonEdgeX(errors.KindInvalidId, "ID failed UUID parsing", err)
}
} else {
dp.Id = uuid.New().String()
}
return addDeviceProfile(conn, dp)
}
// UpdateDeviceProfile updates a new device profile
func (c *Client) UpdateDeviceProfile(dp model.DeviceProfile) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
return updateDeviceProfile(conn, dp)
}
// DeviceProfileNameExists checks the device profile exists by name
func (c *Client) DeviceProfileNameExists(name string) (bool, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
return deviceProfileNameExists(conn, name)
}
// AddDeviceService adds a new device service
func (c *Client) AddDeviceService(ds model.DeviceService) (model.DeviceService, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
if len(ds.Id) == 0 {
ds.Id = uuid.New().String()
}
return addDeviceService(conn, ds)
}
// DeviceServiceByName gets a device service by name
func (c *Client) DeviceServiceByName(name string) (deviceService model.DeviceService, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
deviceService, edgeXerr = deviceServiceByName(conn, name)
if edgeXerr != nil {
return deviceService, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return
}
// DeviceServiceById gets a device service by id
func (c *Client) DeviceServiceById(id string) (deviceService model.DeviceService, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
deviceService, edgeXerr = deviceServiceById(conn, id)
if edgeXerr != nil {
return deviceService, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return
}
// DeleteDeviceServiceById deletes a device service by id
func (c *Client) DeleteDeviceServiceById(id string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr := deleteDeviceServiceById(conn, id)
if edgeXerr != nil {
return errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to delete the device service with id %s", id), edgeXerr)
}
return nil
}
// DeleteDeviceServiceByName deletes a device service by name
func (c *Client) DeleteDeviceServiceByName(name string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr := deleteDeviceServiceByName(conn, name)
if edgeXerr != nil {
return errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to delete the device service with name %s", name), edgeXerr)
}
return nil
}
// DeviceServiceNameExists checks the device service exists by name
func (c *Client) DeviceServiceNameExists(name string) (bool, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
return deviceServiceNameExist(conn, name)
}
// DeviceProfileByName gets a device profile by name
func (c *Client) DeviceProfileByName(name string) (deviceProfile model.DeviceProfile, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
deviceProfile, edgeXerr = deviceProfileByName(conn, name)
if edgeXerr != nil {
return deviceProfile, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return
}
// DeleteDeviceProfileById deletes a device profile by id
func (c *Client) DeleteDeviceProfileById(id string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr := deleteDeviceProfileById(conn, id)
if edgeXerr != nil {
return errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to delete the device profile with id %s", id), edgeXerr)
}
return nil
}
// DeleteDeviceProfileByName deletes a device profile by name
func (c *Client) DeleteDeviceProfileByName(name string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr := deleteDeviceProfileByName(conn, name)
if edgeXerr != nil {
return errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to delete the device profile with name %s", name), edgeXerr)
}
return nil
}
// AllDeviceProfiles query device profiles with offset and limit
func (c *Client) AllDeviceProfiles(offset int, limit int, labels []string) ([]model.DeviceProfile, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
deviceProfiles, edgeXerr := deviceProfilesByLabels(conn, offset, limit, labels)
if edgeXerr != nil {
return deviceProfiles, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return deviceProfiles, nil
}
// EventTotalCount returns the total count of Event from the database
func (c *Client) EventTotalCount() (uint32, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
count, edgeXerr := c.eventTotalCount(conn)
if edgeXerr != nil {
return 0, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return count, nil
}
// EventCountByDevice returns the count of Event associated a specific Device from the database
func (c *Client) EventCountByDevice(deviceName string) (uint32, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
count, edgeXerr := c.eventCountByDevice(deviceName, conn)
if edgeXerr != nil {
return 0, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return count, nil
}
// AllDeviceServices returns multiple device services per query criteria, including
// offset: the number of items to skip before starting to collect the result set
// limit: The numbers of items to return
// labels: allows for querying a given object by associated user-defined labels
func (c *Client) AllDeviceServices(offset int, limit int, labels []string) (deviceServices []model.DeviceService, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
deviceServices, edgeXerr = deviceServicesByLabels(conn, offset, limit, labels)
if edgeXerr != nil {
return deviceServices, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return deviceServices, nil
}
// Add a new device
func (c *Client) AddDevice(d model.Device) (model.Device, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
if len(d.Id) == 0 {
d.Id = uuid.New().String()
}
return addDevice(conn, d)
}
// Update the pushed timestamp of an event
func (c *Client) UpdateEventPushedById(id string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
return updateEventPushedById(conn, id)
}
// DeleteDeviceById deletes a device by id
func (c *Client) DeleteDeviceById(id string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr := deleteDeviceById(conn, id)
if edgeXerr != nil {
return errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to delete the device with id %s", id), edgeXerr)
}
return nil
}
// DeleteDeviceByName deletes a device by name
func (c *Client) DeleteDeviceByName(name string) errors.EdgeX {
conn := c.Pool.Get()
defer conn.Close()
edgeXerr := deleteDeviceByName(conn, name)
if edgeXerr != nil {
return errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to delete the device with name %s", name), edgeXerr)
}
return nil
}
// DevicesByServiceName query devices by offset, limit and name
func (c *Client) DevicesByServiceName(offset int, limit int, name string) (devices []model.Device, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
devices, edgeXerr = devicesByServiceName(conn, offset, limit, name)
if edgeXerr != nil {
return devices, errors.NewCommonEdgeX(errors.Kind(edgeXerr),
fmt.Sprintf("fail to query devices by offset %d, limit %d and name %s", offset, limit, name), edgeXerr)
}
return devices, nil
}
// DeviceIdExists checks the device existence by id
func (c *Client) DeviceIdExists(id string) (bool, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
exists, err := deviceIdExists(conn, id)
if err != nil {
return exists, errors.NewCommonEdgeX(errors.Kind(err), fmt.Sprintf("fail to check the device existence by id %s", id), err)
}
return exists, nil
}
// DeviceNameExists checks the device existence by name
func (c *Client) DeviceNameExists(name string) (bool, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
exists, err := deviceNameExists(conn, name)
if err != nil {
return exists, errors.NewCommonEdgeX(errors.Kind(err), fmt.Sprintf("fail to check the device existence by name %s", name), err)
}
return exists, nil
}
// DeviceById gets a device by id
func (c *Client) DeviceById(id string) (device model.Device, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
device, edgeXerr = deviceById(conn, id)
if edgeXerr != nil {
return device, errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to query device by id %s", id), edgeXerr)
}
return
}
// DeviceByName gets a device by name
func (c *Client) DeviceByName(name string) (device model.Device, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
device, edgeXerr = deviceByName(conn, name)
if edgeXerr != nil {
return device, errors.NewCommonEdgeX(errors.Kind(edgeXerr), fmt.Sprintf("fail to query device by name %s", name), edgeXerr)
}
return
}
// AllEvents query events by offset and limit
func (c *Client) AllEvents(offset int, limit int) ([]model.Event, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
events, edgeXerr := c.allEvents(conn, offset, limit)
if edgeXerr != nil {
return events, errors.NewCommonEdgeX(errors.Kind(edgeXerr),
fmt.Sprintf("fail to query events by offset %d and limit %d", offset, limit), edgeXerr)
}
return events, nil
}
// AllDevices query the devices with offset, limit, and labels
func (c *Client) AllDevices(offset int, limit int, labels []string) ([]model.Device, errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
devices, edgeXerr := devicesByLabels(conn, offset, limit, labels)
if edgeXerr != nil {
return devices, errors.NewCommonEdgeXWrapper(edgeXerr)
}
return devices, nil
}
// EventsByDeviceName query events by offset, limit and device name
func (c *Client) EventsByDeviceName(offset int, limit int, name string) (events []model.Event, edgeXerr errors.EdgeX) {
conn := c.Pool.Get()
defer conn.Close()
events, edgeXerr = eventsByDeviceName(conn, offset, limit, name)
if edgeXerr != nil {
return events, errors.NewCommonEdgeX(errors.Kind(edgeXerr),
fmt.Sprintf("fail to query events by offset %d, limit %d and name %s", offset, limit, name), edgeXerr)
}
return events, nil
}