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,46 @@ 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 . 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+ }
143192 } ) ;
144193 }
145194
@@ -190,7 +239,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
190239 }
191240 }
192241
193- if ( this . deviceId !== 0 ) {
242+ if ( this . deviceId !== 0 && ! this . isDeviceCopy ) {
194243 this . updateIoTDevice ( this . deviceId ) ;
195244 } else {
196245 this . postIoTDevice ( ) ;
@@ -275,23 +324,55 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
275324 }
276325 }
277326
327+ private navigateToDeviceDetails ( device : IotDevice ) {
328+ this . router . navigate ( [ "applications/" + this . iotDevice . applicationId + "/iot-device/" + device . id + "/details" ] ) ;
329+ }
330+
278331 postIoTDevice ( ) {
279- // Sanitize devEUI for non-hex characters
332+ // Sanitize devEUI
280333 if ( this . iotDevice . type === DeviceType . LORAWAN && this . iotDevice . lorawanSettings . devEUI ) {
281334 this . iotDevice . lorawanSettings . devEUI = this . iotDevice . lorawanSettings . devEUI . replace ( / [ ^ 0 - 9 A - F a - f ] / g, "" ) ;
282335 }
283336
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+ } ) ;
289366 } ,
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 ;
295376 }
296377
297378 updateIoTDevice ( id : number ) {
@@ -301,8 +382,7 @@ export class IotDeviceEditComponent implements OnInit, OnDestroy {
301382 this . routeBack ( ) ;
302383 } ,
303384 ( error : HttpErrorResponse ) => {
304- this . handleError ( error ) ;
305- this . formFailedSubmit = true ;
385+ this . formFailedSubmitHandleError ( error ) ;
306386 }
307387 ) ;
308388 }
0 commit comments