/
device_multicast_group.go
105 lines (88 loc) · 2.34 KB
/
device_multicast_group.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
package storage
import (
"time"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lorawan"
)
// AddDeviceToMulticastGroup adds the given device to the given multicast-group.
func AddDeviceToMulticastGroup(db sqlx.Execer, devEUI lorawan.EUI64, multicastGroupID uuid.UUID) error {
_, err := db.Exec(`
insert into device_multicast_group (
dev_eui,
multicast_group_id,
created_at
) values ($1, $2, $3)`,
devEUI, multicastGroupID, time.Now())
if err != nil {
return handlePSQLError(err, "insert error")
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"multicast_group_id": multicastGroupID,
}).Info("device added to multicast-group")
return nil
}
// RemoveDeviceFromMulticastGroup removes the given device from the given
// multicast-group.
func RemoveDeviceFromMulticastGroup(db sqlx.Execer, devEUI lorawan.EUI64, multicastGroupID uuid.UUID) error {
res, err := db.Exec(`
delete from
device_multicast_group
where
dev_eui = $1
and multicast_group_id = $2`,
devEUI[:],
multicastGroupID,
)
if err != nil {
return handlePSQLError(err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return handlePSQLError(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"multicast_group_id": multicastGroupID,
}).Info("device removed from multicast-group")
return nil
}
// GetMulticastGroupsForDevEUI returns the multicast-group ids to which the
// given Device EUI belongs.
func GetMulticastGroupsForDevEUI(db sqlx.Queryer, devEUI lorawan.EUI64) ([]uuid.UUID, error) {
var out []uuid.UUID
err := sqlx.Select(db, &out, `
select
multicast_group_id
from
device_multicast_group
where
dev_eui = $1`,
devEUI[:])
if err != nil {
return nil, handlePSQLError(err, "select error")
}
return out, nil
}
// GetDevEUIsForMulticastGroup returns all Device EUIs within the given
// multicast-group id.
func GetDevEUIsForMulticastGroup(db sqlx.Queryer, multicastGroupID uuid.UUID) ([]lorawan.EUI64, error) {
var out []lorawan.EUI64
err := sqlx.Select(db, &out, `
select
dev_eui
from
device_multicast_group
where
multicast_group_id = $1
`, multicastGroupID)
if err != nil {
return nil, handlePSQLError(err, "select error")
}
return out, nil
}