-
Notifications
You must be signed in to change notification settings - Fork 310
/
end_device.go
210 lines (190 loc) · 8.89 KB
/
end_device.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"github.com/gogo/protobuf/types"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
// EndDevice model.
type EndDevice struct {
Model
ApplicationID string `gorm:"unique_index:end_device_id_index;type:VARCHAR(36);not null;index:end_device_application_index"`
Application *Application
// BEGIN common fields
DeviceID string `gorm:"unique_index:end_device_id_index;type:VARCHAR(36);not null"`
Name string `gorm:"type:VARCHAR"`
Description string `gorm:"type:TEXT"`
Attributes []Attribute `gorm:"polymorphic:Entity;polymorphic_value:device"`
// END common fields
JoinEUI *EUI64 `gorm:"unique_index:end_device_eui_index;index:end_device_join_eui_index;type:VARCHAR(16);column:join_eui"`
DevEUI *EUI64 `gorm:"unique_index:end_device_eui_index;index:end_device_dev_eui_index;type:VARCHAR(16);column:dev_eui"`
BrandID string `gorm:"type:VARCHAR"`
ModelID string `gorm:"type:VARCHAR"`
HardwareVersion string `gorm:"type:VARCHAR"`
FirmwareVersion string `gorm:"type:VARCHAR"`
NetworkServerAddress string `gorm:"type:VARCHAR"`
ApplicationServerAddress string `gorm:"type:VARCHAR"`
JoinServerAddress string `gorm:"type:VARCHAR"`
ServiceProfileID string `gorm:"type:VARCHAR"`
Locations []EndDeviceLocation
Picture *Picture
PictureID *string `gorm:"type:UUID;index:end_device_picture_index"`
}
func init() {
registerModel(&EndDevice{})
}
func mustEndDeviceVersionIDs(pb *ttnpb.EndDevice) *ttnpb.EndDeviceVersionIdentifiers {
if pb.VersionIDs == nil {
pb.VersionIDs = &ttnpb.EndDeviceVersionIdentifiers{}
}
return pb.VersionIDs
}
// functions to set fields from the device model into the device proto.
var devicePBSetters = map[string]func(*ttnpb.EndDevice, *EndDevice){
"ids.join_eui": func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.JoinEUI = dev.JoinEUI.toPB() },
"ids.dev_eui": func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.DevEUI = dev.DevEUI.toPB() },
nameField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.Name = dev.Name },
descriptionField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.Description = dev.Description },
attributesField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.Attributes = attributes(dev.Attributes).toMap() },
versionIDsField: func(pb *ttnpb.EndDevice, dev *EndDevice) {
pb.VersionIDs = &ttnpb.EndDeviceVersionIdentifiers{
BrandID: dev.BrandID,
ModelID: dev.ModelID,
HardwareVersion: dev.HardwareVersion,
FirmwareVersion: dev.FirmwareVersion,
}
},
brandIDField: func(pb *ttnpb.EndDevice, dev *EndDevice) {
mustEndDeviceVersionIDs(pb).BrandID = dev.BrandID
},
modelIDField: func(pb *ttnpb.EndDevice, dev *EndDevice) {
mustEndDeviceVersionIDs(pb).ModelID = dev.ModelID
},
hardwareVersionField: func(pb *ttnpb.EndDevice, dev *EndDevice) {
mustEndDeviceVersionIDs(pb).HardwareVersion = dev.HardwareVersion
},
firmwareVersionField: func(pb *ttnpb.EndDevice, dev *EndDevice) {
mustEndDeviceVersionIDs(pb).FirmwareVersion = dev.FirmwareVersion
},
networkServerAddressField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.NetworkServerAddress = dev.NetworkServerAddress },
applicationServerAddressField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.ApplicationServerAddress = dev.ApplicationServerAddress },
joinServerAddressField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.JoinServerAddress = dev.JoinServerAddress },
serviceProfileIDField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.ServiceProfileID = dev.ServiceProfileID },
locationsField: func(pb *ttnpb.EndDevice, dev *EndDevice) { pb.Locations = deviceLocations(dev.Locations).toMap() },
pictureField: func(pb *ttnpb.EndDevice, dev *EndDevice) {
if dev.Picture == nil {
pb.Picture = nil
} else {
pb.Picture = dev.Picture.toPB()
}
},
}
// functions to set fields from the device proto into the device model.
var deviceModelSetters = map[string]func(*EndDevice, *ttnpb.EndDevice){
"ids.join_eui": func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.JoinEUI = eui(pb.JoinEUI) },
"ids.dev_eui": func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.DevEUI = eui(pb.DevEUI) },
nameField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.Name = pb.Name },
descriptionField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.Description = pb.Description },
attributesField: func(dev *EndDevice, pb *ttnpb.EndDevice) {
dev.Attributes = attributes(dev.Attributes).updateFromMap(pb.Attributes)
},
versionIDsField: func(dev *EndDevice, pb *ttnpb.EndDevice) {
dev.BrandID = pb.GetVersionIDs().GetBrandID()
dev.ModelID = pb.GetVersionIDs().GetModelID()
dev.HardwareVersion = pb.GetVersionIDs().GetHardwareVersion()
dev.FirmwareVersion = pb.GetVersionIDs().GetFirmwareVersion()
},
brandIDField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.BrandID = pb.GetVersionIDs().GetBrandID() },
modelIDField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.ModelID = pb.GetVersionIDs().GetModelID() },
hardwareVersionField: func(dev *EndDevice, pb *ttnpb.EndDevice) {
dev.HardwareVersion = pb.GetVersionIDs().GetHardwareVersion()
},
firmwareVersionField: func(dev *EndDevice, pb *ttnpb.EndDevice) {
dev.FirmwareVersion = pb.GetVersionIDs().GetFirmwareVersion()
},
networkServerAddressField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.NetworkServerAddress = pb.NetworkServerAddress },
applicationServerAddressField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.ApplicationServerAddress = pb.ApplicationServerAddress },
joinServerAddressField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.JoinServerAddress = pb.JoinServerAddress },
serviceProfileIDField: func(dev *EndDevice, pb *ttnpb.EndDevice) { dev.ServiceProfileID = pb.ServiceProfileID },
locationsField: func(dev *EndDevice, pb *ttnpb.EndDevice) {
dev.Locations = deviceLocations(dev.Locations).updateFromMap(pb.Locations)
},
pictureField: func(dev *EndDevice, pb *ttnpb.EndDevice) {
dev.PictureID, dev.Picture = nil, nil
if pb.Picture != nil {
dev.Picture = &Picture{}
dev.Picture.fromPB(pb.Picture)
}
},
}
// fieldMask to use if a nil or empty fieldmask is passed.
var defaultEndDeviceFieldMask = &types.FieldMask{}
func init() {
paths := make([]string, 0, len(devicePBSetters))
for _, path := range ttnpb.EndDeviceFieldPathsNested {
if _, ok := devicePBSetters[path]; ok {
paths = append(paths, path)
}
}
defaultEndDeviceFieldMask.Paths = paths
}
// fieldmask path to column name in devices table.
var deviceColumnNames = map[string][]string{
"ids.join_eui": {"join_eui"},
"ids.dev_eui": {"dev_eui"},
attributesField: {},
nameField: {nameField},
descriptionField: {descriptionField},
versionIDsField: {"brand_id", "model_id", "hardware_version", "firmware_version"},
brandIDField: {"brand_id"},
modelIDField: {"model_id"},
hardwareVersionField: {"hardware_version"},
firmwareVersionField: {"firmware_version"},
networkServerAddressField: {networkServerAddressField},
applicationServerAddressField: {applicationServerAddressField},
joinServerAddressField: {joinServerAddressField},
serviceProfileIDField: {serviceProfileIDField},
locationsField: {},
}
func (dev EndDevice) toPB(pb *ttnpb.EndDevice, fieldMask *types.FieldMask) {
pb.EndDeviceIdentifiers.ApplicationID = dev.ApplicationID
pb.EndDeviceIdentifiers.DeviceID = dev.DeviceID
pb.EndDeviceIdentifiers.JoinEUI = dev.JoinEUI.toPB() // Always present.
pb.EndDeviceIdentifiers.DevEUI = dev.DevEUI.toPB() // Always present.
pb.CreatedAt = cleanTime(dev.CreatedAt)
pb.UpdatedAt = cleanTime(dev.UpdatedAt)
if fieldMask == nil || len(fieldMask.Paths) == 0 {
fieldMask = defaultEndDeviceFieldMask
}
for _, path := range fieldMask.Paths {
if setter, ok := devicePBSetters[path]; ok {
setter(pb, &dev)
}
}
}
func (dev *EndDevice) fromPB(pb *ttnpb.EndDevice, fieldMask *types.FieldMask) (columns []string) {
if fieldMask == nil || len(fieldMask.Paths) == 0 {
fieldMask = defaultEndDeviceFieldMask
}
for _, path := range fieldMask.Paths {
if setter, ok := deviceModelSetters[path]; ok {
setter(dev, pb)
if columnNames, ok := deviceColumnNames[path]; ok {
columns = append(columns, columnNames...)
}
continue
}
}
return
}