-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceupdate.go
56 lines (44 loc) · 1.35 KB
/
deviceupdate.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
package main
import (
"fmt"
"strings"
"github.com/openchirp/framework"
. "github.com/openchirp/lorawan-service/lorawan/deviceconfig"
)
const defaultLorawanClass = LorawanClassA
type DeviceUpdateAdapter struct {
framework.DeviceUpdate
}
func (update DeviceUpdateAdapter) GetDevEUI() string {
return strings.ToUpper(update.Config[configDevEUI])
}
func (update DeviceUpdateAdapter) GetAppKey() string {
return strings.ToUpper(update.Config[configAppKey])
}
func (update DeviceUpdateAdapter) GetClass() LorawanClass {
classStr := update.Config[configClass]
class := LorawanClassFromString(classStr)
if classStr == "" {
class = defaultLorawanClass
}
return class
}
func (update DeviceUpdateAdapter) GetDeviceConfig(c *framework.ServiceClient) (DeviceConfig, error) {
var config DeviceConfig
config.ID = update.Id
switch update.Type {
case framework.DeviceUpdateTypeAdd, framework.DeviceUpdateTypeUpd:
info, err := c.FetchDeviceInfo(update.Id)
if err != nil {
return config, fmt.Errorf("Deviceid \"%s\" was deleted before we could fetch it's config. Skipping.", update.Id)
}
config.Topic = info.Pubsub.Topic
config.Name = info.Name
config.OwnerName = info.Owner.Name
config.OwnerEmail = info.Owner.Email
}
config.SetDevEUI(update.GetDevEUI())
config.SetAppKey(update.GetAppKey())
config.Class = update.GetClass()
return config, nil
}