1
1
import { Location } from "@angular/common" ;
2
2
import { HttpErrorResponse } from "@angular/common/http" ;
3
- import { Component , OnDestroy , OnInit } from "@angular/core" ;
3
+ import { Component , Input , OnDestroy , OnInit } from "@angular/core" ;
4
4
import { Title } from "@angular/platform-browser" ;
5
5
import { ActivatedRoute , Router } from "@angular/router" ;
6
6
import { Application } from "@app/applications/application.model" ;
@@ -17,18 +17,22 @@ import { jsonToList } from "@shared/helpers/json.helper";
17
17
import { ErrorMessage } from "@shared/models/error-message.model" ;
18
18
import { ScrollToTopService } from "@shared/services/scroll-to-top.service" ;
19
19
import { SharedVariableService } from "@shared/shared-variable/shared-variable.service" ;
20
- import { Subscription } from "rxjs" ;
20
+ import { forkJoin , Subscription } from "rxjs" ;
21
21
import { IotDevice } from "../iot-device.model" ;
22
22
import { IoTDeviceService } from "../iot-device.service" ;
23
23
import { MeService } from "@shared/services/me.service" ;
24
24
import { OrganizationAccessScope } from "@shared/enums/access-scopes" ;
25
+ import { PayloadDeviceDatatargetService } from "@payload-decoder/payload-device-datatarget.service" ;
26
+ import { PayloadDeviceDatatargetGetManyResponse } from "@payload-decoder/payload-device-data.model" ;
25
27
26
28
@Component ( {
27
29
selector : "app-iot-device-edit" ,
28
30
templateUrl : "./iot-device-edit.component.html" ,
29
31
styleUrls : [ "./iot-device-edit.component.scss" ] ,
30
32
} )
31
33
export class IotDeviceEditComponent implements OnInit , OnDestroy {
34
+ @Input ( ) isDeviceCopy : boolean = false ;
35
+ public copyPayloadAndDatatarget : boolean = false ;
32
36
public errorMessages : any ;
33
37
public errorFields : string [ ] ;
34
38
public formFailedSubmit = false ;
@@ -57,6 +61,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
57
61
private deviceProfileService : DeviceProfileService ,
58
62
private applicationService : ApplicationService ,
59
63
private iotDeviceService : IoTDeviceService ,
64
+ private datatargetPayloadService : PayloadDeviceDatatargetService ,
60
65
private location : Location ,
61
66
private shareVariable : SharedVariableService ,
62
67
private deviceModelService : DeviceModelService ,
@@ -113,14 +118,18 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
113
118
} ) ;
114
119
}
115
120
116
- isChecked ( event ) {
121
+ isDeviceTypeChecked ( event ) {
117
122
if ( event . target . checked ) {
118
123
this . iotDevice . type = event . target . name ;
119
124
} else if ( ! event . target . checked && this . iotDevice . type . toString ( ) . includes ( event . target . name ) ) {
120
125
event . target . checked = true ;
121
126
}
122
127
}
123
128
129
+ isCopyPayloadAndDatatargetChecked ( event ) {
130
+ this . copyPayloadAndDatatarget = event . target . checked ;
131
+ }
132
+
124
133
getDevice ( id : number ) : void {
125
134
this . deviceSubscription = this . iotDeviceService . getIoTDevice ( id ) . subscribe ( ( device : IotDevice ) => {
126
135
this . iotDevice = device ;
@@ -140,6 +149,46 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
140
149
if ( device . metadata ) {
141
150
this . metadataTags = jsonToList ( device . metadata ) ;
142
151
}
152
+
153
+ //If coming from copy, reset all these properties
154
+ if ( this . isDeviceCopy ) {
155
+ this . iotDevice . id = undefined ;
156
+ this . iotDevice . name = undefined ;
157
+ this . iotDevice . createdAt = undefined ;
158
+ this . iotDevice . createdBy = undefined ;
159
+ this . iotDevice . createdByName = undefined ;
160
+ this . iotDevice . updatedAt = undefined ;
161
+ this . iotDevice . updatedBy = undefined ;
162
+ this . iotDevice . updatedByName = undefined ;
163
+ this . copyPayloadAndDatatarget = true ;
164
+
165
+ switch ( this . iotDevice . type ) {
166
+ case DeviceType . GENERIC_HTTP : {
167
+ this . iotDevice . apiKey = undefined ;
168
+ break ;
169
+ }
170
+ case DeviceType . LORAWAN : {
171
+ this . iotDevice . lorawanSettings . devEUI = undefined ;
172
+ this . iotDevice . lorawanSettings . OTAAapplicationKey = undefined ;
173
+ this . iotDevice . lorawanSettings . applicationSessionKey = undefined ;
174
+ this . iotDevice . lorawanSettings . networkSessionKey = undefined ;
175
+ this . iotDevice . lorawanSettings . devAddr = undefined ;
176
+ this . iotDevice . lorawanSettings . fCntUp = undefined ;
177
+ this . iotDevice . lorawanSettings . nFCntDown = undefined ;
178
+ break ;
179
+ }
180
+ case DeviceType . MQTT_INTERNAL_BROKER : {
181
+ this . iotDevice . mqttInternalBrokerSettings . caCertificate = undefined ;
182
+ this . iotDevice . mqttInternalBrokerSettings . deviceCertificate = undefined ;
183
+ this . iotDevice . mqttInternalBrokerSettings . deviceCertificateKey = undefined ;
184
+ this . iotDevice . mqttInternalBrokerSettings . mqttPort = undefined ;
185
+ this . iotDevice . mqttInternalBrokerSettings . mqttURL = undefined ;
186
+ this . iotDevice . mqttInternalBrokerSettings . mqttpassword = undefined ;
187
+ this . iotDevice . mqttInternalBrokerSettings . mqtttopicname = undefined ;
188
+ this . iotDevice . mqttInternalBrokerSettings . mqttusername = undefined ;
189
+ }
190
+ }
191
+ }
143
192
} ) ;
144
193
}
145
194
@@ -190,7 +239,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
190
239
}
191
240
}
192
241
193
- if ( this . deviceId !== 0 ) {
242
+ if ( this . deviceId !== 0 && ! this . isDeviceCopy ) {
194
243
this . updateIoTDevice ( this . deviceId ) ;
195
244
} else {
196
245
this . postIoTDevice ( ) ;
@@ -275,23 +324,55 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
275
324
}
276
325
}
277
326
327
+ private navigateToDeviceDetails ( device : IotDevice ) {
328
+ this . router . navigate ( [ "applications/" + this . iotDevice . applicationId + "/iot-device/" + device . id + "/details" ] ) ;
329
+ }
330
+
278
331
postIoTDevice ( ) {
279
- // Sanitize devEUI for non-hex characters
332
+ // Sanitize devEUI
280
333
if ( this . iotDevice . type === DeviceType . LORAWAN && this . iotDevice . lorawanSettings . devEUI ) {
281
334
this . iotDevice . lorawanSettings . devEUI = this . iotDevice . lorawanSettings . devEUI . replace ( / [ ^ 0 - 9 A - F a - f ] / g, "" ) ;
282
335
}
283
336
284
- this . iotDeviceService . createIoTDevice ( this . iotDevice ) . subscribe (
285
- ( response : IotDevice ) => {
286
- this . router . navigate ( [
287
- "applications/" + this . iotDevice . applicationId + "/iot-device/" + response . id + "/details" ,
288
- ] ) ;
337
+ this . iotDeviceService . createIoTDevice ( this . iotDevice ) . subscribe ( {
338
+ next : ( createdDevice : IotDevice ) => {
339
+ if ( ! this . copyPayloadAndDatatarget ) {
340
+ this . navigateToDeviceDetails ( createdDevice ) ;
341
+ return ;
342
+ }
343
+
344
+ this . datatargetPayloadService . getByIoTDevice ( this . deviceId ) . subscribe ( {
345
+ next : ( result : PayloadDeviceDatatargetGetManyResponse ) => {
346
+ const appendObservables = result . data . map ( element =>
347
+ this . datatargetPayloadService . appendCopiedIoTDevice ( element . id , { deviceId : createdDevice . id } )
348
+ ) ;
349
+
350
+ if ( appendObservables . length === 0 ) {
351
+ this . navigateToDeviceDetails ( createdDevice ) ;
352
+ return ;
353
+ }
354
+
355
+ forkJoin ( appendObservables ) . subscribe ( {
356
+ next : ( ) => this . navigateToDeviceDetails ( createdDevice ) ,
357
+ error : ( error : HttpErrorResponse ) => {
358
+ this . formFailedSubmitHandleError ( error ) ;
359
+ } ,
360
+ } ) ;
361
+ } ,
362
+ error : ( error : HttpErrorResponse ) => {
363
+ this . formFailedSubmitHandleError ( error ) ;
364
+ } ,
365
+ } ) ;
289
366
} ,
290
- ( error : HttpErrorResponse ) => {
291
- this . handleError ( error ) ;
292
- this . formFailedSubmit = true ;
293
- }
294
- ) ;
367
+ error : ( error : HttpErrorResponse ) => {
368
+ this . formFailedSubmitHandleError ( error ) ;
369
+ } ,
370
+ } ) ;
371
+ }
372
+
373
+ formFailedSubmitHandleError ( error : HttpErrorResponse ) {
374
+ this . handleError ( error ) ;
375
+ this . formFailedSubmit = true ;
295
376
}
296
377
297
378
updateIoTDevice ( id : number ) {
@@ -301,8 +382,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
301
382
this . routeBack ( ) ;
302
383
} ,
303
384
( error : HttpErrorResponse ) => {
304
- this . handleError ( error ) ;
305
- this . formFailedSubmit = true ;
385
+ this . formFailedSubmitHandleError ( error ) ;
306
386
}
307
387
) ;
308
388
}
0 commit comments