/
device_profile.go
370 lines (326 loc) · 9.45 KB
/
device_profile.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
package storage
import (
"bytes"
"encoding/gob"
"fmt"
"time"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"github.com/lib/pq"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
"github.com/brocaar/loraserver/internal/config"
)
// Templates used for generating Redis keys
const (
DeviceProfileKeyTempl = "lora:ns:dp:%s"
)
// DeviceProfile defines the backend.DeviceProfile with some extra meta-data
type DeviceProfile struct {
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ID uuid.UUID `db:"device_profile_id"`
SupportsClassB bool `db:"supports_class_b"`
ClassBTimeout int `db:"class_b_timeout"` // Unit: seconds
PingSlotPeriod int `db:"ping_slot_period"`
PingSlotDR int `db:"ping_slot_dr"`
PingSlotFreq int `db:"ping_slot_freq"` // in Hz
SupportsClassC bool `db:"supports_class_c"`
ClassCTimeout int `db:"class_c_timeout"` // Unit: seconds
MACVersion string `db:"mac_version"` // Example: "1.0.2" [LW102]
RegParamsRevision string `db:"reg_params_revision"` // Example: "B" [RP102B]
RXDelay1 int `db:"rx_delay_1"`
RXDROffset1 int `db:"rx_dr_offset_1"`
RXDataRate2 int `db:"rx_data_rate_2"` // Unit: bits-per-second
RXFreq2 int `db:"rx_freq_2"` // In Hz
FactoryPresetFreqs []int `db:"factory_preset_freqs"` // In Hz
MaxEIRP int `db:"max_eirp"` // In dBm
MaxDutyCycle int `db:"max_duty_cycle"` // Example: 10 indicates 10%
SupportsJoin bool `db:"supports_join"`
RFRegion string `db:"rf_region"`
Supports32bitFCnt bool `db:"supports_32bit_fcnt"`
}
// CreateDeviceProfile creates the given device-profile.
func CreateDeviceProfile(db sqlx.Execer, dp *DeviceProfile) error {
now := time.Now()
if dp.ID == uuid.Nil {
var err error
dp.ID, err = uuid.NewV4()
if err != nil {
return errors.Wrap(err, "new uuid v4 error")
}
}
dp.CreatedAt = now
dp.UpdatedAt = now
_, err := db.Exec(`
insert into device_profile (
created_at,
updated_at,
device_profile_id,
supports_class_b,
class_b_timeout,
ping_slot_period,
ping_slot_dr,
ping_slot_freq,
supports_class_c,
class_c_timeout,
mac_version,
reg_params_revision,
rx_delay_1,
rx_dr_offset_1,
rx_data_rate_2,
rx_freq_2,
factory_preset_freqs,
max_eirp,
max_duty_cycle,
supports_join,
rf_region,
supports_32bit_fcnt
) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22)`,
dp.CreatedAt,
dp.UpdatedAt,
dp.ID,
dp.SupportsClassB,
dp.ClassBTimeout,
dp.PingSlotPeriod,
dp.PingSlotDR,
dp.PingSlotFreq,
dp.SupportsClassC,
dp.ClassCTimeout,
dp.MACVersion,
dp.RegParamsRevision,
dp.RXDelay1,
dp.RXDROffset1,
dp.RXDataRate2,
dp.RXFreq2,
pq.Array(dp.FactoryPresetFreqs),
dp.MaxEIRP,
dp.MaxDutyCycle,
dp.SupportsJoin,
dp.RFRegion,
dp.Supports32bitFCnt,
)
if err != nil {
return handlePSQLError(err, "insert error")
}
log.WithFields(log.Fields{
"id": dp.ID,
}).Info("device-profile created")
return nil
}
// CreateDeviceProfileCache caches the given device-profile in Redis.
// The TTL of the device-profile is the same as that of the device-sessions.
func CreateDeviceProfileCache(p *redis.Pool, dp DeviceProfile) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(dp); err != nil {
return errors.Wrap(err, "gob encode device-profile error")
}
c := p.Get()
defer c.Close()
key := fmt.Sprintf(DeviceProfileKeyTempl, dp.ID)
exp := int64(config.C.NetworkServer.DeviceSessionTTL) / int64(time.Millisecond)
_, err := c.Do("PSETEX", key, exp, buf.Bytes())
if err != nil {
return errors.Wrap(err, "set device-profile error")
}
return nil
}
// GetDeviceProfileCache returns a cached device-profile.
func GetDeviceProfileCache(p *redis.Pool, id uuid.UUID) (DeviceProfile, error) {
var dp DeviceProfile
key := fmt.Sprintf(DeviceProfileKeyTempl, id)
c := p.Get()
defer c.Close()
val, err := redis.Bytes(c.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
return dp, ErrDoesNotExist
}
return dp, errors.Wrap(err, "get error")
}
err = gob.NewDecoder(bytes.NewReader(val)).Decode(&dp)
if err != nil {
return dp, errors.Wrap(err, "gob decode error")
}
return dp, nil
}
// FlushDeviceProfileCache deletes a cached device-profile.
func FlushDeviceProfileCache(p *redis.Pool, id uuid.UUID) error {
key := fmt.Sprintf(DeviceProfileKeyTempl, id)
c := p.Get()
defer c.Close()
_, err := c.Do("DEL", key)
if err != nil {
return errors.Wrap(err, "delete error")
}
return nil
}
// GetAndCacheDeviceProfile returns the device-profile from cache
// in case available, else it will be retrieved from the database and then
// stored in cache.
func GetAndCacheDeviceProfile(db sqlx.Queryer, p *redis.Pool, id uuid.UUID) (DeviceProfile, error) {
dp, err := GetDeviceProfileCache(p, id)
if err == nil {
return dp, nil
}
if err != ErrDoesNotExist {
log.WithFields(log.Fields{
"device_profile_id": id,
}).WithError(err).Error("get device-profile cache error")
// we don't return as we can still fall-back onto db retrieval
}
dp, err = GetDeviceProfile(db, id)
if err != nil {
return DeviceProfile{}, errors.Wrap(err, "get device-profile error")
}
err = CreateDeviceProfileCache(p, dp)
if err != nil {
log.WithFields(log.Fields{
"device_profile_id": id,
}).WithError(err).Error("create device-profile cache error")
}
return dp, nil
}
// GetDeviceProfile returns the device-profile matching the given id.
func GetDeviceProfile(db sqlx.Queryer, id uuid.UUID) (DeviceProfile, error) {
var dp DeviceProfile
row := db.QueryRowx(`
select
created_at,
updated_at,
device_profile_id,
supports_class_b,
class_b_timeout,
ping_slot_period,
ping_slot_dr,
ping_slot_freq,
supports_class_c,
class_c_timeout,
mac_version,
reg_params_revision,
rx_delay_1,
rx_dr_offset_1,
rx_data_rate_2,
rx_freq_2,
factory_preset_freqs,
max_eirp,
max_duty_cycle,
supports_join,
rf_region,
supports_32bit_fcnt
from device_profile
where
device_profile_id = $1
`, id)
var factoryPresetFreqs []int64
err := row.Scan(
&dp.CreatedAt,
&dp.UpdatedAt,
&dp.ID,
&dp.SupportsClassB,
&dp.ClassBTimeout,
&dp.PingSlotPeriod,
&dp.PingSlotDR,
&dp.PingSlotFreq,
&dp.SupportsClassC,
&dp.ClassCTimeout,
&dp.MACVersion,
&dp.RegParamsRevision,
&dp.RXDelay1,
&dp.RXDROffset1,
&dp.RXDataRate2,
&dp.RXFreq2,
pq.Array(&factoryPresetFreqs),
&dp.MaxEIRP,
&dp.MaxDutyCycle,
&dp.SupportsJoin,
&dp.RFRegion,
&dp.Supports32bitFCnt,
)
if err != nil {
return dp, handlePSQLError(err, "select error")
}
for _, f := range factoryPresetFreqs {
dp.FactoryPresetFreqs = append(dp.FactoryPresetFreqs, int(f))
}
return dp, nil
}
// UpdateDeviceProfile updates the given device-profile.
func UpdateDeviceProfile(db sqlx.Execer, dp *DeviceProfile) error {
dp.UpdatedAt = time.Now()
res, err := db.Exec(`
update device_profile set
updated_at = $2,
supports_class_b = $3,
class_b_timeout = $4,
ping_slot_period = $5,
ping_slot_dr = $6,
ping_slot_freq = $7,
supports_class_c = $8,
class_c_timeout = $9,
mac_version = $10,
reg_params_revision = $11,
rx_delay_1 = $12,
rx_dr_offset_1 = $13,
rx_data_rate_2 = $14,
rx_freq_2 = $15,
factory_preset_freqs = $16,
max_eirp = $17,
max_duty_cycle = $18,
supports_join = $19,
rf_region = $20,
supports_32bit_fcnt = $21
where
device_profile_id = $1`,
dp.ID,
dp.UpdatedAt,
dp.SupportsClassB,
dp.ClassBTimeout,
dp.PingSlotPeriod,
dp.PingSlotDR,
dp.PingSlotFreq,
dp.SupportsClassC,
dp.ClassCTimeout,
dp.MACVersion,
dp.RegParamsRevision,
dp.RXDelay1,
dp.RXDROffset1,
dp.RXDataRate2,
dp.RXFreq2,
pq.Array(dp.FactoryPresetFreqs),
dp.MaxEIRP,
dp.MaxDutyCycle,
dp.SupportsJoin,
dp.RFRegion,
dp.Supports32bitFCnt,
)
if err != nil {
return handlePSQLError(err, "update error")
}
ra, err := res.RowsAffected()
if err != nil {
return handlePSQLError(err, "get rows affected error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithField("id", dp.ID).Info("device-profile updated")
return nil
}
// DeleteDeviceProfile deletes the device-profile matching the given id.
func DeleteDeviceProfile(db sqlx.Execer, id uuid.UUID) error {
res, err := db.Exec("delete from device_profile where device_profile_id = $1", id)
if err != nil {
return handlePSQLError(err, "delete error")
}
ra, err := res.RowsAffected()
if err != nil {
return handlePSQLError(err, "get rows affacted error")
}
if ra == 0 {
return ErrDoesNotExist
}
log.WithField("id", id).Info("device-profile deleted")
return nil
}