11import { Location } from "@angular/common" ;
22import { HttpErrorResponse } from "@angular/common/http" ;
3- import { Component , OnDestroy , OnInit } from "@angular/core" ;
3+ import { Component , Input , OnDestroy , OnInit } from "@angular/core" ;
44import { Title } from "@angular/platform-browser" ;
55import { ActivatedRoute , Router } from "@angular/router" ;
66import { Application } from "@app/applications/application.model" ;
@@ -17,18 +17,22 @@ import { jsonToList } from "@shared/helpers/json.helper";
1717import { ErrorMessage } from "@shared/models/error-message.model" ;
1818import { ScrollToTopService } from "@shared/services/scroll-to-top.service" ;
1919import { SharedVariableService } from "@shared/shared-variable/shared-variable.service" ;
20- import { Subscription } from "rxjs" ;
20+ import { forkJoin , Subscription } from "rxjs" ;
2121import { IotDevice } from "../iot-device.model" ;
2222import { IoTDeviceService } from "../iot-device.service" ;
2323import { MeService } from "@shared/services/me.service" ;
2424import { 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" ;
2527
2628@Component ( {
2729 selector : "app-iot-device-edit" ,
2830 templateUrl : "./iot-device-edit.component.html" ,
2931 styleUrls : [ "./iot-device-edit.component.scss" ] ,
3032} )
3133export class IotDeviceEditComponent implements OnInit , OnDestroy {
34+ @Input ( ) isDeviceCopy : boolean = false ;
35+ public copyPayloadAndDatatarget : boolean = false ;
3236 public errorMessages : any ;
3337 public errorFields : string [ ] ;
3438 public formFailedSubmit = false ;
@@ -57,6 +61,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
5761 private deviceProfileService : DeviceProfileService ,
5862 private applicationService : ApplicationService ,
5963 private iotDeviceService : IoTDeviceService ,
64+ private datatargetPayloadService : PayloadDeviceDatatargetService ,
6065 private location : Location ,
6166 private shareVariable : SharedVariableService ,
6267 private deviceModelService : DeviceModelService ,
@@ -113,14 +118,18 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
113118 } ) ;
114119 }
115120
116- isChecked ( event ) {
121+ isDeviceTypeChecked ( event ) {
117122 if ( event . target . checked ) {
118123 this . iotDevice . type = event . target . name ;
119124 } else if ( ! event . target . checked && this . iotDevice . type . toString ( ) . includes ( event . target . name ) ) {
120125 event . target . checked = true ;
121126 }
122127 }
123128
129+ isCopyPayloadAndDatatargetChecked ( event ) {
130+ this . copyPayloadAndDatatarget = event . target . checked ;
131+ }
132+
124133 getDevice ( id : number ) : void {
125134 this . deviceSubscription = this . iotDeviceService . getIoTDevice ( id ) . subscribe ( ( device : IotDevice ) => {
126135 this . iotDevice = device ;
@@ -140,6 +149,44 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
140149 if ( device . metadata ) {
141150 this . metadataTags = jsonToList ( device . metadata ) ;
142151 }
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 . mqttpassword = undefined ;
185+ this . iotDevice . mqttInternalBrokerSettings . mqtttopicname = undefined ;
186+ this . iotDevice . mqttInternalBrokerSettings . mqttusername = undefined ;
187+ }
188+ }
189+ }
143190 } ) ;
144191 }
145192
@@ -190,7 +237,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
190237 }
191238 }
192239
193- if ( this . deviceId !== 0 ) {
240+ if ( this . deviceId !== 0 && ! this . isDeviceCopy ) {
194241 this . updateIoTDevice ( this . deviceId ) ;
195242 } else {
196243 this . postIoTDevice ( ) ;
@@ -275,23 +322,49 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
275322 }
276323 }
277324
325+ private navigateToDeviceDetails ( device : IotDevice ) {
326+ this . router . navigate ( [ "applications/" + this . iotDevice . applicationId + "/iot-device/" + device . id + "/details" ] ) ;
327+ }
328+
278329 postIoTDevice ( ) {
279- // Sanitize devEUI for non-hex characters
330+ // Sanitize devEUI
280331 if ( this . iotDevice . type === DeviceType . LORAWAN && this . iotDevice . lorawanSettings . devEUI ) {
281332 this . iotDevice . lorawanSettings . devEUI = this . iotDevice . lorawanSettings . devEUI . replace ( / [ ^ 0 - 9 A - F a - f ] / g, "" ) ;
282333 }
283334
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- ] ) ;
289- } ,
290- ( error : HttpErrorResponse ) => {
291- this . handleError ( error ) ;
292- this . formFailedSubmit = true ;
335+ //First create the device
336+ this . iotDeviceService . createIoTDevice ( this . iotDevice ) . subscribe ( ( createdDevice : IotDevice ) => {
337+ if ( ! this . copyPayloadAndDatatarget ) {
338+ this . navigateToDeviceDetails ( createdDevice ) ;
339+ return ;
293340 }
294- ) ;
341+
342+ //If it's the copy device flow, then get all datatargets from the device that we want to copy.
343+ this . datatargetPayloadService
344+ . getByIoTDevice ( this . deviceId )
345+ . subscribe ( ( result : PayloadDeviceDatatargetGetManyResponse ) => {
346+ //For each of these datatargets, append the copied device to that datatarget. First we make the observables
347+ const appendToDatatargetObservables = result . data . map ( element =>
348+ this . datatargetPayloadService . appendCopiedIoTDevice ( element . id , { deviceId : createdDevice . id } )
349+ ) ;
350+
351+ if ( appendToDatatargetObservables . length === 0 ) {
352+ this . navigateToDeviceDetails ( createdDevice ) ;
353+ return ;
354+ }
355+
356+ //Forkjoin is running all observables in parallel and when all are done it returns.
357+ forkJoin ( appendToDatatargetObservables ) . subscribe (
358+ ( ) => this . navigateToDeviceDetails ( createdDevice ) ,
359+ this . formFailedSubmitHandleError
360+ ) ;
361+ } , this . formFailedSubmitHandleError ) ;
362+ } , this . formFailedSubmitHandleError ) ;
363+ }
364+
365+ formFailedSubmitHandleError ( error : HttpErrorResponse ) {
366+ this . handleError ( error ) ;
367+ this . formFailedSubmit = true ;
295368 }
296369
297370 updateIoTDevice ( id : number ) {
@@ -301,8 +374,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
301374 this . routeBack ( ) ;
302375 } ,
303376 ( error : HttpErrorResponse ) => {
304- this . handleError ( error ) ;
305- this . formFailedSubmit = true ;
377+ this . formFailedSubmitHandleError ( error ) ;
306378 }
307379 ) ;
308380 }
0 commit comments